Skip to content
Browse files

ugen defs loading

  • Loading branch information...
1 parent 110be52 commit b775dbfd23a56680807a6d8ecee8a0f9666fb282 @maca committed Oct 26, 2008
View
28 lib/scruby/audio/ugens/temp.rb
@@ -4,40 +4,44 @@
dir = File.expand_path(File.dirname(__FILE__))
-
file = File.new File.join( dir, 'ugendefs.xml')
-
# p file.read
-
doc = Document.new( file )
-
ugens = {}
doc.root.each_element('ugen') do |e|
+ klass = e.attribute(:class).value
- args = {}
- klass = e.attribute(:class).value
- rates = e.attribute(:rates).value.scan(/\w+/).collect{ |r| r.to_sym }
- # rates = e.attributes(:rates).value
+ rates = {}
+
+ e.attribute(:rates).value.scan(/\w+/).each do |r|
+ args = []
+
e.each_element('arg') do |a|
name = a.attribute( :name ).value.to_sym
begin
value = a.attribute( :def ).value.to_f
rescue
value = nil
end
- args[name] = value
+ args << [name, value].dup
end
- ugens[klass] = { :rates => rates, :args => args}
+
+
+
+ rates[r.to_sym] = Array.new(args)
+ end
+
+ ugens[klass] = rates
end
-puts ugens.to_yaml
+puts u = ugens.to_yaml
file = File.new("#{dir}/ugen_defs.yaml", "w")
-file.write( ugens.to_yaml )
+file.write( u )
file.close
View
5,026 lib/scruby/audio/ugens/ugen_defs.yaml
3,100 additions, 1,926 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
32 lib/scruby/audio/ugens/ugens.rb
@@ -1,24 +1,30 @@
module Scruby
module Audio
module Ugens
- UGEN_DEFS = YAML::load( File.open( "#{LIB_DIR}/audio/ugens/ugen_defs.yaml" ) )
+ ugen_defs = YAML::load( File.open( "#{LIB_DIR}/audio/ugens/ugen_defs.yaml" ) )
- module Instantiation
- def ar( *args )
- end
+ def self.define_ugen(name, rates)
+ rate_name = {:audio => :ar, :control => :kr, :scalar => :ir, :demand => :new}
+ rates.delete_if{ |key, value| key == :demand } #I don't know what to do with these
- def kr( *args )
+ klass = "class #{name} < Ugen\n" +
+ rates.collect do |r|
+ new_args = ( [r.first] + r.last.collect{|a|a.first} ).join(', ')
+ args = (r.last + [[:mul,1],[:add,0]]).collect{ |a| a.compact.join(' = ')}.join(', ')
+ " def self.#{rate_name[r.first]}(#{args})\n" +
+ " new(:#{new_args}).muladd(mul, add)" +
+ "\n end"
+ end.join("\n\n") + "\nend\n\n"
+
+ unless %w(InRect TWindex RecordBuf BufWr ScopeOut).include?(name)
+ self.class_eval klass
+ klass = eval(name)
+ methods = rates.collect{ |r| ":#{rate_name[r.first]}" }.join(', ')
+ self.class_eval "class #{name}; class << self; named_args_for #{methods}; end; end;"
end
end
- def self.define_ugen(name, args)
- self.module_eval("class #{name} < Ugen; end")
- klass = eval(name)
- klass.send :extend, Instantiation
- klass
- end
-
- UGEN_DEFS.each_pair do |key, value|
+ ugen_defs.each_pair do |key, value|
self.define_ugen(key, value)
end
View
2 lib/scruby/named_args.rb
@@ -21,8 +21,6 @@ def named_args_for( *methods )
"#{m}_original(" + args + ");" +
"end"
-
-
self.class_eval hacked_method
alias_method "#{m}_original", m
View
99 spec/audio/extensions_spec.rb
@@ -1,99 +0,0 @@
-require File.join(File.expand_path(File.dirname(__FILE__)),"helper")
-
-require "#{LIB_DIR}/audio/ugens/ugen_operations"
-require "#{LIB_DIR}/extensions"
-require "ruby2ruby"
-
-describe Numeric do
-
- before :all do
- @bin_op = mock('binop')
- BinaryOpUgen = mock( 'BinaryOpUgen', :new => @bin_on )
- @ugen = mock( 'ugen' )
- Ugen = mock( 'Ugen', :new => @ugen)
-
-
- end
-
- it "shoud have an scalar rate" do
- 1.rate.should eql(:scalar)
- end
-
- it "should have an scalar rate" do
- 100.0.rate.should == :scalar
- end
-
- it "sum as usual" do
- (100 + 100).should == 200
- end
-
-end
-
-describe Array, "monkey patches" do
- describe "#collect_with_index" do
- it do
- [].should respond_to( :collect_with_index )
- end
-
- it "should return an array the same size as the original" do
- [1,2,3,4].collect_with_index{ nil }.should have( 4 ).items
- end
-
- it "should collect_with_index" do
- array = %w(a, b, c, d)
- array.collect_with_index{ |element, index| [index, element] }.should eql( [0,1,2,3].zip( array ) )
- end
-
- it "should sum with Ugen"
- it "should collect constants"
-
- end
-end
-
-describe Proc do
- describe "#argument_names" do
-
- it do
- Proc.new{}.should respond_to( :argument_names )
- end
-
- it "should get empty array if proc has no args" do
- Proc.new{}.argument_names.should eql( [] )
- end
-
- it "should get one argument name" do
- Proc.new{ |arg| }.argument_names.should eql( [ :arg ] )
- end
-
- it "should get arg names with several args" do
- Proc.new{ |arg, arg2, arg3| }.argument_names.should eql( [ :arg, :arg2, :arg3 ] )
- end
- end
-end
-
-describe Array do
- describe "#wrap_to" do
-
- it do
- Array.new.should respond_to( :wrap_to )
- end
-
- it "should wrap_to!" do
- [1,2].wrap_to!(4).should == [1,2,1,2]
- end
-
- it do
- Array.new.should respond_to( :wrap_to )
- end
-
- it "should return self if the passed size is the same as self.size" do
- a = [1,2,3,4]
- a.wrap_to( 4 ).should == a
- end
-
- it "should etc..."
-
- end
-end
-
-
View
9 spec/audio/helper.rb
@@ -1,9 +0,0 @@
-require "rubygems"
-require "spec"
-require 'yaml'
-
-LIB_DIR = File.join( File.expand_path(File.dirname(__FILE__) ), '..', '..', 'lib', 'scruby')
-
-
-
-
View
2 spec/audio/ugen_spec.rb
@@ -235,7 +235,7 @@
MulAdd = mock( 'MulAdd', :new => nil )
@ugen = Ugen.new(:audio, 100, 100)
MulAdd.should_receive( :new ).with( @ugen, 1, 1)
- @ugen.muladd(1, 1)
+ @ugen.muladd(1, 1).should be_nil
end
end
end
View
20 spec/audio/ugens_spec.rb
@@ -1,18 +1,19 @@
require File.join( File.expand_path(File.dirname(__FILE__)), '..',"helper")
require 'yaml'
+require "#{LIB_DIR}/named_args"
require "#{LIB_DIR}/audio/ugens/ugen_operations"
require "#{LIB_DIR}/audio/ugens/ugen"
require "#{LIB_DIR}/audio/ugens/ugens"
require "#{LIB_DIR}/extensions"
+
module UgenTest
end
class Klass
end
-
include Scruby
include Audio
include Ugens
@@ -21,24 +22,29 @@ class Klass
describe Ugens do
before do
- @udefs = Ugens::UGEN_DEFS
+ @udefs = YAML::load( File.open( "#{LIB_DIR}/audio/ugens/ugen_defs.yaml" ) )
+ end
+
+ it do
+ @udefs.each_pair { |key, val| eval(key).should_not be_nil }
end
it do
- Ugens::UGEN_DEFS.should be_instance_of(Hash)
+ @udefs.each_pair { |key, val| begin; eval(key).superclass.should eql( Scruby::Audio::Ugens::Ugen ); rescue; end }
end
it do
- @udefs.each_pair { |key, val| eval(key).should_not be_nil }
+ Vibrato.should respond_to(:ar)
+ Vibrato.should respond_to(:kr)
end
it do
- @udefs.each_pair { |key, val| eval(key).superclass.should eql( Scruby::Audio::Ugens::Ugen ) }
+ Gendy1.should respond_to(:named_args_for)
end
it do
- Vibrato.should respond_to(:ar)
- Vibrato.should respond_to(:kr)
+ Gendy1.should_receive(:new).with( :audio, 1.0, 1.0, 1.0, 1.0, 440.0, 660.0, 0.5, 0.5, 12.0, nil ).and_return( mock('ugen', :muladd => nil) )
+ Gendy1.ar
end
end
View
39 spec/named_args_specs.rb
@@ -9,15 +9,11 @@ def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7,
end
class B
- # named_args_for :test
def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7, ocho = 8, nueve = 9, diez = 10, once = 11, doce = 12, trece = 13, catorce = 14, quince = 15, dieciseis = 16)
end
- B.send( :named_args_for, :test) #why can't I use act_as style?
+ named_args_for :test
end
-
-
-
describe NamedArgs do
before :all do
@@ -35,6 +31,12 @@ def three( uno = 1, dos = 2, tres = 3 )
def six( uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6)
end
+
+ class << self
+ def dos( uno = 1, dos = 2)
+ end
+ named_args_for :dos
+ end
end
@unbound = mock('unbound', :args => [], :arg_names => [] )
end
@@ -166,6 +168,15 @@ def six( uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6)
end
end
+
+ describe 'Class methods' do
+
+ it do
+ Klass.should_receive(:dos_original)
+ Klass.dos
+ end
+
+ end
end
@@ -190,6 +201,24 @@ def six( uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6)
end
end
end
+
+ it do
+ Benchmark.bm do |x|
+ x.report do
+ class C
+ def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7, ocho = 8, nueve = 9, diez = 10, once = 11, doce = 12, trece = 13, catorce = 14, quince = 15, dieciseis = 16)
+ end
+ end
+ end
+ x.report do
+ class D
+ def test(uno = 1, dos = 2, tres = 3, cuatro = 4, cinco = 5, seis = 6, siete = 7, ocho = 8, nueve = 9, diez = 10, once = 11, doce = 12, trece = 13, catorce = 14, quince = 15, dieciseis = 16)
+ end
+ named_args_for :test
+ end
+ end
+ end
+ end
end

0 comments on commit b775dbf

Please sign in to comment.
Something went wrong with that request. Please try again.