// @title hw4-starter.ck // @author Chris Chafe (cc@ccrma), Hongchan Choi (hongchan@ccrma) // @desc A starter code for homework 4, Music220a-2012 // @note a demonstration/template for auditory streaming // @version chuck-1.3.0 // @revision 2 // ------------------------------------------------------------- // "Try me, I don't break into multiple streams... why?" // // ANSWER: the streaming illusion demonstrates grouping by // similarity, but here there is only one group of sounds and // therefore only one stream // ------------------------------------------------------------- dac => WvOut2 out => blackhole; me.sourceDir() + "/AuditoryStreaming.wav" => string _capture; _capture => out.wavFilename; // Section 1 // how many pitches to play in a cycle 4 => int nPitches; // Section 1 Pitches // array to hold midi pitches (key numbers) int keyn[nPitches]; [65, 60, 62, 64] @=> keyn; // ------------------------------------------------------------- // against a cycle of a different length which we'll use to vary // instrumentparameters 3 => int nInsts; // arrays to hold loudnesses, pitch register transpositions, // channels, fm instrument parameters, etc. // anything that we want to use to break the repeating pitches // into multiple streams // these are all set the same for starters, so no multiple streams... // loudness contour (100dB = loudest) [90.0, 80.0, 80.0] @=> float loud[]; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [1.0, 1.0, 1.0] @=> float tran[]; // channel contour [1, 0, 0] @=> int chan[]; // fmIndex contour [1.0, 1.0, 1.0] @=> float fmIndex[]; // fmRatio contour [1.0, 1.0, 1.0] @=> float fmRatio[]; // pitch skew contour in semitones [0.1, 0.1, 0.1] @=> float skew[]; // instantiate UGens FMFS fm[nInsts]; for (int i; i < nInsts; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; fm[i].out => dac.chan(chan[i]); } // ------------------------------------------------------------- // global parameters // set a common note duration 100::ms => dur duration; // starting inter-onset interval (inverse of tempo) 1000::ms => dur ioi; // accelerate to this smallest ioi 10::ms => dur minIoi; // which pitch is next 0 => int p; // which instrument is next 0 => int i; // When to change 10::second + now => time endOfSection1; // finite loop 1 while (now < endOfSection1) { // 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(100::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.8 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // ————————————————————————————————————————————————————————————— // Section 2 // Section 2 pitches [67, 62, 64, 66] @=> keyn; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [2.0, 1.0, 1.0] @=> float tran1[]; // channel contour [0, 1, 1] @=> int chan1[]; for (int i; i < nInsts; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; fm[i].out => dac.chan(chan1[i]); } 5::second + now => time endOfSection2; // finite loop 2 while (now < endOfSection2) { // print pitch index, instrument index chout <= "PITCH = " <= p <= "\t\t"; chout <= "INST. = " <= i <= IO.newline(); Std.mtof(keyn[p]) * tran1[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(100::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.8 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // ————————————————————————————————————————————————————————————— // Section 3 // Section 3 pitches [69, 64, 66, 68] @=> keyn; // fmIndex contour [3.0, 1.0, 1.0] @=> float fmIndex2[]; for (int i; i < nInsts; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; fm[i].out => dac.chan(chan[i]); } 5.5::second + now => time endOfSection3; // finite loop 3 while (now < endOfSection3) { // print pitch index, instrument index chout <= "PITCH = " <= p <= "\t\t"; chout <= "INST. = " <= i <= IO.newline(); Std.mtof(keyn[p]) * tran1[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(fmIndex2[i]); // assign modulator / carrier frequency ratio fm[i].setRatio(fmRatio[i]); // assign pitch skew fm[i].setSkew(skew[i]); // start note fm[i].attack(100::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.8 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // ————————————————————————————————————————————————————————————— // Section 4 // Section 4 pitches [70, 65, 67, 69] @=> keyn; // fmRatio contour [3.0, 1.0, 1.0] @=> float fmRatio3[]; for (int i; i < nInsts; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; fm[i].out => dac.chan(chan1[i]); } 5.2::second + now => time endOfSection4; // finite loop 4 while (now < endOfSection4) { // print pitch index, instrument index chout <= "PITCH = " <= p <= "\t\t"; chout <= "INST. = " <= i <= IO.newline(); Std.mtof(keyn[p]) * tran1[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(fmIndex2[i]); // assign modulator / carrier frequency ratio fm[i].setRatio(fmRatio3[i]); // assign pitch skew fm[i].setSkew(skew[i]); // start note fm[i].attack(100::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.8 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // ————————————————————————————————————————————————————————————— // Section 5 // Section 5 pitches [65, 53, 57, 60] @=> keyn; for (int i; i < nInsts; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; fm[i].out => dac.chan(chan[i]); } 5.2::second + now => time endOfSection5; // finite loop 5 while (now < endOfSection5) { // print pitch index, instrument index chout <= "PITCH = " <= p <= "\t\t"; chout <= "INST. = " <= i <= IO.newline(); Std.mtof(keyn[p]) * tran1[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(fmIndex2[i]); // assign modulator / carrier frequency ratio fm[i].setRatio(fmRatio3[i]); // assign pitch skew fm[i].setSkew(skew[i]); // start note fm[i].attack(100::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.8 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // ------------------------------------------------------------- // @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 out.closeFile(); // print message in terminal for sox command cherr <= "[score] Finished.\n"; cherr <= me.sourceDir() + "/AuditoryStreaming.wav";