annotate host/simplehost.cpp @ 18:b4043af42278

* Some textual changes
author cannam
date Mon, 10 Apr 2006 10:31:19 +0000
parents 61887dda7fe0
children 08ee18123f5a
rev   line source
cannam@1 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@1 2
cannam@1 3 /*
cannam@1 4 Vamp
cannam@1 5
cannam@1 6 An API for audio analysis and feature extraction plugins.
cannam@1 7
cannam@1 8 Centre for Digital Music, Queen Mary, University of London.
cannam@1 9 Copyright 2006 Chris Cannam.
cannam@16 10 FFT code from Don Cross's public domain FFT implementation.
cannam@1 11
cannam@1 12 Permission is hereby granted, free of charge, to any person
cannam@1 13 obtaining a copy of this software and associated documentation
cannam@1 14 files (the "Software"), to deal in the Software without
cannam@1 15 restriction, including without limitation the rights to use, copy,
cannam@1 16 modify, merge, publish, distribute, sublicense, and/or sell copies
cannam@1 17 of the Software, and to permit persons to whom the Software is
cannam@1 18 furnished to do so, subject to the following conditions:
cannam@1 19
cannam@1 20 The above copyright notice and this permission notice shall be
cannam@1 21 included in all copies or substantial portions of the Software.
cannam@1 22
cannam@1 23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cannam@1 24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cannam@1 25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
cannam@6 26 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
cannam@1 27 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
cannam@1 28 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
cannam@1 29 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cannam@1 30
cannam@1 31 Except as contained in this notice, the names of the Centre for
cannam@1 32 Digital Music; Queen Mary, University of London; and Chris Cannam
cannam@1 33 shall not be used in advertising or otherwise to promote the sale,
cannam@1 34 use or other dealings in this Software without prior written
cannam@1 35 authorization.
cannam@1 36 */
cannam@1 37
cannam@16 38 #include "PluginHostAdapter.h"
cannam@1 39 #include "vamp.h"
cannam@1 40
cannam@16 41 #include <iostream>
cannam@16 42 #include <sndfile.h>
cannam@1 43
cannam@1 44 #include "system.h"
cannam@1 45
cannam@16 46 using std::cout;
cannam@16 47 using std::cerr;
cannam@16 48 using std::endl;
cannam@16 49 using std::string;
cannam@16 50
cannam@16 51 void printFeatures(int, int, int, Vamp::Plugin::FeatureSet);
cannam@16 52 void transformInput(float *, size_t);
cannam@16 53 void fft(unsigned int, bool, double *, double *, double *, double *);
cannam@16 54
cannam@1 55 /*
cannam@16 56 A very simple Vamp plugin host. Given the name of a plugin
cannam@16 57 library and the name of a sound file on the command line, it loads
cannam@16 58 the first plugin in the library and runs it on the sound file,
cannam@16 59 dumping the plugin's first output to stdout.
cannam@1 60 */
cannam@1 61
cannam@1 62 int main(int argc, char **argv)
cannam@1 63 {
cannam@16 64 if (argc < 2 || argc > 4) {
cannam@16 65 cerr << "Usage: " << argv[0] << " pluginlibrary.so[:plugin] [file.wav] [outputno]" << endl;
cannam@1 66 return 2;
cannam@1 67 }
cannam@1 68
cannam@16 69 cerr << endl << argv[0] << ": Running..." << endl;
cannam@1 70
cannam@16 71 string soname = argv[1];
cannam@16 72 string plugname = "";
cannam@16 73 string wavname;
cannam@16 74 if (argc >= 3) wavname = argv[2];
cannam@16 75
cannam@16 76 if (soname.find(":") >= 0) {
cannam@16 77 plugname = soname.substr(soname.find(":") + 1);
cannam@16 78 soname = soname.substr(0, soname.find(":"));
cannam@16 79 }
cannam@1 80
cannam@1 81 void *libraryHandle = DLOPEN(soname, RTLD_LAZY);
cannam@1 82
cannam@1 83 if (!libraryHandle) {
cannam@16 84 cerr << argv[0] << ": Failed to open plugin library "
cannam@16 85 << soname << ": " << DLERROR() << endl;
cannam@1 86 return 1;
cannam@1 87 }
cannam@1 88
cannam@16 89 cerr << argv[0] << ": Opened plugin library " << soname << endl;
cannam@1 90
cannam@1 91 VampGetPluginDescriptorFunction fn = (VampGetPluginDescriptorFunction)
cannam@1 92 DLSYM(libraryHandle, "vampGetPluginDescriptor");
cannam@1 93
cannam@1 94 if (!fn) {
cannam@16 95 cerr << argv[0] << ": No Vamp descriptor function in library "
cannam@16 96 << soname << endl;
cannam@1 97 DLCLOSE(libraryHandle);
cannam@1 98 return 1;
cannam@1 99 }
cannam@1 100
cannam@16 101 cerr << argv[0] << ": Found plugin descriptor function" << endl;
cannam@1 102
cannam@1 103 int index = 0;
cannam@16 104 int plugnumber = -1;
cannam@1 105 const VampPluginDescriptor *descriptor = 0;
cannam@1 106
cannam@1 107 while ((descriptor = fn(index))) {
cannam@1 108
cannam@16 109 Vamp::PluginHostAdapter plugin(descriptor, 48000);
cannam@16 110 cerr << argv[0] << ": Plugin " << (index+1)
cannam@16 111 << " is \"" << plugin.getName() << "\"" << endl;
cannam@16 112
cannam@16 113 if (plugin.getName() == plugname) plugnumber = index;
cannam@1 114
cannam@1 115 ++index;
cannam@1 116 }
cannam@1 117
cannam@16 118 cerr << argv[0] << ": Done\n" << endl;
cannam@16 119
cannam@16 120 if (wavname == "") {
cannam@16 121 DLCLOSE(libraryHandle);
cannam@16 122 return 0;
cannam@16 123 }
cannam@16 124
cannam@16 125 if (plugnumber < 0) {
cannam@16 126 if (plugname != "") {
cannam@16 127 cerr << "ERROR: No such plugin as " << plugname << " in library"
cannam@16 128 << endl;
cannam@16 129 DLCLOSE(libraryHandle);
cannam@16 130 return 0;
cannam@16 131 } else {
cannam@16 132 plugnumber = 0;
cannam@16 133 }
cannam@16 134 }
cannam@16 135
cannam@16 136 descriptor = fn(plugnumber);
cannam@16 137 if (!descriptor) {
cannam@16 138 DLCLOSE(libraryHandle);
cannam@16 139 return 0;
cannam@16 140 }
cannam@16 141
cannam@16 142 SNDFILE *sndfile;
cannam@16 143 SF_INFO sfinfo;
cannam@16 144 memset(&sfinfo, 0, sizeof(SF_INFO));
cannam@16 145
cannam@16 146 sndfile = sf_open(wavname.c_str(), SFM_READ, &sfinfo);
cannam@16 147 if (!sndfile) {
cannam@16 148 cerr << "ERROR: Failed to open input file \"" << wavname << "\": "
cannam@16 149 << sf_strerror(sndfile) << endl;
cannam@16 150 DLCLOSE(libraryHandle);
cannam@16 151 return 1;
cannam@16 152 }
cannam@16 153
cannam@16 154 Vamp::PluginHostAdapter *plugin =
cannam@16 155 new Vamp::PluginHostAdapter(descriptor, sfinfo.samplerate);
cannam@16 156
cannam@16 157 cerr << "Running " << plugin->getName() << "..." << endl;
cannam@16 158
cannam@16 159 int blockSize = plugin->getPreferredBlockSize();
cannam@16 160 int stepSize = plugin->getPreferredStepSize();
cannam@16 161
cannam@16 162 cerr << "Preferred block size = " << blockSize << ", step size = "
cannam@16 163 << stepSize << endl;
cannam@16 164
cannam@16 165 if (blockSize == 0) blockSize = 1024;
cannam@16 166 if (stepSize == 0) stepSize = blockSize;
cannam@16 167
cannam@16 168 int channels = sfinfo.channels;
cannam@16 169
cannam@16 170 float *filebuf = new float[blockSize * channels];
cannam@16 171 float **plugbuf = new float*[channels];
cannam@16 172 for (int c = 0; c < channels; ++c) plugbuf[c] = new float[blockSize];
cannam@16 173
cannam@16 174 cerr << "Using block size = " << blockSize << ", step size = "
cannam@16 175 << stepSize << endl;
cannam@16 176
cannam@16 177 int minch = plugin->getMinChannelCount();
cannam@16 178 int maxch = plugin->getMaxChannelCount();
cannam@16 179 cerr << "Plugin accepts " << minch << " -> " << maxch << " channel(s)" << endl;
cannam@16 180
cannam@16 181 Vamp::Plugin::OutputList outputs = plugin->getOutputDescriptors();
cannam@16 182 Vamp::Plugin::OutputDescriptor od;
cannam@16 183
cannam@16 184 int output = 0;
cannam@16 185 if (argc == 4) output = atoi(argv[3]);
cannam@16 186
cannam@16 187 bool mix = false;
cannam@16 188
cannam@16 189 if (minch > channels || maxch < channels) {
cannam@16 190 if (minch == 1) {
cannam@16 191 cerr << "WARNING: Sound file has " << channels << " channels, mixing down to 1" << endl;
cannam@16 192 mix = true;
cannam@16 193 channels = 1;
cannam@16 194 } else {
cannam@16 195 cerr << "ERROR: Sound file has " << channels << " channels, out of range for plugin" << endl;
cannam@16 196 goto done;
cannam@16 197 }
cannam@16 198 }
cannam@16 199
cannam@16 200 if (outputs.empty()) {
cannam@16 201 cerr << "Plugin has no outputs!" << endl;
cannam@16 202 goto done;
cannam@16 203 }
cannam@16 204
cannam@16 205 if (int(outputs.size()) <= output) {
cannam@16 206 cerr << "Output " << output << " requested, but plugin has only " << outputs.size() << " output(s)" << endl;
cannam@16 207 goto done;
cannam@16 208 }
cannam@16 209
cannam@16 210 od = outputs[output];
cannam@16 211 cerr << "Output is " << od.name << endl;
cannam@16 212
cannam@16 213 plugin->initialise(channels, stepSize, blockSize);
cannam@16 214
cannam@16 215 for (size_t i = 0; i < sfinfo.frames; i += stepSize) {
cannam@16 216
cannam@16 217 int count;
cannam@16 218
cannam@16 219 if (sf_seek(sndfile, i, SEEK_SET) < 0) {
cannam@16 220 cerr << "ERROR: sf_seek failed: " << sf_strerror(sndfile) << endl;
cannam@16 221 break;
cannam@16 222 }
cannam@16 223
cannam@16 224 if ((count = sf_readf_float(sndfile, filebuf, blockSize)) < 0) {
cannam@16 225 cerr << "ERROR: sf_readf_float failed: " << sf_strerror(sndfile) << endl;
cannam@16 226 break;
cannam@16 227 }
cannam@16 228
cannam@16 229 for (int c = 0; c < channels; ++c) {
cannam@16 230 for (int j = 0; j < blockSize; ++j) {
cannam@16 231 plugbuf[c][j] = 0.0f;
cannam@16 232 }
cannam@16 233 }
cannam@16 234
cannam@16 235 for (int c = 0; c < sfinfo.channels; ++c) {
cannam@16 236 int tc = c;
cannam@16 237 if (mix) tc = 0;
cannam@16 238 for (int j = 0; j < blockSize && j < count; ++j) {
cannam@16 239 plugbuf[tc][j] += filebuf[j * channels + c];
cannam@16 240 }
cannam@16 241
cannam@16 242 if (plugin->getInputDomain() == Vamp::Plugin::FrequencyDomain) {
cannam@16 243 transformInput(plugbuf[tc], blockSize);
cannam@16 244 }
cannam@16 245 }
cannam@16 246
cannam@16 247 printFeatures
cannam@16 248 (i, sfinfo.samplerate, output, plugin->process
cannam@16 249 (plugbuf, Vamp::RealTime::frame2RealTime(i, sfinfo.samplerate)));
cannam@16 250 }
cannam@16 251
cannam@16 252 printFeatures(sfinfo.frames, sfinfo.samplerate, output,
cannam@16 253 plugin->getRemainingFeatures());
cannam@16 254
cannam@16 255 done:
cannam@16 256 delete plugin;
cannam@1 257
cannam@1 258 DLCLOSE(libraryHandle);
cannam@16 259 sf_close(sndfile);
cannam@1 260 return 0;
cannam@1 261 }
cannam@1 262
cannam@16 263 void
cannam@16 264 printFeatures(int frame, int sr, int output, Vamp::Plugin::FeatureSet features)
cannam@16 265 {
cannam@16 266 for (unsigned int i = 0; i < features[output].size(); ++i) {
cannam@16 267 Vamp::RealTime rt = Vamp::RealTime::frame2RealTime(frame, sr);
cannam@16 268 if (features[output][i].hasTimestamp) {
cannam@16 269 rt = features[output][i].timestamp;
cannam@16 270 }
cannam@16 271 cout << rt.toString() << ":";
cannam@16 272 for (unsigned int j = 0; j < features[output][i].values.size(); ++j) {
cannam@16 273 cout << " " << features[output][i].values[j];
cannam@16 274 }
cannam@16 275 cout << endl;
cannam@16 276 }
cannam@16 277 }
cannam@16 278
cannam@16 279 void
cannam@16 280 transformInput(float *buffer, size_t size)
cannam@16 281 {
cannam@16 282 double *inbuf = new double[size * 2];
cannam@16 283 double *outbuf = new double[size * 2];
cannam@16 284
cannam@16 285 // Copy across with Hanning window
cannam@16 286 for (size_t i = 0; i < size; ++i) {
cannam@16 287 inbuf[i] = double(buffer[i]) * (0.50 - 0.50 * cos(2 * M_PI * i / size));
cannam@16 288 inbuf[i + size] = 0.0;
cannam@16 289 }
cannam@16 290
cannam@16 291 for (size_t i = 0; i < size/2; ++i) {
cannam@16 292 double temp = inbuf[i];
cannam@16 293 inbuf[i] = inbuf[i + size/2];
cannam@16 294 inbuf[i + size/2] = temp;
cannam@16 295 }
cannam@16 296
cannam@16 297 fft(size, false, inbuf, inbuf + size, outbuf, outbuf + size);
cannam@16 298
cannam@16 299 for (size_t i = 0; i < size/2; ++i) {
cannam@16 300 buffer[i * 2] = outbuf[i];
cannam@16 301 buffer[i * 2 + 1] = outbuf[i + size];
cannam@16 302 }
cannam@16 303
cannam@16 304 delete inbuf;
cannam@16 305 delete outbuf;
cannam@16 306 }
cannam@16 307
cannam@16 308 void
cannam@16 309 fft(unsigned int n, bool inverse, double *ri, double *ii, double *ro, double *io)
cannam@16 310 {
cannam@16 311 if (!ri || !ro || !io) return;
cannam@16 312
cannam@16 313 unsigned int bits;
cannam@16 314 unsigned int i, j, k, m;
cannam@16 315 unsigned int blockSize, blockEnd;
cannam@16 316
cannam@16 317 double tr, ti;
cannam@16 318
cannam@16 319 if (n < 2) return;
cannam@16 320 if (n & (n-1)) return;
cannam@16 321
cannam@16 322 double angle = 2.0 * M_PI;
cannam@16 323 if (inverse) angle = -angle;
cannam@16 324
cannam@16 325 for (i = 0; ; ++i) {
cannam@16 326 if (n & (1 << i)) {
cannam@16 327 bits = i;
cannam@16 328 break;
cannam@16 329 }
cannam@16 330 }
cannam@16 331
cannam@16 332 static unsigned int tableSize = 0;
cannam@16 333 static int *table = 0;
cannam@16 334
cannam@16 335 if (tableSize != n) {
cannam@16 336
cannam@16 337 delete[] table;
cannam@16 338
cannam@16 339 table = new int[n];
cannam@16 340
cannam@16 341 for (i = 0; i < n; ++i) {
cannam@16 342
cannam@16 343 m = i;
cannam@16 344
cannam@16 345 for (j = k = 0; j < bits; ++j) {
cannam@16 346 k = (k << 1) | (m & 1);
cannam@16 347 m >>= 1;
cannam@16 348 }
cannam@16 349
cannam@16 350 table[i] = k;
cannam@16 351 }
cannam@16 352
cannam@16 353 tableSize = n;
cannam@16 354 }
cannam@16 355
cannam@16 356 if (ii) {
cannam@16 357 for (i = 0; i < n; ++i) {
cannam@16 358 ro[table[i]] = ri[i];
cannam@16 359 io[table[i]] = ii[i];
cannam@16 360 }
cannam@16 361 } else {
cannam@16 362 for (i = 0; i < n; ++i) {
cannam@16 363 ro[table[i]] = ri[i];
cannam@16 364 io[table[i]] = 0.0;
cannam@16 365 }
cannam@16 366 }
cannam@16 367
cannam@16 368 blockEnd = 1;
cannam@16 369
cannam@16 370 for (blockSize = 2; blockSize <= n; blockSize <<= 1) {
cannam@16 371
cannam@16 372 double delta = angle / (double)blockSize;
cannam@16 373 double sm2 = -sin(-2 * delta);
cannam@16 374 double sm1 = -sin(-delta);
cannam@16 375 double cm2 = cos(-2 * delta);
cannam@16 376 double cm1 = cos(-delta);
cannam@16 377 double w = 2 * cm1;
cannam@16 378 double ar[3], ai[3];
cannam@16 379
cannam@16 380 for (i = 0; i < n; i += blockSize) {
cannam@16 381
cannam@16 382 ar[2] = cm2;
cannam@16 383 ar[1] = cm1;
cannam@16 384
cannam@16 385 ai[2] = sm2;
cannam@16 386 ai[1] = sm1;
cannam@16 387
cannam@16 388 for (j = i, m = 0; m < blockEnd; j++, m++) {
cannam@16 389
cannam@16 390 ar[0] = w * ar[1] - ar[2];
cannam@16 391 ar[2] = ar[1];
cannam@16 392 ar[1] = ar[0];
cannam@16 393
cannam@16 394 ai[0] = w * ai[1] - ai[2];
cannam@16 395 ai[2] = ai[1];
cannam@16 396 ai[1] = ai[0];
cannam@16 397
cannam@16 398 k = j + blockEnd;
cannam@16 399 tr = ar[0] * ro[k] - ai[0] * io[k];
cannam@16 400 ti = ar[0] * io[k] + ai[0] * ro[k];
cannam@16 401
cannam@16 402 ro[k] = ro[j] - tr;
cannam@16 403 io[k] = io[j] - ti;
cannam@16 404
cannam@16 405 ro[j] += tr;
cannam@16 406 io[j] += ti;
cannam@16 407 }
cannam@16 408 }
cannam@16 409
cannam@16 410 blockEnd = blockSize;
cannam@16 411 }
cannam@16 412
cannam@16 413 if (inverse) {
cannam@16 414
cannam@16 415 double denom = (double)n;
cannam@16 416
cannam@16 417 for (i = 0; i < n; i++) {
cannam@16 418 ro[i] /= denom;
cannam@16 419 io[i] /= denom;
cannam@16 420 }
cannam@16 421 }
cannam@16 422 }
cannam@16 423
cannam@16 424