// @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.1.3 / ma-0.2.2c // @revision 1 Binaural4 b4; // ------------------------------------------------------------- // play a cycle of pitches 4 => int nPitches; // array to hold midi pitches (key numbers) 65 => int note; int keyn[nPitches]; [note, note-5, note-1, note+1] @=> keyn; // ------------------------------------------------------------- // against a cycle of a different length which varies instrument // parameters 3 => int nInsts; // instantiate UGens FMFS fm[3]; for (int i; i < 3; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; fm[i].out => dac; } // ------------------------------------------------------------- // 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; // ----------PARAMETERS---------- // loudness contour (100dB = loudest) [90.0, 80.0, 85.0] @=> float loud[]; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [4.0, 1.0, 2.0] @=> float tran[]; // channel contour [0, 0, 0] @=> int chan[]; // fmIndex contour [1.0, 1.0, 1.0] @=> float fmIndex[]; // fmRatio contour [1.0, 1.0, 1.0] @=> float fmRatio[]; // skew contour [0.1, 0.1, 0.1] @=> float skew[]; // Section 1: Loudness, Octave <<<"Section 1: Loudness, Octave">>>; //change base note 65 => note; [note, note-5, note-1, note+1] @=> keyn; // ----------PARAMETERS---------- // loudness contour (100dB = loudest) [90.0, 80.0, 85.0] @=> loud; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [4.0, 1.0, 2.0] @=> tran; // channel contour [0, 0, 0] @=> chan; // fmIndex contour [1.0, 1.0, 1.0] @=> fmIndex; // fmRatio contour [1.0, 1.0, 1.0] @=> fmRatio; // skew contour [0.1, 0.1, 0.1] @=> skew; // Mix down binauraly for (int i; i < 3; ++i) { for (int j; j < 4; ++j) { fm[i].out => b4.pssp[j]; } } 15::second + now => time sec1end; while (now < sec1end) { // 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: Index, Ratio, Skew <<<"Section 2: Index, Ratio, Skew">>>; //change base note 39 => note; [note, note-5, note-1, note+1] @=> keyn; // ----------PARAMETERS---------- // loudness contour (100dB = loudest) [85.0, 85.0, 85.0] @=> loud; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [2.0, 2.0, 2.0] @=> tran; // channel contour [0, 0, 0] @=> chan; // fmIndex contour [20.0, 10.0, 50.0] @=> fmIndex; // fmRatio contour [12.0, 22.0, 25.0] @=> fmRatio; // skew contour [0.1, 0.3, 0.2] @=> skew; // Mix down binauraly for (int i; i < 3; ++i) { for (int j; j < 4; ++j) { fm[i].out => b4.pssp[j]; } } 15::second + now => time sec2end; while (now < sec2end) { // 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; // deccelerate ioi * 1.02 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } } // Section 3: Everything <<<"Section 3: Everything">>>; //change base note 51 => note; [note, note-5, note-1, note+1] @=> keyn; // ----------PARAMETERS---------- // loudness contour (100dB = loudest) [85.0, 82.0, 87.0] @=> loud; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [1.0, 2.0, 4.0] @=> tran; // channel contour [0, 0, 0] @=> chan; // fmIndex contour [20.0, 95.0, 63.0] @=> fmIndex; // fmRatio contour [32.0, 20.0, 15.0] @=> fmRatio; // skew contour [0.5, 0.2, 0.23] @=> skew; // Mix down binauraly for (int i; i < 3; ++i) { for (int j; j < 4; ++j) { fm[i].out => b4.pssp[j]; } } 15::second + now => time sec3end; while (now < sec3end) { // 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 4: Location <<<"Section 4: Location">>>; //change base note 59 => note; [note, note-5, note-1, note+1] @=> keyn; // ----------PARAMETERS---------- // loudness contour (100dB = loudest) [85.0, 85.0, 85.0] @=> loud; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [2.0, 2.0, 2.0] @=> tran; // channel contour [0, 0, 0] @=> chan; // fmIndex contour [20.0, 20.0, 20.0] @=> fmIndex; // fmRatio contour [2.0, 2.0, 2.0] @=> fmRatio; // skew contour [0.1, 0.1, 0.1] @=> skew; // Mix down binauraly -- this time randomly assign to different channels for (int i; i < 3; ++i) { fm[i].out => b4.pssp[Std.rand2(0,3)]; } 15::second + now => time sec4end; while (now < sec4end) { // 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; // deccelerate ioi * 1.04 => 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