I'm not sure about this, but I have a feeling that s1, s2, e1 and e2 aren't destroyed or unchucked when you leave the function. A quick experiement (remove the keyOffs) could tell if this is so. This means that with every call to ep a set of oscillators will be created and never stop, making you run out of resources eventually.

Comparing with Java's garbage collection, as long as an object is used somewhere (e.g. chucked to a dac), it won't be destroyed and garbage collected.

Please correct me if I've got this wrong.

/Stefan

On Thu, Oct 8, 2009 at 8:58 AM, Atte Andre Jensen <atte.jensen@gmail.com> wrote:
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
_______________________________________________
chuck-users mailing list
chuck-users@lists.cs.princeton.edu
https://lists.cs.princeton.edu/mailman/listinfo/chuck-users



--
Release me, insect, or I will destroy the Cosmos!