Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactored multi_out_ugens

  • Loading branch information...
commit c366ec42b3c00035dc456671c984cd076eb1495b 1 parent 2b61957
@maca authored
View
28 lib/scruby/audio/ugens/multi_out_ugens.rb
@@ -6,29 +6,43 @@ class OutputProxy < Ugen
attr_reader :source, :control_name, :output_index
def initialize( rate, source, name, output_index )
- super(rate)
- @source, @index = source, source.index
- @control_name, @output_index = name, output_index
+ super rate
+ @source, @control_name, @output_index = source, name, output_index
+ end
+
+ def index
+ @source.index
end
def add_to_synthdef; end
end
class MultiOutUgen < Ugen
+ def initialize( rate, *channels )
+ @channels = channels
+ super rate
+ end
+
+ def self.new( rate, *args )
+ super( rate, *args ).channels.compact #returns the channels but gets instantiated
+ end
+
private
- attr_writer :channels
def output_specs
channels.collect{ |output| output.send :output_specs }.flatten
end
end
class Control < MultiOutUgen #:nodoc:
+ def initialize( rate, *names )
+ super rate, *names.collect_with_index{|n, i| OutputProxy.new rate, self, n, i }
+ end
+
def self.and_proxies_from( names )
- control = Control.new( names.first.rate )
- control.send :channels=, names.collect_with_index { |name, index| OutputProxy.new( names.first.rate, control, name, index ) }
+ Control.new( names.first.rate, *names )
end
end
end
end
-end
+end
View
8 lib/scruby/audio/ugens/ugen.rb
@@ -25,7 +25,7 @@ def muladd( mul, add )
end
def to_s
- "#{self.class} inputs: #{self.inputs.join(', ')}"
+ "#{self.class.to_s.split('::').last} inputs:[#{self.inputs.join(',')}]"
end
def ugen?
@@ -33,7 +33,9 @@ def ugen?
end
def encode
- self.class.to_s.split('::').last.encode + [E_RATES.index(rate)].pack('w') + [inputs.size, channels.size, special_index, collect_input_specs].flatten.pack('n*') + output_specs.pack('w*')
+ self.class.to_s.split('::').last.encode + [E_RATES.index(rate)].pack('w') +
+ [inputs.size, channels.size, special_index, collect_input_specs].flatten.pack('n*') +
+ output_specs.pack('w*')
end
private
@@ -54,7 +56,7 @@ def input_specs( synthdef )
end
def collect_input_specs
- @inputs.collect{ |i| i.send( :input_specs, synthdef ) }
+ @inputs.collect{ |i| i.send( :input_specs, synthdef ) }
end
def output_specs
View
18 spec/audio/integration_spec.rb
@@ -21,29 +21,29 @@
describe "synthdef examples" do
-
- before do
+
+ before :all do
@sdef = SynthDef.new(:hola){ |a, b| SinOsc.kr( a ) + SinOsc.kr( b ) }
@expected = [ 83, 67, 103, 102, 0, 0, 0, 1, 0, 1, 4, 104, 111, 108, 97, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 97, 0, 0, 1, 98, 0, 1, 0, 4, 7, 67, 111, 110, 116, 114, 111, 108, 1, 0, 0, 0, 2, 0, 0, 1, 1, 6, 83, 105, 110, 79, 115, 99, 1, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, -1, -1, 0, 0, 1, 6, 83, 105, 110, 79, 115, 99, 1, 0, 2, 0, 1, 0, 0, 0, 0, 0, 1, -1, -1, 0, 0, 1, 12, 66, 105, 110, 97, 114, 121, 79, 112, 85, 71, 101, 110, 1, 0, 2, 0, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0 ].pack('C*')
end
-
+
it "should have correct children" do
- @sdef.should have(4).children
@sdef.children[0].should be_instance_of( Control )
@sdef.children[1].should be_instance_of( SinOsc )
@sdef.children[2].should be_instance_of( SinOsc )
@sdef.children[3].should be_instance_of( BinaryOpUGen )
end
-
+
it "should encode" do
@sdef.encode.should == @expected
end
-
+
it "should encode" do
expected = [ 83, 67, 103, 102, 0, 0, 0, 1, 0, 1, 4, 104, 101, 108, 112, 0, 2, 67, -36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, 6, 83, 105, 110, 79, 115, 99, 2, 0, 2, 0, 1, 0, 0, -1, -1, 0, 0, -1, -1, 0, 1, 2, 6, 83, 105, 110, 79, 115, 99, 2, 0, 2, 0, 1, 0, 0, -1, -1, 0, 0, -1, -1, 0, 1, 2, 12, 66, 105, 110, 97, 114, 121, 79, 112, 85, 71, 101, 110, 2, 0, 2, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 6, 83, 105, 110, 79, 115, 99, 2, 0, 2, 0, 1, 0, 0, -1, -1, 0, 0, -1, -1, 0, 1, 2, 12, 66, 105, 110, 97, 114, 121, 79, 112, 85, 71, 101, 110, 2, 0, 2, 0, 1, 0, 2, 0, 2, 0, 0, 0, 3, 0, 0, 2, 0, 0 ].pack('C*')
SynthDef.new(:help){ (SinOsc.ar() + SinOsc.ar()) * SinOsc.ar() }.encode.should == expected
end
-
-
-
+
+
+
end
+
View
127 spec/audio/multiout_ugen_spec.rb
@@ -10,90 +10,107 @@
include Ugens
+
+
+
describe MultiOutUgen do
-
- it "should respond to channels and channels" do
- # MultiOutUgen.new( :audio, 1 ).should respond_to(:channels)
- MultiOutUgen.new( :audio, 1 ).should respond_to(:channels)
+ before do
+ sdef = mock( 'sdef', :children => [] )
+ Ugen.should_receive( :synthdef ).and_return( sdef )
+ @proxies = MultiOutUgen.new( :audio, 1, 2, 3 )
+ @multi = sdef.children.first
+ end
+
+ it "should return an array of channels" do
+ @proxies.should be_instance_of( Array )
+ @proxies.should == [1,2,3]
+ end
+
+ it "should be instace of Control" do
+ @multi.should be_instance_of( MultiOutUgen )
+ end
+
+ it do
+ @multi.rate.should == :audio
+ end
+
+ it do
+ @multi.channels.should == [1,2,3]
end
+ it do
+ @multi.inputs.should == []
+ end
end
describe Control do
-
-
before do
- @control = mock('control', :channels= => nil)
- @names = Array.new( rand(7) + 3 ){ |i| mock('name', :rate => :control) }
- Ugen.synthdef = nil
+ sdef = mock( 'sdef', :children => [] )
+ Ugen.stub!( :synthdef ).and_return( sdef )
+
@proxy = mock('proxy', :instance_of_proxy? => true)
OutputProxy.stub!( :new ).and_return( @proxy )
+
+ @names = Array.new( rand(7) + 3 ){ |i| mock('name', :rate => :audio, :valid_ugen_input? => true) }
+ @proxies = Control.new( :audio, *@names )
+ @control = sdef.children.first
end
- it "should instantiate just with rate" do
- Control.new( :audio ).should be_instance_of( Control )
+ it "should return an array of proxies" do
+ @proxies.should be_instance_of( Array )
+ @proxies.should have( @names.size ).proxies
end
-
- it "should have empty inputs" do
- Control.new( :audio ).inputs.should == []
+
+ it "should set channels" do
+ @control.should be_instance_of( Control )
+ @control.channels.should == @names.map{ @proxy }
end
-
- it "should add to synthdef" do
- sdef = mock('synthdef', :children => [])
- Ugen.synthdef = sdef
- Control.new( :audio )
- sdef.children.should have(1).control
+
+ it "should be added to synthdef" do
+ Ugen.should_receive( :synthdef )
+ Control.new( :audio, [])
end
- it "should have an empty array for inputs" do
- Control.new( :audio ).inputs.should == []
+ it "should instantiate with #and_proxies_from" do
+ Control.should_receive(:new).with( :audio, *@names )
+ Control.and_proxies_from( @names )
end
- it do
- Control.should respond_to(:and_proxies_from)
+ it "should have index" do
+ @control.index.should == 0
end
- it "should return an array" do
- Control.and_proxies_from( @names ).should be_instance_of(Array)
- end
+end
+
+describe OutputProxy do
- it "should instantiate a control" do
- Control.should_receive(:new).and_return( @control )
- Control.and_proxies_from( @names )
+ before do
+ @sdef = mock( 'sdef', :children => [] )
+ Ugen.stub!( :synthdef ).and_return( @sdef )
+
+ @source = mock('source', :index => 0, :valid_ugen_input? => true )
+ @name = mock('control name', :valid_ugen_input? => true)
+ @output_index = mock('output_idex', :valid_ugen_input? => true)
+
+ @names = [mock('name', :rate => :audio, :valid_ugen_input? => true)]
+
end
- it "should set rate to the rate of the names of the array" do
- Control.should_receive(:new).with(:control).and_return( @control )
- Control.and_proxies_from( @names )
+ it "should receive index from control" do
+ Control.and_proxies_from( @names ).first.index.should == 0
+ @sdef.children.first.index.should == 0
end
- it "should instantiate an output proxy for each passed name" do
- OutputProxy.should_receive( :new ).exactly( @names.size ).times
- Control.and_proxies_from( @names )
+ it "should have empty inputs" do
+ OutputProxy.new( :audio, @source, @name, @output_index ).inputs.should == []
end
- it "should instantiate output proxies with the right attributes" do
- @control = mock('control', :channels= => nil)
- Control.stub!( :new ).and_return( @control )
- @names.collect_with_index { |n, i| OutputProxy.should_receive(:new).with( :control, @control, n, i) }
- Control.and_proxies_from( @names )
- end
- it "should return an array of proxies" do
- Control.and_proxies_from( @names ).each do |proxy|
- proxy.should be_instance_of_proxy
- end
+ it "should not be added to synthdef" do
+ Ugen.should_not_receive( :synthdef )
+ OutputProxy.new( :audio, @source, @name, @output_index )
end
- it "should set channels" do
- @control.should_receive( :channels= )
- Control.stub!( :new ).and_return( @control )
- Control.and_proxies_from( @names )
- end
end
-describe OutputProxy do
-
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.