annotate dsp/segmentation/ClusterMeltSegmenter.cpp @ 252:89a2b34a098f

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