Class: Stretto::MusicElements::Instrument

Inherits:
MusicElement show all
Defined in:
lib/stretto/music_elements/instrument.rb

Overview

Represent an instrument change by the MIDI specification.

The sound played depends on the soundbank installed by the synthesizer, the MIDI standard defines 128 standard instruments (see Variables::INSTRUMENT_VARIABLES) that are reflected by JFugue with predefined variables.

Constant Summary collapse

MAX_INSTRUMENT_VALUE =
127

Instance Attribute Summary

Attributes inherited from MusicElement

#original_string, #pattern

Attributes included from Node

#next, #prev

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MusicElement

#build_music_string, #duration, #end_of_tie?, #start_of_tie?, #to_s

Constructor Details

#initialize(string_or_options, pattern = nil) ⇒ Instrument

Returns a new instance of Instrument.



25
26
27
28
29
30
31
32
# File 'lib/stretto/music_elements/instrument.rb', line 25

def initialize(string_or_options, pattern = nil)
  token = case string_or_options
    when String then Stretto::Parser.parse_instrument!(string_or_options)
    else string_or_options
  end
  super(token[:text_value], pattern )
  @original_value = token[:value]
end

Class Method Details

.default_instrument(pattern = nil) ⇒ Object

Returns an instrument with value 0 (piano)



17
18
19
20
21
22
23
# File 'lib/stretto/music_elements/instrument.rb', line 17

def self.default_instrument(pattern = nil)
  params = {
    :text_value => '',
    :value => Value.new(Value::NumericValue.new(0))
  }
  new(params, pattern)
end

Instance Method Details

#valueNumber

Returns or calculates the value for the instrument

Returns:

  • (Number)

    Returns or calculates the value for the instrument



44
45
46
# File 'lib/stretto/music_elements/instrument.rb', line 44

def value
  @value || @original_value.to_i(@pattern)
end

#value=(value) ⇒ Object

Sets and validates value (0…127)



35
36
37
38
39
40
# File 'lib/stretto/music_elements/instrument.rb', line 35

def value=(value)
  if value < 0 or value > MAX_INSTRUMENT_VALUE
    raise Exceptions::ValueOutOfBoundsException.new("Instrument value should be in range 0..#{MAX_INSTRUMENT_VALUE}")
  end
  @value = value
end