Class: Scruby::SynthDef

Inherits:
Object show all
Defined in:
lib/scruby/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)

Raises:

  • (ArgumentError)


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

def initialize name, options = {}, &block
  @name, @children = name.to_s, []
  raise( ArgumentError.new('An UGen graph (block) must be passed') ) unless block_given?

  values = options.delete( :values ) || []
  rates  = options.delete( :rates )  || []

  @control_names = collect_control_names block, values, rates
  build_ugen_graph block, @control_names
  @constants = collect_constants @children

  @variants  = [] #stub!!!
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#constantsObject (readonly)

Returns the value of attribute constants.



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

def constants
  @constants
end

#control_namesObject (readonly)

Returns the value of attribute control_names.



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

def control_names
  @control_names
end

#nameObject (readonly)

Returns the value of attribute name.



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

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



48
49
50
51
52
53
54
55
# File 'lib/scruby/synthdef.rb', line 48

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') }.join
  
  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:



57
58
59
# File 'lib/scruby/synthdef.rb', line 57

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


79
80
81
82
83
# File 'lib/scruby/synthdef.rb', line 79

def send *servers
  servers.peel!
  (servers.empty? ? Server.all : servers).each{ |s| s.send_synth_def( self ) } 
  self
end

#send_msgObject



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

alias :send_msg :send

#valuesObject

:nodoc:



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

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