Class: WTFChord::Pitch
- Inherits:
-
Object
- Object
- WTFChord::Pitch
- Extended by:
- Forwardable
- Includes:
- Comparable
- Defined in:
- lib/wtf_chord/pitch.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#note ⇒ Object
readonly
Returns the value of attribute note.
-
#octave ⇒ Object
readonly
Returns the value of attribute octave.
Instance Method Summary collapse
- #-(amount) ⇒ Object
- #-@ ⇒ Object
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
- #chord(name) ⇒ Object
-
#initialize(note, octave = nil) ⇒ Pitch
constructor
A new instance of Pitch.
- #move(amount) ⇒ Object (also: #+)
- #to_i ⇒ Object
- #to_s ⇒ Object (also: #inspect)
- #to_str ⇒ Object
Constructor Details
Instance Attribute Details
#note ⇒ Object (readonly)
Returns the value of attribute note.
11 12 13 |
# File 'lib/wtf_chord/pitch.rb', line 11 def note @note end |
#octave ⇒ Object (readonly)
Returns the value of attribute octave.
11 12 13 |
# File 'lib/wtf_chord/pitch.rb', line 11 def octave @octave end |
Instance Method Details
#-(amount) ⇒ Object
45 46 47 |
# File 'lib/wtf_chord/pitch.rb', line 45 def - amount move -amount end |
#-@ ⇒ Object
49 50 51 |
# File 'lib/wtf_chord/pitch.rb', line 49 def -@ -to_i end |
#<=>(other) ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/wtf_chord/pitch.rb', line 65 def <=> other case other when Note then @note <=> other when Integer then to_i <=> other when Pitch then to_i <=> other.to_i end end |
#==(other) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/wtf_chord/pitch.rb', line 57 def == other case other when Integer, Pitch then other == to_i when String then other.casecmp(to_str).zero? when Note then other == @note end end |
#chord(name) ⇒ Object
73 74 75 |
# File 'lib/wtf_chord/pitch.rb', line 73 def chord(name) Chord.new(self, name) end |
#move(amount) ⇒ Object Also known as: +
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/wtf_chord/pitch.rb', line 30 def move(amount) return self if amount.zero? to_octave, to_pos = (to_i + amount).divmod(12) if to_pos.zero? to_octave -= 1 to_pos = 12 end SCALE[to_pos][to_octave] end |
#to_i ⇒ Object
53 54 55 |
# File 'lib/wtf_chord/pitch.rb', line 53 def to_i (@octave * 12) + @note.position end |
#to_s ⇒ Object Also known as: inspect
24 25 26 |
# File 'lib/wtf_chord/pitch.rb', line 24 def to_s "#{@note.to_s}#{octave}" end |
#to_str ⇒ Object
20 21 22 |
# File 'lib/wtf_chord/pitch.rb', line 20 def to_str "#{@note.key}#{octave}" end |