// @title hw2.ck // @desc multi-channel based binaural audio generation // @author HANNAH CHOI (hchoi20@ccrma) /////////////////////////////////////////////////////////////// // set a global value 0 => int LINEAR; 1 => int CONSTANTPOWER; // linear panning, expect pan=[0,1] fun void panLinear( float pan, UGen left, UGen right ){ // clamp to bounds if( pan < 0 ) 0 => pan; else if( pan > 1 ) 1 => pan; // set gains 1-pan => left.gain; pan => right.gain; } // constant power panning, expect pan=[0,1] fun void panPower( float pan, UGen left, UGen right ){ // clamp to bounds if( pan < 0 ) 0 => pan; else if( pan > 1 ) 1 => pan; // set gains Math.cos( pan * pi / 2 ) => left.gain; Math.sin( pan * pi / 2 ) => right.gain; } // pan, which=0:linear|1:constantpower fun void panning( int which, float pan, UGen left, UGen right ){ if( which == LINEAR ) panLinear( pan, left, right); else if( which == CONSTANTPOWER ) panPower( pan, left, right ); else <<< "[pan]: ERROR specifying which pan type!", "" >>>; } // determines spatial position of sound file and plays it fun void soundClip( SndBuf theSound, UGen left, UGen right, float startLoc, float endLoc, int whichPan ){ // get length of sound file theSound.length() => dur len; // start sound from the beginning 0 => theSound.pos; // the initial pan value (starting point) startLoc => float pan; // end time now + len => time endTime; // start time now => time startTime; // go while( now < endTime ){ // pan it panning( whichPan, pan, left, right ); // update where the sound should be startLoc + (endLoc-startLoc)*(now-startTime) / (endTime-startTime) => pan; //print //<<< "pan:", pan >>>; // update rate 1::ms => now; } // optional tail 300::ms => now; } cello_main(); fun void cello_main(){ //start scenes SndBuf sound_main; // read me.dir() + "/cello_main.wav" => sound_main.read; // left and right nodes for panning sound_main => Gain soundL => NRev rL => dac.left; sound_main => Gain soundR => NRev rR => dac.right; // set reverb mix .8 => sound_main.gain; .08 => rL.mix => rR.mix; // call in the sound spork ~ soundClip( sound_main, soundL, soundR, 0, 1, CONSTANTPOWER ); //start scenes SndBuf sound_cello2; // read me.dir() + "/cello_2.wav" => sound_cello2.read; // left and right nodes for panning sound_cello2 => Gain sound_cello2_L => NRev r_cello2_L => dac.left; sound_cello2 => Gain sound_cello2_R => NRev r_cello2_R => dac.right; // set reverb mix .8 => sound_cello2.gain; .08 => r_cello2_L.mix => r_cello2_R.mix; // call in the sound spork ~ soundClip( sound_cello2, sound_cello2_L, sound_cello2_R, 0, 1, CONSTANTPOWER ); //start scenes SndBuf sound_cello3; // read me.dir() + "/cello_3.wav" => sound_cello3.read; // left and right nodes for panning sound_cello3 => Gain sound_cello3_L => NRev r_cello3_L => dac.left; sound_cello3 => Gain sound_cello3_R => NRev r_cello3_R => dac.right; // set reverb mix .8 => sound_cello3.gain; .08 => r_cello3_L.mix => r_cello3_R.mix; // call in the sound spork ~ soundClip( sound_cello3, sound_cello3_L, sound_cello3_R, 0, 1, CONSTANTPOWER ); //start scenes //SndBuf sound_voice; // read //me.dir() + "/voice.wav" => sound_voice.read; // left and right nodes for panning //sound_voice => Gain sound_voice_L => NRev r_voice_L => dac.left; //sound_voice => Gain sound_voice_R => NRev r_voice_R => dac.right; // set reverb mix //.8 => sound_voice.gain; //.08 => r_voice_L.mix => r_voice_R.mix; //spork ~ soundClip( sound_voice, sound_voice_L, sound_voice_R, 0, 1, CONSTANTPOWER ); // call in the sound //voice_normal(); //voice_binaural(); //35::second => now; //voice_binaural_smooth(); voice_binaural_smooth_best(); //More effect //spork ~ randBinauralEffectScene(); sound_main.length() => now; sound_cello2.length() => now; sound_cello3.length() => now; } fun void voice_binaural_smooth_best(){ // call in the sound // Start location of the voice 28.90::second => now; //30::second => now; 1 => int pL1; //Position Left 0 => int pR1; //Position Right voice_binaural_p1(pL1,pR1); //Switch Position from Left to Right 0 => int pL2; //Position Left 1 => int pR2; //Position Right voice_binaural_p2(pL2,pR2); } fun void voice_normal(){ //start scenes SndBuf sound_voice; // read me.dir() + "/voice.wav" => sound_voice.read; // left and right nodes for panning sound_voice => Gain sound_voice_L => NRev r_voice_L => dac.left; sound_voice => Gain sound_voice_R => NRev r_voice_R => dac.right; // set reverb mix .8 => sound_voice.gain; .08 => r_voice_L.mix => r_voice_R.mix; spork ~ soundClip( sound_voice, sound_voice_L, sound_voice_R, 0, 1, CONSTANTPOWER ); } fun void voice_binaural(){ // call in the sound 36::second => now; 1 => int pL1; //Position Left 0 => int pR1; //Position Right voice_p1(pL1,pR1); //4.2::second => now; 0 => int pL2; //Position Left 1 => int pR2; //Position Right voice_p2(pL2,pR2); //4.2::second => now; 1 => int pL3; //Position Left 0 => int pR3; //Position Right voice_p3(pL3,pR3); //4.5::second => now; 0 => int pL4; //Position Left 1 => int pR4; //Position Right voice_p4(pL4,pR4); } 0 => int lastScene1; fun void voice_binaural_smooth(){ //Start position //35::second => now; Math.random2(1,2) => int scene1; while(scene1 == lastScene1){ Math.random2(1,2) => scene1; } Math.random2(0,1) => int startPosition1; 1 => int pL1; //Position Left 0 => int pR1; //Position Right if(startPosition1 == 0){ 0 => pL1; 1 => pR1; } if(scene1 == 1){ voice(pL1,pR1); }else{ voice1(pL1,pR1); } scene1 => lastScene1; voice_binaural_smooth(); } 0 => int lastScene; fun void randBinauralEffectScene(){ Math.random2(1,3) => int scene; while(scene == lastScene){ Math.random2(1,2) => scene; } Math.random2(0,1) => int startPosition; 1 => int pL; //Position Left 0 => int pR; //Position Right if(startPosition == 0){ 0 => pL; 1 => pR; } if(scene == 1){ loneliness(pL, pR); }else if(scene == 2){ angriness(pL, pR); }else{ happiness(pL, pR); } scene => lastScene; randBinauralEffectScene(); } //Temp fun void cello_2(int pL,int pR){ 1::second => now; startSound("/cello_2.wav", Math.random2f(.2,.9), pL , pR); } fun void cello_3(int pL,int pR){ 1::second => now; startSound("/cello_3.wav", Math.random2f(.05,.1), pL , pR); } //For voice_binaural_smooth() fun void voice(int pL,int pR){ //1::second => now; startSound("/voice.wav", Math.random2f(.6,1.0), pL , pR); } fun void voice1(int pL,int pR){ //1::second => now; startSound("/voice.wav", Math.random2f(.2,.9), pL , pR); } //For voice_binaural_smooth() fun void voice_p1(int pL,int pR){ //1::second => now; startSound("/voice_p1.wav", Math.random2f(.6,1.0), pL , pR); } fun void voice_p2(int pL,int pR){ //1::second => now; startSound("/voice_p2.wav", Math.random2f(.6,1.0), pL , pR); //startSound("/voice_p2.wav", Math.random2f(.2,.9), pL , pR); } fun void voice_p3(int pL,int pR){ //1::second => now; startSound("/voice_p3.wav", Math.random2f(.6,1.0), pL , pR); } fun void voice_p4(int pL,int pR){ //1::second => now; startSound("/voice_p4.wav", Math.random2f(.6,1.0), pL , pR); //startSound("/voice_p4.wav", Math.random2f(.05,.1), pL , pR); } //For voice_binaural_smooth_best() fun void voice_binaural_p1(int pL,int pR){ //Smooth out with following statement 1::second => now; startSound("/voice_binaural_p1_best.wav", Math.random2f(.6,1.0), pL , pR); } fun void voice_binaural_p2(int pL,int pR){ 0.27::second => now; startSound("/voice_binaural_p2_best.wav", Math.random2f(.6,1.0), pL , pR); //startSound("/voice_binaural_p2_best.wav", Math.random2f(.2,.9), pL , pR); } //For randBinauralEffectScene() fun void loneliness(int pL,int pR){ 1::second => now; startSound("/loneliness.wav", Math.random2f(.6,1.0), pL , pR); } fun void angriness(int pL,int pR){ 1::second => now; startSound("/angriness.wav", Math.random2f(.2,.9), pL , pR); } fun void happiness(int pL,int pR){ 1::second => now; startSound("/happiness.wav", Math.random2f(.05,.1), pL , pR); } fun void startSound(string file, float volume, int positionL,int PositionR){ SndBuf sound; // read me.dir() + file => sound.read; // left and right nodes for panning sound => Gain soundL => NRev rL => dac.left; sound => Gain soundR => NRev rR => dac.right; // set reverb mix volume => sound.gain; .08 => rL.mix => rR.mix; // call in the sound spork ~ soundClip( sound, soundL, soundR, positionL, PositionR, CONSTANTPOWER ); sound.length() => now; }