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.



5
6
7
8
# File 'lib/head_music/melodic_interval.rb', line 5

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



90
91
92
# File 'lib/head_music/melodic_interval.rb', line 90

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.



3
4
5
# File 'lib/head_music/melodic_interval.rb', line 3

def first_note
  @first_note
end

#second_noteObject (readonly)

Returns the value of attribute second_note.



3
4
5
# File 'lib/head_music/melodic_interval.rb', line 3

def second_note
  @second_note
end

Instance Method Details

#ascending?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/head_music/melodic_interval.rb', line 42

def ascending?
  direction == :ascending
end

#descending?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/head_music/melodic_interval.rb', line 46

def descending?
  direction == :descending
end

#diatonic_intervalObject



10
11
12
# File 'lib/head_music/melodic_interval.rb', line 10

def diatonic_interval
  @diatonic_interval ||= HeadMusic::DiatonicInterval.new(first_pitch, second_pitch)
end

#directionObject



70
71
72
73
74
75
76
77
78
79
# File 'lib/head_music/melodic_interval.rb', line 70

def direction
  @direction ||=
    if first_pitch < second_pitch
      :ascending
    elsif first_pitch > second_pitch
      :descending
    else
      :none
    end
end

#first_pitchObject



30
31
32
# File 'lib/head_music/melodic_interval.rb', line 30

def first_pitch
  @first_pitch ||= first_note.pitch
end

#high_pitchObject



62
63
64
# File 'lib/head_music/melodic_interval.rb', line 62

def high_pitch
  pitches.max
end

#low_pitchObject



66
67
68
# File 'lib/head_music/melodic_interval.rb', line 66

def low_pitch
  pitches.min
end

#moving?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/head_music/melodic_interval.rb', line 50

def moving?
  ascending? || descending?
end

#notesObject



22
23
24
# File 'lib/head_music/melodic_interval.rb', line 22

def notes
  [first_note, second_note]
end

#pitchesObject



26
27
28
# File 'lib/head_music/melodic_interval.rb', line 26

def pitches
  [first_pitch, second_pitch]
end

#position_endObject



18
19
20
# File 'lib/head_music/melodic_interval.rb', line 18

def position_end
  second_note.next_position
end

#position_startObject



14
15
16
# File 'lib/head_music/melodic_interval.rb', line 14

def position_start
  first_note.position
end

#repetition?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/head_music/melodic_interval.rb', line 54

def repetition?
  !moving?
end

#respond_to_missing?(method_name, *_args) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/head_music/melodic_interval.rb', line 94

def respond_to_missing?(method_name, *_args)
  diatonic_interval.respond_to?(method_name)
end

#second_pitchObject



34
35
36
# File 'lib/head_music/melodic_interval.rb', line 34

def second_pitch
  @second_pitch ||= second_note.pitch
end

#spans?(pitch) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/head_music/melodic_interval.rb', line 58

def spans?(pitch)
  pitch >= low_pitch && pitch <= high_pitch
end

#spells_consonant_triad_with?(other_interval) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
88
# File 'lib/head_music/melodic_interval.rb', line 81

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



38
39
40
# File 'lib/head_music/melodic_interval.rb', line 38

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