Difference between revisions of "Example 4"

From CSWiki
Jump to: navigation, search
 
(the ChucK version)
Line 27: Line 27:
 
  SinOsc m => blackhole; // modulator
 
  SinOsc m => blackhole; // modulator
 
  Std.rand2f(-1.0,1.0) => panner.pan;
 
  Std.rand2f(-1.0,1.0) => panner.pan;
  Phasor ampPhasor => CurveTable ampEnv => blackhole;
+
  Std.rand2f(20.0,2000.0) => float cf => c.freq;
 +
  Std.rand2f(0.0,800.0) => float mf => m.freq;
 +
 +
  Phasor ampPhasor => CurveTable ampEnv => blackhole;
 
  [
 
  [
 
  0.0, 0.0, 0.0,
 
  0.0, 0.0, 0.0,
Line 35: Line 38:
 
  ] @=> float times[] => ampEnv.coefs;
 
  ] @=> float times[] => ampEnv.coefs;
 
  1.0/9.0 => ampPhasor.freq;
 
  1.0/9.0 => ampPhasor.freq;
Std.rand2f(20.0,2000.0) => float cf => c.freq;
 
Std.rand2f(0.0,800.0) => float mf => m.freq;
 
 
 
 
 
 
  Phasor indexPhasor => CurveTable indexEnv => blackhole;
 
  Phasor indexPhasor => CurveTable indexEnv => blackhole;
Line 45: Line 46:
 
  ] @=> float indexTimes[] => indexEnv.coefs;
 
  ] @=> float indexTimes[] => indexEnv.coefs;
 
  1.0/9.0 => indexPhasor.freq;
 
  1.0/9.0 => indexPhasor.freq;
  9 * 44100 => int totaltime;
+
+
  9 * 44100 => int totalsamples;
  for (0 => int i; i < totaltime; i++)
+
  for (0 => int i; i < totalsamples; i++)
 
  {
 
  {
 
  cf + (indexEnv.last() * mf * m.last()) => c.freq;
 
  cf + (indexEnv.last() * mf * m.last()) => c.freq;

Revision as of 14:58, 12 October 2007

This is an example from the PMOsc help page in SC3. The arguments for the PMOsc are: PMOsc.ar(carrier freq, modulation freq, index, modulation phase, multiplier). The ChucK version runs at a significantly higher CPU cost, so you might get jittering if you have too many other things running.

the SuperCollider code

Routine({
	loop{
		{
			LinPan2.ar(
				EnvGen.ar(Env.linen(2, 5, 2), doneAction: 2)
				*
				PMOsc.ar(2000.rand, 800.rand, Line.kr(0.0, 12.0.rand, 9), 0, 0.1), 
				1.0.rand2
			)
		}.play;
		2.wait;
	};         
}).play;

the ChucK version

function void makeFMsound(float maxAmp)
{
	SinOsc c => Gain amp => Pan2 panner; // carrier
	panner.left => dac.left;
	panner.right => dac.right;
	SinOsc m => blackhole; // modulator
	Std.rand2f(-1.0,1.0) => panner.pan;
 	Std.rand2f(20.0,2000.0) => float cf => c.freq;
	Std.rand2f(0.0,800.0) => float mf => m.freq;

 	Phasor ampPhasor => CurveTable ampEnv => blackhole;
	[
	0.0, 0.0, 0.0,
	2.0, maxAmp, 0.0,
	7.0, maxAmp, 0.0,
	9.0, 0.0
	] @=> float times[] => ampEnv.coefs;
	1.0/9.0 => ampPhasor.freq;
	
	Phasor indexPhasor => CurveTable indexEnv => blackhole;
	Std.rand2f(0.0,12.0) => float maxIndex;	
	[
	0.0, 0.0, 0.0,
	9.0, maxIndex
	] @=> float indexTimes[] => indexEnv.coefs;
	1.0/9.0 => indexPhasor.freq;

	9 * 44100 => int totalsamples;
	for (0 => int i; i < totalsamples; i++)
	{
		cf + (indexEnv.last() * mf * m.last()) => c.freq;
		ampEnv.last() => amp.gain;
		1::samp => now;
	};
};

while (true)
{
	spork ~ makeFMsound(0.1);
	2::second => now;
};