Class: HeadMusic::MelodicInterval

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

#initialize(note1, note2) ⇒ MelodicInterval

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_noteObject (readonly)

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_noteObject (readonly)

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

Returns:

  • (Boolean)


44
45
46
# File 'lib/head_music/melodic_interval.rb', line 44

def ascending?
  direction == :ascending
end

#descending?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/head_music/melodic_interval.rb', line 48

def descending?
  direction == :descending
end

#diatonic_intervalObject



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

#directionObject



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_pitchObject



32
33
34
# File 'lib/head_music/melodic_interval.rb', line 32

def first_pitch
  @first_pitch ||= first_note.pitch
end

#high_pitchObject



64
65
66
# File 'lib/head_music/melodic_interval.rb', line 64

def high_pitch
  pitches.max
end

#low_pitchObject



68
69
70
# File 'lib/head_music/melodic_interval.rb', line 68

def low_pitch
  pitches.min
end

#moving?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/head_music/melodic_interval.rb', line 52

def moving?
  ascending? || descending?
end

#notesObject



24
25
26
# File 'lib/head_music/melodic_interval.rb', line 24

def notes
  [first_note, second_note]
end

#pitchesObject



28
29
30
# File 'lib/head_music/melodic_interval.rb', line 28

def pitches
  [first_pitch, second_pitch]
end

#position_endObject



20
21
22
# File 'lib/head_music/melodic_interval.rb', line 20

def position_end
  second_note.next_position
end

#position_startObject



16
17
18
# File 'lib/head_music/melodic_interval.rb', line 16

def position_start
  first_note.position
end

#repetition?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/head_music/melodic_interval.rb', line 56

def repetition?
  !moving?
end

#respond_to_missing?(method_name, *_args) ⇒ Boolean

Returns:

  • (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_pitchObject



36
37
38
# File 'lib/head_music/melodic_interval.rb', line 36

def second_pitch
  @second_pitch ||= second_note.pitch
end

#spans?(pitch) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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_sObject



40
41
42
# File 'lib/head_music/melodic_interval.rb', line 40

def to_s
  [direction, diatonic_interval].join(" ")
end