Mercurial > hg > qm-dsp
comparison dsp/tempotracking/DownBeat.cpp @ 505:930b5b0f707d
Merge branch 'codestyle-and-tidy'
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Wed, 05 Jun 2019 12:55:15 +0100 |
parents | 12b5a9244bb0 |
children |
comparison
equal
deleted
inserted
replaced
471:e3335cb213da | 505:930b5b0f707d |
---|---|
20 #include "maths/KLDivergence.h" | 20 #include "maths/KLDivergence.h" |
21 #include "dsp/transforms/FFT.h" | 21 #include "dsp/transforms/FFT.h" |
22 | 22 |
23 #include <iostream> | 23 #include <iostream> |
24 #include <cstdlib> | 24 #include <cstdlib> |
25 | |
26 using std::vector; | |
25 | 27 |
26 DownBeat::DownBeat(float originalSampleRate, | 28 DownBeat::DownBeat(float originalSampleRate, |
27 size_t decimationFactor, | 29 size_t decimationFactor, |
28 size_t dfIncrement) : | 30 size_t dfIncrement) : |
29 m_bpb(0), | 31 m_bpb(0), |
45 m_beatframesize = MathUtilities::nextPowerOfTwo | 47 m_beatframesize = MathUtilities::nextPowerOfTwo |
46 (int((m_rate / decimationFactor) * 1.3)); | 48 (int((m_rate / decimationFactor) * 1.3)); |
47 if (m_beatframesize < 2) { | 49 if (m_beatframesize < 2) { |
48 m_beatframesize = 2; | 50 m_beatframesize = 2; |
49 } | 51 } |
50 // std::cerr << "rate = " << m_rate << ", dec = " << decimationFactor << ", bfs = " << m_beatframesize << std::endl; | |
51 m_beatframe = new double[m_beatframesize]; | 52 m_beatframe = new double[m_beatframesize]; |
52 m_fftRealOut = new double[m_beatframesize]; | 53 m_fftRealOut = new double[m_beatframesize]; |
53 m_fftImagOut = new double[m_beatframesize]; | 54 m_fftImagOut = new double[m_beatframesize]; |
54 m_fft = new FFTReal(m_beatframesize); | 55 m_fft = new FFTReal(m_beatframesize); |
55 } | 56 } |
73 } | 74 } |
74 | 75 |
75 void | 76 void |
76 DownBeat::makeDecimators() | 77 DownBeat::makeDecimators() |
77 { | 78 { |
78 // std::cerr << "m_factor = " << m_factor << std::endl; | |
79 if (m_factor < 2) return; | 79 if (m_factor < 2) return; |
80 size_t highest = Decimator::getHighestSupportedFactor(); | 80 size_t highest = Decimator::getHighestSupportedFactor(); |
81 if (m_factor <= highest) { | 81 if (m_factor <= highest) { |
82 m_decimator1 = new Decimator(m_increment, m_factor); | 82 m_decimator1 = new Decimator(m_increment, m_factor); |
83 // std::cerr << "DownBeat: decimator 1 factor " << m_factor << ", size " << m_increment << std::endl; | |
84 return; | 83 return; |
85 } | 84 } |
86 m_decimator1 = new Decimator(m_increment, highest); | 85 m_decimator1 = new Decimator(m_increment, highest); |
87 // std::cerr << "DownBeat: decimator 1 factor " << highest << ", size " << m_increment << std::endl; | |
88 m_decimator2 = new Decimator(m_increment / highest, m_factor / highest); | 86 m_decimator2 = new Decimator(m_increment / highest, m_factor / highest); |
89 // std::cerr << "DownBeat: decimator 2 factor " << m_factor / highest << ", size " << m_increment / highest << std::endl; | |
90 m_decbuf = new float[m_increment / highest]; | 87 m_decbuf = new float[m_increment / highest]; |
91 } | 88 } |
92 | 89 |
93 void | 90 void |
94 DownBeat::pushAudioBlock(const float *audio) | 91 DownBeat::pushAudioBlock(const float *audio) |
97 if (m_bufsiz == 0) m_bufsiz = m_increment * 16; | 94 if (m_bufsiz == 0) m_bufsiz = m_increment * 16; |
98 else m_bufsiz = m_bufsiz * 2; | 95 else m_bufsiz = m_bufsiz * 2; |
99 if (!m_buffer) { | 96 if (!m_buffer) { |
100 m_buffer = (float *)malloc(m_bufsiz * sizeof(float)); | 97 m_buffer = (float *)malloc(m_bufsiz * sizeof(float)); |
101 } else { | 98 } else { |
102 // std::cerr << "DownBeat::pushAudioBlock: realloc m_buffer to " << m_bufsiz << std::endl; | |
103 m_buffer = (float *)realloc(m_buffer, m_bufsiz * sizeof(float)); | 99 m_buffer = (float *)realloc(m_buffer, m_bufsiz * sizeof(float)); |
104 } | 100 } |
105 } | 101 } |
106 if (!m_decimator1 && m_factor > 1) makeDecimators(); | 102 if (!m_decimator1 && m_factor > 1) { |
107 // float rmsin = 0, rmsout = 0; | 103 makeDecimators(); |
108 // for (int i = 0; i < m_increment; ++i) { | 104 } |
109 // rmsin += audio[i] * audio[i]; | |
110 // } | |
111 if (m_decimator2) { | 105 if (m_decimator2) { |
112 m_decimator1->process(audio, m_decbuf); | 106 m_decimator1->process(audio, m_decbuf); |
113 m_decimator2->process(m_decbuf, m_buffer + m_buffill); | 107 m_decimator2->process(m_decbuf, m_buffer + m_buffill); |
114 } else if (m_decimator1) { | 108 } else if (m_decimator1) { |
115 m_decimator1->process(audio, m_buffer + m_buffill); | 109 m_decimator1->process(audio, m_buffer + m_buffill); |
117 // just copy across (m_factor is presumably 1) | 111 // just copy across (m_factor is presumably 1) |
118 for (size_t i = 0; i < m_increment; ++i) { | 112 for (size_t i = 0; i < m_increment; ++i) { |
119 (m_buffer + m_buffill)[i] = audio[i]; | 113 (m_buffer + m_buffill)[i] = audio[i]; |
120 } | 114 } |
121 } | 115 } |
122 // for (int i = 0; i < m_increment / m_factor; ++i) { | |
123 // rmsout += m_buffer[m_buffill + i] * m_buffer[m_buffill + i]; | |
124 // } | |
125 // std::cerr << "pushAudioBlock: rms in " << sqrt(rmsin) << ", out " << sqrt(rmsout) << std::endl; | |
126 m_buffill += m_increment / m_factor; | 116 m_buffill += m_increment / m_factor; |
127 } | 117 } |
128 | 118 |
129 const float * | 119 const float * |
130 DownBeat::getBufferedAudio(size_t &length) const | 120 DownBeat::getBufferedAudio(size_t &length) const |
134 } | 124 } |
135 | 125 |
136 void | 126 void |
137 DownBeat::resetAudioBuffer() | 127 DownBeat::resetAudioBuffer() |
138 { | 128 { |
139 if (m_buffer) free(m_buffer); | 129 if (m_buffer) { |
130 free(m_buffer); | |
131 } | |
140 m_buffer = 0; | 132 m_buffer = 0; |
141 m_buffill = 0; | 133 m_buffill = 0; |
142 m_bufsiz = 0; | 134 m_bufsiz = 0; |
143 } | 135 } |
144 | 136 |
177 // Also apply a Hanning window to the beat frame buffer, sized | 169 // Also apply a Hanning window to the beat frame buffer, sized |
178 // to the beat extents rather than the frame size. (Because | 170 // to the beat extents rather than the frame size. (Because |
179 // the size varies, it's easier to do this by hand than use | 171 // the size varies, it's easier to do this by hand than use |
180 // our Window abstraction.) | 172 // our Window abstraction.) |
181 | 173 |
182 // std::cerr << "beatlen = " << beatlen << std::endl; | |
183 | |
184 // float rms = 0; | |
185 for (size_t j = 0; j < beatlen && j < m_beatframesize; ++j) { | 174 for (size_t j = 0; j < beatlen && j < m_beatframesize; ++j) { |
186 double mul = 0.5 * (1.0 - cos(TWO_PI * (double(j) / double(beatlen)))); | 175 double mul = 0.5 * (1.0 - cos(TWO_PI * (double(j) / double(beatlen)))); |
187 m_beatframe[j] = audio[beatstart + j] * mul; | 176 m_beatframe[j] = audio[beatstart + j] * mul; |
188 // rms += m_beatframe[j] * m_beatframe[j]; | 177 } |
189 } | |
190 // rms = sqrt(rms); | |
191 // std::cerr << "beat " << i << ": audio rms " << rms << std::endl; | |
192 | 178 |
193 for (size_t j = beatlen; j < m_beatframesize; ++j) { | 179 for (size_t j = beatlen; j < m_beatframesize; ++j) { |
194 m_beatframe[j] = 0.0; | 180 m_beatframe[j] = 0.0; |
195 } | 181 } |
196 | 182 |
211 | 197 |
212 // Calculate JS divergence between new and old spectral frames | 198 // Calculate JS divergence between new and old spectral frames |
213 | 199 |
214 if (i > 0) { // otherwise we have no previous frame | 200 if (i > 0) { // otherwise we have no previous frame |
215 m_beatsd.push_back(measureSpecDiff(oldspec, newspec)); | 201 m_beatsd.push_back(measureSpecDiff(oldspec, newspec)); |
216 // std::cerr << "specdiff: " << m_beatsd[m_beatsd.size()-1] << std::endl; | |
217 } | 202 } |
218 | 203 |
219 // Copy newspec across to old | 204 // Copy newspec across to old |
220 | 205 |
221 for (size_t j = 0; j < m_beatframesize/2; ++j) { | 206 for (size_t j = 0; j < m_beatframesize/2; ++j) { |
240 for (int example = beat-1; example < (int)m_beatsd.size(); example += timesig) { | 225 for (int example = beat-1; example < (int)m_beatsd.size(); example += timesig) { |
241 if (example < 0) continue; | 226 if (example < 0) continue; |
242 dbcand[beat] += (m_beatsd[example]) / timesig; | 227 dbcand[beat] += (m_beatsd[example]) / timesig; |
243 ++count; | 228 ++count; |
244 } | 229 } |
245 if (count > 0) dbcand[beat] /= count; | 230 if (count > 0) { |
246 // std::cerr << "dbcand[" << beat << "] = " << dbcand[beat] << std::endl; | 231 dbcand[beat] /= count; |
232 } | |
247 } | 233 } |
248 | 234 |
249 // first downbeat is beat at index of maximum value of dbcand | 235 // first downbeat is beat at index of maximum value of dbcand |
250 int dbind = MathUtilities::getMax(dbcand); | 236 int dbind = MathUtilities::getMax(dbcand); |
251 | 237 |
258 double | 244 double |
259 DownBeat::measureSpecDiff(d_vec_t oldspec, d_vec_t newspec) | 245 DownBeat::measureSpecDiff(d_vec_t oldspec, d_vec_t newspec) |
260 { | 246 { |
261 // JENSEN-SHANNON DIVERGENCE BETWEEN SPECTRAL FRAMES | 247 // JENSEN-SHANNON DIVERGENCE BETWEEN SPECTRAL FRAMES |
262 | 248 |
263 unsigned int SPECSIZE = 512; // ONLY LOOK AT FIRST 512 SAMPLES OF SPECTRUM. | 249 int SPECSIZE = 512; // ONLY LOOK AT FIRST 512 SAMPLES OF SPECTRUM. |
264 if (SPECSIZE > oldspec.size()/4) { | 250 if (SPECSIZE > int(oldspec.size())/4) { |
265 SPECSIZE = oldspec.size()/4; | 251 SPECSIZE = int(oldspec.size())/4; |
266 } | 252 } |
267 double SD = 0.; | 253 double SD = 0.; |
268 double sd1 = 0.; | 254 double sd1 = 0.; |
269 | 255 |
270 double sumnew = 0.; | 256 double sumnew = 0.; |
271 double sumold = 0.; | 257 double sumold = 0.; |
272 | 258 |
273 for (unsigned int i = 0;i < SPECSIZE;i++) | 259 for (int i = 0;i < SPECSIZE;i++) { |
274 { | 260 |
275 newspec[i] +=EPS; | 261 newspec[i] +=EPS; |
276 oldspec[i] +=EPS; | 262 oldspec[i] +=EPS; |
277 | 263 |
278 sumnew+=newspec[i]; | 264 sumnew+=newspec[i]; |
279 sumold+=oldspec[i]; | 265 sumold+=oldspec[i]; |
280 } | 266 } |
281 | 267 |
282 for (unsigned int i = 0;i < SPECSIZE;i++) | 268 for (int i = 0;i < SPECSIZE;i++) { |
283 { | 269 |
284 newspec[i] /= (sumnew); | 270 newspec[i] /= (sumnew); |
285 oldspec[i] /= (sumold); | 271 oldspec[i] /= (sumold); |
286 | 272 |
287 // IF ANY SPECTRAL VALUES ARE 0 (SHOULDN'T BE ANY!) SET THEM TO 1 | 273 // IF ANY SPECTRAL VALUES ARE 0 (SHOULDN'T BE ANY!) SET THEM TO 1 |
288 if (newspec[i] == 0) | 274 if (newspec[i] == 0) { |
289 { | |
290 newspec[i] = 1.; | 275 newspec[i] = 1.; |
291 } | 276 } |
292 | 277 |
293 if (oldspec[i] == 0) | 278 if (oldspec[i] == 0) { |
294 { | |
295 oldspec[i] = 1.; | 279 oldspec[i] = 1.; |
296 } | 280 } |
297 | 281 |
298 // JENSEN-SHANNON CALCULATION | 282 // JENSEN-SHANNON CALCULATION |
299 sd1 = 0.5*oldspec[i] + 0.5*newspec[i]; | 283 sd1 = 0.5*oldspec[i] + 0.5*newspec[i]; |
300 SD = SD + (-sd1*log(sd1)) + (0.5*(oldspec[i]*log(oldspec[i]))) + (0.5*(newspec[i]*log(newspec[i]))); | 284 SD = SD + (-sd1*log(sd1)) + |
285 (0.5*(oldspec[i]*log(oldspec[i]))) + | |
286 (0.5*(newspec[i]*log(newspec[i]))); | |
301 } | 287 } |
302 | 288 |
303 return SD; | 289 return SD; |
304 } | 290 } |
305 | 291 |
306 void | 292 void |
307 DownBeat::getBeatSD(vector<double> &beatsd) const | 293 DownBeat::getBeatSD(vector<double> &beatsd) const |
308 { | 294 { |
309 for (int i = 0; i < (int)m_beatsd.size(); ++i) beatsd.push_back(m_beatsd[i]); | 295 for (int i = 0; i < (int)m_beatsd.size(); ++i) { |
310 } | 296 beatsd.push_back(m_beatsd[i]); |
311 | 297 } |
298 } | |
299 |