3 categories:: UGens>Analysis>Pitch
4 related:: Classes/BeatTrack, Classes/Loudness, Classes/MFCC, Classes/Onsets, Classes/Pitch
7 A (12TET major/minor) key tracker based on a pitch class profile of energy across FFT bins and matching this to templates for major and minor scales in all transpositions. It assumes a 440 Hz concert A reference. Output is 0-11 C major to B major, 12-23 C minor to B minor.
14 [fft] Audio input to track. This must have been pre-analysed by a 4096 size FFT. No other FFT sizes are valid except as noted below.
16 // With standard hop of half FFT size = 2048 samples
17 b = Buffer.alloc(s,4096,1); // for sampling rates 44100 and 48000
18 //b = Buffer.alloc(s,8192,1); // for sampling rates 88200 and 96000
22 [sk] Number of seconds for the influence of a window on the final key decision to decay by 40dB (to 0.01 its original value).
25 [sk] Each frame, the chroma values are set to the previous value multiplied by the chromadecay. 0.0 will start each frame afresh with no memory.
30 // The following files are test materials on my machine; you will subsitute your own filenames here
32 d = Buffer.read(s,"/Volumes/data/stevebeattrack/samples/100.wav");
33 // F major; hard to track!
34 d = Buffer.read(s,"/Volumes/data/stevebeattrack/samples/115.wav");
36 // straight forward since no transients; training set from MIREX2006
41 d = Buffer.read(s, "/Users/nickcollins/Desktop/ML/training_wav/78.wav")
43 b = Buffer.alloc(s, 4096, 1); // for sampling rates 44100 and 48000
47 var in, fft, resample;
48 var key, transientdetection;
50 in = PlayBuf.ar(1, d, BufRateScale.kr(d), 1, 0, 1);
54 key=KeyTrack.kr(fft, 2.0, 0.5);
58 Out.ar(0,Pan2.ar(in));
66 // alternating major and minor chords as a test
69 var in, fft, resample;
70 var key, transientdetection;
72 in = Mix(SinOsc.ar((60 + [0, MouseX.kr(3, 4).round(1), 7]).midicps, 0, 0.1));
74 // major dom 7 and minor 7; major keys preferred here
75 //in = Mix(SinOsc.ar((60 + (MouseY.kr(0, 11).round(1.0)) + [0, MouseX.kr(3, 4).round(1), 7, 10]).midicps, 0, 0.1));
79 key = KeyTrack.kr(fft);
83 Out.ar(0,Pan2.ar(in));
91 // Nice to hear what KeyTrack thinks:
93 d = Buffer.read(s, "/Users/nickcollins/Desktop/ML/training_wav/78.wav")
94 b = Buffer.alloc(s, 4096, 1); // for sampling rates 44100 and 48000
98 var in, fft, resample, chord, rootnote, sympath;
99 var key, transientdetection;
101 in = PlayBuf.ar(1, d, BufRateScale.kr(d), 1, 0, 1);
105 key = KeyTrack.kr(fft, 2.0, 0.5);
107 key = Median.kr(101, key); // Remove outlier wibbles
109 chord = if(key<12, #[0, 4, 7], #[0, 3, 7]);
110 rootnote = if(key<12, key, key-12) + 60;
112 sympath = SinOsc.ar((rootnote + chord).midicps, 0, 0.4).mean;
114 Out.ar(0,Pan2.ar(in, -0.5) + Pan2.ar(sympath, 0.5));
123 // See the MIREX2006 audio key tracking competition and Emilia Gomez's PhD thesis, Tonal Description of Music Audio Signals
125 // The following code was used to create the datasets for the UGen, and would be the basis of extensions
127 // Need one set of bin data for 44100 and one for 48000
129 // KeyTrack calculations, need to make arrays of FFT bins and weights for each chromatic tone.
130 // greater resolution, 4096 FFT, avoid lower octaves, too messy there
131 // 60*6*2 output arrays
134 var fftN, fftBins, binsize;
142 fftBins = fftN.div(2);
145 midinotes = (33..92); // 60 notes, 55 Hz up to 1661.2187903198 Hz
150 // for each note have six harmonic locations
152 var freq, whichbin, lowerbin, prop;
157 var partialfreq, partialamp;
159 partialamp = 1.0 / (j + 1);
160 partialfreq = freq * (j + 1);
162 whichbin = partialfreq / binsize;
163 lowerbin = whichbin.asInteger;
164 prop = 1.0 - (whichbin - lowerbin);
166 binlist.add(lowerbin).add(lowerbin + 1);
167 wtlist.add(prop * partialamp).add((1.0 - prop) * partialamp);
174 Post << (binlist) << nl<< nl;
176 Post << (wtlist) << nl<< nl;