class HeadMusic::MelodicInterval

A melodic interval is the distance between one note and the next.

Attributes

first_note[R]
second_note[R]

Public Class Methods

new(note1, note2) click to toggle source
# File lib/head_music/melodic_interval.rb, line 7
def initialize(note1, note2)
  @first_note = note1
  @second_note = note2
end

Public Instance Methods

ascending?() click to toggle source
# File lib/head_music/melodic_interval.rb, line 44
def ascending?
  direction == :ascending
end
descending?() click to toggle source
# File lib/head_music/melodic_interval.rb, line 48
def descending?
  direction == :descending
end
diatonic_interval() click to toggle source
# File lib/head_music/melodic_interval.rb, line 12
def diatonic_interval
  @diatonic_interval ||= HeadMusic::DiatonicInterval.new(first_pitch, second_pitch)
end
direction() click to toggle source
# 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() click to toggle source
# File lib/head_music/melodic_interval.rb, line 32
def first_pitch
  @first_pitch ||= first_note.pitch
end
high_pitch() click to toggle source
# File lib/head_music/melodic_interval.rb, line 64
def high_pitch
  pitches.max
end
low_pitch() click to toggle source
# File lib/head_music/melodic_interval.rb, line 68
def low_pitch
  pitches.min
end
method_missing(method_name, *args, &block) click to toggle source
Calls superclass method
# 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
moving?() click to toggle source
# File lib/head_music/melodic_interval.rb, line 52
def moving?
  ascending? || descending?
end
notes() click to toggle source
# File lib/head_music/melodic_interval.rb, line 24
def notes
  [first_note, second_note]
end
pitches() click to toggle source
# File lib/head_music/melodic_interval.rb, line 28
def pitches
  [first_pitch, second_pitch]
end
position_end() click to toggle source
# File lib/head_music/melodic_interval.rb, line 20
def position_end
  second_note.next_position
end
position_start() click to toggle source
# File lib/head_music/melodic_interval.rb, line 16
def position_start
  first_note.position
end
repetition?() click to toggle source
# File lib/head_music/melodic_interval.rb, line 56
def repetition?
  !moving?
end
respond_to_missing?(method_name, *_args) click to toggle source
# 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() click to toggle source
# File lib/head_music/melodic_interval.rb, line 36
def second_pitch
  @second_pitch ||= second_note.pitch
end
spans?(pitch) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/head_music/melodic_interval.rb, line 40
def to_s
  [direction, diatonic_interval].join(' ')
end