From CSWiki
//Polyphony the easy way.... the example program seemed to be high in resource usage and would glitch and cut out after playing for just a bit...
//so I made this.
//You can increase the possible number of tones by changing the values in the arrays and one of the if statements...
//I figured 10 might be a good number to start with though.
//
//
//by drool
//
//
//Do what you will with it.
//this is the device number for your midi controller
0 => int device;
// a MidiIn event!
MidiIn min;
// the message for retrieving data
MidiMsg msg;
Rhodey synth[10]; //an array of synths for up to 10 tones...
int id[10]; //an array to hold the note numbers so that we can match them up with the off signal
int counter; //this is to cycle through the arrays looking for a position where a key isn't pressed
min.open( device ); // open your midi keyboard...
while( true ){
// wait on your midi event
min => now;
//this processes the keypresses
while( min.recv( msg ) ){
if( msg.data1 == 144){ //note on?
0=> counter; //initialize the counter
while(id[counter]!=0) //this looks for an empty position in the array
counter++;
msg.data2=>id[counter]; //puts the midi key number into the id array
ON(id[counter],
msg.data3,counter); }// ON!
if( msg.data1 == 128){//note off?
0=>counter;
while(id[counter]!=msg.data2)//looks for the counter position of the note that turned off
counter++;
OFF(counter); //OFF!!!!!
0=>id[counter]; } //changes the id of the note just turned off to zero for re-use
}
}
public void ON(int note, int velocity, int ham){
synth[ham]=>dac; //connects the 10 synths to the dac as needed... they are never disconected...
Std.mtof( note ) => synth[ham].freq; //changes the midi note to a frequency
velocity / 128.0 => synth[ham].noteOn; //put some fancy velocity algorithms here
}
public void OFF( int cheese){
0=>synth[cheese].noteOn; //noteOff doesn't appear to do anything
//Put some fancy envelopes here
}