Permalink
Browse files

refactored Server, all tests are passing again

  • Loading branch information...
1 parent cccff37 commit 5b3868bc30cbdb01423c3cac5fa3af56c4be73f9 Macario committed Jul 16, 2009
@@ -1,9 +1,7 @@
module Scruby
class Buffer
-
# allocReadChannel
-
# allocReadMsg
# alloc ReadCannelMsg
# read
@@ -87,7 +85,7 @@ def bufnum
# :nodoc:
def allocate_and_read path, start, frames, &completion
@path = path
- @server.send "/b_allocRead", bufnum# , path, start, frames, yield(self)
+ @server.send "/b_allocRead", bufnum, path, start, frames, Message.new("/b_query", bufnum)
self
end
@@ -67,13 +67,13 @@ def to_array
def shape_numbers
curves.collect do |curve|
- Ugen.valid_input?( curve ) ? 5 : SHAPE_NAMES[curve]
+ Audio::Ugens::Ugen.valid_input?( curve ) ? 5 : SHAPE_NAMES[curve]
end
end
def curve_values
curves.collect do |curve|
- Ugen.valid_input?( curve ) ? curve : 0
+ Audio::Ugens::Ugen.valid_input?( curve ) ? curve : 0
end
end
@@ -2,30 +2,36 @@
module Scruby
module Audio
+ include OSC
+
+ class Message < OSC::Message
+ def initialize command, *args
+ args.peel!
+ super command, type_tags(args), *args
+ end
+
+ def type_tags *args
+ args.peel!
+ args.collect{ |arg| OSC::Packet.tag arg }.join
+ end
+ end
+
class UDPSender < OSC::UDPServer #:nodoc:
include Singleton
- include OSC
alias :udp_send :send
def send command, host, port, *args
args = args.collect{ |arg| arg.kind_of?( Symbol ) ? arg.to_s : arg }
- udp_send Message.new( command, type_tag(args), *args ), 0, host, port
+ udp_send Message.new( command, *args ), 0, host, port
end
def send_message message, host, port
udp_send message, 0, host, port
end
-
- def type_tag *args
- args = *args
- args.collect{ |msg| Packet.tag msg }.to_s
- end
end
$UDP_Sender = UDPSender.instance
class Server
- include OSC
-
attr_reader :host, :port, :path, :buffers
# Initializes and registers a new Server instance and sets the host and port for it.
@@ -53,7 +59,7 @@ def boot
ready = false
@thread = Thread.new do
- IO.popen( "cd #{ File.dirname @path }; ./#{ File.basename @path } -u #{ @port }" ) do |pipe|
+ IO.popen "cd #{ File.dirname @path }; ./#{ File.basename @path } -u #{ @port }" do |pipe|
loop do
if response = pipe.gets
puts response
@@ -92,9 +98,7 @@ def send command, *args
# Encodes and sends a SynthDef to the scsynth server
def send_synth_def synth_def
- blob = Blob.new( synth_def.encode ), 0
- def_message = Message.new '/d_recv', $UDP_Sender.type_tag(blob), *blob
- send_message Bundle.new nil, def_message
+ send_message Bundle.new nil, Message.new( '/d_recv', Blob.new(synth_def.encode), 0 )
end
def send_message message #:nodoc:
@@ -104,7 +108,7 @@ def send_message message #:nodoc:
def allocate_buffers *buffers
buffers.peel!
if @buffers.compact.size + buffers.size > 1024
- raise SCError, 'No more buffer numbers -- free some buffers before allocation more.'
+ raise SCError, 'No more buffer numbers -- free some buffers before allocating more.'
end
@buffers += buffers
end
@@ -84,30 +84,28 @@ def send *servers
end
private
- #:nodoc:
def collect_control_names function, values, rates
names = function.arguments
names.zip( values, rates ).collect_with_index{ |array, index| ControlName.new *(array << index) }
end
- #:nodoc:
+
def build_controls control_names
# control_names.select{ |c| c.rate == :noncontrol }.sort_by{ |c| c.control_name.index } +
[:scalar, :trigger, :control].collect do |rate|
same_rate_array = control_names.select{ |control| control.rate == rate }
Control.and_proxies_from( same_rate_array ) unless same_rate_array.empty?
end.flatten.compact.sort_by{ |proxy| proxy.control_name.index }
end
- #:nodoc:
+
def build_ugen_graph function, control_names
Ugen.synthdef = self
function.call *build_controls(control_names)
Ugen.synthdef = nil
end
- #:nodoc:
+
def collect_constants children
children.send( :collect_constants ).flatten.compact.uniq
end
-
end
end
end
@@ -48,7 +48,7 @@ module Ugens
#
# end
#
- # For more info and limitations on named arguments check the gem: http://github.com/maca/namedarguments/tree/master
+ # For more info and limitations on named arguments check the gem: http://github.com/maca/arguments
#
# Otherwise usage is pretty the same as in SuperCollider
#
@@ -62,24 +62,17 @@ class Ugen
def initialize rate, *inputs
@rate, @inputs = rate, inputs.compact
-
@special_index ||= 0
@output_index ||= 0
@channels ||= [1]
-
@index = add_to_synthdef || 0
end
# Instantiate a new MulAdd passing self and the multiplication and addition arguments
def muladd mul, add
MulAdd.new self, mul, add
end
-
- # Demodulized class name
- def to_s
- "#{self.class.to_s.split('::').last}"
- 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*') +
@@ -112,12 +105,6 @@ def output_specs #:nodoc:
end
class << self
- def valid_input? obj
- not [Ugen, ControlName, Env, UgenOperations].collect do |m|
- true if obj.kind_of? m
- end.compact.empty?
- end
-
#:nodoc:
def new rate, *inputs
raise ArgumentError.new( "#{rate} not a defined rate") unless RATES.include?( rate.to_sym )
@@ -135,7 +122,7 @@ def new rate, *inputs
#:nodoc:
def instantiate *args
- obj = allocate
+ obj = allocate
obj.__send__ :initialize, *args
obj
end
@@ -148,6 +135,12 @@ def synthdef
def synthdef= synthdef #:nodoc:
@@synthdef = synthdef
end
+
+ def valid_input? obj
+ not [Ugen, ControlName, Env, UgenOperations].collect do |m|
+ true if obj.kind_of? m
+ end.compact.empty?
+ end
end
end
end
@@ -75,7 +75,9 @@ class << self
# # TODO: Load from ~/Ugens directory
end
- YAML::load( File.open( File.dirname(__FILE__) + "/ugen_defs.yaml" ) ).each_pair{ |key, value| self.define_ugen key, value }
+ YAML::load( File.open( File.dirname(__FILE__) + "/ugen_defs.yaml" ) ).each_pair do |key, value|
+ self.define_ugen key, value
+ end
end
end
end
@@ -1,122 +0,0 @@
-
-class Object
- # Wraps self int an array, #to_a seems to be deprecated
- def to_array
- [*self]
- end
-
- # Returns false
- def valid_ugen_input?; false; end
-end
-
-class Numeric
- # Rate is :scalar
- def rate; :scalar; end
-
- # Compares itself with +other+ and returns biggest
- def max other
- self > other ? self : other
- end
-
- # Compares itself with +other+ and returns smallest
- def min other
- self < other ? self : other
- end
-end
-
-class Numeric
- private
- def collect_constants #:nodoc:
- self
- end
-
- def input_specs synthdef
- [-1, synthdef.constants.index(self)]
- end
-end
-
-class Array
-
- #collect with index
- def collect_with_index
- self.zip( (0...self.size).map ).collect{ |element, index| yield element, index }
- end
-
- def wrap_to size
- return self if size == self.size
- self.dup.wrap_to! size
- end
-
- def wrap_to! size
- return nil if size == self.size
- original_size = self.size
- size.times { |i| self[ i ] = self[ i % original_size ] }
- self
- end
-
- def wrap_and_zip *args
- max = args.map{ |a| instance_of?(Array) ? a.size : 0 }.max.max( self.size )
- args = args.collect{ |a| a.to_array.wrap_to( max ) }
- self.wrap_to( max ).zip( *args )
- end
-
- # Returns self
- def to_array; self; end
-
- def encode_floats #:nodoc:
- [self.size].pack('n') + self.pack('g*') #TODO: Deprecate
- end
-
- def muladd mul, add #:nodoc:
- self.collect{ |u| MulAdd.new( u, mul, add ) }
- end
-
- def peel!
- self.replace self.first if self.first.kind_of? Array if self.size == 1
- end
-
- def peel
- self.dup.peel! || self
- end
-
- private
- def collect_constants #:nodoc:
- self.collect{ |e| e.send( :collect_constants ) }
- end
-end
-
-class Proc
- # Returns an array of symbols corresponding to the argument names
- def arguments
- case self.arity
- when -1..0 then []
- when 1 then self.to_sexp.assoc( :dasgn_curr )[1].to_array
- else self.to_sexp[2][1][1..-1].collect{ |arg| arg[1] }
- end
- end
-end
-
-class String
- def encode #:nodoc:
- [self.size & 255].pack('C*') + self[0..255]
- end
-end
-
-
-class Symbol
- def to_proc
- proc { |obj, *args| obj.send(self, *args) }
- end
-end
-
-# Musical math
-class Fixnum
- def freq
- 440 * (2 ** ((self - 69) * 0.083333333333) )
- end
-end
-
-
-
-
-
Oops, something went wrong.

0 comments on commit 5b3868b

Please sign in to comment.