Example 2

From CSWiki
Jump to: navigation, search

This is a slightly modified example which was first written in SuperCollider. It's from example code on the SuperCollider home page and was written by Lance Putnam.

the SuperCollider code

This code was written to highlight the efficiency of SC code. This makes it a bit harder to read, so I've presented an expanded view of the same thing after the original.

play{SinOsc.ar(Mix(LFSaw.ar([1,0.99],[0,0.6],2000,2000).trunc([400,600])*[1,-1])).dup*0.1};	

The verbose version:

{
	SinOsc.ar(
		Mix( // the frequency of the SinOsc is a summed signal comprised of...
			LFSaw.ar( // ...Low-frequency Saw oscillators. 
				[1,0.99], // the freq here is an array, which SC interprets
						  // as multi-channel expansion.
						  // the small difference in freq will result in a
						  // Steve Reich-style phasing between the two LFSaws.
				[0,0.6],  // the phase argument is also an array
				2000,     // the multiply argument
				2000	  // the add argument
			).trunc([400,600])*[1,-1]
		)
					// one of the LFSaws is rounded to the nearest 400 Hz
					// the other to the nearest 600
					// the 600 Hz one is then multiplied by -1, to flip the phase
	).dup*0.1 // .dup is short for "duplicate", and creates a second version of this SinOsc
			  // this is a cheap way of getting the mono signal in both channels
			  // the *0.1 is just scaling for quieter playback
}.play

the ChucK code

SinOsc sin => Gain gain => dac;
Osc saw1;
Osc saw2;

   1 => saw1.freq;
   0 => saw1.phase;
0.99 => saw2.freq;
 0.6 => saw2.phase;
  
 0.1 => gain.gain;
 
saw1 => blackhole;
saw2 => blackhole; 

while (true)
{
	(400.00 * Math.trunc(saw1.last() * 10) )
	+
	(600.00 * Math.trunc(saw2.last() * 10) * -1) 
	=> sin.freq;
	1::samp => now;
};