Class: HeadMusic::Rudiment::ReferencePitch
- Includes:
- Named
- Defined in:
- lib/head_music/rudiment/reference_pitch.rb
Overview
A reference pitch has a pitch and a frequency With no arguments, it assumes that A4 = 440.0 Hz
Constant Summary collapse
- DEFAULT_PITCH_NAME =
"A4"- DEFAULT_FREQUENCY =
440.0- DEFAULT_REFERENCE_PITCH_NAME =
"A440"- NAMED_REFERENCE_PITCHES =
{ frequency: 415.0, key: :baroque, alias_keys: %i[chamber_tone] }, { frequency: 430.0, key: :classical, alias_keys: %i[haydn mozart] }, { pitch: "C4", frequency: 256.0, key: :scientific, alias_keys: %i[philosophical sauveur] }, { frequency: 432.0, tuning: "Pythagorean", key: :verdi }, { frequency: 435.0, key: :french, alias_keys: %i[continental international] }, { frequency: 439.0, key: :new_philharmonic, alias_keys: %i[low] }, { frequency: 440.0, key: :a440, alias_keys: %i[concert stuttgart scheibler iso_16] }, { frequency: 441.0, key: :sydney_symphony_orchestra }, { frequency: 442.0, key: :new_york_philharmonic }, { frequency: 443.0, key: :berlin_philharmonic }, { frequency: 444.0, key: :boston_symphony_orchestra }, { frequency: 452.4, key: :old_philharmonic, alias_keys: %i[high] }, { frequency: 466.0, key: :chorton, alias_keys: %i[choir] } ].freeze
Instance Attribute Summary collapse
-
#alias_name_keys ⇒ Object
included
from Named
readonly
Returns the value of attribute alias_name_keys.
-
#frequency ⇒ Object
readonly
Returns the value of attribute frequency.
-
#name_key ⇒ Object
included
from Named
readonly
Returns the value of attribute name_key.
-
#pitch ⇒ Object
readonly
Returns the value of attribute pitch.
Class Method Summary collapse
Instance Method Summary collapse
- #description ⇒ Object
- #ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object included from Named
-
#initialize(name = DEFAULT_REFERENCE_PITCH_NAME) ⇒ ReferencePitch
constructor
A new instance of ReferencePitch.
- #initialize_keys_from_record(record) ⇒ Object private
- #localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object included from Named
- #localized_name_in_default_locale ⇒ Object included from Named private
- #localized_name_in_locale_matching_language(locale) ⇒ Object included from Named private
- #localized_name_in_matching_locale(locale) ⇒ Object included from Named private
-
#localized_names ⇒ Object
included
from Named
Returns an array of LocalizedName instances that are synonymous with the name.
- #name(locale_code: Locale::DEFAULT_CODE) ⇒ Object included from Named
- #name=(name) ⇒ Object included from Named
- #name_and_alias_translations_for_keys(name_keys) ⇒ Object private
- #name_keys_from_record(record) ⇒ Object private
- #named_reference_pitch_record_for_name(name) ⇒ Object private
- #names_from_record(record) ⇒ Object private
- #normalized_name_string(name) ⇒ Object private
- #normalized_translations_for_keys(name_keys) ⇒ Object private
- #to_s ⇒ Object
Constructor Details
#initialize(name = DEFAULT_REFERENCE_PITCH_NAME) ⇒ ReferencePitch
Returns a new instance of ReferencePitch.
85 86 87 88 89 90 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 85 def initialize(name = DEFAULT_REFERENCE_PITCH_NAME) record = named_reference_pitch_record_for_name(name) @pitch = HeadMusic::Rudiment::Pitch.get(record.fetch(:pitch, DEFAULT_PITCH_NAME)) @frequency = record.fetch(:frequency, DEFAULT_FREQUENCY) initialize_keys_from_record(record) end |
Instance Attribute Details
#alias_name_keys ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute alias_name_keys.
#frequency ⇒ Object (readonly)
Returns the value of attribute frequency.
78 79 80 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 78 def frequency @frequency end |
#name_key ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute name_key.
#pitch ⇒ Object (readonly)
Returns the value of attribute pitch.
78 79 80 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 78 def pitch @pitch end |
Class Method Details
.get(name) ⇒ Object
80 81 82 83 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 80 def self.get(name) return name if name.is_a?(self) get_by_name(name) end |
Instance Method Details
#description ⇒ Object
92 93 94 95 96 97 98 99 100 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 92 def description [ pitch.letter_name, format( "%<with_digits>g", with_digits: format("%.2<frequency>f", frequency: frequency) ) ].join("=") end |
#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named
#initialize_keys_from_record(record) ⇒ Object (private)
128 129 130 131 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 128 def initialize_keys_from_record(record) @key = record[:key] @alias_keys = [record[:alias_keys]].flatten.compact end |
#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named
#localized_name_in_default_locale ⇒ Object (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_names ⇒ Object Originally defined in module Named
Returns an array of LocalizedName instances that are synonymous with the name.
#name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named
#name=(name) ⇒ Object Originally defined in module Named
#name_and_alias_translations_for_keys(name_keys) ⇒ Object (private)
139 140 141 142 143 144 145 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 139 def name_and_alias_translations_for_keys(name_keys) name_keys.map do |name_key| I18n.config.available_locales.map do |locale_code| I18n.translate(name_key, scope: %i[head_music reference_pitches], locale: locale_code) end.flatten.uniq.compact end.flatten.uniq.compact end |
#name_keys_from_record(record) ⇒ Object (private)
115 116 117 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 115 def name_keys_from_record(record) names_from_record(record).map { |name| HeadMusic::Utilities::HashKey.for(name) } end |
#named_reference_pitch_record_for_name(name) ⇒ Object (private)
108 109 110 111 112 113 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 108 def named_reference_pitch_record_for_name(name) key = HeadMusic::Utilities::HashKey.for(normalized_name_string(name)) NAMED_REFERENCE_PITCHES.detect do |record| name_keys_from_record(record).include?(key) end || named_reference_pitch_record_for_name(DEFAULT_REFERENCE_PITCH_NAME) end |
#names_from_record(record) ⇒ Object (private)
119 120 121 122 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 119 def names_from_record(record) name_keys = ([record[:key]] + [record[:alias_keys]]).flatten.compact.uniq normalized_translations_for_keys(name_keys) end |
#normalized_name_string(name) ⇒ Object (private)
124 125 126 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 124 def normalized_name_string(name) name.gsub(" pitch", "").gsub(" tone", "").gsub(" tuning", "") end |
#normalized_translations_for_keys(name_keys) ⇒ Object (private)
133 134 135 136 137 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 133 def normalized_translations_for_keys(name_keys) name_and_alias_translations_for_keys(name_keys).map do |name| normalized_name_string(name) end end |
#to_s ⇒ Object
102 103 104 |
# File 'lib/head_music/rudiment/reference_pitch.rb', line 102 def to_s description end |