Class: HeadMusic::DiatonicInterval

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/head_music/diatonic_interval.rb

Overview

A diatonic interval is the distance between two spelled pitches.

Defined Under Namespace

Classes: Category, Naming, Parser, Semitones, Size

Constant Summary collapse

NUMBER_NAMES =

TODO: include Named module

%w[
  unison second third fourth fifth sixth seventh octave
  ninth tenth eleventh twelfth thirteenth fourteenth fifteenth
  sixteenth seventeenth
].freeze
NAME_SUFFIXES =
Hash.new("th").merge(1 => "st", 2 => "nd", 3 => "rd").freeze
QUALITY_SEMITONES =
{
  unison: {perfect: 0},
  second: {major: 2},
  third: {major: 4},
  fourth: {perfect: 5},
  fifth: {perfect: 7},
  sixth: {major: 9},
  seventh: {major: 11},
  octave: {perfect: 12},
  ninth: {major: 14},
  tenth: {major: 16},
  eleventh: {perfect: 17},
  twelfth: {perfect: 19},
  thirteenth: {major: 21},
  fourteenth: {major: 23},
  fifteenth: {perfect: 24},
  sixteenth: {major: 26},
  seventeenth: {major: 28}
}.freeze
QUALITY_ABBREVIATIONS =
{
  P: "perfect",
  M: "major",
  m: "minor",
  d: "diminished",
  A: "augmented"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pitch1, pitch2) ⇒ DiatonicInterval

Returns a new instance of DiatonicInterval.



292
293
294
295
296
# File 'lib/head_music/diatonic_interval.rb', line 292

def initialize(pitch1, pitch2)
  pitch1 = HeadMusic::Pitch.get(pitch1)
  pitch2 = HeadMusic::Pitch.get(pitch2)
  @lower_pitch, @higher_pitch = [pitch1, pitch2].sort
end

Instance Attribute Details

#higher_pitchObject (readonly)

Returns the value of attribute higher_pitch.



43
44
45
# File 'lib/head_music/diatonic_interval.rb', line 43

def higher_pitch
  @higher_pitch
end

#lower_pitchObject (readonly)

Returns the value of attribute lower_pitch.



43
44
45
# File 'lib/head_music/diatonic_interval.rb', line 43

def lower_pitch
  @lower_pitch
end

Class Method Details

.get(identifier) ⇒ Object

Accepts a name and returns the interval with middle c on the bottom



285
286
287
288
289
290
# File 'lib/head_music/diatonic_interval.rb', line 285

def self.get(identifier)
  name = Parser.new(identifier)
  semitones = Semitones.new(name.degree_name.to_sym, name.quality_name).count
  higher_pitch = HeadMusic::Pitch.from_number_and_letter(HeadMusic::Pitch.middle_c + semitones, name.higher_letter)
  new(HeadMusic::Pitch.middle_c, higher_pitch)
end

Instance Method Details

#<=>(other) ⇒ Object



356
357
358
359
# File 'lib/head_music/diatonic_interval.rb', line 356

def <=>(other)
  other = self.class.get(other) unless other.is_a?(HeadMusic::DiatonicInterval)
  semitones <=> other.semitones
end

#above(pitch) ⇒ Object



334
335
336
337
# File 'lib/head_music/diatonic_interval.rb', line 334

def above(pitch)
  pitch = HeadMusic::Pitch.get(pitch)
  HeadMusic::Pitch.from_number_and_letter(pitch + semitones, pitch.letter_name.steps_up(number - 1))
end

#below(pitch) ⇒ Object



339
340
341
342
# File 'lib/head_music/diatonic_interval.rb', line 339

def below(pitch)
  pitch = HeadMusic::Pitch.get(pitch)
  HeadMusic::Pitch.from_number_and_letter(pitch - semitones, pitch.letter_name.steps_down(number - 1))
end

#consonance(style = :standard_practice) ⇒ Object



311
312
313
314
315
# File 'lib/head_music/diatonic_interval.rb', line 311

def consonance(style = :standard_practice)
  consonance_for_perfect(style) ||
    consonance_for_major_and_minor ||
    HeadMusic::Consonance.get(:dissonant)
end

#consonance?(style = :standard_practice) ⇒ Boolean Also known as: consonant?

Returns:

  • (Boolean)


317
318
319
# File 'lib/head_music/diatonic_interval.rb', line 317

def consonance?(style = :standard_practice)
  consonance(style).perfect? || consonance(style).imperfect?
end

#dissonance?(style = :standard_practice) ⇒ Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/head_music/diatonic_interval.rb', line 330

def dissonance?(style = :standard_practice)
  consonance(style).dissonant?
end

#imperfect_consonance?(style = :standard_practice) ⇒ Boolean

Returns:

  • (Boolean)


326
327
328
# File 'lib/head_music/diatonic_interval.rb', line 326

def imperfect_consonance?(style = :standard_practice)
  consonance(style).imperfect?
end

#interval_classObject



344
345
346
# File 'lib/head_music/diatonic_interval.rb', line 344

def interval_class
  [simple_semitones, 12 - simple_semitones].min
end

#interval_class_nameObject



348
349
350
# File 'lib/head_music/diatonic_interval.rb', line 348

def interval_class_name
  "ic #{interval_class}"
end

#inversionObject Also known as: invert



302
303
304
305
306
307
308
# File 'lib/head_music/diatonic_interval.rb', line 302

def inversion
  inverted_low_pitch = lower_pitch
  while inverted_low_pitch < higher_pitch
    inverted_low_pitch = HeadMusic::Pitch.fetch_or_create(lower_pitch.spelling, inverted_low_pitch.register + 1)
  end
  HeadMusic::DiatonicInterval.new(higher_pitch, inverted_low_pitch)
end

#perfect_consonance?(style = :standard_practice) ⇒ Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/head_music/diatonic_interval.rb', line 322

def perfect_consonance?(style = :standard_practice)
  consonance(style).perfect?
end

#qualityObject



298
299
300
# File 'lib/head_music/diatonic_interval.rb', line 298

def quality
  HeadMusic::Quality.get(quality_name)
end