Class: RhythmCompiler

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

Overview

rhythm composition class (use as a class) compiles rhythm snippets into a rhythm string/pattern

Constant Summary collapse

@@silenceMarker =

symbol for silence

'-'
@@eventMarker =

symbol for an event / hit

'#'

Class Method Summary collapse

Class Method Details

.createRhythm(snippets, snippetIDx, snippetRep, totalRepeat) ⇒ String

creates a rhythm string from rhythm snippets

Parameters:

  • snippets (Array<String>)

    array of rhythm snippets (from ‘createSnippets)

  • snippetIDx (Array<Fixnum>)

    indexes, defining sequential order of snippets

  • snippetRep (Array<Fixnum>)

    defining how often snippets are repeated… if nil, all snippets are repeated once, if snippetRep.length==1, all snippets are repeated the same amount otherwise snippetRep should be the same length as snippetIdx and define repetition per snippet

  • totalRepeat, (Fixnum)

    number of times the total rhythm is repeated

Returns:

  • (String)

    rhythmString, symbolic rhythm string



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rhythmruby/RhythmCompiler.rb', line 58

def self.createRhythm(snippets, snippetIDx, snippetRep, totalRepeat)
  if snippetRep == nil # test whether snippetRep is defined, if not all snippets are repeated once
    snippetRep = [1]*snippetIDx.length # make snippetRep and snippetIDx the same length for iteration
  end 
  if totalRepeat == nil # if undefined set repeats equal to 1
    totalRepeat = 1
  end
  
  
  rhythmString = "" # empty string where all the snippets will be added to
  
  # iterate over combinations of snippetID and repeats
  snippetIDx.zip(snippetRep).each do
    |snippetID, repeats|
    # add the snippet with snippetID, repeat times, to the rhythm string 
    rhythmString += snippets[snippetID]*repeats 
  end
  
  return rhythmString*totalRepeat # return the rhythm string repeated totalRepeat times, ready for parsing
end

.createSnippets(snippetLengths, eventPositions) ⇒ Array<String>

create an array of snippets of length in snippetLengths, with events at position(s) in eventPositions

Parameters:

  • snippetLengths (Array<Fixnum>)

    length of each snippet (if snippetLengths.length == 1, length is constant for all snippets )

  • eventPositions (Array<Array>)

    containing arrays of event positions per snippet (if eventPositions.length == 1, then event positions are constant for all snippets)

Returns:

  • (Array<String>)

    snippets array of string rhythm snippets, for use with ‘createString’ method



16
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
# File 'lib/rhythmruby/RhythmCompiler.rb', line 16

def self.createSnippets(snippetLengths, eventPositions)
  snippets = [] # empty array to append the snippets
  
  # make input arrays suitable for fur
  if snippetLengths.length == 1
    # make snippetLengths as long as eventPositions      
    snippetLengths *= eventPositions.length 
           
  elsif eventPositions.length == 1 
   # make eventPositions as long as snippetLengths 
   eventPositions *= snippetLengths.length 
   
  else # both input arrays have more than one element and should be the same length
    if not(eventPositions.length == snippetLengths.length)
      raise ArgumentError, 'lengths of both arrays should be the same, \
       or one should have length 1'
    end
  end    
  
  #create a rhythm snippet for each set of event positions and snippetlength
  snippetLengths.zip(eventPositions).each do  
      |length, positions| # unpack the length and position information   
      # create a snippet of defined length consisting of silences
      snippet = @@silenceMarker*length 
              
      positions.each{|pos| # for each provided event positon of the snippet
        snippet[pos] = @@eventMarker # add an event marker at the position
      }
      
      snippets<<snippet # add the snippet to the returned snippets array
   end   
   return snippets # return the array with the snippets
end