Hid hi; HidMsg msg; 0 => int device; if( me.args() ) me.arg(0) => Std.atoi => device; if( !hi.openKeyboard( device ) ) me.exit(); /*CLASS*/ class BPM{ static dur N2, N4, N8, N16, N32; float bpm; public void setTempo(float tempo){ (60.0/tempo)::second => N4; tempo => bpm; 2*N4 => N2; 0.5*N4 => N8; 0.5*N8 => N16; 0.5*N16 => N32; } } BPM bpm; bpm.setTempo(75.0); class LoadSound{ SndBuf s => Gain g; 0::ms => dur len; 0 => float gain; setGain(gain); fun void setSound(string file){ me.dir() + file => s.read; s.length() => len; } fun void setVolume(float volume){ volume => gain; } fun void setGain(float volume){ volume => g.gain; } fun void setDur(int data){ if(data == 2){ bpm.N2 => len; }else if(data == 4){ bpm.N4 => len; }else if(data == 8){ bpm.N8 => len; }else if(data == 16){ bpm.N16 => len; }else if(data == 32){ bpm.N32 => len; }else{ s.length() => len; } } } fun void changeVolume(Gain g, float volume, int on){ 0.05::second => dur timer; 0.02 => float interval; if(on == 0){ <<< "offClip" >>>; volume => float foo; while(foo >= interval){ timer => now; foo-interval => foo; g.gain(foo); } g.gain(0); }else{ <<< "onClip" >>>; 0 => float foo; while(foo < volume){ timer => now; foo+interval => foo; g.gain(foo); } } } class LoopSound extends LoadSound{ g => dac; 0 => int on; public void setOnOff(){ if(on){ 0 => on; changeVolume(g, gain, 0); }else{ 1 => on; changeVolume(g, gain, 1); } } public void playLoop(){ while(true){ 0 => s.pos; len => now; } } } // custom Event class MachEvent extends Event { // measure counter 0 => int measure; // where we are in the cycle 0 => int n; // max (free free to change this up, but make sure to update code that assume 16) 16 => int max; // this can be set to tell shreds to stop 1 => int keepGoing; // advance fun void next() { n++; if( n >= max ) { 0 => n; measure++; } } } // the event MachEvent boom; class ChangerSound extends LoadSound { g => dac; 0 => int n; 16 => int max; int pitch[16]; float probs[16]; fun void next() { n++; if( n >= max ) { 0 => n; } } fun void run(){ setGain(probs[n]); s.rate(pitch[n]); <<< len >>>; 0 => s.pos; len => now; } fun void playPattern(){ while( true ) { run(); next(); len => now; } } } class turnSound extends LoadSound { g => ResonZ r => dac; r.freq(440); 0 => float myFreq; 0 => int n; 16 => int max; int steps[0]; int freqsStart[0]; int freqsEnd[0]; fun void next() { n++; if( n >= max ) { 0 => n; } } fun void run(){ setSound(steps[n]+".wav"); setGain(1); freqsStart.size()-1 => int freqSize; now + len => time soundEnd; len/50::ms => float step; while(now < soundEnd){ if(n < freqSize){ myFreq + ((freqsStart[n+1] - freqsStart[n]) / step) => myFreq; r.freq(myFreq); } 50::ms => now; } } fun void playPattern(){ while( true ) { run(); next(); } } } /*SPORKS*/ turnSound turn; 29 => turn.max; [ 1, 2, 3, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 5, 6, 7, 5, 6, 7, 8, 1, 2, 3, 1, 2, 3, 1, 2, 3] @=> turn.steps; 55 => turn.myFreq; [55, 110, 220, 330, 440] @=> turn.freqsStart; spork ~ turn.playPattern(); LoopSound bit; bit.setSound("bar.wav"); bit.setVolume(0.5); bit.setDur(8); spork ~ bit.playLoop(); LoopSound cap; cap.setSound("cap.wav"); cap.setVolume(0.1); cap.setDur(8); spork ~ cap.playLoop(); LoopSound cap2; cap2.setSound("cap2.wav"); cap2.setVolume(0.2); cap2.setDur(2); spork ~ cap2.playLoop(); LoopSound clap; clap.setSound("clap.wav"); clap.setVolume(0.2); clap.setDur(16); spork ~ clap.playLoop(); //Event eBit; 0 => int btnX; spork ~ playPatternBit(); Event btnC; spork ~ kbd(); /*START*/ while(true){ 9.30::second => now; spork ~ bit.setOnOff(); 9.30::second => now; spork ~ bit.setOnOff(); spork ~ cap.setOnOff(); 9.30::second => now; spork ~ cap.setOnOff(); spork ~ clap.setOnOff(); 9.30::second => now; spork ~ clap.setOnOff(); spork ~ bit.setOnOff(); 9.30::second => now; spork ~ bit.setOnOff(); spork ~ cap.setOnOff(); 9.30::second => now; spork ~ cap.setOnOff(); spork ~ clap.setOnOff(); 9.30::second => now; spork ~ clap.setOnOff(); } /*FUNCTIONS*/ fun void kbd() { while (true) { hi => now; while( hi.recv( msg ) ) // get one or more messages { if( msg.isButtonDown() )// check for action type { <<< msg.key >>>; if (msg.key == 29){ //z spork ~ playPatternShake( btnC ); 0.01::second => now; btnC.signal(); } if (msg.key == 27){ //x spork ~ bit.setOnOff(); } if (msg.key == 6){ //c spork ~ cap.setOnOff(); } if (msg.key == 25){ //v spork ~ cap2.setOnOff(); } if (msg.key == 5){ //b spork ~ clap.setOnOff(); } /* if (msg.key == 17){ //n } if (msg.key == 16){ //m }*/ if(msg.key == 20){ //q bpm.setTempo(120.0); } } } } } fun void playPatternBit( /*Event e*/ ){ while( true ) { // wait on event //e => now; playPattern("bar.wav"); } } fun void playPattern(string file ){ SndBuf s => Gain g =>dac; me.dir() + file => s.read; while(true){ if(btnX){ 1 => g.gain; }else{ 0 => g.gain; } 0 => s.pos; bpm.N4 => now; } } fun void playPatternShake( Event e ){ while( true ) { // wait on event e => now; patternShake(); } } fun void patternShake(){ Shakers shake => dac; while( true ) { 11 => shake.which; 1 => shake.noteOn; bpm.N4 => now; } }