Class: HeadMusic::Rudiment::Tuning
- 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
Defined Under Namespace
Classes: JustIntonation, Meantone, Pythagorean
Instance Attribute Summary collapse
-
#reference_pitch ⇒ Object
Returns the value of attribute reference_pitch.
-
#tonal_center ⇒ Object
readonly
Returns the value of attribute tonal_center.
Class Method Summary collapse
Instance Method Summary collapse
-
#calculate_tonal_center_frequency ⇒ Object
private
-
#equal_temperament_frequency(pitch) ⇒ Object
private
-
#frequency_for(pitch) ⇒ Object
-
#initialize(reference_pitch: :a440, tonal_center: nil) ⇒ Tuning
constructor
A new instance of Tuning.
-
#ratio_for_interval(semitones) ⇒ Object
private
Constructor Details
#initialize(reference_pitch: :a440, tonal_center: nil) ⇒ Tuning
Returns a new instance of Tuning.
28 29 30 31 |
# File 'lib/head_music/rudiment/tuning.rb', line 28 def initialize(reference_pitch: :a440, tonal_center: nil) @reference_pitch = HeadMusic::Rudiment::ReferencePitch.get(reference_pitch) @tonal_center = tonal_center ? HeadMusic::Rudiment::Pitch.get(tonal_center) : nil end |
Instance Attribute Details
#reference_pitch ⇒ Object
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 |
#tonal_center ⇒ Object (readonly)
Returns the value of attribute tonal_center.
26 27 28 |
# File 'lib/head_music/rudiment/tuning.rb', line 26 def tonal_center @tonal_center 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, **) case tuning_type.to_s.downcase when "just_intonation", "just", "ji" HeadMusic::Rudiment::Tuning::JustIntonation.new(**) when "pythagorean", "pythag" HeadMusic::Rudiment::Tuning::Pythagorean.new(**) when "meantone", "quarter_comma_meantone", "1/4_comma" HeadMusic::Rudiment::Tuning::Meantone.new(**) when "equal_temperament", "equal", "et", "12tet" new(**) else new(**) end end |
Instance Method Details
#calculate_tonal_center_frequency ⇒ Object (private)
49 50 51 52 53 |
# File 'lib/head_music/rudiment/tuning.rb', line 49 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 |
#equal_temperament_frequency(pitch) ⇒ Object (private)
45 46 47 |
# File 'lib/head_music/rudiment/tuning.rb', line 45 def equal_temperament_frequency(pitch) reference_pitch_frequency * (2**(1.0 / 12))**(pitch - reference_pitch.pitch).semitones end |
#frequency_for(pitch) ⇒ Object
33 34 35 36 37 38 39 40 41 |
# File 'lib/head_music/rudiment/tuning.rb', line 33 def frequency_for(pitch) pitch = HeadMusic::Rudiment::Pitch.get(pitch) return equal_temperament_frequency(pitch) unless tonal_center tonal_center_frequency = calculate_tonal_center_frequency interval_from_tonal_center = (pitch - tonal_center).semitones ratio = ratio_for_interval(interval_from_tonal_center) tonal_center_frequency * ratio end |
#ratio_for_interval(semitones) ⇒ Object (private)
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/head_music/rudiment/tuning.rb', line 55 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 |