Class: HeadMusic::Rudiment::Tuning

Inherits:
Base
  • Object
show all
Defined in:
lib/head_music/rudiment/tuning.rb

Overview

A tuning has a reference pitch and frequency and provides frequencies for all pitches The base class assumes equal temperament tuning. By default, A4 = 440.0 Hz

Direct Known Subclasses

JustIntonation, Meantone, Pythagorean

Defined Under Namespace

Classes: JustIntonation, Meantone, Pythagorean

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reference_pitch: :a440, tonal_center: nil) ⇒ Tuning

Returns a new instance of Tuning.



26
27
28
29
# File 'lib/head_music/rudiment/tuning.rb', line 26

def initialize(reference_pitch: :a440, tonal_center: nil)
  @reference_pitch = HeadMusic::Rudiment::ReferencePitch.get(reference_pitch)
  @tonal_center = tonal_center
end

Instance Attribute Details

#reference_pitchObject

Returns the value of attribute reference_pitch.



7
8
9
# File 'lib/head_music/rudiment/tuning.rb', line 7

def reference_pitch
  @reference_pitch
end

Class Method Details

.get(tuning_type = :equal_temperament, **options) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/head_music/rudiment/tuning.rb', line 11

def self.get(tuning_type = :equal_temperament, **options)
  case tuning_type.to_s.downcase
  when "just_intonation", "just", "ji"
    HeadMusic::Rudiment::Tuning::JustIntonation.new(**options)
  when "pythagorean", "pythag"
    HeadMusic::Rudiment::Tuning::Pythagorean.new(**options)
  when "meantone", "quarter_comma_meantone", "1/4_comma"
    HeadMusic::Rudiment::Tuning::Meantone.new(**options)
  when "equal_temperament", "equal", "et", "12tet"
    new(**options)
  else
    new(**options)
  end
end

Instance Method Details

#calculate_tonal_center_frequencyObject (private)



38
39
40
41
42
# File 'lib/head_music/rudiment/tuning.rb', line 38

def calculate_tonal_center_frequency
  # Use equal temperament to get the tonal center frequency from the reference pitch
  interval_to_tonal_center = (tonal_center - reference_pitch.pitch).semitones
  reference_pitch_frequency * (2**(interval_to_tonal_center / 12.0))
end

#frequency_for(pitch) ⇒ Object



31
32
33
34
# File 'lib/head_music/rudiment/tuning.rb', line 31

def frequency_for(pitch)
  pitch = HeadMusic::Rudiment::Pitch.get(pitch)
  reference_pitch_frequency * (2**(1.0 / 12))**(pitch - reference_pitch.pitch).semitones
end

#ratio_for_interval(semitones) ⇒ Object (private)



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/head_music/rudiment/tuning.rb', line 44

def ratio_for_interval(semitones)
  # Handle octaves
  octaves = semitones / 12
  interval_within_octave = semitones % 12

  # Get the base ratio from the subclass's INTERVAL_RATIOS constant
  base_ratio = self.class::INTERVAL_RATIOS[self.class::INTERVAL_RATIOS.keys[interval_within_octave]]

  # Apply octave adjustments
  base_ratio * (2**octaves)
end