Class: HeadMusic::Instrument

Inherits:
Object
  • Object
show all
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
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
pitch_variants:
  a hash of default and alternative fundamental pitches.

Associations:

family: the family of the instrument (e.g. "saxophone")
orchestra_section: the section of the orchestra (e.g. "strings")

Defined Under Namespace

Classes: PitchVariant, Staff, StaffScheme

Constant Summary collapse

INSTRUMENTS =
YAML.load_file(File.expand_path("data/instruments.yml", __dir__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Named

#ensure_localized_name, included, #localized_name, #localized_names, #name, #name=

Instance Attribute Details

#alias_name_keysObject (readonly)

Returns the value of attribute alias_name_keys.



32
33
34
# File 'lib/head_music/instrument.rb', line 32

def alias_name_keys
  @alias_name_keys
end

#classification_keysObject (readonly)

Returns the value of attribute classification_keys.



32
33
34
# File 'lib/head_music/instrument.rb', line 32

def classification_keys
  @classification_keys
end

#family_keyObject (readonly)

Returns the value of attribute family_key.



32
33
34
# File 'lib/head_music/instrument.rb', line 32

def family_key
  @family_key
end

#name_keyObject (readonly)

Returns the value of attribute name_key.



32
33
34
# File 'lib/head_music/instrument.rb', line 32

def name_key
  @name_key
end

#orchestra_section_keyObject (readonly)

Returns the value of attribute orchestra_section_key.



32
33
34
# File 'lib/head_music/instrument.rb', line 32

def orchestra_section_key
  @orchestra_section_key
end

#pitch_variantsObject (readonly)

Returns the value of attribute pitch_variants.



32
33
34
# File 'lib/head_music/instrument.rb', line 32

def pitch_variants
  @pitch_variants
end

Class Method Details

.allObject



26
27
28
29
30
# File 'lib/head_music/instrument.rb', line 26

def self.all
  HeadMusic::InstrumentFamily.all
  @all ||=
    INSTRUMENTS.map { |key, _data| get(key) }.sort_by(&:name)
end

.get(name) ⇒ Object



22
23
24
# File 'lib/head_music/instrument.rb', line 22

def self.get(name)
  get_by_name(name)
end

Instance Method Details

#==(other) ⇒ Object



38
39
40
# File 'lib/head_music/instrument.rb', line 38

def ==(other)
  to_s == other.to_s
end

#default_clefsObject



90
91
92
# File 'lib/head_music/instrument.rb', line 90

def default_clefs
  default_staves&.map(&:clef) || []
end

#default_pitch_variantObject



78
79
80
# File 'lib/head_music/instrument.rb', line 78

def default_pitch_variant
  pitch_variants.find(&:default?) || pitch_variants.first
end

#default_sounding_transpositionObject



94
95
96
# File 'lib/head_music/instrument.rb', line 94

def default_sounding_transposition
  default_staves&.first&.sounding_transposition || 0
end

#default_staff_schemeObject



82
83
84
# File 'lib/head_music/instrument.rb', line 82

def default_staff_scheme
  default_pitch_variant&.default_staff_scheme
end

#default_stavesObject



86
87
88
# File 'lib/head_music/instrument.rb', line 86

def default_staves
  default_staff_scheme&.staves || []
end

#familyObject



48
49
50
51
52
# File 'lib/head_music/instrument.rb', line 48

def family
  return unless family_key

  HeadMusic::InstrumentFamily.get(family_key)
end

#multiple_staves?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/head_music/instrument.rb', line 68

def multiple_staves?
  default_staves.length > 1
end

#pitched?Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/head_music/instrument.rb', line 72

def pitched?
  return false if default_clefs.compact.uniq == [HeadMusic::Clef.get("neutral_clef")]

  default_clefs.any?
end

#single_staff?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/head_music/instrument.rb', line 64

def single_staff?
  default_staves.length == 1
end

#translation(locale = :en) ⇒ Object



42
43
44
45
46
# File 'lib/head_music/instrument.rb', line 42

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.

Returns:

  • (Boolean)


55
56
57
# File 'lib/head_music/instrument.rb', line 55

def transposing?
  default_sounding_transposition != 0
end

#transposing_at_the_octave?Boolean

Returns true if the instrument sounds at a different register than written.

Returns:

  • (Boolean)


60
61
62
# File 'lib/head_music/instrument.rb', line 60

def transposing_at_the_octave?
  transposing? && default_sounding_transposition % 12 == 0
end