Class: Coltrane::Theory::Chord

Inherits:
Object
  • Object
show all
Includes:
ChordSubstitutions
Defined in:
lib/coltrane/theory/chord.rb

Overview

It describe a chord

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ChordSubstitutions

#tritone_substitution

Constructor Details

#initialize(notes: nil, root_note: nil, quality: nil, name: nil) ⇒ Chord

Returns a new instance of Chord.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/coltrane/theory/chord.rb', line 11

def initialize(notes: nil, root_note: nil, quality: nil, name: nil)
  if notes
    notes      = NoteSet[*notes] if notes.is_a?(Array)
    @notes     = notes
    @root_note = notes.first
    @quality   = ChordQuality.new(notes: notes)
  elsif root_note && quality
    @notes     = quality.notes_for(root_note)
    @root_note = root_note
    @quality   = quality
  elsif name
    @root_note, @quality, @notes = parse_from_name(name)
  else
    raise WrongKeywordsError,
          '[notes:] || [root_note:, quality:] || [name:]'
  end
end

Instance Attribute Details

#notesObject (readonly)

Returns the value of attribute notes.



8
9
10
# File 'lib/coltrane/theory/chord.rb', line 8

def notes
  @notes
end

#qualityObject (readonly)

Returns the value of attribute quality.



8
9
10
# File 'lib/coltrane/theory/chord.rb', line 8

def quality
  @quality
end

#root_noteObject (readonly)

Returns the value of attribute root_note.



8
9
10
# File 'lib/coltrane/theory/chord.rb', line 8

def root_note
  @root_note
end

Instance Method Details

#+(other) ⇒ Object



73
74
75
76
77
78
# File 'lib/coltrane/theory/chord.rb', line 73

def +(other)
  case other
  when Note, NoteSet, Interval then Chord.new(notes: notes + other)
  when Chord then Chord.new(notes: notes + other.notes)
  end
end

#-(other) ⇒ Object



80
81
82
83
84
85
# File 'lib/coltrane/theory/chord.rb', line 80

def -(other)
  case other
  when Note, NoteSet, Interval, Numeric then Chord.new(notes: notes - other)
  when Chord then Chord.new(notes: notes - other.notes)
  end
end

#==(other) ⇒ Object Also known as: eql?



29
30
31
# File 'lib/coltrane/theory/chord.rb', line 29

def ==(other)
  (notes & other.notes).size == notes.size
end

#hashObject



41
42
43
# File 'lib/coltrane/theory/chord.rb', line 41

def hash
  notes.hash
end

#intervalsObject



49
50
51
# File 'lib/coltrane/theory/chord.rb', line 49

def intervals
  IntervalSequence.new(NoteSet.new(notes))
end

#invert(n = 1) ⇒ Object



65
66
67
# File 'lib/coltrane/theory/chord.rb', line 65

def invert(n = 1)
  Chord.new(notes: notes.rotate(n))
end

#nameObject Also known as: to_s



35
36
37
# File 'lib/coltrane/theory/chord.rb', line 35

def name
  "#{root_note}#{quality}"
end

#next_inversionObject



61
62
63
# File 'lib/coltrane/theory/chord.rb', line 61

def next_inversion
  Chord.new(notes: notes.rotate(1))
end

#pretty_nameObject



45
46
47
# File 'lib/coltrane/theory/chord.rb', line 45

def pretty_name
  "#{root_note.pretty_name}#{quality.name}"
end

#previous_inversionObject



69
70
71
# File 'lib/coltrane/theory/chord.rb', line 69

def previous_inversion
  Chord.new(notes: notes.rotate(-1))
end

#scalesObject



57
58
59
# File 'lib/coltrane/theory/chord.rb', line 57

def scales
  Scale.having_chord(name)
end

#sizeObject



53
54
55
# File 'lib/coltrane/theory/chord.rb', line 53

def size
  notes.size
end