jamie@141: /* jamie@141: * Copyright (C) 2012 Jamie Bullock jamie@86: * jamie@141: * Permission is hereby granted, free of charge, to any person obtaining a copy jamie@141: * of this software and associated documentation files (the "Software"), to jamie@141: * deal in the Software without restriction, including without limitation the jamie@141: * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or jamie@141: * sell copies of the Software, and to permit persons to whom the Software is jamie@141: * furnished to do so, subject to the following conditions: jamie@86: * jamie@141: * The above copyright notice and this permission notice shall be included in jamie@141: * all copies or substantial portions of the Software. jamie@86: * jamie@141: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jamie@141: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jamie@141: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jamie@141: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jamie@141: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING jamie@141: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS jamie@141: * IN THE SOFTWARE. jamie@141: * jamie@86: */ jamie@29: jamie@29: #include "xtract/libxtract.h" jamie@29: #include jamie@135: #include jamie@29: jamie@135: #define BLOCKSIZE 1024 jamie@135: #define SAMPLERATE 44100 jamie@135: #define PERIOD 100 jamie@135: #define MFCC_FREQ_BANDS 13 jamie@135: #define MFCC_FREQ_MIN 20 jamie@135: #define MFCC_FREQ_MAX 20000 jamie@29: jamie@135: int main(void) jamie@135: { jamie@135: jamie@146: double mean = 0.f; jamie@146: double input[BLOCKSIZE]; jamie@146: double spectrum[BLOCKSIZE]; jamie@146: double mfccs[MFCC_FREQ_BANDS * sizeof(double)]; jamie@146: double argd[4]; jamie@135: int n; jamie@135: xtract_mel_filter mel_filters; jamie@120: jamie@135: /* fill the input array with a sawtooth wave */ jamie@135: for(n = 0; n < BLOCKSIZE; ++n) jamie@135: { jamie@146: input[n] = ((n % PERIOD) / (double)PERIOD) - .5; jamie@135: } jamie@135: jamie@135: /* get the mean of the input */ jamie@156: xtract[XTRACT_MEAN](input, BLOCKSIZE, NULL, &mean); jamie@135: printf("\nInput mean = %.2f\n\n", mean); jamie@135: jamie@135: /* get the spectrum */ jamie@146: argd[0] = SAMPLERATE / (double)BLOCKSIZE; jamie@146: argd[1] = XTRACT_MAGNITUDE_SPECTRUM; jamie@146: argd[2] = 0.f; /* No DC component */ jamie@146: argd[3] = 0.f; /* No Normalisation */ jamie@29: jamie@135: xtract_init_fft(BLOCKSIZE, XTRACT_SPECTRUM); jamie@156: xtract[XTRACT_SPECTRUM](input, BLOCKSIZE, &argd[0], spectrum); jamie@120: jamie@135: /* print the spectral bins */ jamie@135: printf("\nSpectral bins:\n"); jamie@135: for(n = 0; n < (BLOCKSIZE >> 1); ++n){ jamie@135: printf("freq: %.1f\tamp: %.6f\n", spectrum[n + (BLOCKSIZE >> 1)], spectrum[n]); jamie@120: } jamie@120: printf("\n"); jamie@120: jamie@135: /* compute the MFCCs */ jamie@135: mel_filters.n_filters = MFCC_FREQ_BANDS; jamie@155: mel_filters.filters = (double **)malloc(MFCC_FREQ_BANDS * sizeof(double *)); jamie@135: for(n = 0; n < MFCC_FREQ_BANDS; ++n) jamie@135: { jamie@155: mel_filters.filters[n] = (double *)malloc(BLOCKSIZE * sizeof(double)); jamie@135: } jamie@135: jamie@135: xtract_init_mfcc(BLOCKSIZE >> 1, SAMPLERATE >> 1, XTRACT_EQUAL_GAIN, MFCC_FREQ_MIN, MFCC_FREQ_MAX, mel_filters.n_filters, mel_filters.filters); jamie@135: xtract_mfcc(spectrum, BLOCKSIZE >> 1, &mel_filters, mfccs); jamie@135: jamie@135: /* print the MFCCs */ jamie@135: printf("MFCCs:\n"); jamie@135: for(n = 0; n < MFCC_FREQ_BANDS; ++n) jamie@135: { jamie@135: printf("band: %d\t", n); jamie@135: if(n < 10) { jamie@135: printf("\t"); jamie@135: } jamie@135: printf("coeff: %f\n", mfccs[n]); jamie@135: } jamie@135: jamie@135: /* cleanup */ jamie@135: for(n = 0; n < MFCC_FREQ_BANDS; ++n) jamie@135: { jamie@135: free(mel_filters.filters[n]); jamie@135: } jamie@135: free(mel_filters.filters); jamie@135: jamie@29: return 0; jamie@135: jamie@29: }