Author |
Message |
sir honey
Joined: Aug 04, 2006 Posts: 36 Location: NY
|
Posted: Sat Mar 10, 2007 10:13 pm Post subject:
Trouble Breaking Out of a Loop Using MIDI events. |
 |
|
Hi,
I'd like to trigger record my samples with MIDI events, and make use of variable length sampling. So I have this framework (not fully functional yet)
Code: |
// yada yada yada.
MidiIn min;
MidiMsg msg;
min.open(2);
fun void sampler(int y)
{
0 => int seconds;
while(y == 1)
{
<<<seconds>>>;
1::second => now;
seconds++;
}
}
spork ~ sampler(0);
while(true)
{
min => now;
while(min.recv(msg))
{
if(msg.data1 == 176 && msg.data2 == 64 && msg.data3 == 127)
{
<<<"sampling">>>;
sampler(1);
}
if(msg.data1 == 176 && msg.data2 == 64 && msg.data3 == 0)
{
<<<"done sampling">>>;
sampler(0);
}
}
}
|
The only real problem I'm having is that once I trigger the sample loop, I can't break back out of it. I'm not sure how to get sampler() to listen to the 0 command I'm sending via my footswitch to stop recording. Any suggestions?
Thanks!
jack  |
|
Back to top
|
|
 |
Kassen
Janitor


Joined: Jul 06, 2004 Posts: 7678 Location: The Hague, NL
G2 patch files: 3
|
Posted: Sun Mar 11, 2007 12:35 pm Post subject:
Re: Trouble Breaking Out of a Loop Using MIDI events. |
 |
|
sir honey wrote: |
fun void sampler(int y)
{
0 => int seconds;
while(y == 1)
{
<<<seconds>>>;
1::second => now;
seconds++;
}
}
spork ~ sampler(0);
|
I don't think this will work. Let's trace what will happen.
As soon as you spork this function and it has initated it's "Seconds" variable it will see wether Y equals 1. Well; it doesn't since you just defined it as being 0 so that loop isn't going to run. Execution will continue below that loop. Whoops! Below that loop there is no more code so that shred will asume it's done and drift up to shred-heaven.
From there on it will be called again in your main loop, this time as a normal inline function. However; at that point Y will be 1 so that function won't ever return execution to the main loop so the main loop will be stuck while your function keeps endlessly looping.
I'd move "y" out of that function to the main shred. You could then spork the function where you now call it for the first time in your main loop. Where you call it for the second time you could move "Y" back to 0 which should make the sporked shred stop execting, hopefully having completed it's task by that time.
Note that function can be sporked to seperate shreds but can also be used to repalce a block of code you would like to use in-line several times with just a call for compactness-sake. Those uses are quite different; one is in paralel, the other serial. Int his case you accidentally used them in series where one of the elements in the series won't ever end. You can verify this by using ChucK --status froma second window once it's gotten stuck; you should only see one shred.
Good question; managing concurency is at the core of ChucKing so I hope this helped. _________________ Kassen |
|
Back to top
|
|
 |
|