| Author |
Message |
yoshima
Joined: Nov 28, 2009 Posts: 15 Location: UK
|
Posted: Mon Apr 26, 2010 3:08 pm Post subject:
Stepping through an array on key press Subject description: How do I make it loop? |
 |
|
Hello,
I'm trying to get ChucK to step through an array each time a key is pressed on the keyboard. It works but it only goes through the array once....
How can I get it to keep looping through?
Thank for any help....
Heres my code so far.....
| Code: | // HID
Hid hi;
HidMsg msg;
// which keyboard
0 => int device;
// get from command line
if( me.args() ) me.arg(0) => Std.atoi => device;
// open keyboard (get device number from command line)
if( !hi.openKeyboard( device ) ) me.exit();
<<< "keyboard '" + hi.name() + "' ready", "" >>>;
// Patch
FMVoices v => JCRev r => dac;
0 => v.gain;
[ 0, 0, 3, 5, 5, 6, 9, 3, 4, 2, 1, 5, 7 ] @=> int song [];
int i;
// infinite event loop
while( true )
{
// wait for event
hi => now;
// get message
while( hi.recv( msg ) )
{
{
if (msg.isButtonDown())
{
Std.mtof(60+song[i++]) => float freq;
freq => v.freq;
<<<song>>>;
1 => v.noteOn;
0.5 => v.gain;
0.2::second => now;
}
else
{
0 => v.noteOff;
}
}
}
} |
|
|
|
Back to top
|
|
 |
vrachnasormora

Joined: Mar 22, 2007 Posts: 42 Location: Preveza,Greece
|
Posted: Mon Apr 26, 2010 3:32 pm Post subject:
|
 |
|
Hi, try changing this line:
| Code: | | Std.mtof(60+song[i++]) => float freq; |
to:
| Code: | | Std.mtof(60+song[++i%song.size()]) => float freq; |
|
|
|
Back to top
|
|
 |
yoshima
Joined: Nov 28, 2009 Posts: 15 Location: UK
|
Posted: Mon Apr 26, 2010 3:49 pm Post subject:
|
 |
|
It works!! Thanks for the help...
But I have to remove the line:
What code do I need for it to print the number in the array?
Thanks, |
|
|
Back to top
|
|
 |
vrachnasormora

Joined: Mar 22, 2007 Posts: 42 Location: Preveza,Greece
|
Posted: Mon Apr 26, 2010 4:16 pm Post subject:
|
 |
|
You could again use:
| Code: | | <<< song[i%song.size()] >>>; |
but because the modulo operation is quite expensive CPU-wise to re-express as you have already compute it, I would suggest:
| Code: | if (msg.isButtonDown())
{
Std.mtof(60+song[i]) => float freq;
freq => v.freq;
<<<song[i]>>>;
++i%song.size() => i;
1 => v.noteOn;
0.5 => v.gain;
0.2::second => now;
} |
plus now you don't lose the first element of the array at the start of the program.Bye |
|
|
Back to top
|
|
 |
yoshima
Joined: Nov 28, 2009 Posts: 15 Location: UK
|
Posted: Mon Apr 26, 2010 4:33 pm Post subject:
|
 |
|
wow... thanks...
you have been really helpful...
cheers...  |
|
|
Back to top
|
|
 |
|