Permalink
Browse files

implemented BufIO classes, refactored Multi Out Ugens

  • Loading branch information...
Macario
Macario committed Aug 18, 2009
1 parent 8080ee0 commit 74a8e8e3e1c461ea23f0c921a2b5c2727be528d6
View
@@ -19,12 +19,13 @@
require 'rubygems'
require 'arguments'
require 'rosc'
+require 'eventmachine'
require 'yaml'
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
module Scruby
- VERSION = '0.2.2'
+ VERSION = '0.2.5'
end
require "scruby/core_ext/object"
@@ -42,6 +43,8 @@ module Scruby
require "scruby/ugens/ugen"
require "scruby/ugens/ugen_operations"
require "scruby/ugens/multi_out_ugens"
+require "scruby/ugens/panner"
+require "scruby/ugens/buffer_read_write"
require "scruby/ugens/in_out"
require "scruby/ugens/operation_ugens"
@@ -57,5 +60,7 @@ module Scruby
require "scruby/bus"
require "scruby/buffer"
+require "scruby/ticker"
+
include Scruby
include Ugens
@@ -1,12 +1,11 @@
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
-
alias :value :call
end
View
@@ -0,0 +1,93 @@
+module Scruby
+ Thread.new do
+ EventMachine.run do
+ EM.set_quantum 5
+
+ EM.error_handler do |e|
+ puts e
+ end
+ end
+ end
+
+ # A timer will call a given block periodically. The period is specified in beats per minute.
+ class Ticker
+ attr_reader :start, :tick, :interval
+ attr_accessor :tempo, :resolution, :size, :loop
+
+ def initialize tempo = 120, size = 16, resolution = 1, loop = true, &block
+ @tempo , @resolution, @size, @loop = tempo , resolution, size, loop
+ @interval = 60.0 / @tempo
+ @tick = 0
+ @block = block
+ end
+
+ named_args_for :initialize
+
+ def block &block
+ @block = block
+ end
+
+ def run
+ return self if @timer
+ @start = Time.now
+ @timer = EventMachine::PeriodicTimer.new @interval * 0.01 do
+ if @next.nil? or Time.now >= @next
+ dispatch
+ @tick += @resolution
+ next_time
+ end
+ end
+ self
+ end
+
+ def index
+ return @tick unless @size
+ tick = @tick % @size
+ if tick == 0 and @tick > 0 and !@loop
+ stop
+ nil
+ else
+ tick
+ end
+ end
+
+ def next_time
+ @next = @start + @tick * @interval
+ end
+
+ def stop
+ @timer.cancel if @timer
+ @timer = nil
+ @next = nil
+ @tick = 0
+ self
+ end
+
+ def running?
+ not @timer.nil?
+ end
+
+ def dispatch
+ @block.call index if @block
+ end
+ end
+
+ class Scheduler < Ticker
+ def initialize opts = {}
+ super
+ @queue = []
+ end
+
+ def dispatch
+ if blocks = @queue[index]
+ blocks.each{ |b| b.call }
+ end
+ end
+
+ def at tick, &proc
+ @queue[tick] ||= []
+ @queue[tick].push proc
+ end
+ end
+
+end
@@ -0,0 +1,99 @@
+module Scruby
+ module Ugens
+ class PlayBuf < Ugen
+ include MultiOut
+
+ class << self
+ def kr channels, bufnum = 0, rate = 1.0, trigger = 1.0, start = 0.0, loop = 0.0, doneAction = 0
+ new :control, channels, bufnum, rate, trigger, start, loop, doneAction
+ end
+
+ def ar channels, bufnum = 0, rate = 1.0, trigger = 1.0, start = 0.0, loop = 0.0, doneAction = 0;
+ new :audio, channels, bufnum, rate, trigger, start, loop, doneAction
+ end
+ named_args_for :kr, :ar
+ end
+ end
+
+ class TGrains < Ugen
+ include MultiOut
+
+ def initialize rate, channels, *inputs
+ raise ArgumentError.new("#{ self.class } instance needs at least two channels.") unless channels > 1
+ super
+ end
+
+ class << self
+ def ar channels, trigger = 0, bufnum = 0, rate = 1, centerPos = 0
+ new :audio, channels, trigger, bufnum, rate, centerPos
+ end
+ named_args_for :ar
+ end
+ end
+
+ class BufRd < Ugen
+ include MultiOut
+
+ class << self
+ def kr channels, bufnum = 0, phase = 0.0, loop = 1.0, interpolation = 2
+ new :control, channels, bufnum, phase, loop, interpolation
+ end
+
+ def ar channels, bufnum = 0, phase = 0.0, loop = 1.0, interpolation = 2
+ new :audio, channels, bufnum, phase, loop, interpolation
+ end
+ named_args_for :kr, :ar
+ end
+ end
+
+ class BufWr < Ugen
+ class << self
+ def kr input, bufnum = 0, phase = 0.0, loop = 1.0
+ new :control, bufnum, phase, loop, *input.to_array
+ end
+
+ def ar input, bufnum = 0, phase = 0.0, loop = 1.0
+ new :audio, bufnum, phase, loop, *input.to_array
+ end
+ named_args_for :kr, :ar
+ end
+ end
+
+ class RecordBuf < Ugen
+ class << self
+ def kr input, bufnum = 0, offset = 0.0, rec_level = 1.0, pre_level = 0.0, run = 1.0, loop = 1.0, trigger = 1.0, doneAction=0
+ new :control, bufnum, offset, rec_level, pre_level, run, loop, trigger, doneAction, *input.to_array
+ end
+
+ def ar input, bufnum = 0, offset = 0.0, rec_level = 1.0, pre_level = 0.0, run = 1.0, loop = 1.0, trigger = 1.0, doneAction=0
+ new :audio, bufnum, offset, rec_level, pre_level, run, loop, trigger, doneAction, *input.to_array
+ end
+ named_args_for :kr, :ar
+ end
+ end
+
+ class ScopeOut < Ugen
+ class << self
+ def kr input, bufnum = 0
+ new :control, bufnum, *input.to_array
+ end
+
+ def ar input, bufnum = 0
+ new :audio, bufnum, *input.to_array
+ end
+ named_args_for :kr, :ar
+ end
+ end
+
+ class Tap < Ugen
+ class << self
+ def ar bufnum = 0, num_channels = 1, delay_time = 0.2
+ n = SampleRate.ir.neg * 3
+ PlayBuf.ar num_channels, bufnum, 1, 0, n, 1
+ end
+ named_args_for :ar
+ end
+ end
+
+ end
+end
View
@@ -1,11 +1,7 @@
module Scruby
module Ugens
- class In < MultiOutUgen
- #:nodoc:
- def initialize rate, channels, bus
- super rate, *(0...channels).map{ |i| OutputProxy.new rate, self, i }
- @inputs = [bus]
- end
+ class In < Ugen
+ include MultiOut
class << self
# New In with :audio rate, inputs should be valid Ugen inputs or Ugens, arguments can be passed as an options hash or in the given order
@@ -16,18 +12,11 @@ def ar bus, channels = 1
def kr bus, channels = 1
new :control, channels, bus
end
-
- def params #:nodoc:
- {:audio => [[:bus, nil], [:channels, 1], [:mul, 1], [:add, 0]], :control => [[:bus, nil], [:channels, 1], [:mul, 1], [:add, 0]]}
- end
-
- private
- def new *args; super; end
end
end
class Out < Ugen
- # ar and kr should be use for instantiatio
+ # ar and kr should be use for instantiation
def initialize *args
super
@channels = []
@@ -48,14 +37,6 @@ def kr bus, *inputs
inputs.peel!
new :control, bus, *inputs; 0.0 #Out has no output
end
-
-
- def params #:nodoc:
- {:audio => [[:bus,nil], [:inputs, []], [:mul, 1], [:add, 0]], :control => [[:bus,nil], [:inputs, []], [:mul, 1], [:add, 0]]}
- end
-
- private
- def new *args; super; end
end
end
end
@@ -1,7 +1,30 @@
module Scruby
module Ugens
+ module MultiOut #:nodoc:
+ def self.included base
+ base.extend ClassMethods
+ end
+
+ def initialize rate, channels, *inputs
+ super rate, *inputs
+ @channels = Array === channels ? channels : (0...channels).map{ |i| OutputProxy.new rate, self, i }
+ end
+
+ private
+ def output_specs
+ channels.collect{ |output| output.send :output_specs }.flatten
+ end
+
+ module ClassMethods
+ def new *args
+ super.channels #returns the channels but gets instantiated, TODO: Maybe not a good idea
+ end
+ end
+ end
+
class OutputProxy < Ugen #:nodoc:
attr_reader :source, :control_name, :output_index
+ class << self; public :new; end
def initialize rate, source, output_index, name = nil
super rate
@@ -11,29 +34,16 @@ def initialize rate, source, output_index, name = nil
def index; @source.index; end
def add_to_synthdef; end
+
end
- class MultiOutUgen < Ugen #:nodoc:
- def initialize rate, *channels
- super rate
- @channels = channels
- end
-
- def self.new rate, *args
- super.channels #returns the channels but gets instantiated, TODO: Maybe not a good idea
- end
-
- private
- def output_specs
- channels.collect{ |output| output.send :output_specs }.flatten
- end
- end
-
- class Control < MultiOutUgen #:nodoc:
+ class Control < Ugen #:nodoc:
+ include MultiOut
+
def initialize rate, *names
- super rate, *names.collect_with_index{|n, i| OutputProxy.new rate, self, i, n }
+ super rate, names.collect_with_index{ |n, i| OutputProxy.new rate, self, i, n }
end
-
+
def self.and_proxies_from names
new names.first.rate, *names
end
Oops, something went wrong.

0 comments on commit 74a8e8e

Please sign in to comment.