annotate dsp/segmentation/ClusterMeltSegmenter.cpp @ 58:d72fcd34d9a7

* Fixes to problems shown up by vamp-plugin-tester. Still not all plugins pass all tests, though
author cannam
date Mon, 23 Mar 2009 16:28:53 +0000
parents 00603b8a940f
children 6cb2b3cd5356
rev   line source
cannam@24 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
cannam@24 2
cannam@18 3 /*
cannam@24 4 * ClusterMeltSegmenter.cpp
cannam@18 5 *
cannam@24 6 * Created by Mark Levy on 23/03/2006.
cannam@24 7 * Copyright 2006 Centre for Digital Music, Queen Mary, University of London.
cannam@24 8 * All rights reserved.
cannam@18 9 */
cannam@18 10
cannam@18 11 #include <cfloat>
cannam@18 12 #include <cmath>
cannam@18 13
cannam@18 14 #include "ClusterMeltSegmenter.h"
cannam@18 15 #include "cluster_segmenter.h"
cannam@18 16 #include "segment.h"
cannam@18 17
cannam@20 18 #include "dsp/transforms/FFT.h"
cannam@24 19 #include "dsp/chromagram/ConstantQ.h"
cannam@24 20 #include "dsp/rateconversion/Decimator.h"
cannam@26 21 #include "dsp/mfcc/MFCC.h"
cannam@20 22
cannam@24 23 ClusterMeltSegmenter::ClusterMeltSegmenter(ClusterMeltSegmenterParams params) :
cannam@24 24 window(NULL),
cannam@24 25 constq(NULL),
cannam@26 26 mfcc(NULL),
cannam@24 27 featureType(params.featureType),
cannam@24 28 hopSize(params.hopSize),
cannam@24 29 windowSize(params.windowSize),
cannam@24 30 fmin(params.fmin),
cannam@24 31 fmax(params.fmax),
cannam@24 32 nbins(params.nbins),
cannam@24 33 ncomponents(params.ncomponents), // NB currently not passed - no. of PCA components is set in cluser_segmenter.c
cannam@24 34 nHMMStates(params.nHMMStates),
cannam@24 35 nclusters(params.nclusters),
cannam@24 36 histogramLength(params.histogramLength),
cannam@24 37 neighbourhoodLimit(params.neighbourhoodLimit),
cannam@26 38 decimator(NULL)
cannam@18 39 {
cannam@18 40 }
cannam@18 41
cannam@18 42 void ClusterMeltSegmenter::initialise(int fs)
cannam@18 43 {
cannam@24 44 samplerate = fs;
cannam@24 45
cannam@26 46 if (featureType == FEATURE_TYPE_CONSTQ ||
cannam@26 47 featureType == FEATURE_TYPE_CHROMA) {
cannam@26 48
cannam@26 49 // run internal processing at 11025 or thereabouts
cannam@24 50 int internalRate = 11025;
cannam@24 51 int decimationFactor = samplerate / internalRate;
cannam@24 52 if (decimationFactor < 1) decimationFactor = 1;
cannam@24 53
cannam@24 54 // must be a power of two
cannam@24 55 while (decimationFactor & (decimationFactor - 1)) ++decimationFactor;
cannam@24 56
cannam@24 57 if (decimationFactor > Decimator::getHighestSupportedFactor()) {
cannam@24 58 decimationFactor = Decimator::getHighestSupportedFactor();
cannam@24 59 }
cannam@24 60
cannam@24 61 if (decimationFactor > 1) {
cannam@24 62 decimator = new Decimator(getWindowsize(), decimationFactor);
cannam@24 63 }
cannam@24 64
cannam@24 65 CQConfig config;
cannam@24 66 config.FS = samplerate / decimationFactor;
cannam@24 67 config.min = fmin;
cannam@24 68 config.max = fmax;
cannam@24 69 config.BPO = nbins;
cannam@24 70 config.CQThresh = 0.0054;
cannam@24 71
cannam@24 72 constq = new ConstantQ(config);
cannam@24 73 constq->sparsekernel();
cannam@26 74
cannam@26 75 ncoeff = constq->getK();
cannam@26 76
cannam@26 77 } else if (featureType == FEATURE_TYPE_MFCC) {
cannam@24 78
cannam@27 79 // run internal processing at 22050 or thereabouts
cannam@27 80 int internalRate = 22050;
cannam@27 81 int decimationFactor = samplerate / internalRate;
cannam@27 82 if (decimationFactor < 1) decimationFactor = 1;
cannam@27 83
cannam@27 84 // must be a power of two
cannam@27 85 while (decimationFactor & (decimationFactor - 1)) ++decimationFactor;
cannam@27 86
cannam@27 87 if (decimationFactor > Decimator::getHighestSupportedFactor()) {
cannam@27 88 decimationFactor = Decimator::getHighestSupportedFactor();
cannam@27 89 }
cannam@27 90
cannam@27 91 if (decimationFactor > 1) {
cannam@27 92 decimator = new Decimator(getWindowsize(), decimationFactor);
cannam@27 93 }
cannam@27 94
cannam@30 95 MFCCConfig config(samplerate / decimationFactor);
cannam@27 96 config.fftsize = 2048;
cannam@27 97 config.nceps = 19;
cannam@27 98 config.want_c0 = true;
cannam@26 99
cannam@26 100 mfcc = new MFCC(config);
cannam@27 101 ncoeff = config.nceps + 1;
cannam@24 102 }
cannam@18 103 }
cannam@18 104
cannam@18 105 ClusterMeltSegmenter::~ClusterMeltSegmenter()
cannam@18 106 {
cannam@24 107 delete window;
cannam@24 108 delete constq;
cannam@24 109 delete decimator;
cannam@20 110 }
cannam@20 111
cannam@20 112 int
cannam@20 113 ClusterMeltSegmenter::getWindowsize()
cannam@20 114 {
cannam@44 115 return static_cast<int>(windowSize * samplerate + 0.001);
cannam@20 116 }
cannam@20 117
cannam@20 118 int
cannam@20 119 ClusterMeltSegmenter::getHopsize()
cannam@20 120 {
cannam@44 121 return static_cast<int>(hopSize * samplerate + 0.001);
cannam@18 122 }
cannam@18 123
cannam@24 124 void ClusterMeltSegmenter::extractFeatures(const double* samples, int nsamples)
cannam@18 125 {
cannam@26 126 if (featureType == FEATURE_TYPE_CONSTQ ||
cannam@26 127 featureType == FEATURE_TYPE_CHROMA) {
cannam@26 128 extractFeaturesConstQ(samples, nsamples);
cannam@26 129 } else if (featureType == FEATURE_TYPE_MFCC) {
cannam@26 130 extractFeaturesMFCC(samples, nsamples);
cannam@26 131 }
cannam@26 132 }
cannam@26 133
cannam@26 134 void ClusterMeltSegmenter::extractFeaturesConstQ(const double* samples, int nsamples)
cannam@26 135 {
cannam@24 136 if (!constq) {
cannam@26 137 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeaturesConstQ: "
cannam@26 138 << "No const-q: initialise not called?"
cannam@24 139 << std::endl;
cannam@24 140 return;
cannam@24 141 }
cannam@20 142
cannam@24 143 if (nsamples < getWindowsize()) {
cannam@24 144 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeatures: nsamples < windowsize (" << nsamples << " < " << getWindowsize() << ")" << std::endl;
cannam@24 145 return;
cannam@24 146 }
cannam@24 147
cannam@24 148 int fftsize = constq->getfftlength();
cannam@24 149
cannam@24 150 if (!window || window->getSize() != fftsize) {
cannam@24 151 delete window;
cannam@24 152 window = new Window<double>(HammingWindow, fftsize);
cannam@24 153 }
cannam@24 154
cannam@24 155 vector<double> cq(ncoeff);
cannam@24 156
cannam@24 157 for (int i = 0; i < ncoeff; ++i) cq[i] = 0.0;
cannam@24 158
cannam@24 159 const double *psource = samples;
cannam@24 160 int pcount = nsamples;
cannam@24 161
cannam@24 162 if (decimator) {
cannam@24 163 pcount = nsamples / decimator->getFactor();
cannam@24 164 double *decout = new double[pcount];
cannam@24 165 decimator->process(samples, decout);
cannam@24 166 psource = decout;
cannam@24 167 }
cannam@24 168
cannam@24 169 int origin = 0;
cannam@24 170
cannam@24 171 // std::cerr << "nsamples = " << nsamples << ", pcount = " << pcount << std::endl;
cannam@24 172
cannam@24 173 int frames = 0;
cannam@24 174
cannam@24 175 double *frame = new double[fftsize];
cannam@24 176 double *real = new double[fftsize];
cannam@24 177 double *imag = new double[fftsize];
cannam@24 178 double *cqre = new double[ncoeff];
cannam@24 179 double *cqim = new double[ncoeff];
cannam@24 180
cannam@24 181 while (origin <= pcount) {
cannam@24 182
cannam@24 183 // always need at least one fft window per block, but after
cannam@24 184 // that we want to avoid having any incomplete ones
cannam@24 185 if (origin > 0 && origin + fftsize >= pcount) break;
cannam@24 186
cannam@24 187 for (int i = 0; i < fftsize; ++i) {
cannam@24 188 if (origin + i < pcount) {
cannam@24 189 frame[i] = psource[origin + i];
cannam@24 190 } else {
cannam@24 191 frame[i] = 0.0;
cannam@24 192 }
cannam@24 193 }
cannam@24 194
cannam@24 195 for (int i = 0; i < fftsize/2; ++i) {
cannam@24 196 double value = frame[i];
cannam@24 197 frame[i] = frame[i + fftsize/2];
cannam@24 198 frame[i + fftsize/2] = value;
cannam@24 199 }
cannam@24 200
cannam@24 201 window->cut(frame);
cannam@24 202
cannam@24 203 FFT::process(fftsize, false, frame, 0, real, imag);
cannam@24 204
cannam@24 205 constq->process(real, imag, cqre, cqim);
cannam@18 206
cannam@24 207 for (int i = 0; i < ncoeff; ++i) {
cannam@24 208 cq[i] += sqrt(cqre[i] * cqre[i] + cqim[i] * cqim[i]);
cannam@24 209 }
cannam@24 210 ++frames;
cannam@20 211
cannam@24 212 origin += fftsize/2;
cannam@24 213 }
cannam@20 214
cannam@24 215 delete [] cqre;
cannam@24 216 delete [] cqim;
cannam@24 217 delete [] real;
cannam@24 218 delete [] imag;
cannam@24 219 delete [] frame;
cannam@20 220
cannam@24 221 for (int i = 0; i < ncoeff; ++i) {
cannam@24 222 cq[i] /= frames;
cannam@24 223 }
cannam@20 224
cannam@24 225 if (decimator) delete[] psource;
cannam@20 226
cannam@24 227 features.push_back(cq);
cannam@18 228 }
cannam@18 229
cannam@26 230 void ClusterMeltSegmenter::extractFeaturesMFCC(const double* samples, int nsamples)
cannam@26 231 {
cannam@26 232 if (!mfcc) {
cannam@26 233 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeaturesMFCC: "
cannam@26 234 << "No mfcc: initialise not called?"
cannam@26 235 << std::endl;
cannam@26 236 return;
cannam@26 237 }
cannam@26 238
cannam@26 239 if (nsamples < getWindowsize()) {
cannam@26 240 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeatures: nsamples < windowsize (" << nsamples << " < " << getWindowsize() << ")" << std::endl;
cannam@26 241 return;
cannam@26 242 }
cannam@26 243
cannam@26 244 int fftsize = mfcc->getfftlength();
cannam@26 245
cannam@26 246 vector<double> cc(ncoeff);
cannam@26 247
cannam@26 248 for (int i = 0; i < ncoeff; ++i) cc[i] = 0.0;
cannam@26 249
cannam@26 250 const double *psource = samples;
cannam@26 251 int pcount = nsamples;
cannam@26 252
cannam@27 253 if (decimator) {
cannam@27 254 pcount = nsamples / decimator->getFactor();
cannam@27 255 double *decout = new double[pcount];
cannam@27 256 decimator->process(samples, decout);
cannam@27 257 psource = decout;
cannam@27 258 }
cannam@27 259
cannam@26 260 int origin = 0;
cannam@26 261 int frames = 0;
cannam@26 262
cannam@26 263 double *frame = new double[fftsize];
cannam@26 264 double *ccout = new double[ncoeff];
cannam@26 265
cannam@26 266 while (origin <= pcount) {
cannam@26 267
cannam@26 268 // always need at least one fft window per block, but after
cannam@26 269 // that we want to avoid having any incomplete ones
cannam@26 270 if (origin > 0 && origin + fftsize >= pcount) break;
cannam@26 271
cannam@26 272 for (int i = 0; i < fftsize; ++i) {
cannam@26 273 if (origin + i < pcount) {
cannam@26 274 frame[i] = psource[origin + i];
cannam@26 275 } else {
cannam@26 276 frame[i] = 0.0;
cannam@26 277 }
cannam@26 278 }
cannam@26 279
cannam@30 280 mfcc->process(frame, ccout);
cannam@26 281
cannam@26 282 for (int i = 0; i < ncoeff; ++i) {
cannam@26 283 cc[i] += ccout[i];
cannam@26 284 }
cannam@26 285 ++frames;
cannam@26 286
cannam@26 287 origin += fftsize/2;
cannam@26 288 }
cannam@26 289
cannam@26 290 delete [] ccout;
cannam@26 291 delete [] frame;
cannam@26 292
cannam@26 293 for (int i = 0; i < ncoeff; ++i) {
cannam@26 294 cc[i] /= frames;
cannam@26 295 }
cannam@26 296
cannam@27 297 if (decimator) delete[] psource;
cannam@27 298
cannam@26 299 features.push_back(cc);
cannam@26 300 }
cannam@26 301
cannam@18 302 void ClusterMeltSegmenter::segment(int m)
cannam@18 303 {
cannam@24 304 nclusters = m;
cannam@24 305 segment();
cannam@18 306 }
cannam@18 307
cannam@18 308 void ClusterMeltSegmenter::setFeatures(const vector<vector<double> >& f)
cannam@18 309 {
cannam@24 310 features = f;
cannam@24 311 featureType = FEATURE_TYPE_UNKNOWN;
cannam@18 312 }
cannam@18 313
cannam@18 314 void ClusterMeltSegmenter::segment()
cannam@18 315 {
cannam@26 316 delete constq;
cannam@26 317 constq = 0;
cannam@26 318 delete mfcc;
cannam@26 319 mfcc = 0;
cannam@26 320 delete decimator;
cannam@26 321 decimator = 0;
cannam@58 322
cannam@58 323 if (features.size() < histogramLength) return;
cannam@58 324 /*
cannam@24 325 std::cerr << "ClusterMeltSegmenter::segment: have " << features.size()
cannam@24 326 << " features with " << features[0].size() << " coefficients (ncoeff = " << ncoeff << ", ncomponents = " << ncomponents << ")" << std::endl;
cannam@58 327 */
cannam@24 328 // copy the features to a native array and use the existing C segmenter...
cannam@24 329 double** arrFeatures = new double*[features.size()];
cannam@24 330 for (int i = 0; i < features.size(); i++)
cannam@24 331 {
cannam@24 332 if (featureType == FEATURE_TYPE_UNKNOWN) {
cannam@24 333 arrFeatures[i] = new double[features[0].size()];
cannam@24 334 for (int j = 0; j < features[0].size(); j++)
cannam@24 335 arrFeatures[i][j] = features[i][j];
cannam@24 336 } else {
cannam@24 337 arrFeatures[i] = new double[ncoeff+1]; // allow space for the normalised envelope
cannam@24 338 for (int j = 0; j < ncoeff; j++)
cannam@24 339 arrFeatures[i][j] = features[i][j];
cannam@24 340 }
cannam@24 341 }
cannam@18 342
cannam@24 343 q = new int[features.size()];
cannam@18 344
cannam@26 345 if (featureType == FEATURE_TYPE_UNKNOWN ||
cannam@26 346 featureType == FEATURE_TYPE_MFCC)
cannam@24 347 cluster_segment(q, arrFeatures, features.size(), features[0].size(), nHMMStates, histogramLength,
cannam@24 348 nclusters, neighbourhoodLimit);
cannam@24 349 else
cannam@24 350 constq_segment(q, arrFeatures, features.size(), nbins, ncoeff, featureType,
cannam@24 351 nHMMStates, histogramLength, nclusters, neighbourhoodLimit);
cannam@18 352
cannam@24 353 // convert the cluster assignment sequence to a segmentation
cannam@24 354 makeSegmentation(q, features.size());
cannam@18 355
cannam@24 356 // de-allocate arrays
cannam@24 357 delete [] q;
cannam@24 358 for (int i = 0; i < features.size(); i++)
cannam@24 359 delete [] arrFeatures[i];
cannam@24 360 delete [] arrFeatures;
cannam@18 361
cannam@24 362 // clear the features
cannam@24 363 clear();
cannam@18 364 }
cannam@18 365
cannam@18 366 void ClusterMeltSegmenter::makeSegmentation(int* q, int len)
cannam@18 367 {
cannam@24 368 segmentation.segments.clear();
cannam@24 369 segmentation.nsegtypes = nclusters;
cannam@24 370 segmentation.samplerate = samplerate;
cannam@18 371
cannam@24 372 Segment segment;
cannam@24 373 segment.start = 0;
cannam@24 374 segment.type = q[0];
cannam@18 375
cannam@24 376 for (int i = 1; i < len; i++)
cannam@24 377 {
cannam@24 378 if (q[i] != q[i-1])
cannam@24 379 {
cannam@24 380 segment.end = i * getHopsize();
cannam@24 381 segmentation.segments.push_back(segment);
cannam@24 382 segment.type = q[i];
cannam@24 383 segment.start = segment.end;
cannam@24 384 }
cannam@24 385 }
cannam@24 386 segment.end = len * getHopsize();
cannam@24 387 segmentation.segments.push_back(segment);
cannam@18 388 }
cannam@18 389