Class: RhythmCompiler
- Inherits:
-
Object
- Object
- RhythmCompiler
- 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
-
.createRhythm(snippets, snippetIDx, snippetRep, totalRepeat) ⇒ String
creates a rhythm string from rhythm snippets.
-
.createSnippets(snippetLengths, eventPositions) ⇒ Array<String>
create an array of snippets of length in snippetLengths, with events at position(s) in eventPositions.
Class Method Details
.createRhythm(snippets, snippetIDx, snippetRep, totalRepeat) ⇒ String
creates a rhythm string from rhythm snippets
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
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 |