annotate src/CQInverse.cpp @ 196:da283326bcd3 tip master

Update plugin versions in RDF
author Chris Cannam <cannam@all-day-breakfast.com>
date Fri, 28 Feb 2020 09:43:02 +0000
parents 1081c73fbbe3
children
rev   line source
c@116 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@116 2 /*
c@116 3 Constant-Q library
c@116 4 Copyright (c) 2013-2014 Queen Mary, University of London
c@116 5
c@116 6 Permission is hereby granted, free of charge, to any person
c@116 7 obtaining a copy of this software and associated documentation
c@116 8 files (the "Software"), to deal in the Software without
c@116 9 restriction, including without limitation the rights to use, copy,
c@116 10 modify, merge, publish, distribute, sublicense, and/or sell copies
c@116 11 of the Software, and to permit persons to whom the Software is
c@116 12 furnished to do so, subject to the following conditions:
c@116 13
c@116 14 The above copyright notice and this permission notice shall be
c@116 15 included in all copies or substantial portions of the Software.
c@116 16
c@116 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@116 18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@116 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@116 20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
c@116 21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@116 22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@116 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@116 24
c@116 25 Except as contained in this notice, the names of the Centre for
c@116 26 Digital Music; Queen Mary, University of London; and Chris Cannam
c@116 27 shall not be used in advertising or otherwise to promote the sale,
c@116 28 use or other dealings in this Software without prior written
c@116 29 authorization.
c@116 30 */
c@116 31
c@116 32 #include "CQInverse.h"
c@116 33
c@121 34 #include "dsp/Resampler.h"
c@121 35 #include "dsp/MathUtilities.h"
c@121 36 #include "dsp/FFT.h"
c@116 37
c@116 38 #include <algorithm>
c@116 39 #include <iostream>
c@116 40 #include <stdexcept>
c@116 41
c@164 42 #include <cmath>
c@164 43
c@116 44 using std::vector;
c@116 45 using std::cerr;
c@116 46 using std::endl;
c@116 47
c@116 48 //#define DEBUG_CQ 1
c@116 49
c@127 50 CQInverse::CQInverse(CQParameters params) :
c@127 51 m_inparams(params),
c@127 52 m_sampleRate(params.sampleRate),
c@127 53 m_maxFrequency(params.maxFrequency),
c@127 54 m_minFrequency(params.minFrequency),
c@127 55 m_binsPerOctave(params.binsPerOctave),
c@116 56 m_fft(0)
c@116 57 {
c@127 58 if (m_minFrequency <= 0.0 || m_maxFrequency <= 0.0) {
c@116 59 throw std::invalid_argument("Frequency extents must be positive");
c@116 60 }
c@116 61
c@116 62 initialise();
c@116 63 }
c@116 64
c@116 65 CQInverse::~CQInverse()
c@116 66 {
c@116 67 delete m_fft;
c@116 68 for (int i = 0; i < (int)m_upsamplers.size(); ++i) {
c@116 69 delete m_upsamplers[i];
c@116 70 }
c@116 71 delete m_kernel;
c@116 72 }
c@116 73
c@116 74 double
c@116 75 CQInverse::getMinFrequency() const
c@116 76 {
c@116 77 return m_p.minFrequency / pow(2.0, m_octaves - 1);
c@116 78 }
c@116 79
c@116 80 double
c@145 81 CQInverse::getBinFrequency(double bin) const
c@116 82 {
c@145 83 // our bins are returned in high->low order
c@145 84 bin = (getBinsPerOctave() * getOctaves()) - bin - 1;
c@145 85 return getMinFrequency() * pow(2, (bin / getBinsPerOctave()));
c@116 86 }
c@116 87
c@116 88 void
c@116 89 CQInverse::initialise()
c@116 90 {
c@164 91 m_octaves = int(ceil(log(m_maxFrequency / m_minFrequency) / log(2)));
c@150 92
c@150 93 if (m_octaves < 1) {
c@150 94 m_kernel = 0; // incidentally causing isValid() to return false
c@150 95 return;
c@150 96 }
c@150 97
c@127 98 m_kernel = new CQKernel(m_inparams);
c@116 99 m_p = m_kernel->getProperties();
c@116 100
c@116 101 // Use exact powers of two for resampling rates. They don't have
c@116 102 // to be related to our actual samplerate: the resampler only
c@116 103 // cares about the ratio, but it only accepts integer source and
c@116 104 // target rates, and if we start from the actual samplerate we
c@116 105 // risk getting non-integer rates for lower octaves
c@116 106
c@116 107 int sourceRate = pow(2, m_octaves);
c@116 108 vector<int> latencies;
c@116 109
c@116 110 // top octave, no resampling
c@116 111 latencies.push_back(0);
c@116 112 m_upsamplers.push_back(0);
c@116 113
c@116 114 for (int i = 1; i < m_octaves; ++i) {
c@116 115
c@116 116 int factor = pow(2, i);
c@116 117
c@116 118 Resampler *r = new Resampler
c@116 119 (sourceRate / factor, sourceRate, 50, 0.05);
c@116 120
c@116 121 #ifdef DEBUG_CQ
c@116 122 cerr << "inverse: octave " << i << ": resample from " << sourceRate/factor << " to " << sourceRate << endl;
c@116 123 #endif
c@116 124
c@116 125 // See ConstantQ.cpp for discussion on latency -- output
c@116 126 // latency here is at target rate which, this way around, is
c@116 127 // what we want
c@116 128
c@116 129 latencies.push_back(r->getLatency());
c@116 130 m_upsamplers.push_back(r);
c@116 131 }
c@116 132
c@116 133 // additionally we will have fftHop latency at individual octave
c@116 134 // rate (before upsampling) for the overlap-add in each octave
c@116 135 for (int i = 0; i < m_octaves; ++i) {
c@116 136 latencies[i] += m_p.fftHop * pow(2, i);
c@116 137 }
c@116 138
c@116 139 // Now reverse the drop adjustment made in ConstantQ to align the
c@116 140 // atom centres across different octaves (but this time at output
c@116 141 // sample rate)
c@116 142
c@116 143 int emptyHops = m_p.firstCentre / m_p.atomSpacing;
c@116 144
c@116 145 vector<int> pushes;
c@116 146 for (int i = 0; i < m_octaves; ++i) {
c@116 147 int factor = pow(2, i);
c@116 148 int pushHops = emptyHops * pow(2, m_octaves - i - 1) - emptyHops;
c@116 149 int push = ((pushHops * m_p.fftHop) * factor) / m_p.atomsPerFrame;
c@116 150 pushes.push_back(push);
c@116 151 }
c@116 152
c@116 153 int maxLatLessPush = 0;
c@116 154 for (int i = 0; i < m_octaves; ++i) {
c@116 155 int latLessPush = latencies[i] - pushes[i];
c@116 156 if (latLessPush > maxLatLessPush) maxLatLessPush = latLessPush;
c@116 157 }
c@116 158
c@116 159 int totalLatency = maxLatLessPush + 10;
c@116 160 if (totalLatency < 0) totalLatency = 0;
c@116 161
c@116 162 m_outputLatency = totalLatency + m_p.firstCentre * pow(2, m_octaves-1);
c@116 163
c@116 164 #ifdef DEBUG_CQ
c@116 165 cerr << "totalLatency = " << totalLatency << ", m_outputLatency = " << m_outputLatency << endl;
c@116 166 #endif
c@116 167
c@116 168 for (int i = 0; i < m_octaves; ++i) {
c@116 169
c@116 170 // Calculate the difference between the total latency applied
c@116 171 // across all octaves, and the existing latency due to the
c@116 172 // upsampler for this octave.
c@116 173
c@116 174 int latencyPadding = totalLatency - latencies[i] + pushes[i];
c@116 175
c@116 176 #ifdef DEBUG_CQ
c@116 177 cerr << "octave " << i << ": push " << pushes[i] << ", resampler latency inc overlap space " << latencies[i] << ", latencyPadding = " << latencyPadding << " (/factor = " << latencyPadding / pow(2, i) << ")" << endl;
c@116 178 #endif
c@116 179
c@116 180 m_buffers.push_back(RealSequence(latencyPadding, 0.0));
c@116 181 }
c@116 182
c@116 183 for (int i = 0; i < m_octaves; ++i) {
c@116 184 // Fixed-size buffer for IFFT overlap-add
c@116 185 m_olaBufs.push_back(RealSequence(m_p.fftSize, 0.0));
c@116 186 }
c@116 187
c@116 188 m_fft = new FFTReal(m_p.fftSize);
c@116 189 }
c@116 190
c@116 191 CQInverse::RealSequence
c@116 192 CQInverse::process(const ComplexBlock &block)
c@116 193 {
c@116 194 // The input data is of the form produced by ConstantQ::process --
c@116 195 // an unknown number N of columns of varying height. We assert
c@116 196 // that N is a multiple of atomsPerFrame * 2^(octaves-1), as must
c@116 197 // be the case for data that came directly from our ConstantQ
c@116 198 // implementation.
c@116 199
c@116 200 int widthProvided = block.size();
c@116 201
c@116 202 if (widthProvided == 0) {
c@116 203 return drawFromBuffers();
c@116 204 }
c@116 205
c@116 206 int blockWidth = m_p.atomsPerFrame * int(pow(2, m_octaves - 1));
c@116 207
c@116 208 if (widthProvided % blockWidth != 0) {
c@116 209 cerr << "ERROR: CQInverse::process: Input block size ("
c@116 210 << widthProvided
c@116 211 << ") must be a multiple of processing block width "
c@116 212 << "(atoms-per-frame * 2^(octaves-1) = "
c@116 213 << m_p.atomsPerFrame << " * 2^(" << m_octaves << "-1) = "
c@116 214 << blockWidth << ")" << endl;
c@116 215 throw std::invalid_argument
c@116 216 ("Input block size must be a multiple of processing block width");
c@116 217 }
c@116 218
c@116 219 // Procedure:
c@116 220 //
c@116 221 // 1. Slice the list of columns into a set of lists of columns,
c@116 222 // one per octave, each of width N / (2^octave-1) and height
c@116 223 // binsPerOctave, containing the values present in that octave
c@116 224 //
c@116 225 // 2. Group each octave list by atomsPerFrame columns at a time,
c@116 226 // and stack these so as to achieve a list, for each octave, of
c@116 227 // taller columns of height binsPerOctave * atomsPerFrame
c@116 228 //
c@116 229 // 3. For each taller column, take the product with the inverse CQ
c@116 230 // kernel (which is the conjugate of the forward kernel) and
c@116 231 // perform an inverse FFT
c@116 232 //
c@116 233 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
c@116 234 //
c@116 235 // 5. Resample each octave's overlap-add stream to the original
c@116 236 // rate
c@116 237 //
c@116 238 // 6. Sum the resampled streams and return
c@116 239
c@116 240 for (int i = 0; i < m_octaves; ++i) {
c@116 241
c@116 242 // Step 1
c@116 243
c@116 244 ComplexBlock oct;
c@116 245
c@116 246 for (int j = 0; j < widthProvided; ++j) {
c@116 247 int h = block[j].size();
c@116 248 if (h < m_binsPerOctave * (i+1)) {
c@116 249 continue;
c@116 250 }
c@116 251 ComplexColumn col(block[j].begin() + m_binsPerOctave * i,
c@116 252 block[j].begin() + m_binsPerOctave * (i+1));
c@116 253 oct.push_back(col);
c@116 254 }
c@116 255
c@116 256 // Steps 2, 3, 4, 5
c@116 257 processOctave(i, oct);
c@116 258 }
c@116 259
c@116 260 // Step 6
c@116 261 return drawFromBuffers();
c@116 262 }
c@116 263
c@116 264 CQInverse::RealSequence
c@116 265 CQInverse::drawFromBuffers()
c@116 266 {
c@116 267 // 6. Sum the resampled streams and return
c@116 268
c@116 269 int available = 0;
c@116 270
c@116 271 for (int i = 0; i < m_octaves; ++i) {
c@116 272 if (i == 0 || int(m_buffers[i].size()) < available) {
c@116 273 available = m_buffers[i].size();
c@116 274 }
c@116 275 }
c@116 276
c@116 277 RealSequence result(available, 0);
c@116 278
c@116 279 if (available == 0) {
c@116 280 return result;
c@116 281 }
c@116 282
c@116 283 for (int i = 0; i < m_octaves; ++i) {
c@116 284 for (int j = 0; j < available; ++j) {
c@116 285 result[j] += m_buffers[i][j];
c@116 286 }
c@116 287 m_buffers[i] = RealSequence(m_buffers[i].begin() + available,
c@116 288 m_buffers[i].end());
c@116 289 }
c@116 290
c@116 291 return result;
c@116 292 }
c@116 293
c@116 294 CQInverse::RealSequence
c@116 295 CQInverse::getRemainingOutput()
c@116 296 {
c@116 297 for (int j = 0; j < m_octaves; ++j) {
c@116 298 int factor = pow(2, j);
c@116 299 int latency = (j > 0 ? m_upsamplers[j]->getLatency() : 0) / factor;
c@116 300 for (int i = 0; i < (latency + m_p.fftSize) / m_p.fftHop; ++i) {
c@116 301 overlapAddAndResample(j, RealSequence(m_olaBufs[j].size(), 0));
c@116 302 }
c@116 303 }
c@116 304
c@116 305 return drawFromBuffers();
c@116 306 }
c@116 307
c@116 308 void
c@116 309 CQInverse::processOctave(int octave, const ComplexBlock &columns)
c@116 310 {
c@116 311 // 2. Group each octave list by atomsPerFrame columns at a time,
c@116 312 // and stack these so as to achieve a list, for each octave, of
c@116 313 // taller columns of height binsPerOctave * atomsPerFrame
c@116 314
c@116 315 int ncols = columns.size();
c@116 316
c@116 317 if (ncols % m_p.atomsPerFrame != 0) {
c@116 318 cerr << "ERROR: CQInverse::process: Number of columns ("
c@116 319 << ncols
c@116 320 << ") in octave " << octave
c@116 321 << " must be a multiple of atoms-per-frame ("
c@116 322 << m_p.atomsPerFrame << ")" << endl;
c@116 323 throw std::invalid_argument
c@116 324 ("Columns in octave must be a multiple of atoms per frame");
c@116 325 }
c@116 326
c@116 327 for (int i = 0; i < ncols; i += m_p.atomsPerFrame) {
c@116 328
c@116 329 ComplexColumn tallcol;
c@116 330 for (int b = 0; b < m_binsPerOctave; ++b) {
c@116 331 for (int a = 0; a < m_p.atomsPerFrame; ++a) {
c@116 332 tallcol.push_back(columns[i + a][m_binsPerOctave - b - 1]);
c@116 333 }
c@116 334 }
c@116 335
c@116 336 processOctaveColumn(octave, tallcol);
c@116 337 }
c@116 338 }
c@116 339
c@116 340 void
c@116 341 CQInverse::processOctaveColumn(int octave, const ComplexColumn &column)
c@116 342 {
c@116 343 // 3. For each taller column, take the product with the inverse CQ
c@116 344 // kernel (which is the conjugate of the forward kernel) and
c@116 345 // perform an inverse FFT
c@116 346
c@116 347 if ((int)column.size() != m_p.atomsPerFrame * m_binsPerOctave) {
c@116 348 cerr << "ERROR: CQInverse::processOctaveColumn: Height of column ("
c@116 349 << column.size() << ") in octave " << octave
c@116 350 << " must be atoms-per-frame * bins-per-octave ("
c@116 351 << m_p.atomsPerFrame << " * " << m_binsPerOctave << " = "
c@116 352 << m_p.atomsPerFrame * m_binsPerOctave << ")" << endl;
c@116 353 throw std::invalid_argument
c@116 354 ("Column height must match atoms-per-frame * bins-per-octave");
c@116 355 }
c@116 356
c@116 357 ComplexSequence transformed = m_kernel->processInverse(column);
c@116 358
c@116 359 int halfLen = m_p.fftSize/2 + 1;
c@116 360
c@116 361 RealSequence ri(halfLen, 0);
c@116 362 RealSequence ii(halfLen, 0);
c@116 363
c@116 364 for (int i = 0; i < halfLen; ++i) {
c@116 365 ri[i] = transformed[i].real();
c@116 366 ii[i] = transformed[i].imag();
c@116 367 }
c@116 368
c@116 369 RealSequence timeDomain(m_p.fftSize, 0);
c@116 370
c@116 371 m_fft->inverse(ri.data(), ii.data(), timeDomain.data());
c@116 372
c@116 373 overlapAddAndResample(octave, timeDomain);
c@116 374 }
c@116 375
c@116 376 void
c@116 377 CQInverse::overlapAddAndResample(int octave, const RealSequence &seq)
c@116 378 {
c@116 379 // 4. Overlap-add each octave's resynthesised blocks (unwindowed)
c@116 380 //
c@116 381 // and
c@116 382 //
c@116 383 // 5. Resample each octave's overlap-add stream to the original
c@116 384 // rate
c@116 385
c@116 386 if (seq.size() != m_olaBufs[octave].size()) {
c@116 387 cerr << "ERROR: CQInverse::overlapAdd: input sequence length ("
c@116 388 << seq.size() << ") is expected to match OLA buffer size ("
c@116 389 << m_olaBufs[octave].size() << ")" << endl;
c@116 390 throw std::invalid_argument
c@116 391 ("Input sequence length should match OLA buffer size");
c@116 392 }
c@116 393
c@116 394 RealSequence toResample(m_olaBufs[octave].begin(),
c@116 395 m_olaBufs[octave].begin() + m_p.fftHop);
c@116 396
c@116 397 RealSequence resampled =
c@116 398 octave > 0 ?
c@116 399 m_upsamplers[octave]->process(toResample.data(), toResample.size()) :
c@116 400 toResample;
c@116 401
c@116 402 m_buffers[octave].insert(m_buffers[octave].end(),
c@116 403 resampled.begin(),
c@116 404 resampled.end());
c@116 405
c@116 406 m_olaBufs[octave] = RealSequence(m_olaBufs[octave].begin() + m_p.fftHop,
c@116 407 m_olaBufs[octave].end());
c@116 408
c@116 409 RealSequence pad(m_p.fftHop, 0);
c@116 410
c@116 411 m_olaBufs[octave].insert(m_olaBufs[octave].end(),
c@116 412 pad.begin(),
c@116 413 pad.end());
c@116 414
c@116 415 for (int i = 0; i < m_p.fftSize; ++i) {
c@116 416 m_olaBufs[octave][i] += seq[i];
c@116 417 }
c@116 418 }
c@116 419