annotate vamp-sdk/hostext/PluginInputDomainAdapter.cpp @ 65:a712ed15d158

* Remove some (but not all) debug output
author cannam
date Fri, 01 Jun 2007 15:11:37 +0000
parents 9d3272c7db60
children 47d6e670a810
rev   line source
cannam@64 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@64 2
cannam@64 3 /*
cannam@64 4 Vamp
cannam@64 5
cannam@64 6 An API for audio analysis and feature extraction plugins.
cannam@64 7
cannam@64 8 Centre for Digital Music, Queen Mary, University of London.
cannam@64 9 Copyright 2006 Chris Cannam.
cannam@64 10
cannam@64 11 Permission is hereby granted, free of charge, to any person
cannam@64 12 obtaining a copy of this software and associated documentation
cannam@64 13 files (the "Software"), to deal in the Software without
cannam@64 14 restriction, including without limitation the rights to use, copy,
cannam@64 15 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@64 16 of the Software, and to permit persons to whom the Software is
cannam@64 17 furnished to do so, subject to the following conditions:
cannam@64 18
cannam@64 19 The above copyright notice and this permission notice shall be
cannam@64 20 included in all copies or substantial portions of the Software.
cannam@64 21
cannam@64 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@64 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@64 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@64 25 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@64 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@64 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@64 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@64 29
cannam@64 30 Except as contained in this notice, the names of the Centre for
cannam@64 31 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@64 32 shall not be used in advertising or otherwise to promote the sale,
cannam@64 33 use or other dealings in this Software without prior written
cannam@64 34 authorization.
cannam@64 35 */
cannam@64 36
cannam@64 37 #include "PluginInputDomainAdapter.h"
cannam@64 38
cannam@64 39 #include <cmath>
cannam@64 40
cannam@64 41 namespace Vamp {
cannam@64 42
cannam@64 43 namespace HostExt {
cannam@64 44
cannam@64 45 PluginInputDomainAdapter::PluginInputDomainAdapter(Plugin *plugin) :
cannam@64 46 PluginWrapper(plugin),
cannam@64 47 m_channels(0),
cannam@64 48 m_blockSize(0),
cannam@64 49 m_freqbuf(0)
cannam@64 50 {
cannam@64 51 }
cannam@64 52
cannam@64 53 PluginInputDomainAdapter::~PluginInputDomainAdapter()
cannam@64 54 {
cannam@64 55 }
cannam@64 56
cannam@64 57 bool
cannam@64 58 PluginInputDomainAdapter::initialise(size_t channels, size_t stepSize, size_t blockSize)
cannam@64 59 {
cannam@64 60 if (m_plugin->getInputDomain() == TimeDomain) {
cannam@64 61
cannam@64 62 m_blockSize = blockSize;
cannam@64 63 m_channels = channels;
cannam@64 64
cannam@64 65 return m_plugin->initialise(channels, stepSize, blockSize);
cannam@64 66 }
cannam@64 67
cannam@64 68 if (blockSize < 2) {
cannam@64 69 std::cerr << "ERROR: Vamp::HostExt::PluginInputDomainAdapter::initialise: blocksize < 2 not supported" << std::endl;
cannam@64 70 return false;
cannam@64 71 }
cannam@64 72
cannam@64 73 if (blockSize & (blockSize-1)) {
cannam@64 74 std::cerr << "ERROR: Vamp::HostExt::PluginInputDomainAdapter::initialise: non-power-of-two\nblocksize " << blockSize << " not supported" << std::endl;
cannam@64 75 return false;
cannam@64 76 }
cannam@64 77
cannam@64 78 if (m_channels > 0) {
cannam@64 79 for (size_t c = 0; c < m_channels; ++c) {
cannam@64 80 delete[] m_freqbuf[c];
cannam@64 81 }
cannam@64 82 delete[] m_freqbuf;
cannam@64 83 delete[] m_ri;
cannam@64 84 delete[] m_ro;
cannam@64 85 delete[] m_io;
cannam@64 86 }
cannam@64 87
cannam@64 88 m_blockSize = blockSize;
cannam@64 89 m_channels = channels;
cannam@64 90
cannam@64 91 m_freqbuf = new float *[m_channels];
cannam@64 92 for (size_t c = 0; c < m_channels; ++c) {
cannam@64 93 m_freqbuf[c] = new float[m_blockSize + 2];
cannam@64 94 }
cannam@64 95 m_ri = new double[m_blockSize];
cannam@64 96 m_ro = new double[m_blockSize];
cannam@64 97 m_io = new double[m_blockSize];
cannam@64 98
cannam@64 99 return m_plugin->initialise(channels, stepSize, blockSize);
cannam@64 100 }
cannam@64 101
cannam@64 102 Plugin::InputDomain
cannam@64 103 PluginInputDomainAdapter::getInputDomain() const
cannam@64 104 {
cannam@64 105 return TimeDomain;
cannam@64 106 }
cannam@64 107
cannam@64 108 size_t
cannam@64 109 PluginInputDomainAdapter::getPreferredStepSize() const
cannam@64 110 {
cannam@64 111 size_t step = m_plugin->getPreferredStepSize();
cannam@64 112
cannam@64 113 if (step == 0 && (m_plugin->getInputDomain() == FrequencyDomain)) {
cannam@64 114 step = getPreferredBlockSize() / 2;
cannam@64 115 }
cannam@64 116
cannam@64 117 return step;
cannam@64 118 }
cannam@64 119
cannam@64 120 size_t
cannam@64 121 PluginInputDomainAdapter::getPreferredBlockSize() const
cannam@64 122 {
cannam@64 123 size_t block = m_plugin->getPreferredBlockSize();
cannam@64 124
cannam@64 125 if (m_plugin->getInputDomain() == FrequencyDomain) {
cannam@64 126 if (block == 0) {
cannam@64 127 block = 1024;
cannam@64 128 } else {
cannam@64 129 block = makeBlockSizeAcceptable(block);
cannam@64 130 }
cannam@64 131 }
cannam@64 132
cannam@64 133 return block;
cannam@64 134 }
cannam@64 135
cannam@64 136 size_t
cannam@64 137 PluginInputDomainAdapter::makeBlockSizeAcceptable(size_t blockSize) const
cannam@64 138 {
cannam@64 139 if (blockSize < 2) {
cannam@64 140
cannam@64 141 std::cerr << "WARNING: Vamp::HostExt::PluginInputDomainAdapter::initialise: blocksize < 2 not" << std::endl
cannam@64 142 << "supported, increasing from " << blockSize << " to 2" << std::endl;
cannam@64 143 blockSize = 2;
cannam@64 144
cannam@64 145 } else if (blockSize & (blockSize-1)) {
cannam@64 146
cannam@64 147 // not a power of two, can't handle that with our current fft
cannam@64 148 // implementation
cannam@64 149
cannam@64 150 size_t nearest = blockSize;
cannam@64 151 size_t power = 0;
cannam@64 152 while (nearest > 1) {
cannam@64 153 nearest >>= 1;
cannam@64 154 ++power;
cannam@64 155 }
cannam@64 156 nearest = 1;
cannam@64 157 while (power) {
cannam@64 158 nearest <<= 1;
cannam@64 159 --power;
cannam@64 160 }
cannam@64 161
cannam@64 162 if (blockSize - nearest > (nearest*2) - blockSize) {
cannam@64 163 nearest = nearest*2;
cannam@64 164 }
cannam@64 165
cannam@64 166 std::cerr << "WARNING: Vamp::HostExt::PluginInputDomainAdapter::initialise: non-power-of-two\nblocksize " << blockSize << " not supported, using blocksize " << nearest << " instead" << std::endl;
cannam@64 167 blockSize = nearest;
cannam@64 168 }
cannam@64 169
cannam@64 170 return blockSize;
cannam@64 171 }
cannam@64 172
cannam@64 173 Plugin::FeatureSet
cannam@64 174 PluginInputDomainAdapter::process(const float *const *inputBuffers, RealTime timestamp)
cannam@64 175 {
cannam@64 176 if (m_plugin->getInputDomain() == TimeDomain) {
cannam@64 177 return m_plugin->process(inputBuffers, timestamp);
cannam@64 178 }
cannam@64 179
cannam@64 180 // The timestamp supplied should be (according to the Vamp::Plugin
cannam@64 181 // spec) the time of the start of the time-domain input block.
cannam@64 182 // However, we want to pass to the plugin an FFT output calculated
cannam@64 183 // from the block of samples _centred_ on that timestamp.
cannam@64 184 //
cannam@64 185 // We have two options:
cannam@64 186 //
cannam@64 187 // 1. Buffer the input, calculating the fft of the values at the
cannam@64 188 // passed-in block minus blockSize/2 rather than starting at the
cannam@64 189 // passed-in block. So each time we call process on the plugin,
cannam@64 190 // we are passing in the same timestamp as was passed to our own
cannam@64 191 // process plugin, but not (the frequency domain representation
cannam@64 192 // of) the same set of samples. Advantages: avoids confusion in
cannam@64 193 // the host by ensuring the returned values have timestamps
cannam@64 194 // comparable with that passed in to this function (in fact this
cannam@64 195 // is pretty much essential for one-value-per-block outputs);
cannam@64 196 // consistent with hosts such as SV that deal with the
cannam@64 197 // frequency-domain transform themselves. Disadvantages: means
cannam@64 198 // making the not necessarily correct assumption that the samples
cannam@64 199 // preceding the first official block are all zero (or some other
cannam@64 200 // known value).
cannam@64 201 //
cannam@64 202 // 2. Increase the passed-in timestamps by half the blocksize. So
cannam@64 203 // when we call process, we are passing in the frequency domain
cannam@64 204 // representation of the same set of samples as passed to us, but
cannam@64 205 // with a different timestamp. Advantages: simplicity; avoids
cannam@64 206 // iffy assumption mentioned above. Disadvantages: inconsistency
cannam@64 207 // with SV in cases where stepSize != blockSize/2; potential
cannam@64 208 // confusion arising from returned timestamps being calculated
cannam@64 209 // from the adjusted input timestamps rather than the original
cannam@64 210 // ones (and inaccuracy where the returned timestamp is implied,
cannam@64 211 // as in one-value-per-block).
cannam@64 212 //
cannam@64 213 // Neither way is ideal, but I don't think either is strictly
cannam@64 214 // incorrect either. I think this is just a case where the same
cannam@64 215 // plugin can legitimately produce differing results from the same
cannam@64 216 // input data, depending on how that data is packaged.
cannam@64 217 //
cannam@64 218 // We'll go for option 2, adjusting the timestamps. Note in
cannam@64 219 // particular that this means some results can differ from those
cannam@64 220 // produced by SV.
cannam@64 221
cannam@65 222 // std::cerr << "PluginInputDomainAdapter: sampleRate " << m_inputSampleRate << ", blocksize " << m_blockSize << ", adjusting time from " << timestamp;
cannam@64 223
cannam@64 224 timestamp = timestamp + RealTime::frame2RealTime(m_blockSize/2,
cannam@64 225 m_inputSampleRate);
cannam@64 226
cannam@65 227 // std::cerr << " to " << timestamp << std::endl;
cannam@64 228
cannam@64 229 for (size_t c = 0; c < m_channels; ++c) {
cannam@64 230
cannam@64 231 for (size_t i = 0; i < m_blockSize; ++i) {
cannam@64 232 // Hanning window
cannam@64 233 m_ri[i] = double(inputBuffers[c][i])
cannam@64 234 * (0.50 - 0.50 * cos((2 * M_PI * i)
cannam@64 235 / m_blockSize));
cannam@64 236 }
cannam@64 237
cannam@64 238 for (size_t i = 0; i < m_blockSize/2; ++i) {
cannam@64 239 // FFT shift
cannam@64 240 double value = m_ri[i];
cannam@64 241 m_ri[i] = m_ri[i + m_blockSize/2];
cannam@64 242 m_ri[i + m_blockSize/2] = value;
cannam@64 243 }
cannam@64 244
cannam@64 245 fft(m_blockSize, false, m_ri, 0, m_ro, m_io);
cannam@64 246
cannam@64 247 for (size_t i = 0; i <= m_blockSize/2; ++i) {
cannam@64 248 m_freqbuf[c][i * 2] = m_ro[i];
cannam@64 249 m_freqbuf[c][i * 2 + 1] = m_io[i];
cannam@64 250 }
cannam@64 251 }
cannam@64 252
cannam@64 253 return m_plugin->process(m_freqbuf, timestamp);
cannam@64 254 }
cannam@64 255
cannam@64 256 void
cannam@64 257 PluginInputDomainAdapter::fft(unsigned int n, bool inverse,
cannam@64 258 double *ri, double *ii, double *ro, double *io)
cannam@64 259 {
cannam@64 260 if (!ri || !ro || !io) return;
cannam@64 261
cannam@64 262 unsigned int bits;
cannam@64 263 unsigned int i, j, k, m;
cannam@64 264 unsigned int blockSize, blockEnd;
cannam@64 265
cannam@64 266 double tr, ti;
cannam@64 267
cannam@64 268 if (n < 2) return;
cannam@64 269 if (n & (n-1)) return;
cannam@64 270
cannam@64 271 double angle = 2.0 * M_PI;
cannam@64 272 if (inverse) angle = -angle;
cannam@64 273
cannam@64 274 for (i = 0; ; ++i) {
cannam@64 275 if (n & (1 << i)) {
cannam@64 276 bits = i;
cannam@64 277 break;
cannam@64 278 }
cannam@64 279 }
cannam@64 280
cannam@64 281 static unsigned int tableSize = 0;
cannam@64 282 static int *table = 0;
cannam@64 283
cannam@64 284 if (tableSize != n) {
cannam@64 285
cannam@64 286 delete[] table;
cannam@64 287
cannam@64 288 table = new int[n];
cannam@64 289
cannam@64 290 for (i = 0; i < n; ++i) {
cannam@64 291
cannam@64 292 m = i;
cannam@64 293
cannam@64 294 for (j = k = 0; j < bits; ++j) {
cannam@64 295 k = (k << 1) | (m & 1);
cannam@64 296 m >>= 1;
cannam@64 297 }
cannam@64 298
cannam@64 299 table[i] = k;
cannam@64 300 }
cannam@64 301
cannam@64 302 tableSize = n;
cannam@64 303 }
cannam@64 304
cannam@64 305 if (ii) {
cannam@64 306 for (i = 0; i < n; ++i) {
cannam@64 307 ro[table[i]] = ri[i];
cannam@64 308 io[table[i]] = ii[i];
cannam@64 309 }
cannam@64 310 } else {
cannam@64 311 for (i = 0; i < n; ++i) {
cannam@64 312 ro[table[i]] = ri[i];
cannam@64 313 io[table[i]] = 0.0;
cannam@64 314 }
cannam@64 315 }
cannam@64 316
cannam@64 317 blockEnd = 1;
cannam@64 318
cannam@64 319 for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
cannam@64 320
cannam@64 321 double delta = angle / (double)blockSize;
cannam@64 322 double sm2 = -sin(-2 * delta);
cannam@64 323 double sm1 = -sin(-delta);
cannam@64 324 double cm2 = cos(-2 * delta);
cannam@64 325 double cm1 = cos(-delta);
cannam@64 326 double w = 2 * cm1;
cannam@64 327 double ar[3], ai[3];
cannam@64 328
cannam@64 329 for (i = 0; i < n; i += blockSize) {
cannam@64 330
cannam@64 331 ar[2] = cm2;
cannam@64 332 ar[1] = cm1;
cannam@64 333
cannam@64 334 ai[2] = sm2;
cannam@64 335 ai[1] = sm1;
cannam@64 336
cannam@64 337 for (j = i, m = 0; m < blockEnd; j++, m++) {
cannam@64 338
cannam@64 339 ar[0] = w * ar[1] - ar[2];
cannam@64 340 ar[2] = ar[1];
cannam@64 341 ar[1] = ar[0];
cannam@64 342
cannam@64 343 ai[0] = w * ai[1] - ai[2];
cannam@64 344 ai[2] = ai[1];
cannam@64 345 ai[1] = ai[0];
cannam@64 346
cannam@64 347 k = j + blockEnd;
cannam@64 348 tr = ar[0] * ro[k] - ai[0] * io[k];
cannam@64 349 ti = ar[0] * io[k] + ai[0] * ro[k];
cannam@64 350
cannam@64 351 ro[k] = ro[j] - tr;
cannam@64 352 io[k] = io[j] - ti;
cannam@64 353
cannam@64 354 ro[j] += tr;
cannam@64 355 io[j] += ti;
cannam@64 356 }
cannam@64 357 }
cannam@64 358
cannam@64 359 blockEnd = blockSize;
cannam@64 360 }
cannam@64 361
cannam@64 362 if (inverse) {
cannam@64 363
cannam@64 364 double denom = (double)n;
cannam@64 365
cannam@64 366 for (i = 0; i < n; i++) {
cannam@64 367 ro[i] /= denom;
cannam@64 368 io[i] /= denom;
cannam@64 369 }
cannam@64 370 }
cannam@64 371 }
cannam@64 372
cannam@64 373 }
cannam@64 374
cannam@64 375 }
cannam@64 376