Class: HeadMusic::Instrument
- Inherits:
-
Object
- Object
- HeadMusic::Instrument
- Includes:
- Named
- Defined in:
- lib/head_music/instrument.rb
Overview
A musical instrument. An instrument object can be assigned to a staff object. Attributes:
name_key: the name of the instrument
alias_name_keys: an array of alternative names for the instrument
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
transposition: the number of semitones between the written and the sounding pitch (optional, default: 0)
default_clefs: the default clef or system of clefs for the instrument
- [treble] for instruments that use the treble clef
- [treble, bass] for instruments that use the grand staff
notation:
a hash of default and alternative notation systems,
each with a staffs key with an array of hashes
including clef and transposition (where applicable)
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.("data/instruments.yml", __dir__)).freeze
Instance Attribute Summary collapse
-
#alias_name_keys ⇒ Object
readonly
Returns the value of attribute alias_name_keys.
-
#classification_keys ⇒ Object
readonly
Returns the value of attribute classification_keys.
-
#default_clefs ⇒ Object
readonly
Returns the value of attribute default_clefs.
-
#default_staffs ⇒ Object
readonly
Returns the value of attribute default_staffs.
-
#family_key ⇒ Object
readonly
Returns the value of attribute family_key.
-
#fundamental_pitch_spelling ⇒ Object
readonly
Returns the value of attribute fundamental_pitch_spelling.
-
#name_key ⇒ Object
readonly
Returns the value of attribute name_key.
-
#notation ⇒ Object
readonly
Returns the value of attribute notation.
-
#orchestra_section_key ⇒ Object
readonly
Returns the value of attribute orchestra_section_key.
-
#transposition ⇒ Object
readonly
Returns the value of attribute transposition.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #family ⇒ Object
- #multiple_staffs? ⇒ Boolean
- #pitched? ⇒ Boolean
- #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.
Methods included from Named
#ensure_localized_name, included, #localized_name, #localized_names, #name, #name=
Instance Attribute Details
#alias_name_keys ⇒ Object (readonly)
Returns the value of attribute alias_name_keys.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def alias_name_keys @alias_name_keys end |
#classification_keys ⇒ Object (readonly)
Returns the value of attribute classification_keys.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def classification_keys @classification_keys end |
#default_clefs ⇒ Object (readonly)
Returns the value of attribute default_clefs.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def default_clefs @default_clefs end |
#default_staffs ⇒ Object (readonly)
Returns the value of attribute default_staffs.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def default_staffs @default_staffs end |
#family_key ⇒ Object (readonly)
Returns the value of attribute family_key.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def family_key @family_key end |
#fundamental_pitch_spelling ⇒ Object (readonly)
Returns the value of attribute fundamental_pitch_spelling.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def fundamental_pitch_spelling @fundamental_pitch_spelling end |
#name_key ⇒ Object (readonly)
Returns the value of attribute name_key.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def name_key @name_key end |
#notation ⇒ Object (readonly)
Returns the value of attribute notation.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def notation @notation end |
#orchestra_section_key ⇒ Object (readonly)
Returns the value of attribute orchestra_section_key.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def orchestra_section_key @orchestra_section_key end |
#transposition ⇒ Object (readonly)
Returns the value of attribute transposition.
38 39 40 |
# File 'lib/head_music/instrument.rb', line 38 def transposition @transposition end |
Class Method Details
.all ⇒ Object
32 33 34 35 36 |
# File 'lib/head_music/instrument.rb', line 32 def self.all HeadMusic::InstrumentFamily.all @all ||= INSTRUMENTS.map { |key, _data| get(key) }.sort_by(&:name) end |
.get(name) ⇒ Object
27 28 29 30 |
# File 'lib/head_music/instrument.rb', line 27 def self.get(name) result = get_by_name(name) || get_by_name(key_for_name(name)) || get_by_alias(name) result || new(name) end |
Instance Method Details
#==(other) ⇒ Object
46 47 48 |
# File 'lib/head_music/instrument.rb', line 46 def ==(other) to_s == other.to_s end |
#family ⇒ Object
56 57 58 59 60 |
# File 'lib/head_music/instrument.rb', line 56 def family return unless family_key HeadMusic::InstrumentFamily.get(family_key) end |
#multiple_staffs? ⇒ Boolean
76 77 78 |
# File 'lib/head_music/instrument.rb', line 76 def multiple_staffs? default_staffs.length > 1 end |
#pitched? ⇒ Boolean
80 81 82 83 84 |
# File 'lib/head_music/instrument.rb', line 80 def pitched? return false if default_clefs.compact.uniq == ["percussion"] default_clefs.any? end |
#single_staff? ⇒ Boolean
72 73 74 |
# File 'lib/head_music/instrument.rb', line 72 def single_staff? default_staffs.length == 1 end |
#translation(locale = :en) ⇒ Object
50 51 52 53 54 |
# File 'lib/head_music/instrument.rb', line 50 def translation(locale = :en) return name unless name_key I18n.translate(name_key, scope: [:instruments], locale: locale) end |
#transposing? ⇒ Boolean
Returns true if the instrument sounds at a different pitch than written.
63 64 65 |
# File 'lib/head_music/instrument.rb', line 63 def transposing? transposition != 0 end |
#transposing_at_the_octave? ⇒ Boolean
Returns true if the instrument sounds at a different register than written.
68 69 70 |
# File 'lib/head_music/instrument.rb', line 68 def transposing_at_the_octave? transposing? && transposition % 12 == 0 end |