lib/patchmaster/dsl.rb in patchmaster-0.0.6 vs lib/patchmaster/dsl.rb in patchmaster-1.0.0

- old
+ new

@@ -5,56 +5,61 @@ # Implements a DSL for describing a PatchMaster setup. class DSL include PM - def initialize(no_midi=false) - @no_midi = no_midi + def initialize @pm = PatchMaster.instance + init end - def load(file) - contents = IO.read(file) + # Initialize state used for reading. + def init @inputs = {} @outputs = {} @triggers = [] @filters = [] @songs = {} # key = name, value = song + end + + def load(file) + contents = IO.read(file) + init instance_eval(contents) read_triggers(contents) read_filters(contents) end def input(port_num, sym, name=nil) raise "input: two inputs can not have the same symbol (:#{sym})" if @inputs[sym] - input = InputInstrument.new(sym, name, port_num, @no_midi) + input = InputInstrument.new(sym, name, port_num, @pm.use_midi?) @inputs[sym] = input @pm.inputs << input rescue => ex - raise "input: error creating input instrument \"#{name}\" on input port #{port_num}: #{ex}" + raise "input: error creating input instrument \"#{name || sym}\" on input port #{port_num}: #{ex}" end - alias_method :in, :input + alias_method :inp, :input def output(port_num, sym, name=nil) raise "output: two outputs can not have the same symbol (:#{sym})" if @outputs[sym] - output = OutputInstrument.new(sym, name, port_num, @no_midi) + output = OutputInstrument.new(sym, name, port_num, @pm.use_midi?) @outputs[sym] = output @pm.outputs << output rescue => ex - raise "output: error creating output instrument \"#{name}\" on output port #{port_num}: #{ex}" + raise "output: error creating output instrument \"#{name || sym}\" on output port #{port_num}: #{ex}" end alias_method :out, :output def message(name, bytes) @pm.messages[name.downcase] = bytes end def message_key(name, key_or_sym) - if !@pm.no_gui # TODO get rid of double negative - PM::Main.instance.bind_message(name, key_or_sym) + if @pm.gui + @pm.gui.bind_message(name, key_or_sym) end end def trigger(instrument_sym, bytes, &block) instrument = @inputs[instrument_sym] @@ -82,13 +87,19 @@ def stop_bytes(bytes) @patch.stop_bytes = bytes end - def connection(in_sym, in_chan, out_sym, out_chan) + # in_chan can be skipped, so "connection :foo, :bar, 1" is the same as + # "connection :foo, nil, :bar, 1". + def connection(in_sym, in_chan, out_sym, out_chan=nil) input = @inputs[in_sym] - in_chan = nil if in_chan == :all || in_chan == :any + if in_chan.kind_of? Symbol + out_chan = out_sym + out_sym = in_chan + in_chan = nil + end raise "can't find input instrument #{in_sym}" unless input output = @outputs[out_sym] raise "can't find outputput instrument #{out_sym}" unless output @conn = Connection.new(input, in_chan, output, out_chan) @@ -264,10 +275,10 @@ else containers[i].text << line end end end - containers.each { |thing| thing.text.strip! } + containers.each { |thing| thing.text.strip! if thing.text } end end end