[chuck-users] instrument in function - good idea, or?

Atte Andre Jensen atte.jensen at gmail.com
Thu Oct 8 02:58:16 EDT 2009


Hi

I know I'm a bit slow, but I just found that it's quite simple to 
totally encapsulate an instrument in a function, ugens and all, as the 
example below shows. Now I'm wondering how much overhead this yields 
compared to keeping the ugens, ADSR.set and stuff like that outside the 
function?

I guess part of the answer is it depends on what one needs to do. Do you 
need to play the instrument monophonically, with a small, fixed number 
of voices or ad-hoc polyphony. The instrument-in-a-function solution 
elegantly solves the polyphony problem, I can just spork as many voices 
as I need, and they are automatically destroyed when finished. But how 
great a peak in cpu usage does the creation of the ugens generate?

5 => int octave;
.1 => float gain;
[0,2,4,7,9,11,14,16] @=> int notes[];

while(true){
     notes[Std.rand2(0,notes.cap()-1)] + 12*octave => int note;
     ms * Std.rand2f(1500,2000) => dur length;
     spork ~ ep(note, length, gain);
     ms * Std.rand2f(150,1500) => now;
}

fun void ep(int note, dur length, float gain){
     2::second => dur decay;
     10::ms => dur release;
     SinOsc s1 => ADSR e1 => dac;
     SinOsc s2 => ADSR e2 => dac;
     gain => s1.gain;
     gain * .2 => s2.gain;
     e1.set(1::ms, decay, 0, release);
     e2.set(1::ms, decay * .3, 0, release);
     Std.mtof(note) => s1.freq;
     s1.freq() * 2=> s2.freq;
     e1.keyOn();
     e2.keyOn();
     if(length < decay){
         length => now;
         e1.keyOff();
         e2.keyOff();
         release => now;
     } else {
         decay => now;
     }
}

-- 
Atte

http://atte.dk   http://modlys.dk   http://virb.com/atte


More information about the chuck-users mailing list