Class: Scruby::Audio::SynthDef

Inherits:
Object
  • Object
show all
Defined in:
lib/scruby/audio/synthdef.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}, &block) ⇒ SynthDef

Creates a new SynthDef instance An “ugen graph” block should be passed:

SynthDef.new('simple') do |rate|
  Out.ar( 0, SinOsc.ar(rate) )
end

Default values and rates for the block can be passed with the :values => [] and :rates => [] options: E.g.

SynthDef.new( :am, :values => [1, 1000, 10, 1] ) do |gate, portadora, moduladora, amp|
  modulacion = SinOsc.kr( moduladora, 0, 0.5, 0.5 )
  sig = SinOsc.ar( portadora, 0, modulacion )
  env = EnvGen.kr( Env.asr(2,1,2), gate, :doneAction => 2 )
  Out.ar( 0, sig*env*amp )
end

is equivalent to the Sclang SynthDef

SynthDef(\am, {|gate=1, portadora=1000, moduladora=10, amp=1|
    var modulacion, sig, env;
    modulacion = SinOsc.kr(moduladora, 0, 0.5, 0.5);
    sig = SinOsc.ar(portadora, 0, modulacion);
    env = EnvGen.kr(Env.asr(2,1,2), gate, doneAction:2);
    Out.ar(0, sig*env*amp);
}).send(s)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/scruby/audio/synthdef.rb', line 30

def initialize( name, options = {}, &block )
  @name, @children = name.to_s, []

  values = options.delete( :values ) || []
  rates  = options.delete( :rates )  || []
  block  = block || Proc.new{}

  @control_names = collect_control_names( block, values, rates )
  build_ugen_graph( block, @control_names )
  @constants = collect_constants( @children )
  
  @variants = [] #stub!!!
  
  warn( 'A SynthDef without a block is useless' ) unless block_given?
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



4
5
6
# File 'lib/scruby/audio/synthdef.rb', line 4

def children
  @children
end

#constantsObject (readonly)

Returns the value of attribute constants.



4
5
6
# File 'lib/scruby/audio/synthdef.rb', line 4

def constants
  @constants
end

#control_namesObject (readonly)

Returns the value of attribute control_names.



4
5
6
# File 'lib/scruby/audio/synthdef.rb', line 4

def control_names
  @control_names
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/scruby/audio/synthdef.rb', line 4

def name
  @name
end

Instance Method Details

#encodeObject

Returns a string representing the encoded SynthDef in a way scsynth can interpret and generate. This method is called by a server instance when sending the synthdef via OSC.

For complex synthdefs the encoded synthdef can vary a little bit from what SClang would generate but the results will be interpreted in the same way



51
52
53
54
55
56
57
58
# File 'lib/scruby/audio/synthdef.rb', line 51

def encode
  controls = @control_names.reject { |cn| cn.non_control? }
  encoded_controls = [controls.size].pack('n') + controls.collect{ |c| c.name.encode + [c.index].pack('n') }.to_s
  
  init_stream + name.encode + constants.encode_floats + values.flatten.encode_floats + encoded_controls +
      [children.size].pack('n') + children.collect{ |u| u.encode }.join('') +
      [@variants.size].pack('n') #stub!!!
end

#init_stream(file_version = 1, number_of_synths = 1) ⇒ Object

:nodoc:



60
61
62
# File 'lib/scruby/audio/synthdef.rb', line 60

def init_stream(file_version = 1, number_of_synths = 1) #:nodoc:
  'SCgf' + [file_version].pack('N') + [number_of_synths].pack('n')
end

#send(*servers) ⇒ Object

Sends itself to the given servers. One or more servers or an array of servers can be passed. If no arguments are given the synthdef gets sent to all instantiated servers E.g.

s = Server.new('localhost', 5114)
s.boot
r = Server.new('127.1.1.2', 5114)

SynthDef.new('sdef'){ Out.ar(0, SinOsc.ar(220)) }.send(s)
# this synthdef is only sent to s

SynthDef.new('sdef2'){ Out.ar(1, SinOsc.ar(222)) }.send
# this synthdef is sent to both s and r


82
83
84
85
86
# File 'lib/scruby/audio/synthdef.rb', line 82

def send( *servers )
  servers = *servers
  (servers ? servers.to_array : Server.all).each{ |s| s.send_synth_def( self ) } 
  self
end

#send_msgObject



68
# File 'lib/scruby/audio/synthdef.rb', line 68

alias :send_msg :send

#valuesObject

:nodoc:



64
65
66
# File 'lib/scruby/audio/synthdef.rb', line 64

def values #:nodoc:
  @control_names.collect{ |control| control.value }
end