Class: Scruby::Audio::Ugens::Ugen

Inherits:
Object
  • Object
show all
Includes:
UgenOperations
Defined in:
lib/scruby/audio/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_args_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_args_for :ar
  end

end

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

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 ]
@@synthdef =
nil

Constants included from UgenOperations

Scruby::Audio::Ugens::UgenOperations::BINARY, Scruby::Audio::Ugens::UgenOperations::OP_SYMBOLS, Scruby::Audio::Ugens::UgenOperations::UNARY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from UgenOperations

included, #valid_ugen_input?

Constructor Details

#initialize(rate, *inputs) ⇒ Ugen

Returns a new instance of Ugen.



65
66
67
68
69
70
71
72
73
# File 'lib/scruby/audio/ugens/ugen.rb', line 65

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.



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

def channels
  @channels
end

#indexObject (readonly)

Returns the value of attribute index.



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

def index
  @index
end

#inputsObject (readonly)

Returns the value of attribute inputs.



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

def inputs
  @inputs
end

#output_indexObject (readonly)

Returns the value of attribute output_index.



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

def output_index
  @output_index
end

#rateObject (readonly)

Returns the value of attribute rate.



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

def rate
  @rate
end

#special_indexObject (readonly)

Returns the value of attribute special_index.



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

def special_index
  @special_index
end

Class Method Details

.instantiate(*args) ⇒ Object

:nodoc:



136
137
138
139
140
# File 'lib/scruby/audio/ugens/ugen.rb', line 136

def instantiate( *args ) #:nodoc:
  obj = allocate        
  obj.__send__( :initialize, *args )
  obj
end

.new(rate, *inputs) ⇒ Object

:nodoc:

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/scruby/audio/ugens/ugen.rb', line 122

def new( rate, *inputs ) #:nodoc:
  raise ArgumentError.new( "#{rate} not a defined rate") unless RATES.include?( rate.to_sym )
  inputs.each{ |i| raise ArgumentError.new( "#{i} is not a valid ugen input") unless i.valid_ugen_input? }

  inputs = *inputs #if args is an array peel off one array skin
  inputs = inputs.to_array #may return a non_array object so we turn it into array, does nothing if already array
  size = inputs.select{ |a| a.instance_of? Array }.map{ |a| a.size }.max || 1 #get the size of the largest array element if present
  inputs.flatten! if size == 1 #if there is one or more arrays with just one element flatten the input array
  return instantiate( rate, *inputs ) unless size > 1 #return an Ugen if no array was passed as an input

  inputs = inputs.map{ |argument| argument.instance_of?( Array ) ? argument.wrap_to( size ) : Array.new( size, argument ) }.transpose
  inputs.map{ |new_inputs| new( rate, *new_inputs ) }
end

.synthdefObject

:nodoc:



142
143
144
# File 'lib/scruby/audio/ugens/ugen.rb', line 142

def synthdef #:nodoc:
  @@synthdef
end

.synthdef=(synthdef) ⇒ Object

:nodoc:



146
147
148
# File 'lib/scruby/audio/ugens/ugen.rb', line 146

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

Instance Method Details

#encodeObject



90
91
92
93
94
# File 'lib/scruby/audio/ugens/ugen.rb', line 90

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



76
77
78
# File 'lib/scruby/audio/ugens/ugen.rb', line 76

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

#to_sObject

Demodulized class name



81
82
83
# File 'lib/scruby/audio/ugens/ugen.rb', line 81

def to_s
  "#{self.class.to_s.split('::').last}"
end

#ugen?Boolean

True

Returns:

  • (Boolean)


86
87
88
# File 'lib/scruby/audio/ugens/ugen.rb', line 86

def ugen?
  true
end