Class: Music::Chord
- Inherits:
-
Object
- Object
- Music::Chord
- Defined in:
- lib/music/chord.rb
Constant Summary collapse
- RULES =
{ "5" => ["1", "major 3", "5"], "m5" => ["1", "minor 3", "5"], "7" => ["1", "major 3", "5", "major 7"], "m7" => ["1", "minor 3", "5", "minor 7"], }
- REGEXP =
/ (?<root> [CDEFGAB][#♭b]? ) (?<kind> m?\d* ) /x
Instance Attribute Summary collapse
- #kind ⇒ String readonly
- #root ⇒ Music::Note readonly
Instance Method Summary collapse
-
#==(other) ⇒ Object
Compares the names.
-
#initialize(name) ⇒ Chord
constructor
A new instance of Chord.
- #name ⇒ Object
- #notes ⇒ Array<Music::Note>
- #transpose_by(interval) ⇒ Music::Note
- #transpose_down(interval) ⇒ Music::Note
- #transpose_up(interval) ⇒ Music::Note
Constructor Details
#initialize(name) ⇒ Chord
Returns a new instance of Chord.
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/music/chord.rb', line 39 def initialize(name) unless match = name.match(/^#{REGEXP}$/) raise ArgumentError, "invalid chord format: #{name}" end @root = Note.new(match[:root]) @kind = match[:kind] @kind << "5" if @kind !~ /\d+/ end |
Instance Attribute Details
#kind ⇒ String (readonly)
32 33 34 |
# File 'lib/music/chord.rb', line 32 def kind @kind end |
Instance Method Details
#==(other) ⇒ Object
Compares the names.
57 58 59 |
# File 'lib/music/chord.rb', line 57 def ==(other) self.name == other.name end |
#name ⇒ Object
50 51 52 |
# File 'lib/music/chord.rb', line 50 def name [root, kind].join end |
#notes ⇒ Array<Music::Note>
64 65 66 67 68 69 70 71 72 |
# File 'lib/music/chord.rb', line 64 def notes RULES[kind].map do |interval_name| quality = interval_name[/^[a-z]+/] || "perfect" number = interval_name[/\d+$/] interval = Interval.new(number.to_i, quality.to_sym) root.transpose_by(interval) end end |
#transpose_by(interval) ⇒ Music::Note
82 83 84 |
# File 'lib/music/chord.rb', line 82 def transpose_by(interval) Chord.new(root.transpose_by(interval).name + kind) end |
#transpose_down(interval) ⇒ Music::Note
102 103 104 |
# File 'lib/music/chord.rb', line 102 def transpose_down(interval) transpose_by(-interval) end |
#transpose_up(interval) ⇒ Music::Note
92 93 94 |
# File 'lib/music/chord.rb', line 92 def transpose_up(interval) transpose_by(interval) end |