Class: RhythmParser

Inherits:
Object
  • Object
show all
Defined in:
lib/rhythmruby/RhythmParser.rb

Overview

Parser (use as class) of the rhythm strings, consisting event and silence symbols

Constant Summary collapse

@@silenceNote =

pitch of a note played during initial silence in a sequence

0
@@silenceMark =

symbol identified as a silence

'-'
@@eventMark =

symbol identified as an event

'#'
@@eventNote =

(unused default) midi note of an event

50
@@countBase =

(unused default) time duration of one symbol in quarter note lengths

1.0/4.0

Class Method Summary collapse

Class Method Details

.parseRhythm(rhythmString, countBase, midiNote) ⇒ Array<Array>

parses the rhythm string and generates midi info ready for MidiWriter side note: a string starting with a silence needs an initial ‘silence note’ (note value: @@silenceNote)



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rhythmruby/RhythmParser.rb', line 17

def self.parseRhythm(rhythmString, countBase, midiNote)
  @@countBase = countBase # time duration of one symbol (quarternote lengths)
  @@eventNote = midiNote # midi note assigned to events
  
  midiSequence = [] # empty array to store parsed midi data
  curNote = nil # initialize current midi note
  curLen = nil  # initialze current midi note length
  first = true # state variable testing for first / sequential events
  
  rhythmString.each_char do
    |symbol| # symbol in the string either a silence or an event
    
    if first # if true this is the first parsed symbol
      first = false # skip on next symbol
      
      # if first symbol is an event (handles when the rhythm starts with a silence)
      if symbol == @@eventMark 
        curNote = @@eventNote
        curLen = 1
        
      elsif symbol == @@silenceMark
        curNote = @@silenceNote
        curLen = 1
        
      end 
    else # when first symbol has already been parsed
      if symbol == @@eventMark # an event is parsed, write previous event and generate new one
          # add midi event to midiSequence (note length in countbase units)
          midiSequence << [curNote,(curLen*@@countBase)] 
          curNote = @@eventNote # generate new event
          curLen = 1 # reset note length to 1
        
      elsif symbol == @@silenceMark # a silence is parsed
        curLen += 1 # add one countbase note length to the current note
      
     end
   end
  end
  
  return midiSequence
end