annotate vamp-sdk/hostext/PluginInputDomainAdapter.cpp @ 68:47d6e670a810

* avoid warning about float-to-int conversion on frame2RealTime * define pi if it isn't already for some reason
author cannam
date Mon, 04 Jun 2007 13:31:04 +0000
parents a712ed15d158
children fd58037b4a7b
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@68 173 // for some visual studii apparently
cannam@68 174 #ifndef M_PI
cannam@68 175 #define M_PI 3.14159265358979232846
cannam@68 176 #endif
cannam@68 177
cannam@64 178 Plugin::FeatureSet
cannam@64 179 PluginInputDomainAdapter::process(const float *const *inputBuffers, RealTime timestamp)
cannam@64 180 {
cannam@64 181 if (m_plugin->getInputDomain() == TimeDomain) {
cannam@64 182 return m_plugin->process(inputBuffers, timestamp);
cannam@64 183 }
cannam@64 184
cannam@64 185 // The timestamp supplied should be (according to the Vamp::Plugin
cannam@64 186 // spec) the time of the start of the time-domain input block.
cannam@64 187 // However, we want to pass to the plugin an FFT output calculated
cannam@64 188 // from the block of samples _centred_ on that timestamp.
cannam@64 189 //
cannam@64 190 // We have two options:
cannam@64 191 //
cannam@64 192 // 1. Buffer the input, calculating the fft of the values at the
cannam@64 193 // passed-in block minus blockSize/2 rather than starting at the
cannam@64 194 // passed-in block. So each time we call process on the plugin,
cannam@64 195 // we are passing in the same timestamp as was passed to our own
cannam@64 196 // process plugin, but not (the frequency domain representation
cannam@64 197 // of) the same set of samples. Advantages: avoids confusion in
cannam@64 198 // the host by ensuring the returned values have timestamps
cannam@64 199 // comparable with that passed in to this function (in fact this
cannam@64 200 // is pretty much essential for one-value-per-block outputs);
cannam@64 201 // consistent with hosts such as SV that deal with the
cannam@64 202 // frequency-domain transform themselves. Disadvantages: means
cannam@64 203 // making the not necessarily correct assumption that the samples
cannam@64 204 // preceding the first official block are all zero (or some other
cannam@64 205 // known value).
cannam@64 206 //
cannam@64 207 // 2. Increase the passed-in timestamps by half the blocksize. So
cannam@64 208 // when we call process, we are passing in the frequency domain
cannam@64 209 // representation of the same set of samples as passed to us, but
cannam@64 210 // with a different timestamp. Advantages: simplicity; avoids
cannam@64 211 // iffy assumption mentioned above. Disadvantages: inconsistency
cannam@64 212 // with SV in cases where stepSize != blockSize/2; potential
cannam@64 213 // confusion arising from returned timestamps being calculated
cannam@64 214 // from the adjusted input timestamps rather than the original
cannam@64 215 // ones (and inaccuracy where the returned timestamp is implied,
cannam@64 216 // as in one-value-per-block).
cannam@64 217 //
cannam@64 218 // Neither way is ideal, but I don't think either is strictly
cannam@64 219 // incorrect either. I think this is just a case where the same
cannam@64 220 // plugin can legitimately produce differing results from the same
cannam@64 221 // input data, depending on how that data is packaged.
cannam@64 222 //
cannam@64 223 // We'll go for option 2, adjusting the timestamps. Note in
cannam@64 224 // particular that this means some results can differ from those
cannam@64 225 // produced by SV.
cannam@64 226
cannam@65 227 // std::cerr << "PluginInputDomainAdapter: sampleRate " << m_inputSampleRate << ", blocksize " << m_blockSize << ", adjusting time from " << timestamp;
cannam@64 228
cannam@68 229 timestamp = timestamp + RealTime::frame2RealTime
cannam@68 230 (m_blockSize/2, int(m_inputSampleRate + 0.5));
cannam@64 231
cannam@65 232 // std::cerr << " to " << timestamp << std::endl;
cannam@64 233
cannam@64 234 for (size_t c = 0; c < m_channels; ++c) {
cannam@64 235
cannam@64 236 for (size_t i = 0; i < m_blockSize; ++i) {
cannam@64 237 // Hanning window
cannam@64 238 m_ri[i] = double(inputBuffers[c][i])
cannam@64 239 * (0.50 - 0.50 * cos((2 * M_PI * i)
cannam@64 240 / m_blockSize));
cannam@64 241 }
cannam@64 242
cannam@64 243 for (size_t i = 0; i < m_blockSize/2; ++i) {
cannam@64 244 // FFT shift
cannam@64 245 double value = m_ri[i];
cannam@64 246 m_ri[i] = m_ri[i + m_blockSize/2];
cannam@64 247 m_ri[i + m_blockSize/2] = value;
cannam@64 248 }
cannam@64 249
cannam@64 250 fft(m_blockSize, false, m_ri, 0, m_ro, m_io);
cannam@64 251
cannam@64 252 for (size_t i = 0; i <= m_blockSize/2; ++i) {
cannam@64 253 m_freqbuf[c][i * 2] = m_ro[i];
cannam@64 254 m_freqbuf[c][i * 2 + 1] = m_io[i];
cannam@64 255 }
cannam@64 256 }
cannam@64 257
cannam@64 258 return m_plugin->process(m_freqbuf, timestamp);
cannam@64 259 }
cannam@64 260
cannam@64 261 void
cannam@64 262 PluginInputDomainAdapter::fft(unsigned int n, bool inverse,
cannam@64 263 double *ri, double *ii, double *ro, double *io)
cannam@64 264 {
cannam@64 265 if (!ri || !ro || !io) return;
cannam@64 266
cannam@64 267 unsigned int bits;
cannam@64 268 unsigned int i, j, k, m;
cannam@64 269 unsigned int blockSize, blockEnd;
cannam@64 270
cannam@64 271 double tr, ti;
cannam@64 272
cannam@64 273 if (n < 2) return;
cannam@64 274 if (n & (n-1)) return;
cannam@64 275
cannam@64 276 double angle = 2.0 * M_PI;
cannam@64 277 if (inverse) angle = -angle;
cannam@64 278
cannam@64 279 for (i = 0; ; ++i) {
cannam@64 280 if (n & (1 << i)) {
cannam@64 281 bits = i;
cannam@64 282 break;
cannam@64 283 }
cannam@64 284 }
cannam@64 285
cannam@64 286 static unsigned int tableSize = 0;
cannam@64 287 static int *table = 0;
cannam@64 288
cannam@64 289 if (tableSize != n) {
cannam@64 290
cannam@64 291 delete[] table;
cannam@64 292
cannam@64 293 table = new int[n];
cannam@64 294
cannam@64 295 for (i = 0; i < n; ++i) {
cannam@64 296
cannam@64 297 m = i;
cannam@64 298
cannam@64 299 for (j = k = 0; j < bits; ++j) {
cannam@64 300 k = (k << 1) | (m & 1);
cannam@64 301 m >>= 1;
cannam@64 302 }
cannam@64 303
cannam@64 304 table[i] = k;
cannam@64 305 }
cannam@64 306
cannam@64 307 tableSize = n;
cannam@64 308 }
cannam@64 309
cannam@64 310 if (ii) {
cannam@64 311 for (i = 0; i < n; ++i) {
cannam@64 312 ro[table[i]] = ri[i];
cannam@64 313 io[table[i]] = ii[i];
cannam@64 314 }
cannam@64 315 } else {
cannam@64 316 for (i = 0; i < n; ++i) {
cannam@64 317 ro[table[i]] = ri[i];
cannam@64 318 io[table[i]] = 0.0;
cannam@64 319 }
cannam@64 320 }
cannam@64 321
cannam@64 322 blockEnd = 1;
cannam@64 323
cannam@64 324 for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
cannam@64 325
cannam@64 326 double delta = angle / (double)blockSize;
cannam@64 327 double sm2 = -sin(-2 * delta);
cannam@64 328 double sm1 = -sin(-delta);
cannam@64 329 double cm2 = cos(-2 * delta);
cannam@64 330 double cm1 = cos(-delta);
cannam@64 331 double w = 2 * cm1;
cannam@64 332 double ar[3], ai[3];
cannam@64 333
cannam@64 334 for (i = 0; i < n; i += blockSize) {
cannam@64 335
cannam@64 336 ar[2] = cm2;
cannam@64 337 ar[1] = cm1;
cannam@64 338
cannam@64 339 ai[2] = sm2;
cannam@64 340 ai[1] = sm1;
cannam@64 341
cannam@64 342 for (j = i, m = 0; m < blockEnd; j++, m++) {
cannam@64 343
cannam@64 344 ar[0] = w * ar[1] - ar[2];
cannam@64 345 ar[2] = ar[1];
cannam@64 346 ar[1] = ar[0];
cannam@64 347
cannam@64 348 ai[0] = w * ai[1] - ai[2];
cannam@64 349 ai[2] = ai[1];
cannam@64 350 ai[1] = ai[0];
cannam@64 351
cannam@64 352 k = j + blockEnd;
cannam@64 353 tr = ar[0] * ro[k] - ai[0] * io[k];
cannam@64 354 ti = ar[0] * io[k] + ai[0] * ro[k];
cannam@64 355
cannam@64 356 ro[k] = ro[j] - tr;
cannam@64 357 io[k] = io[j] - ti;
cannam@64 358
cannam@64 359 ro[j] += tr;
cannam@64 360 io[j] += ti;
cannam@64 361 }
cannam@64 362 }
cannam@64 363
cannam@64 364 blockEnd = blockSize;
cannam@64 365 }
cannam@64 366
cannam@64 367 if (inverse) {
cannam@64 368
cannam@64 369 double denom = (double)n;
cannam@64 370
cannam@64 371 for (i = 0; i < n; i++) {
cannam@64 372 ro[i] /= denom;
cannam@64 373 io[i] /= denom;
cannam@64 374 }
cannam@64 375 }
cannam@64 376 }
cannam@64 377
cannam@64 378 }
cannam@64 379
cannam@64 380 }
cannam@64 381