Class: HeadMusic::MelodicInterval
- Inherits:
-
Object
- Object
- HeadMusic::MelodicInterval
show all
- Defined in:
- lib/head_music/melodic_interval.rb
Overview
A melodic interval is the distance between one note and the next.
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of MelodicInterval.
7
8
9
10
|
# File 'lib/head_music/melodic_interval.rb', line 7
def initialize(note1, note2)
@first_note = note1
@second_note = note2
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
92
93
94
|
# File 'lib/head_music/melodic_interval.rb', line 92
def method_missing(method_name, *args, &block)
respond_to_missing?(method_name) ? diatonic_interval.send(method_name, *args, &block) : super
end
|
Instance Attribute Details
#first_note ⇒ Object
Returns the value of attribute first_note.
5
6
7
|
# File 'lib/head_music/melodic_interval.rb', line 5
def first_note
@first_note
end
|
#second_note ⇒ Object
Returns the value of attribute second_note.
5
6
7
|
# File 'lib/head_music/melodic_interval.rb', line 5
def second_note
@second_note
end
|
Instance Method Details
#ascending? ⇒ Boolean
44
45
46
|
# File 'lib/head_music/melodic_interval.rb', line 44
def ascending?
direction == :ascending
end
|
#descending? ⇒ Boolean
48
49
50
|
# File 'lib/head_music/melodic_interval.rb', line 48
def descending?
direction == :descending
end
|
#diatonic_interval ⇒ Object
12
13
14
|
# File 'lib/head_music/melodic_interval.rb', line 12
def diatonic_interval
@diatonic_interval ||= HeadMusic::DiatonicInterval.new(first_pitch, second_pitch)
end
|
#direction ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/head_music/melodic_interval.rb', line 72
def direction
@direction ||=
if first_pitch < second_pitch
:ascending
elsif first_pitch > second_pitch
:descending
else
:none
end
end
|
#first_pitch ⇒ Object
32
33
34
|
# File 'lib/head_music/melodic_interval.rb', line 32
def first_pitch
@first_pitch ||= first_note.pitch
end
|
#high_pitch ⇒ Object
64
65
66
|
# File 'lib/head_music/melodic_interval.rb', line 64
def high_pitch
pitches.max
end
|
#low_pitch ⇒ Object
68
69
70
|
# File 'lib/head_music/melodic_interval.rb', line 68
def low_pitch
pitches.min
end
|
#moving? ⇒ Boolean
52
53
54
|
# File 'lib/head_music/melodic_interval.rb', line 52
def moving?
ascending? || descending?
end
|
#notes ⇒ Object
24
25
26
|
# File 'lib/head_music/melodic_interval.rb', line 24
def notes
[first_note, second_note]
end
|
#pitches ⇒ Object
28
29
30
|
# File 'lib/head_music/melodic_interval.rb', line 28
def pitches
[first_pitch, second_pitch]
end
|
#position_end ⇒ Object
20
21
22
|
# File 'lib/head_music/melodic_interval.rb', line 20
def position_end
second_note.next_position
end
|
#position_start ⇒ Object
16
17
18
|
# File 'lib/head_music/melodic_interval.rb', line 16
def position_start
first_note.position
end
|
#repetition? ⇒ Boolean
56
57
58
|
# File 'lib/head_music/melodic_interval.rb', line 56
def repetition?
!moving?
end
|
#respond_to_missing?(method_name, *_args) ⇒ Boolean
96
97
98
|
# File 'lib/head_music/melodic_interval.rb', line 96
def respond_to_missing?(method_name, *_args)
diatonic_interval.respond_to?(method_name)
end
|
#second_pitch ⇒ Object
36
37
38
|
# File 'lib/head_music/melodic_interval.rb', line 36
def second_pitch
@second_pitch ||= second_note.pitch
end
|
#spans?(pitch) ⇒ Boolean
60
61
62
|
# File 'lib/head_music/melodic_interval.rb', line 60
def spans?(pitch)
pitch >= low_pitch && pitch <= high_pitch
end
|
#spells_consonant_triad_with?(other_interval) ⇒ Boolean
83
84
85
86
87
88
89
90
|
# File 'lib/head_music/melodic_interval.rb', line 83
def spells_consonant_triad_with?(other_interval)
return false if step? || other_interval.step?
combined_pitches = (pitches + other_interval.pitches).uniq
return false if combined_pitches.length < 3
HeadMusic::PitchSet.new(combined_pitches).consonant_triad?
end
|
#to_s ⇒ Object
40
41
42
|
# File 'lib/head_music/melodic_interval.rb', line 40
def to_s
[direction, diatonic_interval].join(" ")
end
|