[chuck-users] stack class
Atte André Jensen
atte.jensen at gmail.com
Wed Nov 23 18:04:45 EST 2005
Hi
I took Grahams advise and started coding some of what I'll be needing
and came up with the following way to do legato the IMO prober way;
latest note on is sounding. I figured I needed a stack, so I made one.
Three questions arise:
1) Did I reinvent a wheel? Is there alreasy a stack implemented in chuck
(I looked in the docs but didn't find one)?
2) Is there a smarter/smaller/more elegant way of implementing a legato
instrument the way I want; latest note on is sounding?
3) Most importently (for my learning chuck): Is there a way of getting
rid of the static stack size (int stack_size)? In other words; can I
have a dynamically growing array in chuck?
Thanks in advance for any reply...
--
peace, love & harmony
Atte
http://www.atte.dk
-------------- next part --------------
1 => int device;
MidiIn midi_in;
MidiMsg msg;
if(!midi_in.open(device)) me.exit();
<<< "MIDI device:", midi_in.num(), " -> ", midi_in.name() >>>;
sinosc s => ADSR env => gain g => dac;
.01 => env.attackTime;
.1 => env.releaseTime;
.35 => g.gain;
1 => env.keyOff;
stack held_keys;
while(true)
{
// wait on the event 'midi_in'
midi_in => now;
// get the message(s)
while(midi_in.recv(msg))
{
// print out midi message
<<< msg.data1, msg.data2, msg.data3 >>>;
if(msg.data3 != 0)
{
held_keys.push(msg.data2);
}
else
{
held_keys.remove_by_value(msg.data2);
}
if(held_keys.count() > 0)
{
std.mtof(held_keys.pop()) => s.freq;
held_keys.unpop();
if(msg.data3 != 0){
1 => env.keyOn;
}
}
else
{
1=> env.keyOff;
}
}
}
public class stack
{
200 => int stack_size;
int latest_popped;
int the_stack[stack_size];
0 => int nb_elements;
fun void push(int element)
{
if(nb_elements < stack_size)
{
element => the_stack[nb_elements];
nb_elements++;
}
}
fun int pop()
{
if(nb_elements > 0)
{
the_stack[nb_elements--] => latest_popped;
return latest_popped;
}
else
{
return 0;
}
}
fun void unpop()
{
this.push(latest_popped);
}
fun void remove_by_value(int value_to_be_removed)
{
for(0 => int i; i <nb_elements; i++){
if(the_stack[i] == value_to_be_removed)
{
this.remove_by_index(i);
i--;
}
}
}
fun void remove_by_index(int index_to_be_removed)
{
if(index_to_be_removed >= 0 && index_to_be_removed < nb_elements)
{
for(index_to_be_removed => int i; i <nb_elements; i++){
the_stack[i + 1] => the_stack[i];
}
nb_elements--;
}
}
fun int count()
{
return nb_elements;
}
}
More information about the chuck-users
mailing list