Product SiteDocumentation Site

11.3.10.7. Creating Change Anyway

The way to create change and pseudo-randomness anyway is to incorporate another UGen to do it for you. Remember: when you send synthesis information to the server, that information can't change unless you replace it. This doesn't mean that the output produced by the synth can't change!
The way to do this is with control-rate UGen's. The following example uses a control-rate SinOsc to set the frequency of an audio-rate SinOsc.
(
   var myRandFunc =
   {
      var frequency = SinOsc.kr( freq:0.5, add:660, mul:220 ); // oscillates between 440 and 880, hitting each extreme every 2 seconds
      Out.ar( 0, SinOsc.ar( freq:frequency, mul:0.2 ) );
   };

   SynthDef.new( \myRandFunc, myRandFunc ).send( s );

   Synth.new( \myRandFunc );
)
When you use a UGen as a control-rate UGen, you have to think about its arguments quite differently than when using it as an audio-rate UGen. This table shows how the same argument gives a different result for an audio-rate vs. control-rate UGen used for pitch:
Table 11.4. Parameters in Audio-Rate and Control-Rate SinOsc UGens
Parameter In an audio-rate UGen... In a control-rate UGen...
freq controls the pitch controls the speed of oscillation
add ?? sets the middle point of the sine wave by adding this to the output
mul controls volume level sets the deviation from "add"

For an audio-rate SinOsc UGen, you set the frequency and the volume level. For a control-rate UGen, you set the mid-point of oscillation with add, the extremes of oscillation which will be add - mul and add + mul, and the speed of oscillation with freq. The end result is very different numbers.
There is a handy UGen designed specifically for replacing pseudo-randomness in Functions. The following example restores the "ten different pitches" to the example from the last section.
(
   var myRandFunc =
   {
      var frequency = Rand( 440, 880 ); // produces an integer between 440 and 880
      Out.ar( 0, SinOsc.ar( freq:frequency, mul:0.025 ) );
   };

   SynthDef.new( \myRandFunc, myRandFunc ).send( s );

   10.do( { Synth.new( \myRandFunc ); } );
)
If you run this multiple times, you will again hear ten different pitches. Depending on audio hardware, previous musical experience, and other factors, some people may have difficulty hearing that the pitches are different. Try reducing the number of synths created in the loop.