Class: HeadMusic::Analysis::DiatonicInterval

Inherits:
Object
  • Object
show all
Includes:
Comparable, ConsonanceQuestions, Named
Defined in:
lib/head_music/analysis/diatonic_interval.rb,
lib/head_music/analysis/diatonic_interval/inversion.rb,
lib/head_music/analysis/diatonic_interval/localization.rb,
lib/head_music/analysis/diatonic_interval/consonance_questions.rb

Overview

A diatonic interval is the distance between two spelled pitches.

Defined Under Namespace

Modules: ConsonanceQuestions Classes: Category, Inversion, Localization, Naming, Parser, Semitones, Size

Constant Summary collapse

NUMBER_NAMES =
%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(first_pitch, second_pitch) ⇒ DiatonicInterval

Returns a new instance of DiatonicInterval.



81
82
83
84
85
# File 'lib/head_music/analysis/diatonic_interval.rb', line 81

def initialize(first_pitch, second_pitch)
  first_pitch = HeadMusic::Rudiment::Pitch.get(first_pitch)
  second_pitch = HeadMusic::Rudiment::Pitch.get(second_pitch)
  @lower_pitch, @higher_pitch = [first_pitch, second_pitch].sort
end

Instance Attribute Details

#alias_name_keysObject (readonly) Originally defined in module Named

Returns the value of attribute alias_name_keys.

#higher_pitchObject (readonly)

Returns the value of attribute higher_pitch.



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

def higher_pitch
  @higher_pitch
end

#lower_pitchObject (readonly)

Returns the value of attribute lower_pitch.



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

def lower_pitch
  @lower_pitch
end

#name_keyObject (readonly) Originally defined in module Named

Returns the value of attribute name_key.

Class Method Details

.get(identifier) ⇒ Object

Accepts a name and returns the interval with middle c on the bottom. Anything else is assumed to be an interval already.



73
74
75
76
77
78
79
# File 'lib/head_music/analysis/diatonic_interval.rb', line 73

def self.get(identifier)
  return identifier unless identifier.is_a?(String) || identifier.is_a?(Symbol)

  interval = Parser.new(identifier).interval
  interval.ensure_localized_name(name: identifier.to_s)
  interval
end

Instance Method Details

#<=>(other) ⇒ Object



120
121
122
123
# File 'lib/head_music/analysis/diatonic_interval.rb', line 120

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

#above(pitch) ⇒ Object



100
101
102
# File 'lib/head_music/analysis/diatonic_interval.rb', line 100

def above(pitch)
  displaced(pitch, 1)
end

#below(pitch) ⇒ Object



104
105
106
# File 'lib/head_music/analysis/diatonic_interval.rb', line 104

def below(pitch)
  displaced(pitch, -1)
end

#categoryObject (private)



149
150
151
# File 'lib/head_music/analysis/diatonic_interval.rb', line 149

def category
  @category ||= Category.new(number)
end

#consonance(style = STANDARD_PRACTICE) ⇒ Object Originally defined in module ConsonanceQuestions

#consonance_analysis(style = STANDARD_PRACTICE) ⇒ Object Originally defined in module ConsonanceQuestions

#consonance_classification(style: STANDARD_PRACTICE) ⇒ Object Originally defined in module ConsonanceQuestions

#consonant?(style = STANDARD_PRACTICE) ⇒ Boolean Also known as: consonance? Originally defined in module ConsonanceQuestions

Returns:

  • (Boolean)

#displaced(pitch, direction) ⇒ Object (private)

Both directions are the same move with the sign flipped: as many semitones and as many letter names as the interval spans, counted the other way.



137
138
139
140
141
142
143
# File 'lib/head_music/analysis/diatonic_interval.rb', line 137

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

#dissonant?(style = STANDARD_PRACTICE) ⇒ Boolean Also known as: dissonance? Originally defined in module ConsonanceQuestions

Returns:

  • (Boolean)

#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named

#imperfect_consonance?(style = STANDARD_PRACTICE) ⇒ Boolean Originally defined in module ConsonanceQuestions

Returns:

  • (Boolean)

#interval_classObject



108
109
110
# File 'lib/head_music/analysis/diatonic_interval.rb', line 108

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

#interval_class_nameObject



112
113
114
# File 'lib/head_music/analysis/diatonic_interval.rb', line 112

def interval_class_name
  "ic #{interval_class}"
end

#inversionObject Also known as: invert



95
96
97
# File 'lib/head_music/analysis/diatonic_interval.rb', line 95

def inversion
  Inversion.new(lower_pitch, higher_pitch).interval
end

#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named

#localized_name_in_default_localeObject (private) Originally defined in module Named

#localized_name_in_locale_matching_language(locale) ⇒ Object (private) Originally defined in module Named

#localized_name_in_matching_locale(locale) ⇒ Object (private) Originally defined in module Named

#localized_namesObject Originally defined in module Named

Returns an array of LocalizedName instances that are synonymous with the name.

#name(locale_code: nil) ⇒ Object

Overrides Named, which registers a name per object; an interval computes its own and looks the translation up by it.



63
64
65
# File 'lib/head_music/analysis/diatonic_interval.rb', line 63

def name(locale_code: nil)
  Localization.new(naming.name, locale_code).name
end

#name=(name) ⇒ Object Originally defined in module Named

#namingObject (private)



153
154
155
# File 'lib/head_music/analysis/diatonic_interval.rb', line 153

def naming
  @naming ||= Naming.new(number: number, semitones: semitones)
end

#perfect_consonance?(style = STANDARD_PRACTICE) ⇒ Boolean Originally defined in module ConsonanceQuestions

Returns:

  • (Boolean)

#qualityObject



91
92
93
# File 'lib/head_music/analysis/diatonic_interval.rb', line 91

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

#sizeObject (private)



145
146
147
# File 'lib/head_music/analysis/diatonic_interval.rb', line 145

def size
  @size ||= Size.new(@lower_pitch, @higher_pitch)
end

#spans?(pitch) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/head_music/analysis/diatonic_interval.rb', line 87

def spans?(pitch)
  pitch.between?(lower_pitch, higher_pitch)
end

#to_sObject



67
68
69
# File 'lib/head_music/analysis/diatonic_interval.rb', line 67

def to_s
  name
end