// @title hw3-exampleScore.ck // @author Chris Chafe (cc@ccrma), Hongchan Choi (hongchan@ccrma) // @desc An example score file for homework 3, Music220a-2012 // @version chuck-1.3.2.0 // @revision 2 // pick a physical model synth, send to left chan (0) Clarinet physmod => dac.left; // bring in a mono .wav file from which to play sample // pass through Envelope to avoid clicks from SndBuf // to right chan (1) SndBuf sampler => Envelope samplerEnv => dac.right; sampler => samplerEnv => dac.left; "C:/Users/Jaguirre/Desktop/ChucK" => string chuck_directory; chuck_directory + "/sample.wav" => string _file; _file => sampler.read; cherr <= "[score] SndBuf: " <= _file <= IO.newline(); 10::ms => samplerEnv.duration; // write signals to file dac => WvOut2 out => blackhole; chuck_directory + "/result.wav" => string _capture; _capture => out.wavFilename; 32 => int num_frames; float buffer[num_frames]; 0 => int buffer_cnt; float buffer2[num_frames]; 0 => int buffer_cnt_p; 0 => int section_counter; 0 => int section; 1 => int synch; [1,1,1,1,0, 1,1,0, 1,1,1,0, 1,0, 1,0, 1,1,1,1,0, 1,1,0, 1,1,1,1,0, 1,0, 1,1,1,1,1,1,1,1,0, 1,0, 1,0, 1,1,1,1,0, 1,1,0, 1,1,1,0, 1,0, 1,0, 1,1,1,1,0, 1,1,0, 1,1,1,1,0, 1,0, 1,1,1,1,0, 1,1,1,1,1,1,1,0, 1,0 ] @=> int melody[]; 64 => int melody_length; 0 => int melody_cnt; 0 => int melody_cnt1; // ------------------------------------------------------------ // three algorithms which generate numbers in range -1.0 to 1.0 // see 1) logisticMapNotes.ck 2) logisticMapNotesOSC.ck for // more info fun float logistic(float x, float r) { return r * x * (1.0 - x); } // see 1) psuedoRandomNotes.ck 2) psuedoRandomNotesOSC.ck for // more info fun float random(float r) { if (r > 1.0) { 1.0 => r; } // get a random value return Math.random2f(-r, r); } // see 1) sinNotes.ck 2) sinNotesOSC.ck fun float sinusoid(float r) { // rate in radians at 1 sec period 2 * pi => float sinScale; // convert to desired rate in radians if (r > 0.0) r /=> sinScale; // current time in seconds now / second => float nowInSeconds; // convert to phase angle using rate in radians nowInSeconds * sinScale => float currentPhase; // or could be another trig function return Math.sin(currentPhase); } // ------------------------------------------------------------ // tick the chosen algorithm to get a new value fun float algorithm(float state, string type, float r) { if (type == "logistic") { // choose me to iterate the map and hear logistic map "tune" return logistic(state, r); } else if (type == "random") { // choose me for random "tune" return random(r); } else if (type == "sinusoid") { // choose me for sin function "tune" return sinusoid(r); } else { cherr <= "[score] Unknown algorithm type.\n"; } } // sporkable function that plays physical model notes forever fun void pLoop(string type, float r) { 0.1 => float state; 0 => buffer_cnt_p; getRhythm(); physmod.noteOn(1); while (true) { // note off in case it's already on physmod.stopBlowing(1.0); // a short rest //150::ms => now; algorithm(state, type, r) => state; // use state as a frequency 440.0 + (state * 220.0) => float f; // to clarinet physmod.freq(f); // note on physmod.startBlowing(0.6); physmod.gain(0.1); physmod.vibratoFreq(3.0); physmod.vibratoGain(0.6); getNoteLength()::ms => now; // end time of sample if(buffer_cnt_p >= num_frames){ 0 => buffer_cnt_p; getRhythm(); } // OPTIONAL -------------------------------------- // add any other Clarinet parameters physmod.reed(0.0);// - reed stiffness [1.0] physmod.stopBlowing(1.0); } } // sporkable function that plays sampler notes forever fun void sLoop(string type, float r) { 0.1 => float state; getRhythm2(); while (true) { // turn off envelope in case it's already on samplerEnv.target(0.0); // a short rest 150::ms => now; algorithm(state, type, r) => state; sampler.length() => dur totalFile; // time in file to start playing sample from ((state + 1.0) * 0.5) * totalFile * r => dur start; // sample duration //cherr <= Math.random2(1,5); getNoteLength2()::ms => dur duration; // end time of sample if(buffer_cnt >= num_frames){ 0 => buffer_cnt; getRhythm2(); section_counter++; } if(section_counter != 0 && section_counter %4 == 0 && buffer_cnt == 0){ if(section == 0) 1 => section; else 0 => section; } start + duration => dur end; if (end > totalFile) { end - duration => start; } // start position in file in actual samples sampler.pos(Math.random2(0,12863475)); // ramp up the envelope samplerEnv.target(0.5); duration => now; } } // sporkable function that plays sampler notes forever fun void cLoop(string type, float r) { 0.1 => float state; 100::ms => now; while (true) { // turn off envelope in case it's already on samplerEnv.target(0.0); // a short rest //0::ms => now; algorithm(state, type, r) => state; sampler.length() => dur totalFile; // time in file to start playing sample from ((state + 1.0) * 0.5) * totalFile * r => dur start; // sample duration //cherr <= Math.random2(1,5); getMelodyLength()::ms => dur duration; // end time of sample if(melody_cnt >= melody_length){ 0 => melody_cnt; } start + duration => dur end; if (end > totalFile) { end - duration => start; } // start position in file in actual samples sampler.pos(Math.random2(0,12863475)); // ramp up the envelope samplerEnv.target(0.5); duration => now; } } // sporkable function that plays physical model notes forever fun void bLoop(string type, float r) { 0.1 => float state; while (true) { getMelodyLength1() => int note_length; // note off in case it's already on physmod.stopBlowing(1.0); // a short rest //150::ms => now; algorithm(state, type, r) => state; // use state as a frequency 440.0 + (state * 220.0) => float f; // to clarinet physmod.freq(f); // note on physmod.startBlowing(0.4); physmod.gain(0.1); physmod.vibratoFreq(3.0); physmod.vibratoGain(0.6); //getNoteLength()::ms => now; // end time of sample if(melody_cnt1 >= melody_length){ 0 => melody_cnt1; } if(section_counter != 0 && section_counter %4 == 0 && melody_cnt1 == 0){ if(section == 0) 1 => section; else 0 => section; } // OPTIONAL -------------------------------------- // add any other Clarinet parameters physmod.reed(0.0);// - reed stiffness [1.0] note_length::ms => now; } } fun void getRhythm(){ for(0 => int i; i < num_frames; i++){ 1 => buffer[i]; } 0 => int index; Math.random2(0,8) => int subdivisions; if(!section){ for(0 => int j; j < subdivisions; j++){ Math.random2(0,num_frames-1) => index; 0 => buffer[index]; } } if(section) 0 => buffer[Math.random2(0, num_frames -1)]; } fun void getRhythm2(){ for(0 => int i; i < num_frames; i++){ 1 => buffer2[i]; } 0 => int index; Math.random2(0,8) => int subdivisions; if(!section){ for(0 => int j; j < subdivisions; j++){ Math.random2(0,num_frames-1) => index; 0 => buffer2[index]; } } if(section) 0 => buffer2[Math.random2(0, num_frames -1)]; } fun void getRhythm2(){ for(0 => int i; i < num_frames; i++){ 1 => buffer[i]; } 0 => int index; Math.random2(0,8) => int subdivisions; if(!section){ for(0 => int j; j < subdivisions; j++){ Math.random2(0,num_frames-1) => index; 0 => buffer[index]; } } if(section) 0 => buffer[Math.random2(0, num_frames -1)]; } fun int getNoteLength(){ 1 => int one_count; for(buffer_cnt_p => int i; i < num_frames; i++){ if(buffer[i] >= 1){ one_count++; } if(buffer[i] == 0){ break; } } (one_count + buffer_cnt_p) => buffer_cnt_p; return ((one_count+5) * 50); } fun int getNoteLength2(){ 1 => int one_count; for(buffer_cnt => int i; i < num_frames; i++){ if(buffer2[i] >= 1){ one_count++; } if(buffer2[i] == 0){ break; } } (one_count + buffer_cnt) => buffer_cnt; return ((one_count*2) * 50); } fun int getMelodyLength(){ 1 => int one_count; for(melody_cnt => int i; i < melody_length; i++){ if(melody[i] >= 1){ one_count++; } if(melody[i] == 0){ break; } } (one_count + melody_cnt) => melody_cnt; return ((one_count*5 ) * 50); } fun int getMelodyLength1(){ 1 => int one_count; for(melody_cnt1 => int i; i < melody_length; i++){ if(melody[i] >= 1){ one_count++; } if(melody[i] == 0){ break; } } (one_count + melody_cnt1) => melody_cnt1; return ((one_count*5) * 50); } // ------------------------------------------------------------ // prepare the show Shred pShred; Shred sShred; Shred cShred; Shred bShred; Shred zShred; // needed to stop both shreds gracefully fun void killShreds() { pShred.exit(); sShred.exit(); me.yield(); // note off //physmod.stopBlowing(1.0); //physmod.noteOff(0); // turn off envelope samplerEnv.target(0.0); 10::ms => now; } // ------------------------------------------------------------ // start the show, this is just and example score with changing sections //spork ~pLoop("sinusoid", 3.9) @=> pShred; // clarinet played by logistic map frequencies //SOLO Orchestral spork ~sLoop("logistic", 0.9) @=> sShred; 10::second => now; killShreds(); //SOLO Clarinet spork ~pLoop("logistic", 3.9) @=> pShred; 10::second => now; killShreds(); //Individual Polyphany spork ~sLoop("logistic", 0.9) @=> sShred; // samples pulled randomly spork ~pLoop("logistic", 3.9) @=> pShred; 10::second => now; killShreds(); //Unity spork ~cLoop("logistic", 0.9) @=> sShred; // samples pulled randomly spork ~bLoop("logistic", 3.9) @=> pShred; // samples pulled randomly 25::second => now; killShreds(); //Individual spork ~sLoop("logistic", 0.9) @=> sShred; // samples pulled randomly spork ~pLoop("logistic", 3.9) @=> sShred; // samples pulled randomly 10::second => now; killShreds(); // ------------------------------------------------------------ // finish the show out.closeFile(); // print message in terminal for sox command cherr <= "[score] Finished.\n"; cherr <= me.sourceDir() + "/exampleScore.wav";