RhythmGuitarHero.ck

From CSWiki
Jump to: navigation, search
class RhythmGuitarHero { 
    int setCapo; 
    fun void capo(int input) { 
        input => setCapo; 
    }
    
    // Set the sound, with the Karplus-Strong algorithm
    fun void playString(int string, int fretPosition) {
        // patch and feedback
        Noise imp => OneZero lowpass => dac;
        lowpass => Delay delay => lowpass;
        
        // Calibrate the strings, (E, A, D, G, B, E)
        float stringNum; 
        if(string <= 3) { 
            5.0*string => stringNum;
            }
        if(string == 4) { 
            string + 15.0 => stringNum; 
        }
        if(string == 5) { 
            string + 19.0 => stringNum; 
        }
        if(fretPosition == -1) return; 
        
        // our radius (for decay length)
        .99994 => float R;
        // our delay order, calibrated to the right pitch
        500*Math.pow(2, (3.0 - fretPosition - setCapo - stringNum)/12) => float L;
        // set delay
        L::samp => delay.delay;
        // set dissipation factor
        Math.pow( R, L ) => delay.gain;
        // place zero
        -1 => lowpass.zero;
        // fire excitation
        1 => imp.gain;
        // for one delay round trip
        L::samp => now;
        // cease fire
        0 => imp.gain;
        // advance time
        (Math.log(.0001) / Math.log(R))::samp => now;
    }
    
    0.03 => float strumSpeed;   //default strum speed
    fun void setStrumSpeed(float input) { 
        input*.2 => strumSpeed;
    }
    
    
   fun void strumDown(int frets[]) {
        for (0 => int i; i < frets.cap(); i++) {  
            spork ~ playString(i, frets[i]); 
            strumSpeed::second => now; 
        }
        3*strumSpeed::second => now; 
    }
    fun void strumUp(int frets[]) { 
        for (frets.cap() - 1 => int i; i >= 0; i--) {  
            spork ~ playString(i, frets[i]); 
            strumSpeed::second => now; 
        }
        strumSpeed::second => now; 
    }   
    
// define billions of chords.  and arrays of chords.  this is madness.
    int a[6]; int b[6]; int c[6]; int d[6]; int e[6]; int f[6]; 
    int g[6]; int aflat[6]; int bflat[6]; int dflat[6]; 
    int eflat[6]; int gflat[6]; 

    int a7[6]; int b7[6]; int c7[6]; int d7[6]; int e7[6]; int f7[6]; 
    int g7[6]; int aflat7[6]; int bflat7[6]; int dflat7[6]; 
    int eflat7[6]; int gflat7[6]; 

    int dminor[6]; int eminor[6]; int aminor[6]; int gminor[6]; 
    int cminor[6]; int fminor[6]; int bminor[6]; int aflatminor[6]; 
    int bflatminor[6]; int dflatminor[6]; int eflatminor[6]; int gflatminor[6]; 

    int openstrings[6]; 
    int chord[6]; 

    int oneChord[6][12]; int twoChord[6][12]; int threeChord[6][12];
    int fourChord[6][12]; int fiveChord[6][12]; int sixChord[6][12]; 
    int sevenChord[6][12]; int oneAlteredChord[6][12]; int twoAlteredChord[6][12]; 
    int threeAlteredChord[6][12]; int fourAlteredChord[6][12]; int fiveAlteredChord[6][12]; 
    int sixAlteredChord[6][12]; 


    [-1, -1, 0, 2, 3, 2] @=> d; 
    [0, 2, 2, 1, 0, 0] @=> e; 
    [-1, 0, 2, 2, 2, 0] @=> a; 
    [3, 2, 0, 0, 0, 3] @=> g; 
    [-1, 3, 2, 0, 1, 0] @=> c; 
    [1, 3, 3, 2, 1, 1] @=> f; 
    [-1, 2, 4, 4, 4, 2] @=> b; 
    [4, 6, 6, 5, 4, 4] @=> aflat; 
    [-1, 1, 3, 3, 3, 1] @=> bflat; 
    [-1, 4, 6, 6, 6, 4] @=> dflat;
    [-1, 6, 8, 8, 8, 6] @=> eflat; 
    [2, 4, 4, 3, 2, 2] @=> gflat; 


    [-1, -1, 0, 2, 1, 2] @=> d7; 
    [0, 2, 0, 1, 0, 0] @=> e7; 
    [-1, 0, 2, 0, 2, 0] @=> a7; 
    [3, 2, 0, 0, 0, 1] @=> g7; 
    [-1, 3, 2, 3, 1, 0] @=> c7; 
    [1, 3, 1, 2, 1, 1] @=> f7; 
    [-1, -1, 1, 2, 0, 2] @=> b7; 
    [4, 6, 4, 5, 4, 4] @=> aflat7; 
    [-1, 1, 3, 1, 3, 1] @=> bflat7; 
    [-1, 4, 6, 4, 6, 4] @=> dflat7;
    [-1, 6, 8, 6, 8, 6] @=> eflat7; 
    [2, 4, 2, 3, 2, 2] @=> gflat7; 

    [-1, -1, 0, 2, 3, 1] @=> dminor; 
    [0, 2, 2, 0, 0, 0] @=> eminor; 
    [-1, 0, 2, 2, 1, 0] @=> aminor; 
    [3, 1, 0, 0, 3, 3] @=> gminor; 
    [-1, 3, 5, 5, 4, 3] @=> cminor; 
    [1, 3, 3, 1, 1, 1] @=> fminor; 
    [-1, 2, 4, 4, 3, 2] @=> bminor;
    [4, 6, 6, 4, 4, 4] @=> aflatminor; 
    [-1, 1, 3, 3, 2, 1] @=> bflatminor; 
    [-1, 4, 6, 6, 5, 4] @=> dflatminor;
    [-1, 6, 8, 8, 7, 6] @=> eflatminor; 
    [2, 4, 4, 2, 2, 2] @=> gflatminor; 
    [0, 0, 0, 0, 0, 0] @=> openstrings;

