Chord-o-matic.ck

From CSWiki
Jump to: navigation, search
JCRev master => dac;

125.0 => float bpm;

60::second / bpm => dur t4;
t4 / 2.0 => dur t8;
t4 / 4.0 => dur t16;
t4 / 8.0 => dur t32;
t4 / 16.0 => dur t64;
t4 * 2.0 => dur t2;
t4 * 4.0 => dur t1;


fun int getmiditype(int i)
{
    return (i>>16)&0xf0;
}

fun int getdata1(int i)
{
    return (i>>8)&0xff;
}

fun int getdata2(int i)
{
    return (i&0xff);
}

fun void playnote(int data1, int data2, dur length)
{
    if (data2 == 0)
        return;
    if (data1 < 0)
        return;
    if (data1 > 127)
        return;
    // patch
    Mandolin mand => master;
    0.0 => mand.pluck;
    0.7 => mand.pluckPos;
    0.3 => mand.stringDamping;
    // freq
    11.25 * math.pow( 2.0, (float)(data1+3) / 12.0 ) => mand.freq;
    // pluck!
    (float)data2 / 127.0 => mand.pluck;	

    length => now;
}

fun void playchord(int base, int major, int volume, dur length, int mode)
{
    if (mode == 0)
    {
        spork ~playnote(base, volume, length);
        spork ~playnote(base + 3 + major, volume, length);
        spork ~playnote(base + 7, volume, length);
    }
    else if (mode == 1)
    {
        spork ~playnote(base, volume, length);
        spork ~playnote(base + 3 + major, volume, length);
        spork ~playnote(base - 5, volume, length);
    }
    else if (mode == 2)
    {
        spork ~playnote(base, volume, length);
        spork ~playnote(base - 9 + major, volume, length);
        spork ~playnote(base - 5, volume, length);
    }
    
    length => now;
}
 
// time loop
while( true )
{
    while ( midiin => int i )
    {
        (i>>16)&0xf0 => int miditype;
        (i>>8)&0xff => int data1;
        i&0xff => int data2;
        chout => "type: " => miditype
              => " data1: " => data1
              => " data2: " => data2
              => endl;		// pos
        if ( miditype == 144)
        {		
            spork ~playchord(data1, maybe, data2, t4, std.rand2(0,2));
        }
    }

     
    std.rand2(2, 50)::samp => now;	
}