Poly demo.ck

From CSWiki
Jump to: navigation, search
MidiIn min;
MidiMsg msg;

Event noteoffs[127];

// open midi receiver, exit on fail
// need to set this argument depending on your MIDI config
// try "chuck --probe" in the Terminal to see what MIDI devices are present
if ( !min.open(1) ) me.exit(); 

while( true )
{
    // wait on midi event
    min => now;
    
    // receive midimsg(s)
    while( min.recv( msg ) )
    {
        // print content
        <<< msg.data1, msg.data2, msg.data3 >>>;
        //message type + channel number, noteNumber (if note message), noteVelocity (if note message)
        
        //this will work if your MIDI device sends noteOff messages as noteOn messages with velocity (msg.data3) = 0.
        if(msg.data3 > 0) spork ~ mand_note(Std.mtof(msg.data2), msg.data3/127., noteoffs[msg.data2]);
        else noteoffs[msg.data2].signal();

        //however, many MIDI devices use proper noteOff messages (msg.data1 = 128 + channel number) as well as
        //noteOn messages (msg.data1 = 144 + channel number), so the if/else structure above would have to be
        //modified accordingly; an exercise left for the ChucKer.
    }
}

fun void mand_note(float freq, float amplitude, Event mynoteoff) {
    
    Mandolin mand => dac;
    
    mand.freq(freq);
    mand.pluck(amplitude);
    
    mynoteoff => now;
    
    mand =< dac;

    //think about how to make this better with an envelope or ADSR
    
}