Class: HeadMusic::Rudiment::Pitch::Arithmetic
- Inherits:
-
Object
- Object
- HeadMusic::Rudiment::Pitch::Arithmetic
- Defined in:
- lib/head_music/rudiment/pitch/arithmetic.rb
Overview
What it means to add to or subtract from a pitch depends on what the other operand is: an interval moves the pitch, a bare number moves it by that many semitones, and subtracting another pitch measures the distance between the two. Every branch reads the operand rather than the pitch, so the dispatch lives here with both in hand rather than on Pitch.
Instance Attribute Summary collapse
-
#other ⇒ Object
readonly
Returns the value of attribute other.
-
#pitch ⇒ Object
readonly
Returns the value of attribute pitch.
Instance Method Summary collapse
-
#difference ⇒ Object
-
#initialize(pitch, other) ⇒ Arithmetic
constructor
A new instance of Arithmetic.
-
#interval? ⇒ Boolean
private
-
#pitch? ⇒ Boolean
private
-
#pitch_at(number) ⇒ Object
private
-
#semitones_apart ⇒ Object
private
-
#sum ⇒ Object
Constructor Details
#initialize(pitch, other) ⇒ Arithmetic
Returns a new instance of Arithmetic.
10 11 12 13 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 10 def initialize(pitch, other) @pitch = pitch @other = other end |
Instance Attribute Details
#other ⇒ Object (readonly)
Returns the value of attribute other.
8 9 10 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 8 def other @other end |
#pitch ⇒ Object (readonly)
Returns the value of attribute pitch.
8 9 10 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 8 def pitch @pitch end |
Instance Method Details
#difference ⇒ Object
21 22 23 24 25 26 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 21 def difference return other.below(pitch) if interval? return HeadMusic::Rudiment::ChromaticInterval.get(semitones_apart) if pitch? pitch_at(semitones_apart) end |
#interval? ⇒ Boolean (private)
34 35 36 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 34 def interval? other.is_a?(HeadMusic::Analysis::DiatonicInterval) end |
#pitch? ⇒ Boolean (private)
38 39 40 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 38 def pitch? other.is_a?(HeadMusic::Rudiment::Pitch) end |
#pitch_at(number) ⇒ Object (private)
42 43 44 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 42 def pitch_at(number) HeadMusic::Rudiment::Pitch.get(number) end |
#semitones_apart ⇒ Object (private)
30 31 32 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 30 def semitones_apart pitch.to_i - other.to_i end |
#sum ⇒ Object
15 16 17 18 19 |
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 15 def sum return other.above(pitch) if interval? pitch_at(pitch.to_i + other.to_i) end |