I&#39;m not sure about this, but I have a feeling that s1, s2, e1 and e2 aren&#39;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.<br>
<br>Comparing with Java&#39;s garbage collection, as long as an object is used somewhere (e.g. chucked to a dac), it won&#39;t be destroyed and garbage collected.<br><br>Please correct me if I&#39;ve got this wrong.<br><br>
/Stefan<br><br><div class="gmail_quote">On Thu, Oct 8, 2009 at 8:58 AM, Atte Andre Jensen <span dir="ltr">&lt;<a href="mailto:atte.jensen@gmail.com">atte.jensen@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi<br>
<br>
I know I&#39;m a bit slow, but I just found that it&#39;s quite simple to totally encapsulate an instrument in a function, ugens and all, as the example below shows. Now I&#39;m wondering how much overhead this yields compared to keeping the ugens, ADSR.set and stuff like that outside the function?<br>

<br>
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?<br>

<br>
5 =&gt; int octave;<br>
.1 =&gt; float gain;<br>
[0,2,4,7,9,11,14,16] @=&gt; int notes[];<br>
<br>
while(true){<br>
    notes[Std.rand2(0,notes.cap()-1)] + 12*octave =&gt; int note;<br>
    ms * Std.rand2f(1500,2000) =&gt; dur length;<br>
    spork ~ ep(note, length, gain);<br>
    ms * Std.rand2f(150,1500) =&gt; now;<br>
}<br>
<br>
fun void ep(int note, dur length, float gain){<br>
    2::second =&gt; dur decay;<br>
    10::ms =&gt; dur release;<br>
    SinOsc s1 =&gt; ADSR e1 =&gt; dac;<br>
    SinOsc s2 =&gt; ADSR e2 =&gt; dac;<br>
    gain =&gt; s1.gain;<br>
    gain * .2 =&gt; s2.gain;<br>
    e1.set(1::ms, decay, 0, release);<br>
    e2.set(1::ms, decay * .3, 0, release);<br>
    Std.mtof(note) =&gt; s1.freq;<br>
    s1.freq() * 2=&gt; s2.freq;<br>
    e1.keyOn();<br>
    e2.keyOn();<br>
    if(length &lt; decay){<br>
        length =&gt; now;<br>
        e1.keyOff();<br>
        e2.keyOff();<br>
        release =&gt; now;<br>
    } else {<br>
        decay =&gt; now;<br>
    }<br>
}<br><font color="#888888">
<br>
-- <br>
Atte<br>
<br>
<a href="http://atte.dk" target="_blank">http://atte.dk</a>   <a href="http://modlys.dk" target="_blank">http://modlys.dk</a>   <a href="http://virb.com/atte" target="_blank">http://virb.com/atte</a><br>
_______________________________________________<br>
chuck-users mailing list<br>
<a href="mailto:chuck-users@lists.cs.princeton.edu" target="_blank">chuck-users@lists.cs.princeton.edu</a><br>
<a href="https://lists.cs.princeton.edu/mailman/listinfo/chuck-users" target="_blank">https://lists.cs.princeton.edu/mailman/listinfo/chuck-users</a><br>
</font></blockquote></div><br><br clear="all"><br>-- <br>Release me, insect, or I will destroy the Cosmos!<br>