Class: HeadMusic::Instruments::Staff

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/instruments/staff.rb

Constant Summary collapse

DEFAULT_CLEF =
"treble_clef"

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/head_music/instruments/staff.rb', line 6

def attributes
  @attributes
end

#staff_schemeObject (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

#clefObject



13
14
15
# File 'lib/head_music/instruments/staff.rb', line 13

def clef
  HeadMusic::Rudiment::Clef.get(smart_clef_key)
end

#componentsArray<Instrument>

Get all unique instruments used in this staff's mappings

Examples:

drum_kit_staff.components  #=> [#<Instrument: bass_drum>, #<Instrument: snare_drum>, ...]

Returns:

  • (Array<Instrument>)

    array of unique instruments



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

Examples:

staff.instrument_for_position(4)  #=> #<Instrument name: "snare drum">

Parameters:

  • position_index (Integer)

    the staff position index

Returns:

  • (Instrument, nil)

    the instrument at that position or nil



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

Examples:

staff.mapping_for_position(4)  #=> #<Notation::StaffMapping instrument: snare_drum...>

Parameters:

  • position_index (Integer)

    the staff position index

Returns:



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

#mappingsArray<Notation::StaffMapping>

Get all staff mappings for composite instruments

Examples:

drum_kit_staff.mappings  #=> [#<Notation::StaffMapping...>, #<Notation::StaffMapping...>]

Returns:



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

def mappings
  @mappings ||= parse_mappings
end

#nameObject



29
30
31
# File 'lib/head_music/instruments/staff.rb', line 29

def name
  name_key.to_s.tr("_", " ")
end

#name_keyObject



25
26
27
# File 'lib/head_music/instruments/staff.rb', line 25

def name_key
  attributes["name_key"] || ""
end

#parse_mappingsObject (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)

Examples:

staff.positions_for_instrument("hi_hat")  #=> [-1, 9]

Parameters:

  • instrument_key (String, Symbol)

    the instrument key

Returns:

  • (Array<Integer>)

    array of position indices



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_keyObject



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_transpositionObject



21
22
23
# File 'lib/head_music/instruments/staff.rb', line 21

def sounding_transposition
  attributes["sounding_transposition"] || 0
end