| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: mfcc-mean.ck |
| 3 |
// desc: mel-frequency cepstral coefficients |
| 4 |
// this example aggregates multiple MFCC frames; compute mean |
| 5 |
// |
| 6 |
// version: need chuck version 1.5.0.0 or higher |
| 7 |
// sorting: part of ChAI (ChucK for AI) |
| 8 |
// |
| 9 |
// to see API, uncomment: |
| 10 |
// MFCC.help(); |
| 11 |
// |
| 12 |
// author: Yikai Li and Ge Wang |
| 13 |
// date: Spring 2023 |
| 14 |
//--------------------------------------------------------------------- |
| 15 |
|
| 16 |
// analysis patch |
| 17 |
adc => FFT fft =^ MFCC mfcc => blackhole; |
| 18 |
|
| 19 |
// FFT size |
| 20 |
2048 => fft.size; |
| 21 |
// FFT hop size |
| 22 |
512 => int hopSize; |
| 23 |
// number of filters in MEL space |
| 24 |
10 => mfcc.numFilters; |
| 25 |
// number of MFCC coefficients to compute per frame |
| 26 |
13 => mfcc.numCoeffs => int nCoeffs; |
| 27 |
// used to compute the frame size |
| 28 |
.1::second => dur aggregateTime; |
| 29 |
// set FFT window |
| 30 |
Windowing.hamming(fft.size()) => fft.window; |
| 31 |
// current sample sample |
| 32 |
second / samp => float sampleRate; |
| 33 |
// this should already be set to the current chuck srate |
| 34 |
// but could be overridden, if desired |
| 35 |
// sampleRate => mfcc.sampleRate; |
| 36 |
|
| 37 |
// calculate the number of frames |
| 38 |
(aggregateTime/second * sampleRate / hopSize + .5) $ int => int nFrames; |
| 39 |
|
| 40 |
// print out |
| 41 |
chout <= IO.newline(); |
| 42 |
<<< "--------------------------------------------", "" >>>; |
| 43 |
<<< "num filters:", mfcc.numFilters() >>>; |
| 44 |
<<< "num MFCC coefficients:", mfcc.numCoeffs() >>>; |
| 45 |
<<< "agregrateTime:", aggregateTime/second, "seconds" >>>; |
| 46 |
<<< "num frames:", nFrames >>>; |
| 47 |
<<< "--------------------------------------------", "" >>>; |
| 48 |
chout <= IO.newline(); |
| 49 |
|
| 50 |
// per-frame MFCC coefficients |
| 51 |
float mfccs[nFrames][nCoeffs]; |
| 52 |
// average of MFCC coefficients across all frames |
| 53 |
float mfccMean [nCoeffs]; |
| 54 |
|
| 55 |
// let one FFTsize of time pass |
| 56 |
fft.size()::samp => now; |
| 57 |
|
| 58 |
// analysis loop |
| 59 |
while( true ) |
| 60 |
{ |
| 61 |
// aggregate mfcc features |
| 62 |
for( 0 => int fi; fi < nFrames; fi++ ) |
| 63 |
{ |
| 64 |
// triggers MFCC and its dependencies (e.g., FFT) |
| 65 |
mfcc.upchuck(); |
| 66 |
// get contents |
| 67 |
for( 0 => int i; i < nCoeffs; i++ ) |
| 68 |
{ |
| 69 |
// copy the resulting coeffs |
| 70 |
mfcc.fval(i) => mfccs[fi][i]; |
| 71 |
} |
| 72 |
// advance time by one hop |
| 73 |
hopSize::samp => now; |
| 74 |
} |
| 75 |
|
| 76 |
// compute mean |
| 77 |
for( 0 => int i; i < nCoeffs; i++ ) |
| 78 |
{ |
| 79 |
// zero out |
| 80 |
0.0 => mfccMean[i]; |
| 81 |
// sum |
| 82 |
for (0 => int j; j < nFrames; j++) |
| 83 |
{ |
| 84 |
mfccs[j][i] +=> mfccMean[i]; |
| 85 |
} |
| 86 |
// divide by frames |
| 87 |
nFrames /=> mfccMean[i]; |
| 88 |
// print |
| 89 |
chout <= mfccMean[i] <= " "; |
| 90 |
} |
| 91 |
|
| 92 |
// new line |
| 93 |
chout <= IO.newline(); |
| 94 |
} |