Class: HeadMusic::Notation::StaffMapping

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/notation/staff_mapping.rb

Overview

Represents the mapping of an instrument (and optional playing technique) to a staff position

Examples:

Basic mapping

mapping = StaffMapping.new({
  "staff_position" => 4,
  "instrument" => "snare_drum"
})
mapping.instrument.name    #=> "snare drum"
mapping.position_index     #=> 4

Mapping with playing technique

mapping = StaffMapping.new({
  "staff_position" => -1,
  "instrument" => "hi_hat",
  "playing_technique" => "pedal"
})
mapping.playing_technique.name  #=> "pedal"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ StaffMapping

Initialize a new StaffMapping

Parameters:

  • attributes (Hash)

    the mapping attributes

Options Hash (attributes):

  • "staff_position" (Integer, String)

    the staff position index

  • "instrument" (String)

    the instrument key

  • "playing_technique" (String)

    optional playing technique key



30
31
32
33
34
# File 'lib/head_music/notation/staff_mapping.rb', line 30

def initialize(attributes)
  @staff_position = HeadMusic::Notation::StaffPosition.new(attributes["staff_position"].to_i)
  @instrument_key = attributes["instrument"]
  @playing_technique_key = attributes["playing_technique"]
end

Instance Attribute Details

#instrument_keyObject (readonly)

Returns the value of attribute instrument_key.



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

def instrument_key
  @instrument_key
end

#playing_technique_keyObject (readonly)

Returns the value of attribute playing_technique_key.



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

def playing_technique_key
  @playing_technique_key
end

#staff_positionObject (readonly)

Returns the value of attribute staff_position.



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

def staff_position
  @staff_position
end

Instance Method Details

#instrumentInstrument?

Get the Instrument object

Returns:

  • (Instrument, nil)

    the instrument or nil if not found



39
40
41
# File 'lib/head_music/notation/staff_mapping.rb', line 39

def instrument
  HeadMusic::Instruments::Instrument.get(instrument_key) if instrument_key
end

#playing_techniquePlayingTechnique?

Get the PlayingTechnique object

Returns:

  • (PlayingTechnique, nil)

    the playing technique or nil if not specified



46
47
48
# File 'lib/head_music/notation/staff_mapping.rb', line 46

def playing_technique
  HeadMusic::Instruments::PlayingTechnique.get(playing_technique_key) if playing_technique_key
end

#position_indexInteger

Get the staff position index

Returns:

  • (Integer)

    the position index



53
54
55
# File 'lib/head_music/notation/staff_mapping.rb', line 53

def position_index
  staff_position.index
end

#to_sString

String representation of this mapping

Examples:

mapping.to_s  #=> "snare drum at line 3"
mapping.to_s  #=> "hi hat (pedal) at Space 0"

Returns:

  • (String)

    human-readable description



63
64
65
66
67
68
69
# File 'lib/head_music/notation/staff_mapping.rb', line 63

def to_s
  parts = []
  parts << (instrument&.name || instrument_key) if instrument_key
  parts << "(#{playing_technique})" if playing_technique_key
  parts << "at #{staff_position}"
  parts.compact.join(" ")
end