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.<br>
<br>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.<br><br>Please correct me if I'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"><<a href="mailto:atte.jensen@gmail.com">atte.jensen@gmail.com</a>></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'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?<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 => int octave;<br>
.1 => float gain;<br>
[0,2,4,7,9,11,14,16] @=> int notes[];<br>
<br>
while(true){<br>
notes[Std.rand2(0,notes.cap()-1)] + 12*octave => int note;<br>
ms * Std.rand2f(1500,2000) => dur length;<br>
spork ~ ep(note, length, gain);<br>
ms * Std.rand2f(150,1500) => now;<br>
}<br>
<br>
fun void ep(int note, dur length, float gain){<br>
2::second => dur decay;<br>
10::ms => dur release;<br>
SinOsc s1 => ADSR e1 => dac;<br>
SinOsc s2 => ADSR e2 => dac;<br>
gain => s1.gain;<br>
gain * .2 => s2.gain;<br>
e1.set(1::ms, decay, 0, release);<br>
e2.set(1::ms, decay * .3, 0, release);<br>
Std.mtof(note) => s1.freq;<br>
s1.freq() * 2=> s2.freq;<br>
e1.keyOn();<br>
e2.keyOn();<br>
if(length < decay){<br>
length => now;<br>
e1.keyOff();<br>
e2.keyOff();<br>
release => now;<br>
} else {<br>
decay => 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>