supernova: fix boost.thread move semantics
[supercollider.git] / Help / UGens / MachineListening / KeyTrack.html
blob423d262b2f95a6c8a01face84ddaeb94b112e891
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.44">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #7c2a1b}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #606060}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #5b2e22}
17 p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
18 p.p9 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #872718}
19 span.s1 {color: #000000}
20 span.s2 {color: #1431aa}
21 span.s3 {color: #25449a}
22 span.s4 {color: #20409d}
23 span.s5 {color: #102aae}
24 span.s6 {color: #7c2a1b}
25 span.Apple-tab-span {white-space:pre}
26 </style>
27 </head>
28 <body>
29 <p class="p1"><b>KeyTrack<span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>key tracker</b></p>
30 <p class="p2"><br></p>
31 <p class="p3"><b>key = KeyTrack.kr(chain, keydecay=2.0, chromadecay= 1.0)</b></p>
32 <p class="p2"><br></p>
33 <p class="p3"><b>chain- [fft] </b>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. <span class="Apple-converted-space"> </span></p>
34 <p class="p2"><br></p>
35 <p class="p4">//With standard hop of half FFT size = 2048 samples</p>
36 <p class="p4"><span class="s1">b = </span><span class="s2">Buffer</span><span class="s1">.alloc(s,4096,1); </span>//for sampling rates 44100 and 48000</p>
37 <p class="p4">//b = Buffer.alloc(s,8192,1); //for sampling rates 88200 and 96000</p>
38 <p class="p2"><br></p>
39 <p class="p3"><b>keydecay- [sk] </b>Number of seconds for the influence of a window on the final key decision to decay by 40dB (to 0.01 its original value)</p>
40 <p class="p3"><b>chromaleak- [sk] </b>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.<span class="Apple-converted-space"> </span></p>
41 <p class="p2"><br></p>
42 <p class="p2"><br></p>
43 <p class="p3">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<span class="Apple-converted-space">  </span>C major to B major, 12-23 C minor to B minor</p>
44 <p class="p5"><br></p>
45 <p class="p5"><br></p>
46 <p class="p5"><br></p>
47 <p class="p5"><br></p>
48 <p class="p5"><br></p>
49 <p class="p4">//The following files are test materials on my machine; you will subsitute your own filenames here</p>
50 <p class="p4">//A major</p>
51 <p class="p6"><span class="s1">d=</span><span class="s2">Buffer</span><span class="s1">.read(s,</span>"/Volumes/data/stevebeattrack/samples/100.wav"<span class="s1">);</span></p>
52 <p class="p4">//F major; hard to track!</p>
53 <p class="p6"><span class="s1">d=</span><span class="s2">Buffer</span><span class="s1">.read(s,</span>"/Volumes/data/stevebeattrack/samples/115.wav"<span class="s1">);</span></p>
54 <p class="p5"><br></p>
55 <p class="p4">//straight forward since no transients; training set from MIREX2006<span class="Apple-converted-space"> </span></p>
56 <p class="p4">//01= A major</p>
57 <p class="p4">//57 = b minor</p>
58 <p class="p4">//78 e minor</p>
59 <p class="p4">//08 Bb major</p>
60 <p class="p6"><span class="s1">d=</span><span class="s2">Buffer</span><span class="s1">.read(s,</span>"/Users/nickcollins/Desktop/ML/training_wav/78.wav"<span class="s1">)</span></p>
61 <p class="p5"><br></p>
62 <p class="p5"><br></p>
63 <p class="p7"><span class="s1">b = </span><span class="s3">Buffer</span><span class="s1">.alloc(s,4096,1); </span>//for sampling rates 44100 and 48000</p>
64 <p class="p5"><br></p>
65 <p class="p5"><br></p>
66 <p class="p8">(</p>
67 <p class="p8">{</p>
68 <p class="p5"><br></p>
69 <p class="p8"><span class="s4">var</span> in, fft, resample;<span class="Apple-converted-space"> </span></p>
70 <p class="p8"><span class="s4">var</span> key, transientdetection;</p>
71 <p class="p5"><br></p>
72 <p class="p8">in= <span class="s4">PlayBuf</span>.ar(1,d,<span class="s4">BufRateScale</span>.kr(d),1,0,1);</p>
73 <p class="p5"><br></p>
74 <p class="p8">fft = <span class="s4">FFT</span>(b, in);</p>
75 <p class="p5"><br></p>
76 <p class="p8">key=<span class="s4">KeyTrack</span>.kr(fft, 2.0, 0.5);</p>
77 <p class="p5"><br></p>
78 <p class="p8">key.poll;<span class="Apple-converted-space"> </span></p>
79 <p class="p5"><br></p>
80 <p class="p8"><span class="s4">Out</span>.ar(0,<span class="s4">Pan2</span>.ar(in));<span class="Apple-converted-space"> </span></p>
81 <p class="p8">}.play</p>
82 <p class="p8">)</p>
83 <p class="p5"><br></p>
84 <p class="p5"><br></p>
85 <p class="p5"><br></p>
86 <p class="p5"><br></p>
87 <p class="p4">//alternating major and minor chords as a test</p>
88 <p class="p8">(</p>
89 <p class="p8">{</p>
90 <p class="p5"><br></p>
91 <p class="p8"><span class="s2">var</span> in, fft, resample;<span class="Apple-converted-space"> </span></p>
92 <p class="p8"><span class="s2">var</span> key, transientdetection;</p>
93 <p class="p5"><br></p>
94 <p class="p8">in= <span class="s2">Mix</span>(<span class="s2">SinOsc</span>.ar((60+[0,<span class="s2">MouseX</span>.kr(3,4).round(1),7]).midicps,0,0.1));<span class="Apple-converted-space"> </span></p>
95 <p class="p5"><br></p>
96 <p class="p4">//major dom 7 and minor 7; major keys preferred here</p>
97 <p class="p4">//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));<span class="Apple-converted-space"> </span></p>
98 <p class="p5"><br></p>
99 <p class="p8">fft = <span class="s2">FFT</span>(b, in);</p>
100 <p class="p5"><br></p>
101 <p class="p8">key=<span class="s2">KeyTrack</span>.kr(fft);</p>
102 <p class="p5"><br></p>
103 <p class="p8">key.poll;<span class="Apple-converted-space"> </span></p>
104 <p class="p5"><br></p>
105 <p class="p8"><span class="s2">Out</span>.ar(0,<span class="s2">Pan2</span>.ar(in));<span class="Apple-converted-space"> </span></p>
106 <p class="p8">}.play</p>
107 <p class="p8">)</p>
108 <p class="p5"><br></p>
109 <p class="p5"><br></p>
110 <p class="p5"><br></p>
111 <p class="p5"><br></p>
112 <p class="p5"><br></p>
113 <p class="p5"><br></p>
114 <p class="p5"><br></p>
115 <p class="p9">//Nice to hear what KeyTrack thinks:</p>
116 <p class="p5"><br></p>
117 <p class="p6"><span class="s1">d=</span><span class="s5">Buffer</span><span class="s1">.read(s,</span>"/Users/nickcollins/Desktop/ML/training_wav/78.wav"<span class="s1">)</span></p>
118 <p class="p9"><span class="s1">b = </span><span class="s5">Buffer</span><span class="s1">.alloc(s,4096,1); </span>//for sampling rates 44100 and 48000</p>
119 <p class="p5"><br></p>
120 <p class="p8">(</p>
121 <p class="p8">{</p>
122 <p class="p5"><br></p>
123 <p class="p8"><span class="s5">var</span> in, fft, resample, chord, rootnote, sympath;</p>
124 <p class="p8"><span class="s5">var</span> key, transientdetection;</p>
125 <p class="p5"><br></p>
126 <p class="p8">in= <span class="s5">PlayBuf</span>.ar(1,d,<span class="s5">BufRateScale</span>.kr(d),1,0,1);</p>
127 <p class="p5"><br></p>
128 <p class="p8">fft = <span class="s5">FFT</span>(b, in);</p>
129 <p class="p5"><br></p>
130 <p class="p8">key=<span class="s5">KeyTrack</span>.kr(fft, 2.0, 0.5);</p>
131 <p class="p8">key.poll;</p>
132 <p class="p9"><span class="s1">key = </span><span class="s5">Median</span><span class="s1">.kr(101, key); </span>// Remove outlier wibbles</p>
133 <p class="p5"><br></p>
134 <p class="p8">chord = if(key&lt;12, #[0,4,7], #[0,3,7]);</p>
135 <p class="p8">rootnote = if(key&lt;12, key, key-12) + 60;</p>
136 <p class="p5"><br></p>
137 <p class="p8">sympath = <span class="s5">SinOsc</span>.ar((rootnote + chord).midicps, 0, 0.4).mean;</p>
138 <p class="p5"><br></p>
139 <p class="p8"><span class="s5">Out</span>.ar(0,<span class="s5">Pan2</span>.ar(in, -0.5) + <span class="s5">Pan2</span>.ar(sympath, 0.5));</p>
140 <p class="p8">}.play</p>
141 <p class="p8">)</p>
142 <p class="p5"><br></p>
143 <p class="p5"><br></p>
144 <p class="p5"><br></p>
145 <p class="p5"><br></p>
146 <p class="p5"><br></p>
147 <p class="p5"><br></p>
148 <p class="p5"><br></p>
149 <p class="p5"><br></p>
150 <p class="p5"><br></p>
151 <p class="p5"><br></p>
152 <p class="p4">//Research Notes:<span class="Apple-converted-space"> </span></p>
153 <p class="p4">//See the MIREX2006 audio key tracking competition and Emilia Gomez's PhD thesis, Tonal Description of Music Audio Signals</p>
154 <p class="p5"><br></p>
155 <p class="p4">//The following code was used to create the datasets for the UGen, and would be the basis of extensions<span class="Apple-converted-space"> </span></p>
156 <p class="p5"><br></p>
157 <p class="p4">//Need one set of bin data for 44100 and one for 48000</p>
158 <p class="p5"><br></p>
159 <p class="p4">//KeyTrack calculations, need to make arrays of FFT bins and weights for each chromatic tone.<span class="Apple-converted-space"> </span></p>
160 <p class="p4">//greater resolution, 4096 FFT, avoid lower octaves, too messy there</p>
161 <p class="p4">//60*6*2 output arrays</p>
162 <p class="p5"><br></p>
163 <p class="p8">(</p>
164 <p class="p8"><span class="s2">var</span> fftN, fftBins, binsize;</p>
165 <p class="p8"><span class="s2">var</span> midinotes;</p>
166 <p class="p8"><span class="s2">var</span> sr;</p>
167 <p class="p8"><span class="s2">var</span> wtlist, binlist;</p>
168 <p class="p5"><br></p>
169 <p class="p8">sr = 48000; <span class="s6">//44100;</span></p>
170 <p class="p5"><br></p>
171 <p class="p8">fftN=4096;<span class="Apple-converted-space"> </span></p>
172 <p class="p8">fftBins=fftN.div(2);</p>
173 <p class="p8">binsize=sr/fftN;</p>
174 <p class="p5"><br></p>
175 <p class="p4"><span class="s1">midinotes= (33..92); </span>//60 notes, 55 Hz up to 1661.2187903198 Hz</p>
176 <p class="p5"><br></p>
177 <p class="p8">wtlist=<span class="s2">List</span>[];<span class="Apple-converted-space"> </span></p>
178 <p class="p8">binlist=<span class="s2">List</span>[];<span class="Apple-converted-space"> </span></p>
179 <p class="p5"><span class="Apple-tab-span"> </span></p>
180 <p class="p4">//for each note have six harmonic locations<span class="Apple-tab-span"> </span></p>
181 <p class="p8">midinotes.do{<span class="s2">|note|</span></p>
182 <p class="p8"><span class="Apple-tab-span"> </span><span class="s2">var</span> freq, whichbin, lowerbin, prop;<span class="Apple-converted-space"> </span></p>
183 <p class="p5"><br></p>
184 <p class="p8"><span class="Apple-tab-span"> </span>freq= note.midicps;<span class="Apple-converted-space"> </span></p>
185 <p class="p5"><span class="Apple-tab-span"> </span></p>
186 <p class="p8"><span class="Apple-tab-span"> </span>6.do{<span class="s2">|j|</span></p>
187 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">var</span> partialfreq, partialamp;</p>
188 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
189 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>partialamp= 1.0/(j+1);</p>
190 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>partialfreq= freq*(j+1);<span class="Apple-converted-space"> </span></p>
191 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
192 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>whichbin= partialfreq/binsize;<span class="Apple-converted-space"> </span></p>
193 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>lowerbin= whichbin.asInteger;<span class="Apple-converted-space"> </span></p>
194 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>prop= 1.0-(whichbin-lowerbin);</p>
195 <p class="p5"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></p>
196 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>binlist.add(lowerbin).add(lowerbin+1);</p>
197 <p class="p8"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>wtlist.add(prop*partialamp).add((1.0-prop)*partialamp);</p>
198 <p class="p5"><span class="Apple-tab-span"> </span></p>
199 <p class="p8"><span class="Apple-tab-span"> </span>};</p>
200 <p class="p5"><br></p>
201 <p class="p8">};</p>
202 <p class="p5"><br></p>
203 <p class="p5"><br></p>
204 <p class="p8"><span class="s2">Post</span> &lt;&lt; (binlist) &lt;&lt; nl&lt;&lt; nl;</p>
205 <p class="p5"><br></p>
206 <p class="p8"><span class="s2">Post</span> &lt;&lt; (wtlist) &lt;&lt; nl&lt;&lt; nl;</p>
207 <p class="p5"><br></p>
208 <p class="p8">binlist.size.postln;</p>
209 <p class="p8">wtlist.size.postln;</p>
210 <p class="p8">)</p>
211 <p class="p5"><br></p>
212 </body>
213 </html>