// @title EffEm.ck // @author Priya Shekar (pshekar@ccrma), original example by Chris Chafe (cc@ccrma), Hongchan Choi (hongchan@ccrma) // @desc HW4 Music220A - Auditory Streaming // @notes requires pre-shredding of examples Binaural4.ck and DBAP4e.ck by Chafe and Choi // @version chuck-1.3.1.3 / ma-0.2.2c // @revision 1 // @date 11/9/12 // ------------------------------------------------------------- // play a cycle of pitches 4 => int nPitches; // array to hold midi pitches (key numbers) int keyn[nPitches]; [48, 55, 57, 60] @=> keyn; // ------------------------------------------------------------- // against a cycle of a different length which varies instrument // parameters 3 => int nInsts; // arrays to hold loudnesses, pitch register transpositions, // channels, fm instrument parameters // loudness contour (100dB = loudest) [90.0, 90.0, 90.0] @=> float loud[]; // pitch register contour // e.g., 2.0 for octave higher, 4.0 for two octaves, etc. [2.0, 0.25, 1.0] @=> float tran[]; // channel contour [0, 0, 0] @=> int chan[]; // fmIndex contour [6.0, 30.0, 2.0] @=> float fmIndex[]; // fmRatio contour [5.0, 0.5, 7.0] @=> float fmRatio[]; // skew contour [0.0, 3.0, 0.0] @=> float skew[]; [5.0, 0.0, 5.0] @=> float vibFreq[]; [0.5, 0.0, 2.0] @=> float vibExc[]; [0.0, 10.0, 0.0] @=> float jitFreq[]; [0.0, 2.0, 0.0] @=> float jitExc[]; DBAP4e outInst; outInst.setMode("binaural"); outInst.setReverb(0.0); // instantiate UGens FMFS fm[3]; for (int i; i < 3; ++i) { // start with instruments muted 0.0 => fm[i].out.gain; fm[i].out => outInst; } // ------------------------------------------------------------- // global parameters // set a common note duration 500::ms => dur duration; // starting inter-onset interval (inverse of tempo) 1000::ms => dur ioi; // accelerate to this smallest ioi 5::ms => dur minIoi; // which pitch is next 0 => int p; // which instrument is next 0 => int i; time timeSec; dur total; dur step; float pan; float dynamic; float minDynamic; float maxDynamic; float reverb; float maxReverb; float minReverb; int section; // infinite loop //SECTION 1 10.0::second + now => timeSec; //10.0 0 => pan; 10.0::second => total; loopInst(1); //SECTION 2 8.0::second + now => timeSec; //8.0 [4.0, 0.5, 2.0] @=> tran; [75.0, 90.0, 75.0] @=> loud; 200::ms => duration; 50::ms => ioi; 10::ms => minIoi; 0 => p; 0 => i; loopInst(2); //SECTION 3 10.0::second + now => timeSec; //10.0 [90.0, 90.0, 90.0] @=> loud; 0.2 => minDynamic; 1.0 => maxDynamic; minDynamic => dynamic; 100::ms => duration; 500::ms => ioi; 10::ms => minIoi; 0 => p; 0 => i; 0 => int ch; loopInst(3); //SECTION 4 8.0::second + now => timeSec; //8.0 maxDynamic => dynamic; 1.0 => maxReverb; 0.05 => minReverb; minReverb => reverb; 500::ms => duration; 10::ms => ioi; 0 => p; 0 => i; loopInst(4); //SECTION 5 8.0::second + now => timeSec; //8.0 500::ms => duration; 10::ms => ioi; 0 => p; 0 => i; loopInst(5); //SECTION 6 9.0::second + now => timeSec; //9.0 700::ms => duration; 10::ms => ioi; 1000::ms => dur maxIoi; 0 => p; 0 => i; 0.2 => reverb; maxDynamic => dynamic; 0 => pan; 9.0::second => total; loopInst(6); //allow reverb to fade out 2::second => now; // ------------------------------------------------------------- // @function loopInst fun void loopInst(int section){ while (now < timeSec) { // 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]); fm[i].setVib(vibFreq[i],vibExc[i]); fm[i].setJit(jitFreq[i],jitExc[i]); // start note fm[i].attack(100::ms); // wait duration => now; // stop note fm[i].release(10::ms); // increment note and instrument p++; i++; if (section == 1){ // 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.5 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } 100::ms + duration + 10::ms + ioi => step; //attack + duration + release + ioi (step/total)*1.0 +=> pan; outInst.setPosition(-1.0+pan, 1.0+pan); } if (section == 2){ // cycle pitch through full array nPitches %=> p; if (i == 3){ Std.rand2f(100, 1500) => float hold; hold::ms => now; } nInsts %=> i; ioi => now; outInst.setPosition(0.0, 0.0); } if (section == 3){ // 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; } outInst.setGain(dynamic); dynamic * 1.1 => dynamic; if (dynamic > maxDynamic) { maxDynamic => dynamic; } if (ch == 1){ ch --; outInst.setPosition(-1.0, 1.0); outInst.setPosition(-1.0, -1.0); } else { ch ++; outInst.setPosition(1.0, 1.0); outInst.setPosition(1.0, -1.0); } } if (section == 4){ // cycle pitch through full array nPitches %=> p; // cycle instrument through full array nInsts %=> i; ioi => now; outInst.setGain(dynamic); dynamic * 0.9 => dynamic; if (dynamic < minDynamic) { minDynamic => dynamic; } outInst.setReverb(reverb); reverb * 1.11 => reverb; if (reverb > maxReverb) { maxReverb => reverb; } outInst.setPosition(0.0, 0.0); } if (section == 5){ // 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.5 => ioi; // can't go faster than minIoi if (ioi < minIoi) { minIoi => ioi; } outInst.setGain(dynamic); dynamic * 1.11 => dynamic; if (dynamic > maxDynamic) { maxDynamic => dynamic; } outInst.setReverb(reverb); reverb * 0.9 => reverb; if (reverb < minReverb) { minReverb => reverb; } outInst.setPosition(0.0, 0.0); } if (section == 6){ // 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.2 => ioi; // can't go faster than minIoi if (ioi > maxIoi) { maxIoi => ioi; } outInst.setGain(dynamic); dynamic * 0.7 => dynamic; if (dynamic < minDynamic) { minDynamic => dynamic; } outInst.setReverb(reverb); 100::ms + duration + 10::ms + ioi => step; //attack + duration + release + ioi if(pan <= 2*pi) { (step/total)*2*pi +=> pan; outInst.setPosition(Math.sin(pan), Math.cos(pan)); } } } } // ------------------------------------------------------------- // @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