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@26
|
95 MFCCConfig config;
|
cannam@27
|
96 config.FS = samplerate / decimationFactor;
|
cannam@27
|
97 config.fftsize = 2048;
|
cannam@27
|
98 config.nceps = 19;
|
cannam@27
|
99 config.want_c0 = true;
|
cannam@26
|
100
|
cannam@26
|
101 mfcc = new MFCC(config);
|
cannam@27
|
102 ncoeff = config.nceps + 1;
|
cannam@24
|
103 }
|
cannam@18
|
104 }
|
cannam@18
|
105
|
cannam@18
|
106 ClusterMeltSegmenter::~ClusterMeltSegmenter()
|
cannam@18
|
107 {
|
cannam@24
|
108 delete window;
|
cannam@24
|
109 delete constq;
|
cannam@24
|
110 delete decimator;
|
cannam@20
|
111 }
|
cannam@20
|
112
|
cannam@20
|
113 int
|
cannam@20
|
114 ClusterMeltSegmenter::getWindowsize()
|
cannam@20
|
115 {
|
cannam@24
|
116 return static_cast<int>(windowSize * samplerate);
|
cannam@20
|
117 }
|
cannam@20
|
118
|
cannam@20
|
119 int
|
cannam@20
|
120 ClusterMeltSegmenter::getHopsize()
|
cannam@20
|
121 {
|
cannam@24
|
122 return static_cast<int>(hopSize * samplerate);
|
cannam@18
|
123 }
|
cannam@18
|
124
|
cannam@24
|
125 void ClusterMeltSegmenter::extractFeatures(const double* samples, int nsamples)
|
cannam@18
|
126 {
|
cannam@26
|
127 if (featureType == FEATURE_TYPE_CONSTQ ||
|
cannam@26
|
128 featureType == FEATURE_TYPE_CHROMA) {
|
cannam@26
|
129 extractFeaturesConstQ(samples, nsamples);
|
cannam@26
|
130 } else if (featureType == FEATURE_TYPE_MFCC) {
|
cannam@26
|
131 extractFeaturesMFCC(samples, nsamples);
|
cannam@26
|
132 }
|
cannam@26
|
133 }
|
cannam@26
|
134
|
cannam@26
|
135 void ClusterMeltSegmenter::extractFeaturesConstQ(const double* samples, int nsamples)
|
cannam@26
|
136 {
|
cannam@24
|
137 if (!constq) {
|
cannam@26
|
138 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeaturesConstQ: "
|
cannam@26
|
139 << "No const-q: initialise not called?"
|
cannam@24
|
140 << std::endl;
|
cannam@24
|
141 return;
|
cannam@24
|
142 }
|
cannam@20
|
143
|
cannam@24
|
144 if (nsamples < getWindowsize()) {
|
cannam@24
|
145 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeatures: nsamples < windowsize (" << nsamples << " < " << getWindowsize() << ")" << std::endl;
|
cannam@24
|
146 return;
|
cannam@24
|
147 }
|
cannam@24
|
148
|
cannam@24
|
149 int fftsize = constq->getfftlength();
|
cannam@24
|
150
|
cannam@24
|
151 if (!window || window->getSize() != fftsize) {
|
cannam@24
|
152 delete window;
|
cannam@24
|
153 window = new Window<double>(HammingWindow, fftsize);
|
cannam@24
|
154 }
|
cannam@24
|
155
|
cannam@24
|
156 vector<double> cq(ncoeff);
|
cannam@24
|
157
|
cannam@24
|
158 for (int i = 0; i < ncoeff; ++i) cq[i] = 0.0;
|
cannam@24
|
159
|
cannam@24
|
160 const double *psource = samples;
|
cannam@24
|
161 int pcount = nsamples;
|
cannam@24
|
162
|
cannam@24
|
163 if (decimator) {
|
cannam@24
|
164 pcount = nsamples / decimator->getFactor();
|
cannam@24
|
165 double *decout = new double[pcount];
|
cannam@24
|
166 decimator->process(samples, decout);
|
cannam@24
|
167 psource = decout;
|
cannam@24
|
168 }
|
cannam@24
|
169
|
cannam@24
|
170 int origin = 0;
|
cannam@24
|
171
|
cannam@24
|
172 // std::cerr << "nsamples = " << nsamples << ", pcount = " << pcount << std::endl;
|
cannam@24
|
173
|
cannam@24
|
174 int frames = 0;
|
cannam@24
|
175
|
cannam@24
|
176 double *frame = new double[fftsize];
|
cannam@24
|
177 double *real = new double[fftsize];
|
cannam@24
|
178 double *imag = new double[fftsize];
|
cannam@24
|
179 double *cqre = new double[ncoeff];
|
cannam@24
|
180 double *cqim = new double[ncoeff];
|
cannam@24
|
181
|
cannam@24
|
182 while (origin <= pcount) {
|
cannam@24
|
183
|
cannam@24
|
184 // always need at least one fft window per block, but after
|
cannam@24
|
185 // that we want to avoid having any incomplete ones
|
cannam@24
|
186 if (origin > 0 && origin + fftsize >= pcount) break;
|
cannam@24
|
187
|
cannam@24
|
188 for (int i = 0; i < fftsize; ++i) {
|
cannam@24
|
189 if (origin + i < pcount) {
|
cannam@24
|
190 frame[i] = psource[origin + i];
|
cannam@24
|
191 } else {
|
cannam@24
|
192 frame[i] = 0.0;
|
cannam@24
|
193 }
|
cannam@24
|
194 }
|
cannam@24
|
195
|
cannam@24
|
196 for (int i = 0; i < fftsize/2; ++i) {
|
cannam@24
|
197 double value = frame[i];
|
cannam@24
|
198 frame[i] = frame[i + fftsize/2];
|
cannam@24
|
199 frame[i + fftsize/2] = value;
|
cannam@24
|
200 }
|
cannam@24
|
201
|
cannam@24
|
202 window->cut(frame);
|
cannam@24
|
203
|
cannam@24
|
204 FFT::process(fftsize, false, frame, 0, real, imag);
|
cannam@24
|
205
|
cannam@24
|
206 constq->process(real, imag, cqre, cqim);
|
cannam@18
|
207
|
cannam@24
|
208 for (int i = 0; i < ncoeff; ++i) {
|
cannam@24
|
209 cq[i] += sqrt(cqre[i] * cqre[i] + cqim[i] * cqim[i]);
|
cannam@24
|
210 }
|
cannam@24
|
211 ++frames;
|
cannam@20
|
212
|
cannam@24
|
213 origin += fftsize/2;
|
cannam@24
|
214 }
|
cannam@20
|
215
|
cannam@24
|
216 delete [] cqre;
|
cannam@24
|
217 delete [] cqim;
|
cannam@24
|
218 delete [] real;
|
cannam@24
|
219 delete [] imag;
|
cannam@24
|
220 delete [] frame;
|
cannam@20
|
221
|
cannam@24
|
222 for (int i = 0; i < ncoeff; ++i) {
|
cannam@24
|
223 cq[i] /= frames;
|
cannam@24
|
224 }
|
cannam@20
|
225
|
cannam@24
|
226 if (decimator) delete[] psource;
|
cannam@20
|
227
|
cannam@24
|
228 features.push_back(cq);
|
cannam@18
|
229 }
|
cannam@18
|
230
|
cannam@26
|
231 void ClusterMeltSegmenter::extractFeaturesMFCC(const double* samples, int nsamples)
|
cannam@26
|
232 {
|
cannam@26
|
233 if (!mfcc) {
|
cannam@26
|
234 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeaturesMFCC: "
|
cannam@26
|
235 << "No mfcc: initialise not called?"
|
cannam@26
|
236 << std::endl;
|
cannam@26
|
237 return;
|
cannam@26
|
238 }
|
cannam@26
|
239
|
cannam@26
|
240 if (nsamples < getWindowsize()) {
|
cannam@26
|
241 std::cerr << "ERROR: ClusterMeltSegmenter::extractFeatures: nsamples < windowsize (" << nsamples << " < " << getWindowsize() << ")" << std::endl;
|
cannam@26
|
242 return;
|
cannam@26
|
243 }
|
cannam@26
|
244
|
cannam@26
|
245 int fftsize = mfcc->getfftlength();
|
cannam@26
|
246
|
cannam@26
|
247 vector<double> cc(ncoeff);
|
cannam@26
|
248
|
cannam@26
|
249 for (int i = 0; i < ncoeff; ++i) cc[i] = 0.0;
|
cannam@26
|
250
|
cannam@26
|
251 const double *psource = samples;
|
cannam@26
|
252 int pcount = nsamples;
|
cannam@26
|
253
|
cannam@27
|
254 if (decimator) {
|
cannam@27
|
255 pcount = nsamples / decimator->getFactor();
|
cannam@27
|
256 double *decout = new double[pcount];
|
cannam@27
|
257 decimator->process(samples, decout);
|
cannam@27
|
258 psource = decout;
|
cannam@27
|
259 }
|
cannam@27
|
260
|
cannam@26
|
261 int origin = 0;
|
cannam@26
|
262 int frames = 0;
|
cannam@26
|
263
|
cannam@26
|
264 double *frame = new double[fftsize];
|
cannam@26
|
265 double *ccout = new double[ncoeff];
|
cannam@26
|
266
|
cannam@26
|
267 while (origin <= pcount) {
|
cannam@26
|
268
|
cannam@26
|
269 // always need at least one fft window per block, but after
|
cannam@26
|
270 // that we want to avoid having any incomplete ones
|
cannam@26
|
271 if (origin > 0 && origin + fftsize >= pcount) break;
|
cannam@26
|
272
|
cannam@26
|
273 for (int i = 0; i < fftsize; ++i) {
|
cannam@26
|
274 if (origin + i < pcount) {
|
cannam@26
|
275 frame[i] = psource[origin + i];
|
cannam@26
|
276 } else {
|
cannam@26
|
277 frame[i] = 0.0;
|
cannam@26
|
278 }
|
cannam@26
|
279 }
|
cannam@26
|
280
|
cannam@26
|
281 mfcc->process(fftsize, frame, ccout);
|
cannam@26
|
282
|
cannam@26
|
283 for (int i = 0; i < ncoeff; ++i) {
|
cannam@26
|
284 cc[i] += ccout[i];
|
cannam@26
|
285 }
|
cannam@26
|
286 ++frames;
|
cannam@26
|
287
|
cannam@26
|
288 origin += fftsize/2;
|
cannam@26
|
289 }
|
cannam@26
|
290
|
cannam@26
|
291 delete [] ccout;
|
cannam@26
|
292 delete [] frame;
|
cannam@26
|
293
|
cannam@26
|
294 for (int i = 0; i < ncoeff; ++i) {
|
cannam@26
|
295 cc[i] /= frames;
|
cannam@26
|
296 }
|
cannam@26
|
297
|
cannam@27
|
298 if (decimator) delete[] psource;
|
cannam@27
|
299
|
cannam@26
|
300 features.push_back(cc);
|
cannam@26
|
301 }
|
cannam@26
|
302
|
cannam@18
|
303 void ClusterMeltSegmenter::segment(int m)
|
cannam@18
|
304 {
|
cannam@24
|
305 nclusters = m;
|
cannam@24
|
306 segment();
|
cannam@18
|
307 }
|
cannam@18
|
308
|
cannam@18
|
309 void ClusterMeltSegmenter::setFeatures(const vector<vector<double> >& f)
|
cannam@18
|
310 {
|
cannam@24
|
311 features = f;
|
cannam@24
|
312 featureType = FEATURE_TYPE_UNKNOWN;
|
cannam@18
|
313 }
|
cannam@18
|
314
|
cannam@18
|
315 void ClusterMeltSegmenter::segment()
|
cannam@18
|
316 {
|
cannam@26
|
317 delete constq;
|
cannam@26
|
318 constq = 0;
|
cannam@26
|
319 delete mfcc;
|
cannam@26
|
320 mfcc = 0;
|
cannam@26
|
321 delete decimator;
|
cannam@26
|
322 decimator = 0;
|
cannam@18
|
323
|
cannam@24
|
324 std::cerr << "ClusterMeltSegmenter::segment: have " << features.size()
|
cannam@24
|
325 << " features with " << features[0].size() << " coefficients (ncoeff = " << ncoeff << ", ncomponents = " << ncomponents << ")" << std::endl;
|
cannam@24
|
326
|
cannam@24
|
327 // copy the features to a native array and use the existing C segmenter...
|
cannam@24
|
328 double** arrFeatures = new double*[features.size()];
|
cannam@24
|
329 for (int i = 0; i < features.size(); i++)
|
cannam@24
|
330 {
|
cannam@24
|
331 if (featureType == FEATURE_TYPE_UNKNOWN) {
|
cannam@24
|
332 arrFeatures[i] = new double[features[0].size()];
|
cannam@24
|
333 for (int j = 0; j < features[0].size(); j++)
|
cannam@24
|
334 arrFeatures[i][j] = features[i][j];
|
cannam@24
|
335 } else {
|
cannam@24
|
336 arrFeatures[i] = new double[ncoeff+1]; // allow space for the normalised envelope
|
cannam@24
|
337 for (int j = 0; j < ncoeff; j++)
|
cannam@24
|
338 arrFeatures[i][j] = features[i][j];
|
cannam@24
|
339 }
|
cannam@24
|
340 }
|
cannam@18
|
341
|
cannam@24
|
342 q = new int[features.size()];
|
cannam@18
|
343
|
cannam@26
|
344 if (featureType == FEATURE_TYPE_UNKNOWN ||
|
cannam@26
|
345 featureType == FEATURE_TYPE_MFCC)
|
cannam@24
|
346 cluster_segment(q, arrFeatures, features.size(), features[0].size(), nHMMStates, histogramLength,
|
cannam@24
|
347 nclusters, neighbourhoodLimit);
|
cannam@24
|
348 else
|
cannam@24
|
349 constq_segment(q, arrFeatures, features.size(), nbins, ncoeff, featureType,
|
cannam@24
|
350 nHMMStates, histogramLength, nclusters, neighbourhoodLimit);
|
cannam@18
|
351
|
cannam@24
|
352 // convert the cluster assignment sequence to a segmentation
|
cannam@24
|
353 makeSegmentation(q, features.size());
|
cannam@18
|
354
|
cannam@24
|
355 // de-allocate arrays
|
cannam@24
|
356 delete [] q;
|
cannam@24
|
357 for (int i = 0; i < features.size(); i++)
|
cannam@24
|
358 delete [] arrFeatures[i];
|
cannam@24
|
359 delete [] arrFeatures;
|
cannam@18
|
360
|
cannam@24
|
361 // clear the features
|
cannam@24
|
362 clear();
|
cannam@18
|
363 }
|
cannam@18
|
364
|
cannam@18
|
365 void ClusterMeltSegmenter::makeSegmentation(int* q, int len)
|
cannam@18
|
366 {
|
cannam@24
|
367 segmentation.segments.clear();
|
cannam@24
|
368 segmentation.nsegtypes = nclusters;
|
cannam@24
|
369 segmentation.samplerate = samplerate;
|
cannam@18
|
370
|
cannam@24
|
371 Segment segment;
|
cannam@24
|
372 segment.start = 0;
|
cannam@24
|
373 segment.type = q[0];
|
cannam@18
|
374
|
cannam@24
|
375 for (int i = 1; i < len; i++)
|
cannam@24
|
376 {
|
cannam@24
|
377 if (q[i] != q[i-1])
|
cannam@24
|
378 {
|
cannam@24
|
379 segment.end = i * getHopsize();
|
cannam@24
|
380 segmentation.segments.push_back(segment);
|
cannam@24
|
381 segment.type = q[i];
|
cannam@24
|
382 segment.start = segment.end;
|
cannam@24
|
383 }
|
cannam@24
|
384 }
|
cannam@24
|
385 segment.end = len * getHopsize();
|
cannam@24
|
386 segmentation.segments.push_back(segment);
|
cannam@18
|
387 }
|
cannam@18
|
388
|