<br><br><div class="gmail_quote">2008/5/18 Atte André Jensen &lt;<a href="mailto:atte.jensen@gmail.com">atte.jensen@gmail.com</a>&gt;:<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>
</blockquote><div><br>Hey, Atte!<br>&nbsp;<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><br>
I&#39;d like to resample a 44100 signal to something like 10K *and* hear the alias noise. Also I&#39;d like to resample to lower bitdepth, something like 8 or 6 bit.<br>
</blockquote><div><br>Cool, me too. Morning coffee is a bit late today but it&#39;s a Sunday and there&#39;s a nice ChucK puzzle to go with it. Oh, and it&#39;s nice coffee :¬)<br>&nbsp;<br></div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
1) How it that most efficiently done with chuck?<br>
</blockquote><div><br>Well, quantising in time (sample rate) is without any doubt done most easily by writing the .last() of some signal to the .next of a Step at the required period. <br><br>Quantising in amplitude (bit depth) means rounding. I think my proposal below might be quite efficient but I&#39;m not 100% sure there. It does work fine though and Std has several other rounding schemes you may wish to try for CPU performance and/or sound quality.<br>
<br>Here you go; This example should be quite clear and suitable for extending for your own purposes, I think, but if I&#39;m wrong please do shout.<br>=============================<br>//replace with more interesting signal.<br>
SinOsc my_signal =&gt; blackhole;<br><br>Step resampled =&gt; dac;<br><br>resample();<br>//remove shred to quit....<br><br><br>fun void resample()<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; //8 KHz<br>&nbsp;&nbsp;&nbsp; ms / 8 =&gt; dur re_samp_rate;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; //set bits here<br>
&nbsp;&nbsp;&nbsp; 8 =&gt; int bits;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; //calculate the maximum integer value of the signal&#39;s amplitude at this bitdepth<br>&nbsp;&nbsp;&nbsp; Math.pow(2, bits) * .5 =&gt; float scale;<br>&nbsp;&nbsp;&nbsp; //division is expensive and we only need to do it once<br>
&nbsp;&nbsp;&nbsp; 1/scale =&gt; float downscale;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; while(true)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //digital clipping<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (my_signal.last() &gt; 1) 1 =&gt; resampled.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else if (my_signal.last() &lt; -1) -1 =&gt; resampled.next;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //cast to int for cheep&amp;cheerful rounding<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //then multiply back into the range<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //write result to output<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; downscale * ( (my_signal.last() * scale) $ int) =&gt; resampled.next;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //samplerate is implicid in controll-rate<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; re_samp_rate =&gt; now;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; }<br>===================<br><br>&nbsp;</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br>
2) I event thought it might be possible for me to implement this as a real UGen. My plan would be to start from one of the simple UGens (gain), and expand/modify from there, but before even looking into the chuck source, is there any documentation about doing this? Should I expect this to be more effective in implenenting in chuck-code, and if so about how much? Also. supposed I actually succed in making something that works, could it be included in future chuck-releases?<br>

</blockquote><div><br>Me too!<br><br>I&#39;d also like the following to work but it seems like I can extend Step but the result will -for some strange reason- not be a Ugen. I&#39;m not the greatest OOP wizard but I really feel that if I extend&nbsp; Ugen the result should be a Ugen as well. Right now we can extend it but not actually do anything with the result.<br>
<br>========================<br>//example code, doesn&#39;t actually work<br><br>//this is the offending line<br>LowRezNoise n =&gt; dac;<br><br>minute =&gt; now;<br><br><br>class LowRezNoise extends Step<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; ms =&gt; dur rate;<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; spork ~ fakeTick();<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; fun void freq( float input)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; second / input =&gt; rate;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; fun void fakeTick()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Std.rand2f(-1,1) =&gt; this.next;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rate =&gt; now;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; }<br>====================<br></div></div><br>Hope that helps?<br>
<br>Yours,<br>Kas.<br>