class Music::Chord
Attributes
notes[R]
Public Class Methods
new(notes)
click to toggle source
# File lib/music/chord.rb, line 16 def initialize(notes) raise ArgumentError, 'Chords must have at least two notes' if notes.size < 2 @notes = Set.new(notes) do |note| if note.is_a?(Note) note else Note.new(note) end end end
parse_chord_string(chord_string, assumed_octave = nil)
click to toggle source
# File lib/music/chord.rb, line 96 def parse_chord_string(chord_string, assumed_octave = nil) if note_string_match = chord_string.match(/^([A-Ga-g])([#b]?)([^\d]*)(\d*)$/) full_string, note, accidental, interval, octave = note_string_match.to_a raise ArgumentError, 'No octave found and no octave assumed' if note.empty? && assumed_octave.nil? Note.new(note + accidental + octave, assumed_octave).chord(interval) end end
Public Instance Methods
==(other_chord)
click to toggle source
# File lib/music/chord.rb, line 10 def ==(other_chord) self.eql?(other_chord) end
describe()
click to toggle source
# File lib/music/chord.rb, line 34 def describe note_array = @notes.to_a.sort distances = (1...note_array.size).collect do |i| note_array[0].distance_to(note_array[i]) end quality = case distances when [4, 7] :major when [3, 7] :minor when [3, 6] :diminished when [4, 8] :augmented when [4, 7, 11] :major_7 when [3, 7, 10] :minor_7 when [3, 6, 9] :diminished_7 when [3, 6, 10] :half_diminished_7 when [4, 8, 10] :augmented_7 end [note_array.first.letter, quality] end
eql?(other_chord)
click to toggle source
# File lib/music/chord.rb, line 4 def eql?(other_chord) @notes == other_chord.notes end
first_inversion()
click to toggle source
Calls inversion(1)
# File lib/music/chord.rb, line 81 def first_inversion self.inversion(1) end
hash()
click to toggle source
# File lib/music/chord.rb, line 7 def hash @notes.hash end
inversion(amount)
click to toggle source
Give the Nth inversion of the chord which simply adjusts the lowest N notes up by one octive
@returns [Chord] The specified inversion of chord
# File lib/music/chord.rb, line 71 def inversion(amount) raise ArgumentError, "Inversion amount must be greater than or equal to 1" if amount < 1 raise ArgumentError, "Not enough notes in chord for inversion" if amount >= @notes.size note_array = @notes.to_a.sort notes = (0...amount).collect { note_array.shift.adjust_by_semitones(12) } Chord.new(notes + note_array) end
note_strings()
click to toggle source
Spec and implement def to_i
# File lib/music/chord.rb, line 30 def note_strings Set.new(@notes.collect(&:note_string)) end
second_inversion()
click to toggle source
Calls inversion(2)
# File lib/music/chord.rb, line 86 def second_inversion self.inversion(2) end
third_inversion()
click to toggle source
Calls inversion(3)
# File lib/music/chord.rb, line 91 def third_inversion self.inversion(3) end
to_s()
click to toggle source
# File lib/music/chord.rb, line 64 def to_s @notes.to_a.sort.collect(&:to_s).join(' / ') end