Class: HeadMusic::Analysis::MelodicInterval

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/analysis/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.



8
9
10
11
# File 'lib/head_music/analysis/melodic_interval.rb', line 8

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



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

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.



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

def first_note
  @first_note
end

#second_noteObject (readonly)

Returns the value of attribute second_note.



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

def second_note
  @second_note
end

Instance Method Details

#ascending?Boolean

Returns:

  • (Boolean)


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

def ascending?
  direction == :ascending
end

#descending?Boolean

Returns:

  • (Boolean)


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

def descending?
  direction == :descending
end

#diatonic_intervalObject



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

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

#directionObject



73
74
75
76
77
78
79
80
81
82
# File 'lib/head_music/analysis/melodic_interval.rb', line 73

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

#first_pitchObject



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

def first_pitch
  @first_pitch ||= first_note.pitch
end

#high_pitchObject



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

def high_pitch
  pitches.max
end

#low_pitchObject



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

def low_pitch
  pitches.min
end

#moving?Boolean

Returns:

  • (Boolean)


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

def moving?
  ascending? || descending?
end

#notesObject



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

def notes
  [first_note, second_note]
end

#pitchesObject



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

def pitches
  [first_pitch, second_pitch]
end

#position_endObject



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

def position_end
  second_note.next_position
end

#position_startObject



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

def position_start
  first_note.position
end

#repetition?Boolean

Returns:

  • (Boolean)


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

def repetition?
  !moving?
end

#respond_to_missing?(method_name, *_args) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/head_music/analysis/melodic_interval.rb', line 97

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

#second_pitchObject



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

def second_pitch
  @second_pitch ||= second_note.pitch
end

#spans?(pitch) ⇒ Boolean

Returns:

  • (Boolean)


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

def spans?(pitch)
  pitch.between?(low_pitch, high_pitch)
end

#spells_consonant_triad_with?(other_interval) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
88
89
90
91
# File 'lib/head_music/analysis/melodic_interval.rb', line 84

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::Analysis::PitchSet.new(combined_pitches).consonant_triad?
end

#to_sObject



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

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