Class: HeadMusic::Rudiment::Pitch::Arithmetic

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#otherObject (readonly)

Returns the value of attribute other.



8
9
10
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 8

def other
  @other
end

#pitchObject (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

#differenceObject



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)

Returns:

  • (Boolean)


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)

Returns:

  • (Boolean)


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_apartObject (private)



30
31
32
# File 'lib/head_music/rudiment/pitch/arithmetic.rb', line 30

def semitones_apart
  pitch.to_i - other.to_i
end

#sumObject



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