Class: HeadMusic::Instruments::Staff
- Inherits:
-
Object
- Object
- HeadMusic::Instruments::Staff
- Defined in:
- lib/head_music/instruments/staff.rb
Constant Summary collapse
- DEFAULT_CLEF =
"treble_clef"
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#staff_scheme ⇒ Object
readonly
Returns the value of attribute staff_scheme.
Instance Method Summary collapse
- #clef ⇒ Object
-
#components ⇒ Array<Instrument>
Get all unique instruments used in this staff's mappings.
-
#initialize(staff_scheme, attributes) ⇒ Staff
constructor
A new instance of Staff.
-
#instrument_for_position(position_index) ⇒ Instrument?
Get the instrument at a specific staff position.
-
#mapping_for_position(position_index) ⇒ Notation::StaffMapping?
Find the staff mapping at a specific position.
-
#mappings ⇒ Array<Notation::StaffMapping>
Get all staff mappings for composite instruments.
- #name ⇒ Object
- #name_key ⇒ Object
- #parse_mappings ⇒ Object private
-
#positions_for_instrument(instrument_key) ⇒ Array<Integer>
Get all staff positions for a given instrument.
- #smart_clef_key ⇒ Object
- #sounding_transposition ⇒ Object
Constructor Details
#initialize(staff_scheme, attributes) ⇒ Staff
Returns a new instance of Staff.
8 9 10 11 |
# File 'lib/head_music/instruments/staff.rb', line 8 def initialize(staff_scheme, attributes) @staff_scheme = staff_scheme @attributes = attributes || {} end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
6 7 8 |
# File 'lib/head_music/instruments/staff.rb', line 6 def attributes @attributes end |
#staff_scheme ⇒ Object (readonly)
Returns the value of attribute staff_scheme.
6 7 8 |
# File 'lib/head_music/instruments/staff.rb', line 6 def staff_scheme @staff_scheme end |
Instance Method Details
#clef ⇒ Object
13 14 15 |
# File 'lib/head_music/instruments/staff.rb', line 13 def clef HeadMusic::Rudiment::Clef.get(smart_clef_key) end |
#components ⇒ Array<Instrument>
Get all unique instruments used in this staff's mappings
82 83 84 |
# File 'lib/head_music/instruments/staff.rb', line 82 def components mappings.map(&:instrument).compact.uniq end |
#instrument_for_position(position_index) ⇒ Instrument?
Get the instrument at a specific staff position
58 59 60 61 |
# File 'lib/head_music/instruments/staff.rb', line 58 def instrument_for_position(position_index) mapping = mapping_for_position(position_index) mapping&.instrument end |
#mapping_for_position(position_index) ⇒ Notation::StaffMapping?
Find the staff mapping at a specific position
48 49 50 |
# File 'lib/head_music/instruments/staff.rb', line 48 def mapping_for_position(position_index) mappings.find { |mapping| mapping.position_index == position_index } end |
#mappings ⇒ Array<Notation::StaffMapping>
Get all staff mappings for composite instruments
38 39 40 |
# File 'lib/head_music/instruments/staff.rb', line 38 def mappings @mappings ||= parse_mappings end |
#name ⇒ Object
29 30 31 |
# File 'lib/head_music/instruments/staff.rb', line 29 def name name_key.to_s.tr("_", " ") end |
#name_key ⇒ Object
25 26 27 |
# File 'lib/head_music/instruments/staff.rb', line 25 def name_key attributes["name_key"] || "" end |
#parse_mappings ⇒ Object (private)
88 89 90 91 92 |
# File 'lib/head_music/instruments/staff.rb', line 88 def parse_mappings mappings_data = attributes["mappings"] || [] mappings_data.map { |mapping_attrs| HeadMusic::Notation::StaffMapping.new(mapping_attrs) } end |
#positions_for_instrument(instrument_key) ⇒ Array<Integer>
Get all staff positions for a given instrument
This is useful for instruments that appear at multiple positions (e.g., hi-hat with stick and pedal techniques)
72 73 74 75 |
# File 'lib/head_music/instruments/staff.rb', line 72 def positions_for_instrument(instrument_key) mappings.select { |mapping| mapping.instrument_key.to_s == instrument_key.to_s } .map(&:position_index) end |
#smart_clef_key ⇒ Object
17 18 19 |
# File 'lib/head_music/instruments/staff.rb', line 17 def smart_clef_key "#{attributes["clef"]}_clef".gsub(/_clef_clef$/, "_clef") end |
#sounding_transposition ⇒ Object
21 22 23 |
# File 'lib/head_music/instruments/staff.rb', line 21 def sounding_transposition attributes["sounding_transposition"] || 0 end |