Class: Coltrane::Note
Overview
Describes a musical note, independent of octave (that’d be pitch)
Constant Summary collapse
- NOTES =
{ 'C' => 0, 'C#' => 1, 'Db' => 1, 'D' => 2, 'D#' => 3, 'Eb' => 3, 'E' => 4, 'F' => 5, 'F#' => 6, 'Gb' => 6, 'G' => 7, 'G#' => 8, 'Ab' => 8, 'A' => 9, 'A#' => 10, 'Bb' => 10, 'B' => 11 }.freeze
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #to_s)
readonly
Returns the value of attribute name.
-
#number ⇒ Object
(also: #id)
readonly
Returns the value of attribute number.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #accident? ⇒ Boolean
- #enharmonic?(other) ⇒ Boolean
-
#initialize(name) ⇒ Note
constructor
A new instance of Note.
- #interval_to(note_name) ⇒ Object
- #pretty_name ⇒ Object
Constructor Details
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: to_s
Returns the value of attribute name.
8 9 10 |
# File 'lib/coltrane/note.rb', line 8 def name @name end |
#number ⇒ Object (readonly) Also known as: id
Returns the value of attribute number.
8 9 10 |
# File 'lib/coltrane/note.rb', line 8 def number @number end |
Class Method Details
.[](arg) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/coltrane/note.rb', line 39 def self.[](arg) name = case arg when Note then return arg when String then find_note(arg) when Numeric then NOTES.key(arg % 12) else raise InvalidNoteError, "Wrong type: #{arg.class}" end new(name) || (raise InvalidNoteError, arg.to_s) end |
.all ⇒ Object
52 53 54 |
# File 'lib/coltrane/note.rb', line 52 def self.all %w[C C# D D# E F F# G G# A A# B].map { |n| Note[n] } end |
.find_note(str) ⇒ Object
56 57 58 59 |
# File 'lib/coltrane/note.rb', line 56 def self.find_note(str) NOTES.each_key { |k, _v| return k if str.casecmp?(k) } nil end |
Instance Method Details
#+(other) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/coltrane/note.rb', line 71 def +(other) case other when Interval then Note[number + other.semitones] when Numeric then Note[number + other] when Note then Chord.new(number + other.number) end end |
#-(other) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/coltrane/note.rb', line 79 def -(other) case other when Numeric then Note[number - other] when Note then Interval[other.number - number] end end |
#accident? ⇒ Boolean
67 68 69 |
# File 'lib/coltrane/note.rb', line 67 def accident? [1, 3, 6, 8, 10].include?(number) end |
#enharmonic?(other) ⇒ Boolean
90 91 92 93 94 95 |
# File 'lib/coltrane/note.rb', line 90 def enharmonic?(other) case other when String then number == Note[other].number when Note then number == other.number end end |
#interval_to(note_name) ⇒ Object
86 87 88 |
# File 'lib/coltrane/note.rb', line 86 def interval_to(note_name) Note[note_name] - self end |
#pretty_name ⇒ Object
61 62 63 |
# File 'lib/coltrane/note.rb', line 61 def pretty_name @name.tr('b', "\u266D").tr('#', "\u266F") end |