Class: HeadMusic::Instruments::InstrumentType
- Inherits:
-
Object
- Object
- HeadMusic::Instruments::InstrumentType
- Includes:
- Named
- Defined in:
- lib/head_music/instruments/instrument_type.rb
Overview
A musical instrument type representing a catalog entry. An instrument type defines the base characteristics and available variants for an instrument. Attributes: name_key: the name of the instrument type alias_name_keys: an array of alternative names for the instrument type orchestra_section_key: the section of the orchestra (e.g. "strings") family_key: the key for the family of the instrument (e.g. "saxophone") classification_keys: an array of classification_keys default_clefs: the default clef or system of clefs for the instrument type - [treble] for instruments that use the treble clef - [treble, bass] for instruments that use the grand staff variants: a hash of default and alternative pitch designations Associations: family: the family of the instrument (e.g. "saxophone") orchestra_section: the section of the orchestra (e.g. "strings")
Constant Summary collapse
- INSTRUMENTS =
YAML.load_file(File.("instruments.yml", __dir__)).freeze
Instance Attribute Summary collapse
-
#alias_name_keys ⇒ Object
included
from Named
readonly
Returns the value of attribute alias_name_keys.
-
#alias_name_keys ⇒ Object
readonly
Returns the value of attribute alias_name_keys.
-
#classification_keys ⇒ Object
readonly
Returns the value of attribute classification_keys.
-
#family_key ⇒ Object
readonly
Returns the value of attribute family_key.
-
#name_key ⇒ Object
readonly
Returns the value of attribute name_key.
-
#name_key ⇒ Object
included
from Named
readonly
Returns the value of attribute name_key.
-
#orchestra_section_key ⇒ Object
readonly
Returns the value of attribute orchestra_section_key.
-
#variants ⇒ Object
readonly
Returns the value of attribute variants.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #default_clefs ⇒ Object
- #default_instrument ⇒ Object
- #default_sounding_transposition ⇒ Object
- #default_staff_scheme ⇒ Object
- #default_staves ⇒ Object
- #default_variant ⇒ Object
- #ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object included from Named
- #family ⇒ Object
- #inferred_name ⇒ Object private
- #inherit_family_attributes(record) ⇒ Object private
-
#initialize(name) ⇒ InstrumentType
constructor
private
A new instance of InstrumentType.
- #initialize_attributes(record) ⇒ Object private
- #initialize_data_from_record(record) ⇒ Object private
- #initialize_family(record) ⇒ Object private
- #initialize_names(record) ⇒ Object private
- #key_for_name(name) ⇒ 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.
- #multiple_staves? ⇒ Boolean
- #name(locale_code: Locale::DEFAULT_CODE) ⇒ Object included from Named
- #name=(name) ⇒ Object included from Named
- #pitched? ⇒ Boolean
- #record_for_alias(name) ⇒ Object private
- #record_for_key(key) ⇒ Object private
- #record_for_name(name) ⇒ Object private
- #single_staff? ⇒ Boolean
- #translation(locale = :en) ⇒ Object
-
#transposing? ⇒ Boolean
Returns true if the instrument sounds at a different pitch than written.
-
#transposing_at_the_octave? ⇒ Boolean
Returns true if the instrument sounds at a different register than written.
Constructor Details
#initialize(name) ⇒ InstrumentType (private)
109 110 111 112 113 114 115 116 |
# File 'lib/head_music/instruments/instrument_type.rb', line 109 def initialize(name) record = record_for_name(name) if record initialize_data_from_record(record) else self.name = name.to_s end end |
Instance Attribute Details
#alias_name_keys ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute alias_name_keys.
#alias_name_keys ⇒ Object (readonly)
Returns the value of attribute alias_name_keys.
35 36 37 |
# File 'lib/head_music/instruments/instrument_type.rb', line 35 def alias_name_keys @alias_name_keys end |
#classification_keys ⇒ Object (readonly)
Returns the value of attribute classification_keys.
35 36 37 |
# File 'lib/head_music/instruments/instrument_type.rb', line 35 def classification_keys @classification_keys end |
#family_key ⇒ Object (readonly)
Returns the value of attribute family_key.
35 36 37 |
# File 'lib/head_music/instruments/instrument_type.rb', line 35 def family_key @family_key end |
#name_key ⇒ Object (readonly)
Returns the value of attribute name_key.
35 36 37 |
# File 'lib/head_music/instruments/instrument_type.rb', line 35 def name_key @name_key end |
#name_key ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute name_key.
#orchestra_section_key ⇒ Object (readonly)
Returns the value of attribute orchestra_section_key.
35 36 37 |
# File 'lib/head_music/instruments/instrument_type.rb', line 35 def orchestra_section_key @orchestra_section_key end |
#variants ⇒ Object (readonly)
Returns the value of attribute variants.
35 36 37 |
# File 'lib/head_music/instruments/instrument_type.rb', line 35 def variants @variants end |
Class Method Details
.all ⇒ Object
29 30 31 32 33 |
# File 'lib/head_music/instruments/instrument_type.rb', line 29 def self.all HeadMusic::Instruments::InstrumentFamily.all @all ||= INSTRUMENTS.map { |key, _data| get(key) }.sort_by { |instrument| instrument.name.downcase } end |
.get(name) ⇒ Object
25 26 27 |
# File 'lib/head_music/instruments/instrument_type.rb', line 25 def self.get(name) get_by_name(name) end |
Instance Method Details
#==(other) ⇒ Object
41 42 43 |
# File 'lib/head_music/instruments/instrument_type.rb', line 41 def ==(other) to_s == other.to_s end |
#default_clefs ⇒ Object
97 98 99 |
# File 'lib/head_music/instruments/instrument_type.rb', line 97 def default_clefs default_staves&.map(&:clef) || [] end |
#default_instrument ⇒ Object
85 86 87 |
# File 'lib/head_music/instruments/instrument_type.rb', line 85 def default_instrument @default_instrument ||= HeadMusic::Instruments::Instrument.new(self, default_variant) end |
#default_sounding_transposition ⇒ Object
101 102 103 |
# File 'lib/head_music/instruments/instrument_type.rb', line 101 def default_sounding_transposition default_staves&.first&.sounding_transposition || 0 end |
#default_staff_scheme ⇒ Object
89 90 91 |
# File 'lib/head_music/instruments/instrument_type.rb', line 89 def default_staff_scheme default_variant&.default_staff_scheme end |
#default_staves ⇒ Object
93 94 95 |
# File 'lib/head_music/instruments/instrument_type.rb', line 93 def default_staves default_staff_scheme&.staves || [] end |
#default_variant ⇒ Object
81 82 83 |
# File 'lib/head_music/instruments/instrument_type.rb', line 81 def default_variant variants&.find(&:default?) || variants&.first end |
#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named
#family ⇒ Object
51 52 53 54 55 |
# File 'lib/head_music/instruments/instrument_type.rb', line 51 def family return unless family_key HeadMusic::Instruments::InstrumentFamily.get(family_key) end |
#inferred_name ⇒ Object (private)
185 186 187 |
# File 'lib/head_music/instruments/instrument_type.rb', line 185 def inferred_name name_key.to_s.tr("_", " ") end |
#inherit_family_attributes(record) ⇒ Object (private)
163 164 165 166 167 168 |
# File 'lib/head_music/instruments/instrument_type.rb', line 163 def inherit_family_attributes(record) return unless family @orchestra_section_key = family.orchestra_section_key @classification_keys = family.classification_keys || [] end |
#initialize_attributes(record) ⇒ Object (private)
176 177 178 179 180 181 182 183 |
# File 'lib/head_music/instruments/instrument_type.rb', line 176 def initialize_attributes(record) @orchestra_section_key ||= record["orchestra_section_key"] @classification_keys = [@classification_keys, record["classification_keys"]].flatten.compact.uniq @variants = (record["variants"] || {}).map do |key, attributes| HeadMusic::Instruments::Variant.new(key, attributes) end end |
#initialize_data_from_record(record) ⇒ Object (private)
151 152 153 154 155 156 |
# File 'lib/head_music/instruments/instrument_type.rb', line 151 def initialize_data_from_record(record) initialize_family(record) inherit_family_attributes(record) initialize_names(record) initialize_attributes(record) end |
#initialize_family(record) ⇒ Object (private)
158 159 160 161 |
# File 'lib/head_music/instruments/instrument_type.rb', line 158 def initialize_family(record) @family_key = record["family_key"] @family = HeadMusic::Instruments::InstrumentFamily.get(family_key) end |
#initialize_names(record) ⇒ Object (private)
170 171 172 173 174 |
# File 'lib/head_music/instruments/instrument_type.rb', line 170 def initialize_names(record) @name_key = record["name_key"].to_sym self.name = I18n.translate(name_key, scope: "head_music.instruments", locale: "en", default: inferred_name) @alias_name_keys = record["alias_name_keys"] || [] end |
#key_for_name(name) ⇒ Object (private)
124 125 126 127 128 129 130 131 132 |
# File 'lib/head_music/instruments/instrument_type.rb', line 124 def key_for_name(name) INSTRUMENTS.each do |key, _data| I18n.config.available_locales.each do |locale| translation = I18n.t("head_music.instruments.#{key}", locale: locale) return key if translation.downcase == name.downcase end end nil 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.
#multiple_staves? ⇒ Boolean
71 72 73 |
# File 'lib/head_music/instruments/instrument_type.rb', line 71 def multiple_staves? default_staves.length > 1 end |
#name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named
#name=(name) ⇒ Object Originally defined in module Named
#pitched? ⇒ Boolean
75 76 77 78 79 |
# File 'lib/head_music/instruments/instrument_type.rb', line 75 def pitched? return false if default_clefs.compact.uniq == [HeadMusic::Rudiment::Clef.get("neutral_clef")] default_clefs.any? end |
#record_for_alias(name) ⇒ Object (private)
141 142 143 144 145 146 147 148 149 |
# File 'lib/head_music/instruments/instrument_type.rb', line 141 def record_for_alias(name) normalized_name = HeadMusic::Utilities::HashKey.for(name).to_s INSTRUMENTS.each do |name_key, data| data["alias_name_keys"]&.each do |alias_key| return data.merge!("name_key" => name_key) if HeadMusic::Utilities::HashKey.for(alias_key).to_s == normalized_name end end nil end |
#record_for_key(key) ⇒ Object (private)
134 135 136 137 138 139 |
# File 'lib/head_music/instruments/instrument_type.rb', line 134 def record_for_key(key) INSTRUMENTS.each do |name_key, data| return data.merge!("name_key" => name_key) if name_key.to_s == key.to_s end nil end |
#record_for_name(name) ⇒ Object (private)
118 119 120 121 122 |
# File 'lib/head_music/instruments/instrument_type.rb', line 118 def record_for_name(name) record_for_key(HeadMusic::Utilities::HashKey.for(name)) || record_for_key(key_for_name(name)) || record_for_alias(name) end |
#single_staff? ⇒ Boolean
67 68 69 |
# File 'lib/head_music/instruments/instrument_type.rb', line 67 def single_staff? default_staves.length == 1 end |
#translation(locale = :en) ⇒ Object
45 46 47 48 49 |
# File 'lib/head_music/instruments/instrument_type.rb', line 45 def translation(locale = :en) return name unless name_key I18n.translate(name_key, scope: i[head_music instruments], locale: locale, default: name) end |
#transposing? ⇒ Boolean
Returns true if the instrument sounds at a different pitch than written.
58 59 60 |
# File 'lib/head_music/instruments/instrument_type.rb', line 58 def transposing? default_sounding_transposition != 0 end |
#transposing_at_the_octave? ⇒ Boolean
Returns true if the instrument sounds at a different register than written.
63 64 65 |
# File 'lib/head_music/instruments/instrument_type.rb', line 63 def transposing_at_the_octave? transposing? && default_sounding_transposition % 12 == 0 end |