Class: Stretto::Pattern

Inherits:
Array show all
Includes:
Variables
Defined in:
lib/stretto/music_elements/pattern.rb

Overview

Pattern is a series of MusicElements, that hold a context (like tied notes or key signature modifications) Is the equivalent of the JFugue implementation Pattern - NOTE: This class behavior is not definite, and may change during the development of Stretto until the first stable version +

Constant Summary

DEFAULT_VOICE_INDEX =
0

Constants included from Variables

Variables::CONTROLLER_VARIABLES, Variables::INSTRUMENT_VARIABLES, Variables::PERCUSSION_VARIABLES, Variables::PREDEFINED_VARIABLES, Variables::TEMPO_VARIABLES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#sum

Constructor Details

#initialize(music_string_or_file = "")) ⇒ Pattern

Initializes a pattern



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stretto/music_elements/pattern.rb', line 23

def initialize(music_string_or_file = "")
  @music_string = get_music_string_from(music_string_or_file)
  @parser           = Stretto::Parser.new(@music_string)
  @voices           = { }
  @variables        = { }
  @__key_signature  = { }
  @__instruments    = { }
  if @parser.valid?
    @parser.to_stretto(self).each { |music_element| self << music_element }
  else
    raise "Invalid music string \"#{@music_string}\" at character #{@parser.error_on}"
  end
end

Instance Attribute Details

#variablesObject (readonly)

TODO: Limit access to variables



18
19
20
# File 'lib/stretto/music_elements/pattern.rb', line 18

def variables
  @variables
end

#voicesObject (readonly)

TODO: Limit access to variables



18
19
20
# File 'lib/stretto/music_elements/pattern.rb', line 18

def voices
  @voices
end

Instance Method Details

#<<(other) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/stretto/music_elements/pattern.rb', line 45

def <<(other)
  other.pattern = self

  if other.kind_of?(MusicElements::Variable)
    @variables[other.name.upcase] = other.value
  end

  if other.kind_of?(MusicElements::VoiceChange)
    @current_voice = (@voices[other.index] ||= Voice.new(other.index))
  else
    @voices[DEFAULT_VOICE_INDEX] = @current_voice = Voice.new(DEFAULT_VOICE_INDEX) unless @current_voice
    @current_voice << other
    if @current_voice.size > 1
      @current_voice[-2].next = @current_voice[-1]
      @current_voice[-1].prev = @current_voice[-2]
    end
  end

  if other.kind_of?(MusicElements::KeySignature)
    @__key_signature[@current_voice.index] = other
  else
    other.key_signature = @__key_signature[@current_voice.index] if other.respond_to?(:key_signature=)
  end

  if other.kind_of?(MusicElements::Instrument)
    @__instruments[@current_voice.index] = other
  else
    @__instruments[@current_voice.index] ||= MusicElements::Instrument.default_instrument(self)
    other.instrument = @__instruments[@current_voice.index] if other.respond_to?(:instrument=)
  end

  super(other)
end

#elementsObject



37
38
39
# File 'lib/stretto/music_elements/pattern.rb', line 37

def elements
  to_a
end

#to_sObject



41
42
43
# File 'lib/stretto/music_elements/pattern.rb', line 41

def to_s
  @music_string
end

#variable(name) ⇒ Object



83
84
85
86
87
# File 'lib/stretto/music_elements/pattern.rb', line 83

def variable(name)
  @variables[name.upcase] ||
      (Value.new(Value::NumericValue.new(PREDEFINED_VARIABLES[name.upcase])) if PREDEFINED_VARIABLES[name.upcase]) ||
      raise(Exceptions::VariableNotDefinedException.new("Variable '#{name}' not defined in pattern"))
end

#voice(index) ⇒ Object



79
80
81
# File 'lib/stretto/music_elements/pattern.rb', line 79

def voice(index)
  @voices[index]
end