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 collapse

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

#initialize(rate, *inputs) ⇒ Ugen

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

#channelsObject (readonly)

Returns the value of attribute channels.



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

def channels
  @channels
end

#indexObject (readonly)

Returns the value of attribute index.



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

def index
  @index
end

#inputsObject (readonly)

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#output_indexObject (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

#rateObject (readonly)

Returns the value of attribute rate.



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

def rate
  @rate
end

#special_indexObject (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

.paramsObject



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

def params
  {}
end

.synthdefObject

:nodoc:



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

def synthdef #:nodoc:
  @@synthdef
end

.synthdef=(synthdef) ⇒ Object

:nodoc:



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

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

.valid_input?(obj) ⇒ Boolean

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

#==(other) ⇒ Object



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

#encodeObject



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

#muladd(mul, add) ⇒ Object

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