LeadGuitarHero.ck

From CSWiki
Jump to: navigation, search
class LeadGuitarHero { 
   
    // set the gain
    1.0 => float setGain; 
    fun void gain(float input) { 
        input => setGain; 
    }
    
    
    fun void startPlayingBlues() { 
        Hid hi;
        HidMsg msg;
        // which keyboard
        0 => int device;
        // get from command line
        if( me.args() ) me.arg(0) => Std.atoi => device;
        // open keyboard (get device number from command line)
        if( !hi.openJoystick( device ) ) me.exit();
       <<< "Controller '" + hi.name() + "' ready", "" >>>;
       float note; int string1; int string2; int string3; int string4; int play; int pitch; 
    
    // read in and envelope the sound
    "leadguitar.wav" => string filename; 
    SndBuf buf; 
    filename => buf.read;
    setGain => buf.gain; 
    buf.loop(1); 
    Envelope e; 
    80 => int enveloping;
    e.duration(enveloping::ms);
    buf => e => dac;
    
    // calibrate the blues pitches
    buf.freq() => float blues1; 
    buf.freq() => float store; 
    1.18921*store => float blues2; 
    1.33484*store => float blues3; 
    1.41421*store => float blues4; 
    1.49831*store => float blues5;
    1.7818*store => float blues6; 
    2*store => float blues7; 


    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 == 14 || msg.which == 12) 1 => play; 
                if(msg.which == 9) pitch++; 
                if(msg.which == 8) pitch--; 
                  }     
            else if( msg.isButtonUp() )
            {
                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 == 14 || msg.which == 12) 0 => play; 
                    }    
            if(string1 && !string2 && !string3 && !string4) blues1*Math.pow(2.0, pitch/12.0) => buf.freq => store; 
            if(string1 && string2 && !string3 && !string4) blues2*Math.pow(2.0, pitch/12.0) => buf.freq => store; 
            if(string1 && string2 && string3 && !string4) blues3*Math.pow(2.0, pitch/12.0) => buf.freq => store; 
            if(string1 && string2 && string3 && string4) blues4*Math.pow(2.0, pitch/12.0) => buf.freq => store; 
            if(!string1 && string2 && string3 && string4) blues5*Math.pow(2.0, pitch/12.0) => buf.freq => store; 
            if(!string1 && !string2 && string3 && string4) blues6*Math.pow(2.0, pitch/12.0) => buf.freq => store; 
            if(!string1 && !string2 && !string3 && string4) blues7*Math.pow(2.0, pitch/12.0) => buf.freq => store; 
            
            if(play){ 
                e.keyOn(1);  
                enveloping::ms => now; 
                }
            if(!play) {
                e.keyOff(1);
                enveloping::ms => now; 
                    }
                }
            }
        }
}

LeadGuitarHero adam;
adam.gain(1); 
adam.startPlayingBlues(); 

<\pre>