Class: HeadMusic::HarmonicInterval
- Inherits:
-
Object
- Object
- HeadMusic::HarmonicInterval
show all
- Defined in:
- lib/head_music/harmonic_interval.rb
Overview
A harmonic interval is the diatonic interval between two notes sounding together.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(voice1, voice2, position) ⇒ HarmonicInterval
Returns a new instance of HarmonicInterval.
7
8
9
10
11
|
# File 'lib/head_music/harmonic_interval.rb', line 7
def initialize(voice1, voice2, position)
@voice1 = voice1
@voice2 = voice2
@position = position.is_a?(String) ? HeadMusic::Position.new(voice1.composition, position) : position
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
59
60
61
|
# File 'lib/head_music/harmonic_interval.rb', line 59
def method_missing(method_name, *args, &block)
respond_to_missing?(method_name) ? diatonic_interval.send(method_name, *args, &block) : super
end
|
Instance Attribute Details
#position ⇒ Object
Returns the value of attribute position.
5
6
7
|
# File 'lib/head_music/harmonic_interval.rb', line 5
def position
@position
end
|
#voice1 ⇒ Object
Returns the value of attribute voice1.
5
6
7
|
# File 'lib/head_music/harmonic_interval.rb', line 5
def voice1
@voice1
end
|
#voice2 ⇒ Object
Returns the value of attribute voice2.
5
6
7
|
# File 'lib/head_music/harmonic_interval.rb', line 5
def voice2
@voice2
end
|
Instance Method Details
#diatonic_interval ⇒ Object
13
14
15
|
# File 'lib/head_music/harmonic_interval.rb', line 13
def diatonic_interval
@diatonic_interval ||= HeadMusic::DiatonicInterval.new(lower_pitch, upper_pitch)
end
|
#lower_note ⇒ Object
25
26
27
|
# File 'lib/head_music/harmonic_interval.rb', line 25
def lower_note
notes.first
end
|
#lower_pitch ⇒ Object
37
38
39
|
# File 'lib/head_music/harmonic_interval.rb', line 37
def lower_pitch
pitches.first
end
|
#notes ⇒ Object
21
22
23
|
# File 'lib/head_music/harmonic_interval.rb', line 21
def notes
@notes ||= voices.map { |voice| voice.note_at(position) }.compact.sort_by(&:pitch)
end
|
#pitch_orientation ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/head_music/harmonic_interval.rb', line 45
def pitch_orientation
return if lower_pitch == upper_pitch
if lower_note.voice == voice1
:up
elsif lower_note.voice == voice2
:down
end
end
|
#pitches ⇒ Object
33
34
35
|
# File 'lib/head_music/harmonic_interval.rb', line 33
def pitches
@pitches ||= notes.map(&:pitch).sort_by(&:to_i)
end
|
#respond_to_missing?(method_name, *_args) ⇒ Boolean
63
64
65
|
# File 'lib/head_music/harmonic_interval.rb', line 63
def respond_to_missing?(method_name, *_args)
diatonic_interval.respond_to?(method_name)
end
|
#to_s ⇒ Object
55
56
57
|
# File 'lib/head_music/harmonic_interval.rb', line 55
def to_s
"#{diatonic_interval} at #{position}"
end
|
#upper_note ⇒ Object
29
30
31
|
# File 'lib/head_music/harmonic_interval.rb', line 29
def upper_note
notes.last
end
|
#upper_pitch ⇒ Object
41
42
43
|
# File 'lib/head_music/harmonic_interval.rb', line 41
def upper_pitch
pitches.last
end
|
#voices ⇒ Object
17
18
19
|
# File 'lib/head_music/harmonic_interval.rb', line 17
def voices
[voice1, voice2].compact
end
|