    openstrings @=> chord;

    [c, dflat, d, eflat, e, f, gflat, g, aflat, a, bflat, b] @=> oneChord; 
    [dminor, eflatminor, eminor, fminor, gflatminor, gminor, aflatminor, aminor, bflatminor, bminor, cminor, dflatminor] @=> twoChord;
    [eminor, fminor, gflatminor, gminor, aflatminor, aminor, bflatminor, bminor, cminor, dflatminor, dminor, eflatminor] @=> threeChord; 
    [f, gflat, g, aflat, a, bflat, b, c, dflat, d, eflat, e] @=> fourChord; 
    [g, aflat, a, bflat, b, c, dflat, d, eflat, e, f, gflat] @=> fiveChord;
    [aminor, bflatminor, bminor, cminor, dflatminor, dminor, eflatminor, eminor, fminor, gflatminor, gminor, aflatminor] @=> sixChord; 

    [c7, dflat7, d7, eflat7, e7, f7, gflat7, g7, aflat7, a7, bflat7, b7] @=> oneAlteredChord; 
    [d, eflat, e, f, gflat, g, aflat, a, bflat, b, c, dflat] @=> twoAlteredChord;
    [e, f, gflat, g, aflat, a, bflat, b, c, dflat, d, eflat] @=> threeAlteredChord; 
    [fminor, gflatminor, gminor, aflatminor, aminor, bflatminor, bminor, cminor, dflatminor, dminor, eflatminor, eminor] @=> fourAlteredChord; 
    [g7, aflat7, a7, bflat7, b7, c7, dflat7, d7, eflat7, e7, f7, gflat7] @=> fiveAlteredChord;
    [a, bflat, b, c, dflat, d, eflat, e, f, gflat, g, aflat] @=> sixAlteredChord; 

    // function that begins the interfacing 
    fun void startPlaying() { 
      
          // make HidIn and HidMsg
        Hid hi;
        HidMsg msg;

        // which joystick
        0 => int device;
        // get from command line
        if( me.args() ) me.arg(0) => Std.atoi => device;

        // open joystick 0, exit on fail
        if( !hi.openJoystick( device ) ) me.exit();

        <<< "Controller '" + hi.name() + "' ready", "" >>>;

          // infinite event loop
        time lastTimePlayed;
        int string1; int string2; int string3; int string4; 
        int commonAlteration; int key;
        while( true )
        {
        // wait on HidIn as event
        hi => now;

        // messages received
        while( hi.recv( msg ) )
        {    
            // controller button down
            if( msg.isButtonDown() )
            {
                if(msg.which == 5) 1 => string1; 
                if(msg.which == 1) 1 => string2; 
                if(msg.which == 0) 1 => string3; 
                if(msg.which == 2) 1 => string4; 
                if(msg.which == 3) 1 => commonAlteration;
                if(msg.which == 9) (1 + key) % 12 => key; 
                if(msg.which == 8) (key - 1 + 12) % 12 => key; 
                
                if(string1 && !string2 && !string3 && !string4 && !commonAlteration) oneChord[key] @=> chord; 
                if(string1 && string2 && !string3 && !string4 && !commonAlteration) twoChord[key] @=> chord; 
                if(string1 && string2 && string3 && !string4 && !commonAlteration) threeChord[key] @=> chord; 
                if(string1 && string2 && string3 && string4 && !commonAlteration) fourChord[key] @=> chord; 
                if(!string1 && string2 && string3 && string4 && !commonAlteration) fiveChord[key] @=> chord; 
                if(!string1 && !string2 && string3 && string4 && !commonAlteration) sixChord[key] @=> chord; 
                
                if(string1 && !string2 && !string3 && !string4 && commonAlteration) oneAlteredChord[key] @=> chord; 
                if(string1 && string2 && !string3 && !string4 && commonAlteration) twoAlteredChord[key] @=> chord; 
                if(string1 && string2 && string3 && !string4 && commonAlteration) threeAlteredChord[key] @=> chord; 
                if(string1 && string2 && string3 && string4 && commonAlteration) fourAlteredChord[key] @=> chord; 
                if(!string1 && string2 && string3 && string4 && commonAlteration) fiveAlteredChord[key] @=> chord; 
                if(!string1 && !string2 && string3 && string4 && commonAlteration) sixAlteredChord[key] @=> chord; 
                
                if(msg.which == 14) { 
                        strumDown(chord);  
                        now => lastTimePlayed;
                    }
                if(msg.which == 12) {
                        strumUp(chord);
                        now => lastTimePlayed;
                    }
            }
            
            // button up
            else if( msg.isButtonUp() )
            {
                <<< "joystick button", msg.which, "up" >>>;
                if(msg.which == 5) 0 => string1; 
                if(msg.which == 1) 0 => string2; 
                if(msg.which == 0) 0 => string3; 
                if(msg.which == 2) 0 => string4; 
                if(msg.which == 3) 0 => commonAlteration; 
                if(!string1 && !string2 && !string3 && !string4) openstrings @=> chord; 
                }
                }
            }
        }
}

// Test Client
RhythmGuitarHero adam;
adam.setStrumSpeed(0.2); 
adam.capo(0); 
adam.startPlaying();