// @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 // ------------------------------------------------------------- // how many pitches to play in a cycle 6 => int nPitches; // array to hold midi pitches (key numbers) int keyn[nPitches]; [52, 44, 61, 68, 49, 54] @=> keyn; 22::second + now => time Section1; // ------------------------------------------------------------- // against a cycle of a different length which we'll use to vary // instrumentparameters 5 => 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) [80.0, 80.0, 80.0, 80.0, 80.0] @=> float loud[]; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [2.0, 2.0, 2.0, 2.0, 2.0] @=> float tran[]; // channel contour [0, 1, 3, 0, 2] @=> int chan[]; // fmIndex contour [1.0, 1.0, 1.0, 1.0, 1.0] @=> float fmIndex[]; // fmRatio contour [1.0, 1.0, 1.0, 1.0, 1.0] @=> float fmRatio[]; // pitch skew contour in semitones [0.1, 0.1, 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) 500::ms => dur ioi; // accelerate to this smallest ioi 50::ms => dur minIoi; 200::ms => dur maxIoi; // which pitch is next 0 => int p; // which instrument is next 0 => int i; // ----------------------------------------------------------- //intro [ 36, 37, 38, 39, 40, 41 ] @=> keyn; 5::second + now => time moveOn; while (now < moveOn) { // TODO: code which makes a note from the next pitch in the // sequence in keyn[] 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.9 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // section 1 // section 1 pitches [ 60, 72, 68, 66, 67, 63 ] @=> keyn; [1.0, 2.0, 2.0, 4.0, 1.0] @=> tran; maxIoi => ioi; // when to change 25::second + now => time endOfSection1; 10::second + now => time loudChange; while (now < endOfSection1) { // TODO: code which makes a note from the next pitch in the // sequence in keyn[] if (loudChange < now) { [90.0, 95.0, 80.0, 85.0, 75.0] @=> loud; [1.0, 1.0, 1.0, 1.0, 1.0] @=> fmRatio; [1.0, 1.0, 1.0, 1.0, 1.0] @=> fmIndex; [0.1, 0.6, 0.7, 0.4, 0.2] @=> skew; } 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 1 => int flag; if (flag == 1) { ioi * 0.9 => ioi; 0 => flag; } else { ioi * 2.0 => ioi; 1 => flag; } // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // ----------------------------------------------------------- // section 2 // section 2 pitches [ 36, 37, 38, 39, 40, 41 ] @=> keyn; [1.0, 4.0, 3.0, 2.0, 1.0] @=> fmRatio; [1.0, 1.0, 1.0, 1.0, 1.0] @=> fmIndex; [4.0, 3.5, 1.0, 2.0, 1.0] @=> tran; 25::second + now => time endOfSection2; 12::second + now => time loudChange2; while (now < endOfSection2) { // TODO: code which makes a note from the next pitch in the // sequence in keyn[] if (loudChange2 < now) { [80.0, 80.0, 80.0, 80.0, 80.0] @=> loud; [2.0, 2.0, 2.0, 2.0, 2.0] @=> tran; [1.0, 1.0, 1.0, 1.0, 1.0] @=> fmRatio; [1.0, 1.0, 1.0, 1.0, 1.0] @=> fmIndex; [1.0, 1.0, 1.0, 1.0, 1.0] @=> skew; } 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 * 1.1 => ioi; // can't go faster than minIoi if (maxIoi < ioi) { maxIoi => 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