<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Well, there are a number of ways that this code can be improved.  Here's a way of patching the code to make it work, followed by an explanation:</div><div><div><br></div><div><br></div><div>    fun void cycle()</div><div>    {</div><div>        float targetFreq;</div><div>        dur targetBeat;</div><div>        dur targetDelay;</div><div>        imp => p => dac;</div><div>        while(true)</div><div>        {</div><div>            freq * multiplier => targetFreq;</div><div>            1.0::second / targetFreq => targetBeat;</div><div>            targetBeat - (now % targetBeat) => targetDelay;</div><div>            if(targetDelay < 1::samp)</div><div>                1::samp => targetDelay;</div><div>            targetDelay => now;</div><div>            pan * spread => p.pan;</div><div>            1.0 => imp.next;</div><div>        }</div><div>    }</div></div><div><br></div><div><br></div><div><br></div><div>The problem is that, with certain numbers, the cycle can occur on the exact sample that the click is meant to happen.  The following line is the culprit:</div><div><br></div><div><blockquote type="cite">            targetBeat - (now % targetBeat) => now;</blockquote><br></div><div>What this does is it looks at a duration called targetBeat and, if we're not on the beat, waits until we are.  Time in ChucK is calculated from when the VM was started.  So if the targetBeat is 10ms, and the VM has been running for 234 ms, we will wait for 6 ms, which would put us <i>on </i>the beat at 240ms.  The arithmetic of time isn't exactly intuitive the first time you encounter it, so let's break that down a little further to see how the math works out and gets us what we're looking for.  Let's take the previous example and say we have a target beat length of 10ms.  Substituting 10ms for targetBeat it would look like this:</div><div><br></div><div>10::ms - (now % 10::ms) => now;</div><div><br></div><div>The expression "now % 10::ms", or now % any duration for that matter, says "take the current time (now), divide by the following duration, and return the remainder".  This is commonly called the modulo operator.  For example, 6 % 3 would yield 0, because 6 divides by 3 evenly, but 7 % 3 would yield 1, since in integer mathematics divided by 3 would yield 2 with a remainder of 1.  So in this context, the keyword now simply indicates the current time.  I personally always think of it as "an integer representing the number of samples that have passed since I started ChucK".  Let's pretend that we're currently at a time 234::ms <i>after </i>the VM was launched:</div><div><br></div><div>10::ms - (234::ms % 10::ms) => now;</div><div><br></div><div>Let's simplify the left side.  First we'll reduce the modulo operation in parenthesis.  Since 234 % 10 = 4, we have:</div><div><br></div><div>10:: ms - 4::ms => now;</div><div><br></div><div>which becomes</div><div><br></div><div>6::ms => now;</div><div><br></div><div>Chucking any duration to the keyword "now" will cause the shred to wait for that amount of time.  <i>However</i>, there is no protection against chucking <i>zero time </i>to now; which would say "delay by nothing at all".  Effectively when you write 0::ms => now, nothing happens.  The cycle() function that contains this bit of code has it in a logically infinite loop, since it's contained in a while(true) block.  So when this duration is 0, we're looping infinitely without advancing time; we've stopped time.</div><div><br></div><div>To prevent this from happening store the amount of time you are to delay in a variable (called targetDelay in my example).  Then, check to make sure that variable represents a duration longer than 1 sample (i.e. a positive amount of time, since a sample is the lower limit on time resolution).  If you try to send a negative number to now you get a rather amusing error indicating that you've attempted to make the VM travel backwards in time.  If your duration is either zero or negative, set it to a minimum of 1 sample.  </div><div><br></div><div>There's a more mathematical way of determining which values would be "good" or "bad", but I can't honestly figure out why this only happens at certain values.  I verified that 1.5 does indeed break the original code for me, as well, but with the update it works fine.</div><div><br></div><div><br></div><div>-Jordan</div><div><br></div><div><br></div><div><div><br></div></div><div><br></div><div><br></div><div><br></div><div>If we're in this body of code while we are <i>already </i>on the beat.</div><br><div><div>On Jul 21, 2011, at 3:54 PM, Rustom Mody wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><div class="gmail_quote">On Thu, Jul 21, 2011 at 12:36 PM,  <span dir="ltr"><<a href="mailto:tempjayren@gmail.com" target="_blank">tempjayren@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

-----BEGIN PGP SIGNED MESSAGE-----<br>
Hash: SHA1<br>
<br>
what did i break now?<br>
i snipped the code from the message, though when i ran it it said<br>
something about an undefined variable.<br>
i wanted to see what was going on.<br>
<div><div></div><br></div></blockquote></div><br>Thanks for looking. The full code is below.<br>It works as given ie it starts as a 2 vs 3 polyrhythm.<br>You can speed it up by pressing D until the polyrhythm becomes 2 notes<br>

However if I want to speed up by less than factor of 2 then it breaks.<br><br>About 11 lines from the end: the line<br> 2 *=> Voice.multiplier;<br>if changed to <br>1.5 *=> Voice.multiplier;<br><br>Then it dies after a D and cpu shows 100%<br>

This is debian linux.<br><br>There are one or two other enhancements also I would like<br>Can I make the clicks sound a little different from each other?<br><br>Note the full context is on the other thread: Subject: From 4Hz to 400 Hz<br>
---------------------------<br>
class Voice<br>{<br>    1.0 => static float multiplier;<br>    1.0 => static float spread;<br>    float freq;<br>    float pan;<br>    Impulse imp;<br>    Pan2 p;<br><br>    fun static Voice Voice(float freq, float pan)<br>

    {<br>        Voice v;<br>        freq => v.freq;<br>        pan => v.pan;<br>        spork ~ v.cycle();<br>        return v;<br>    }<br><br>    fun void cycle()<br>    {<br>        float targetFreq;<br>        dur targetBeat;<br>

        imp => p => dac;<br>        while(true)<br>        {<br>            freq * multiplier => targetFreq;<br>            1::second / targetFreq => targetBeat;<br>            targetBeat - (now % targetBeat) => now;<br>

            pan * spread => p.pan;<br>            1.0 => imp.next;<br>        }<br>    }<br>}<br><br>fun void keyboardListener()<br>{<br>    KBHit hi;<br>    int c;<br>    int tt;<br>    while(true)<br>    {<br>        hi => now;<br>

        while(hi.more())<br>        {<br>        hi.getchar() => c;<br>            {<br>                if(c == 68) // D<br>                    2 *=> Voice.multiplier;<br>                else if(c == 72) // H<br>                    2 /=> Voice.multiplier;<br>

            }<br>        }<br>    }<br>}<br><br>Voice.Voice(3.0, -1);<br>Voice.Voice(2.0, 1);<br>spork ~ keyboardListener();<br>while(true) { 100::ms => now; }<br><br>
_______________________________________________<br>chuck-users mailing list<br><a href="mailto:chuck-users@lists.cs.princeton.edu">chuck-users@lists.cs.princeton.edu</a><br>https://lists.cs.princeton.edu/mailman/listinfo/chuck-users<br></blockquote></div><br></body></html>