comparison dsp/tempotracking/DownBeat.cpp @ 479:7e52c034cf62

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