Class: Scruby::Ugens::Ugen

Inherits:
Object show all
Defined in:
lib/scruby/ugens/ugen.rb

Overview

All ugens inherit from this "abstract" class

Creation

Ugens are usually instantiated inside an "ugen graph" or the block passed when creating a SynthDef using either the ar, kr, ir or new methods wich will determine the rate.

* ar: audio rate
* kr: control rate
* ir: scalar rate
* new: demand rate

Not all the ugens provide all the rates

Two ugens inside an ugen graph:

SynthDef.new('simple'){ Out.ar(0, SinOsc.ar) }
# Out and SinOsc are both ugens

Passing arguments when creating

Usually when instantiating an ugen the arguments can be passed in order:

Pitch.kr(0, 220, 80, ...)

Or using a hash where the keys are symbols corresponding to the argument name.

Pitch.kr( :initFreq => 220, :execFreq => 300 )

Or a combination of both ways:

Pitch.kr(0, 220, :execFreq => 300)

Arguments not passed in either way will resort to default

Defining ugens

This named arguments functionality is provided for all the default Ugens but can be provided when defining a new Ugen by calling #named_arguments_for passing a symbol with the name of a defined method:

class Umaguma < Ugen
  class << self
    def ar(karma = 200, pitch = 20, rate = 200)
      ...
    end
    named_arguments_for :ar
  end

end

For more info and limitations on named arguments check the gem: github.com/maca/arguments

Otherwise usage is pretty the same as in SuperCollider

TODO: Provide a way of getting the argument names and default values

Constant Summary

RATES =
:scalar, :trigger, :demand, :control, :audio
E_RATES =
:scalar, :control, :audio, :demand
VALID_INPUTS =
Numeric, Array, Ugen, Env, ControlName
@@synthdef =
nil

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Ugen) initialize(rate, *inputs)

Returns a new instance of Ugen



64
65
66
67
68
69
70
# File 'lib/scruby/ugens/ugen.rb', line 64

def initialize rate, *inputs
  @rate, @inputs   = rate, inputs.compact
  @special_index ||= 0
  @output_index  ||= 0
  @channels      ||= [1]
  @index           = add_to_synthdef || 0
end

Instance Attribute Details

- (Object) channels (readonly)

Returns the value of attribute channels



56
57
58
# File 'lib/scruby/ugens/ugen.rb', line 56

def channels
  @channels
end

- (Object) index (readonly)

Returns the value of attribute index



56
57
58
# File 'lib/scruby/ugens/ugen.rb', line 56

def index
  @index
end

- (Object) inputs (readonly)

Returns the value of attribute inputs



56
57
58
# File 'lib/scruby/ugens/ugen.rb', line 56

def inputs
  @inputs
end

- (Object) output_index (readonly)

Returns the value of attribute output_index



56
57
58
# File 'lib/scruby/ugens/ugen.rb', line 56

def output_index
  @output_index
end

- (Object) rate (readonly)

Returns the value of attribute rate



56
57
58
# File 'lib/scruby/ugens/ugen.rb', line 56

def rate
  @rate
end

- (Object) special_index (readonly)

Returns the value of attribute special_index



56
57
58
# File 'lib/scruby/ugens/ugen.rb', line 56

def special_index
  @special_index
end

Class Method Details

+ (Object) params



166
167
168
# File 'lib/scruby/ugens/ugen.rb', line 166

def params
  {}
end

+ (Object) synthdef

:nodoc:



158
159
160
# File 'lib/scruby/ugens/ugen.rb', line 158

def synthdef #:nodoc:
  @@synthdef
end

+ (Object) synthdef=(synthdef)

:nodoc:



162
163
164
# File 'lib/scruby/ugens/ugen.rb', line 162

def synthdef= synthdef #:nodoc:
  @@synthdef = synthdef
end

+ (Boolean) valid_input?(obj)

Returns:

  • (Boolean)


151
152
153
154
155
156
# File 'lib/scruby/ugens/ugen.rb', line 151

def valid_input? obj
  case obj
  when *VALID_INPUTS then true
  else false
  end
end

Instance Method Details

- (Object) ==(other)



109
110
111
112
113
114
# File 'lib/scruby/ugens/ugen.rb', line 109

def == other
  self.class    == other.class    and
  self.rate     == other.rate     and
  self.inputs   == other.inputs   and
  self.channels == other.channels 
end

- (Object) encode



77
78
79
80
81
# File 'lib/scruby/ugens/ugen.rb', line 77

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*')
end

- (Object) muladd(mul, add)

Instantiate a new MulAdd passing self and the multiplication and addition arguments



73
74
75
# File 'lib/scruby/ugens/ugen.rb', line 73

def muladd mul, add
  MulAdd.new self, mul, add
end