Permalink
Browse files

tracking codes added

  • Loading branch information...
1 parent 6cf88c4 commit 38a03481f69f0b609e61d878d85dcfa17731625d @hoch hoch committed Nov 10, 2012
Showing with 469 additions and 0 deletions.
  1. +138 −0 tracking/amplitudeFrequencyTracker.ck
  2. +134 −0 tracking/amplitudeSpectrumTracker.ck
  3. +110 −0 tracking/amplitudeTracker.ck
  4. +47 −0 tracking/fft2Ifft.ck
  5. +40 −0 tracking/ifft.ck
@@ -0,0 +1,138 @@
+// @title amplitudeFrequencyTracker.ck
+// @author Chris Chafe (cc@ccrma), Hongchan Choi (hongchan@ccrma)
+// @desc A starter code for homework 5, Music220a-2012
+// @note amplitude/frequency tracking using UAna ugens
+// @version chuck-1.3.1.3 / ma-0.2.2c
+// @revision 1
+
+
+// IMPORTANT NOTE: this patch is designed to use microphone.
+// If you're using speakers and your microphone at the same time,
+// you might experience serious feedback. Make sure to use
+// the headphone or earbuds to avoid it.
+
+
+// pipe input into analysis audio graph:
+// track amplitude for gain of an FM patch
+// frequency will be max bin amplitude from the spectrum
+adc => FFT fft =^ RMS rms => blackhole;
+
+// setup FFT: choose high-quality transform parameters
+4096 => fft.size;
+Windowing.hann(fft.size() / 2) => fft.window;
+20 => int overlap;
+0 => int ctr;
+second / samp => float srate;
+
+// actual audio graph and parameter setting
+// NOTE: gain 'g' prevents direct connection bug
+adc => Gain g => dac.left;
+// creating hammond organ-like FM instrument
+BeeThree b3 => dac.right;
+// set initial frequency
+60 => Std.mtof => b3.freq;
+// instantiate a smoother to smooth tracker results (see below)
+Smooth sma, smf;
+// set time constant: shorter time constant gives faster
+// response but more jittery values
+sma.setTimeConstant((fft.size() / 2)::samp);
+smf.setTimeConstant((fft.size() / 5)::samp);
+
+
+// setGainAndFreq()
+spork ~ setGainAndFreq();
+fun void setGainAndFreq() {
+ while (true) {
+ // apply smoothed values
+ b3.gain(sma.getLast());
+ b3.freq(smf.getLast());
+ 1::samp => now;
+ }
+}
+
+
+// main inf-loop
+while(true) {
+ // hop in time by overlap amount
+ (fft.size() / overlap)::samp => now;
+ // then we've gotten our first bufferful
+ if (ctr > overlap) {
+ // compute the FFT and RMS analyses
+ rms.upchuck();
+ rms.fval(0) => float a;
+ Math.rmstodb(a) => float db;
+ // boost the sensitity
+ 30 + db * 15 => db;
+ // but clip at maximum
+ Math.min(100, db) => db;
+ sma.setNext(Math.dbtorms(db));
+
+ 0 => float max;
+ 0 => int where;
+ // look for a frequency peak in the spectrum
+ // half of spectrum to save work
+ for(0 => int i; i < fft.size()/4; ++i) {
+ if(fft.fval(i) > max) {
+ fft.fval(i) => max;
+ i => where;
+ }
+ }
+ // get frequency of peak
+ (where $ float) / fft.size() * srate => float f;
+ // then convert it to MIDI pitch
+ f => Math.ftom => float p;
+ // plus a major third
+ 4 +=> p;
+ // set lower boundary: prevents note too low
+ Math.max(20, p) => p;
+ // new freq if not noise
+ if(db > 10.0) {
+ smf.setNext(Math.mtof(p));
+ }
+ }
+ ctr++;
+}
+
+
+// @class Smooth
+// @desc contral signal generator for smooth transition
+class Smooth
+{
+ // audio graph
+ Step in => Gain out => blackhole;
+ Gain fb => out;
+ out => fb;
+
+ // init: smoothing coefficient, default no smoothing
+ 0.0 => float coef;
+ initGains();
+
+ // initGains()
+ fun void initGains() {
+ in.gain(1.0 - coef);
+ fb.gain(coef);
+ }
+
+ // setNext(): set target value
+ fun void setNext(float value) {
+ in.next(value);
+ }
+
+ // getLast(): return current interpolated value
+ fun float getLast() {
+ 1::samp => now;
+ return out.last();
+ }
+
+ // setExpo(): set smoothing directly from exponent
+ fun void setExpo(float value) {
+ value => coef;
+ initGains();
+ }
+
+ // setTimeConstant(): set smoothing duration
+ fun void setTimeConstant(dur duration) {
+ Math.exp(-1.0 / (duration / samp)) => coef;
+ initGains();
+ }
+} // END OF CLASS: Smooth
@@ -0,0 +1,134 @@
+// @title amplitudeSpectrumTracker.ck
+// @author Chris Chafe (cc@ccrma), Hongchan Choi (hongchan@ccrma)
+// @desc A starter code for homework 5, Music220a-2012
+// @note amplitude/spectrum tracking using UAna ugens
+// @version chuck-1.3.1.3 / ma-0.2.2c
+// @revision 1
+
+
+// IMPORTANT NOTE: this patch is designed to use microphone.
+// If you're using speakers and your microphone at the same time,
+// you might experience serious feedback. Make sure to use
+// the headphone or earbuds to avoid it.
+
+
+// pipe input into analysis audio graph:
+// track amplitude for gain of a resonant filtered-noise
+// frequency will track centroid of the input spectrum
+adc => FFT fft =^ RMS rms => blackhole;
+fft =^ Centroid cent => blackhole;
+
+// setup FFT: choose high-quality transform parameters
+4096 => fft.size;
+Windowing.hann(fft.size() / 2) => fft.window;
+20 => int overlap;
+0 => int ctr;
+second / samp => float srate;
+
+// actual audio graph and parameter setting
+// NOTE: gain 'g' prevents direct connection bug
+adc => Gain g => dac.left;
+// resonant low-pass filtered noise
+Noise n => ResonZ r => dac.right;
+// initial gain, quality(Q) and frequency for resonz
+0.0 => r.gain;
+10 => r.Q;
+60 => Std.mtof => r.freq;
+// instantiate a smoother to smooth tracker results (see below)
+Smooth sma, smf;
+// set time constant: shorter time constant gives faster
+// response but more jittery values
+sma.setTimeConstant((fft.size() / 3)::samp);
+smf.setTimeConstant((fft.size() / 8)::samp);
+
+
+// setGainQAndFreq()
+spork ~ setGainQAndFreq();
+fun void setGainQAndFreq() {
+ while (true) {
+ // apply smoothed values
+ r.gain(sma.getLast()); // apply smoothed value to gain
+ r.Q(10.0 + 30.0 * sma.getLast()); // apply smoothed value to Q
+ r.freq(smf.getLast()); // apply smoothed value to freq
+ 1::samp => now;
+ }
+}
+
+
+// main inf-loop
+while(true) {
+ // hop in time by overlap amount
+ (fft.size() / overlap)::samp => now;
+ // then we've gotten our first bufferful
+ if (ctr > overlap) {
+ // compute the FFT and RMS analyses
+ rms.upchuck();
+ rms.fval(0) => float a;
+ Math.rmstodb(a) => float db;
+ // boost the sensitity
+ 30 + db * 15 => db;
+ // but clip at maximum
+ Math.min(100, db) => db;
+ sma.setNext(Math.dbtorms(db));
+
+ // compute spectral centroid
+ cent.upchuck();
+ cent.fval(0) * srate / 2 => float c;
+ // then convert it to MIDI pitch
+ c => Math.ftom => float p;
+ // minus a major third
+ -4 +=> p;
+ // set lower boundary: prevents note too low
+ Math.max(20, p) => p;
+ // new freq if not noise
+ if(db > 10.0) {
+ smf.setNext(Math.mtof(p));
+ }
+
+ }
+ ctr++;
+}
+
+
+// @class Smooth
+// @desc contral signal generator for smooth transition
+class Smooth
+{
+ // audio graph
+ Step in => Gain out => blackhole;
+ Gain fb => out;
+ out => fb;
+
+ // init: smoothing coefficient, default no smoothing
+ 0.0 => float coef;
+ initGains();
+
+ // initGains()
+ fun void initGains() {
+ in.gain(1.0 - coef);
+ fb.gain(coef);
+ }
+
+ // setNext(): set target value
+ fun void setNext(float value) {
+ in.next(value);
+ }
+
+ // getLast(): return current interpolated value
+ fun float getLast() {
+ 1::samp => now;
+ return out.last();
+ }
+
+ // setExpo(): set smoothing directly from exponent
+ fun void setExpo(float value) {
+ value => coef;
+ initGains();
+ }
+
+ // setTimeConstant(): set smoothing duration
+ fun void setTimeConstant(dur duration) {
+ Math.exp(-1.0 / (duration / samp)) => coef;
+ initGains();
+ }
+} // END OF CLASS: Smooth
@@ -0,0 +1,110 @@
+// @title amplitudeTracker.ck
+// @author Chris Chafe (cc@ccrma), Hongchan Choi (hongchan@ccrma)
+// @desc A starter code for homework 5, Music220a-2012
+// @note amplitude tracking using UAna ugens
+// @version chuck-1.3.1.3 / ma-0.2.2c
+// @revision 1
+
+
+// IMPORTANT NOTE: this patch is designed to use microphone.
+// If you're using speakers and your microphone at the same time,
+// you might experience serious feedback. Make sure to use
+// the headphone or earbuds to avoid it.
+
+
+// pipe input into analysis audio graph:
+// track amplitude to control breath pressure of a clarinet
+adc => FFT fft =^ RMS rms => blackhole;
+
+// choose high-quality transform parameters
+4096 => fft.size;
+Windowing.hann(fft.size()/2) => fft.window;
+20 => int overlap;
+0 => int ctr;
+
+// actual audio graph and parameter setting
+// NOTE: gain 'g' prevents direct connection bug
+adc => Gain g => dac.left;
+// STK clarinet instrument
+Clarinet cl => dac.right;
+60 => Std.mtof => cl.freq;
+// instantiate a smoother to smooth tracker results (see below)
+Smooth sma;
+// set time constant: shorter time constant gives faster
+// response but more jittery values
+sma.setTimeConstant((fft.size() / 2)::samp);
+
+
+// setBlowingPressure()
+spork ~ setBlowingPressure();
+fun void setBlowingPressure() {
+ while (true) {
+ // apply smoothed value to pressure
+ cl.pressure(sma.getLast());
+ 1::samp => now;
+ }
+}
+
+
+// main inf-loop
+while(true) {
+ // hop in time by overlap amount
+ (fft.size() / overlap)::samp => now;
+ // then we've gotten our first bufferful
+ if (ctr > overlap) {
+ // compute the RMS analysis
+ rms.upchuck();
+ rms.fval(0) => float a;
+ Math.rmstodb(a) => float db;
+ // boost the sensitity
+ 75 +=> db;
+ // but clip at maximum
+ Math.min(100, db) => db;
+ sma.setNext(Math.dbtorms(db));
+ }
+ ctr++;
+}
+
+
+// @class Smooth
+// @desc contral signal generator for smooth transition
+class Smooth
+{
+ // audio graph
+ Step in => Gain out => blackhole;
+ Gain fb => out;
+ out => fb;
+
+ // init: smoothing coefficient, default no smoothing
+ 0.0 => float coef;
+ initGains();
+
+ // initGains()
+ fun void initGains() {
+ in.gain(1.0 - coef);
+ fb.gain(coef);
+ }
+
+ // setNext(): set target value
+ fun void setNext(float value) {
+ in.next(value);
+ }
+
+ // getLast(): return current interpolated value
+ fun float getLast() {
+ 1::samp => now;
+ return out.last();
+ }
+
+ // setExpo(): set smoothing directly from exponent
+ fun void setExpo(float value) {
+ value => coef;
+ initGains();
+ }
+
+ // setTimeConstant(): set smoothing duration
+ fun void setTimeConstant(dur duration) {
+ Math.exp(-1.0 / (duration / samp)) => coef;
+ initGains();
+ }
+} // END OF CLASS: Smooth
Oops, something went wrong. Retry.

0 comments on commit 38a0348

Please sign in to comment.