// @title hw4-starter.ck // @author Myles Borins // adapted from code by Chris Chafe (cc@ccrma) and Hongchan Choi (hongchan@ccrma) // @desc A starter code for homework 4, Music220a-2012 // @note a demonstration/template for auditory streaming // @version chuck-1.3.1.3 / ma-0.2.2c // @revision 1 // writing signals to files... dac.left => WvOut leftOut => blackhole; dac.right => WvOut rightOut => blackhole; me.sourceDir() + "/left_final.wav" => string _captureL; me.sourceDir() + "/right_final.wav" => string _captureR; _captureL => leftOut.wavFilename; _captureR => rightOut.wavFilename; // ------------------------------------------------------------- // play a cycle of pitches 16 => int nPitches; // array to hold midi pitches (key numbers) int keyn[nPitches]; [50, 62, 50, 61, 50, 60, 50, 59, 50, 58, 50, 57, 50, 56, 50, 55] @=> keyn; // ------------------------------------------------------------- // against a cycle of a different length which varies instrument // parameters 4 => int nInsts; // arrays to hold loudnesses, pitch register transpositions, // channels, fm instrument parameters // loudness contour (100dB = loudest) [60.0, 70.0, 80.0, 70.0] @=> float loud[]; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [1.0, 1.5, 2.0, 1.5] @=> float tran[]; // channel contour [0, 1, 2, 1] @=> int chan[]; // fmIndex contour [1.0, 2.0, 4.0, 2.0] @=> float fmIndex[]; // fmRatio contour [10.0, 20.0, 30.0, 20.0] @=> float fmRatio[]; // skew contour [0.1, 0.3, 0.7, 0.3] @=> float skew[]; // instantiate UGens FMFS fm[4]; JCRev reverb; DBAP4e b4; Dyno dy; b4.setMode("binaural"); for (int i; i < 4; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; // fm[i].out => reverb => dac; fm[i].out => reverb => dy => b4; } dy.limit(); .2 => dy.gain; 200::ms => dy.releaseTime; 0.2 => dy.thresh; 0.33 => dy.slopeAbove; b4.setMode("binaural"); b4.setReverb(0); b4.setDelayTime([5.0, 5.0, 5.0, 5.0]); b4.setPosition(0,0); // ------------------------------------------------------------- // global parameters // set a common note duration // starting inter-onset interval (inverse of tempo) 1000::ms => dur ioi; // accelerate to this smallest ioi 20::ms => dur minIoi; // which pitch is next 0 => int p; // which instrument is next 0 => int i; 0 => int count; 150 => int currdurr; currdurr::ms => dur duration; // reverb gain reverb.mix(.09); while (count < 175) { // print pitch index, instrument index chout <= "PITCH = " <= p <= "\t\t"; chout <= "INST. = " <= i <= IO.newline(); Std.mtof(keyn[p]) * tran[i] => float tmp; // assign pitch fm[i].setPitch(tmp); // assign loudness after converting to amplitude fm[i].out.gain(Math.dbtorms(loud[i])); // assign modulator index fm[i].setIndex(fmIndex[i]); // assign modulator / carrier frequency ratio fm[i].setRatio(fmRatio[i]); // assign pitch skew fm[i].setSkew(skew[i]); // start note fm[i].attack(10::ms); // wait duration => now; // stop note fm[i].release(10::ms); // increment note and instrument p++; i++; // cycle pitch through full array nPitches %=> p; // cycle instrument through full array nInsts %=> i; // advance time by interval and calculate the next time interval ioi => now; // accelerate ioi * 0.7 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } if (count < 140){ currdurr--; currdurr::ms => duration; } count++; b4.setPosition(Std.rand2f( -1.0, 1.0 ),Std.rand2f( -1.0, 1.0 )); } // Second b4.setPosition(0,0); // set some things .05 => dy.gain; 500 => currdurr; currdurr::ms => duration; 0 => count; reverb.mix(.2); // which instrument is next 0 => i; 0 => p; 1000::ms => ioi; // loudness contour (100dB = loudest) [80.0, 80.0, 80.0, 80.0] @=> loud; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [1.0, 1.5, 2.0, .5] @=> tran; // channel contour [0, 2, 1, 3] @=> chan; // fmIndex contour [10.0, 20.0, 30.0, 20.0] @=> fmIndex; // fmRatio contour [400.0, 300.0, 200.0, 100.0] @=> fmRatio; // skew contour [10.6, 20.4, 200.7, 300.3] @=> skew; while (count < 32) { // print pitch index, instrument index chout <= "PITCH = " <= p <= "\t\t"; chout <= "INST. = " <= i <= IO.newline(); Std.mtof(keyn[p]) * tran[i] => float tmp; // assign pitch fm[i].setPitch(tmp); // assign loudness after converting to amplitude fm[i].out.gain(Math.dbtorms(loud[i])); // assign modulator index fm[i].setIndex(fmIndex[i]); // assign modulator / carrier frequency ratio fm[i].setRatio(fmRatio[i]); // assign pitch skew fm[i].setSkew(skew[i]); // start note fm[i].attack(10::ms); // wait duration => now; // stop note fm[i].release(10::ms); // increment note and instrument p++; i++; // cycle pitch through full array nPitches %=> p; // cycle instrument through full array nInsts %=> i; // advance time by interval and calculate the next time interval ioi => now; // accelerate ioi * 0.2 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } count++; } // THIRD 300 => currdurr; currdurr::ms => duration; 0 => count; // which instrument is next 0 => i; 0 => p; 1000::ms => ioi; // reverb gain reverb.mix(.05); .05 => float mix; .2 => dy.gain; // loudness contour (100dB = loudest) [80.0, 70.0, 80.0, 70.0] @=> loud; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [1.0, 1.0, 1.0, 1.0] @=> tran; // channel contour [0, 1, 3, 2] @=> chan; // fmIndex contour [5.0, 6.0, 7.0, 8.0] @=> fmIndex; // fmRatio contour [70.0, 50.0, 60.0, 80.0] @=> fmRatio; // skew contour [0.6, 0.4, 0.7, 0.3] @=> skew; while (count < 300) { // print pitch index, instrument index chout <= "PITCH = " <= p <= "\t\t"; chout <= "INST. = " <= i <= IO.newline(); Std.mtof(keyn[p]) * tran[i] => float tmp; // assign pitch fm[i].setPitch(tmp); // assign loudness after converting to amplitude fm[i].out.gain(Math.dbtorms(loud[i])); // assign modulator index fm[i].setIndex(fmIndex[i]); // assign modulator / carrier frequency ratio fm[i].setRatio(fmRatio[i]); // assign pitch skew fm[i].setSkew(skew[i]); // start note fm[i].attack(10::ms); // wait duration => now; // stop note fm[i].release(10::ms); // increment note and instrument p++; i++; // cycle pitch through full array nPitches %=> p; // cycle instrument through full array nInsts %=> i; // advance time by interval and calculate the next time interval ioi => now; // accelerate ioi * 0.7 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } if (count < 140){ currdurr--; currdurr--; currdurr::ms => duration; } count++; mix + 0.01 => mix; reverb.mix(mix); b4.setPosition(Std.rand2f( -1.0, 1.0 ),Std.rand2f( -1.0, 1.0 )); } // Let reverb die out 0 => count; b4.setPosition(0,0); 1::second => duration; while (count < 5){ duration => now; count++; } // ------------------------------------------------------------- // @class FMFS fm implementation from scratch with envelopes // @author Chris Chafe (cc@ccrma) class FMFS { // modulator with index envelope and carrier with // amplitude envelope SinOsc mod => Gain ind => ADSR indEnv => SinOsc car; car => ADSR ampEnv => Gain out => blackhole; // generate detailed pitch contour with skew, periodic // vibrato and random jitter // unity constant for center pitch of event Step unity => Gain pit; // add in skew offset controlled by simple Envelope Step skew => Envelope skewEnv => pit; // add in vibrato controlled by ADSR Envelope SinOsc perVib => ADSR vibEnv => pit; // add in some low-frequency randomness Noise ranVib => ResonZ lpf => pit; // apply pitch everywhere that depends on it pit => car; pit => Gain rat => mod; pit => ind; // configure modes of above UG's // config oscillator for fm input (see SinOsc class) 2 => car.sync; // freq is controlled by input only 0.0 => car.freq; // config oscillator for fm input 2 => mod.sync; // freq is controlled by input only 0.0 => mod.freq; // config gain to multiply inputs (see UG class) 3 => ind.op; // initial values setPitch(440.0); setIndex(1.0); setRatio(1.0); // set A, D, S, and R all at once ampEnv.set (10::ms, 10::ms, 0.5, 100::ms); indEnv.set (50::ms, 50::ms, 1, 100::ms); skewEnv.duration (100::ms); vibEnv.set (50::ms, 500::ms, 0.4, 100::ms); // skew in semitones setSkew(1.0); // vibrato frequency, excursion in semitones setVib(6.5, 1.0); // randomness frequency, excursion in semitones setJit(6.5, 1.0); // setPitch() fun void setPitch(float pitch) { pit.gain(pitch); } // setIndex() fun void setIndex(float index) { mod.gain(index); } // setRatio() fun void setRatio(float ratio) { rat.gain(ratio); } // setSkew() fun void setSkew(float semitones) { // units are equal-tempered semitones (Math.pow(2.0, semitones/12.0) - 1.0) => float skewAmt; skew.next( skewAmt ); } // setVib() fun void setVib(float f, float semitones) { perVib.freq(f); // scale it to equal-tempered quartertones // in each direction Math.pow(2.0, semitones/24.0) - 1.0 => float jitAmt; perVib.gain( jitAmt ); } // setJit() fun void setJit(float f, float semitones) { lpf.freq(f); // bandwidth of low-frequency filter resonance, // ok as a constant lpf.Q(1.0); // scale it to equal-tempered quartertones // in each direction Math.pow(2.0, semitones/24.0) - 1.0 => float jitAmt; // empirically scaled up to where it's noticeable 12.0 *=> jitAmt; ranVib.gain(jitAmt); } // attack(): sculpt a note using envelopes fun void attack(dur attack) { // rise time of ADSR ampEnv.attackTime(attack); indEnv.attackTime(attack); vibEnv.attackTime(attack); // duration of simple Envelope skewEnv.duration(attack); // trigger ADSR ampEnv.keyOn(); indEnv.keyOn(); vibEnv.keyOn(); // kind of counterintuitive, but skew from skewAmt // to 0 for attack skewEnv.keyOff(); } // release() fun void release(dur release) { // release time of ADSR ampEnv.releaseTime(release); indEnv.releaseTime(release); vibEnv.releaseTime(release); // duration of simple Envelope skewEnv.duration(release); // trigger ADSR release ampEnv.keyOff(); indEnv.keyOff(); vibEnv.keyOff(); // also counterintuitive, but skew from 0 // to skewAmt during note off skewEnv.keyOn(); } } // END OF CLASS: FMFS // ------------------------------------------------------------ // finish the show leftOut.closeFile(); rightOut.closeFile(); cherr <= "\n[score] Finished.\nMerge two products with the command below.\n\n"; cherr <= "sox -M " <= _captureL <= " " <= _captureR <= " "; cherr <= me.sourceDir() + "/Final.wav\n\n";