Skip to content

Commit

Permalink
got templating working to the point of swapping out all of the 16th n…
Browse files Browse the repository at this point in the history
…otes for the same given probability
  • Loading branch information
Greg Borenstein committed Feb 1, 2009
1 parent 125f284 commit 669417c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
33 changes: 33 additions & 0 deletions drum_definition.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
$clock.bpm = 175
$mutation = L{|measure| 0 == (measure - 1) % 2}
$measures = 4

probabilities = {}

<% probabilities.each do |k,v| %>
probabilities[<%= 36 + k %>] = <%= v.sort.collect{|a| a.last}.inspect %>
<% end %>

def note(midi_note_number)
Note.create(:channel => 2,
:number => midi_note_number,
:duration => 0.25,
:velocity => 100 + rand(27))
end

static = L{1.0}
dynamic = L{rand}
check_every_drum = L{|queue| queue[queue.size - 1]}
check_random_drums = L{|queue| queue[rand(queue.size)]}

notes = []
(36..45).each do |midi_note_number|
notes << Drum.new(:note => note(midi_note_number),
:when => L{|beat| false},
# :number_generator => static,
:number_generator => dynamic,
# :next => check_every_drum,
:next => check_random_drums,
:probabilities => probabilities[midi_note_number])
end
notes
31 changes: 30 additions & 1 deletion templarx.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
class Templarx
require 'erb'

class Templarx
TEMPLATE_PATH = File.expand_path(File.dirname(__FILE__)) + "/drum_definition.erb"

attr_accessor :probabilities
attr_accessor :default_probability

def initialize opts={}
@ddpath = opts[:definition_path]
@default_probability = opts[:probability]
end

# 10x16 hash of 0.0-1.0
# rows represent midi channels, columns sixteenth notes
def initialize_probabilities
@probabilities = {}
(0..9).each{|i| @probabilities[i] = {}}
(0..9).each{|i| k={}; (0..15).each{|j| k[j] = @default_probability || 0.5}; @probabilities[i] = k }
end

def template
@template ||= open(TEMPLATE_PATH).read
end

def rewrite_drum_definition
initialize_probabilities
e = ERB.new template
File.open(@ddpath, "w"){|f| f << e.result(binding)}
end
end

0 comments on commit 669417c

Please sign in to comment.