annotate examples/simpletest/simpletest.c @ 219:eb2ab9b70c29

Correct name for xtract_peak()
author Jamie Bullock <jamie@jamiebullock.com>
date Wed, 04 Jun 2014 14:15:19 +0100
parents 5357a64124f1
children
rev   line source
jamie@141 1 /*
jamie@141 2 * Copyright (C) 2012 Jamie Bullock
jamie@86 3 *
jamie@141 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jamie@141 5 * of this software and associated documentation files (the "Software"), to
jamie@141 6 * deal in the Software without restriction, including without limitation the
jamie@141 7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
jamie@141 8 * sell copies of the Software, and to permit persons to whom the Software is
jamie@141 9 * furnished to do so, subject to the following conditions:
jamie@86 10 *
jamie@141 11 * The above copyright notice and this permission notice shall be included in
jamie@141 12 * all copies or substantial portions of the Software.
jamie@86 13 *
jamie@141 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jamie@141 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jamie@141 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jamie@141 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jamie@141 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jamie@141 19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
jamie@141 20 * IN THE SOFTWARE.
jamie@141 21 *
jamie@86 22 */
jamie@29 23
jamie@29 24 #include "xtract/libxtract.h"
jamie@29 25 #include <stdio.h>
jamie@135 26 #include <stdlib.h>
jamie@165 27 #include <math.h>
jamie@165 28
jamie@166 29 #ifndef M_PI
jamie@166 30 #define M_PI 3.14159265358979323846264338327
jamie@166 31 #endif
jamie@166 32
jamie@165 33 typedef enum waveform_type_
jamie@165 34 {
jamie@165 35 SINE,
jamie@165 36 SAWTOOTH,
jamie@185 37 SQUARE,
jamie@185 38 NOISE
jamie@165 39 }
jamie@165 40 waveform_type;
jamie@29 41
jamie@135 42 #define BLOCKSIZE 1024
jamie@185 43 #define HALF_BLOCKSIZE BLOCKSIZE >> 1
jamie@135 44 #define SAMPLERATE 44100
jamie@165 45 #define PERIOD 102
jamie@135 46 #define MFCC_FREQ_BANDS 13
jamie@135 47 #define MFCC_FREQ_MIN 20
jamie@135 48 #define MFCC_FREQ_MAX 20000
jamie@29 49
jamie@165 50
jamie@165 51 double wavetable[BLOCKSIZE];
jamie@165 52
jamie@165 53 void fill_wavetable(const float frequency, waveform_type type)
jamie@165 54 {
jamie@165 55
jamie@165 56 int samples_per_period = SAMPLERATE / frequency;
jamie@165 57
jamie@165 58 for (int i = 0; i < BLOCKSIZE; ++i)
jamie@165 59 {
jamie@165 60 int phase = i % samples_per_period;
jamie@165 61
jamie@165 62 switch (type)
jamie@165 63 {
jamie@165 64 case SINE:
jamie@165 65 wavetable[i] = sin((phase / (double)PERIOD) * 2 * M_PI);
jamie@165 66 break;
jamie@165 67 case SQUARE:
jamie@165 68 if (phase < (samples_per_period / 2.f))
jamie@165 69 {
jamie@165 70 wavetable[i] = -1.0;
jamie@165 71 }
jamie@165 72 else
jamie@165 73 {
jamie@165 74 wavetable[i] = 1.0;
jamie@165 75 }
jamie@165 76 break;
jamie@165 77 case SAWTOOTH:
jamie@165 78 wavetable[i] = ((phase / (double)PERIOD) * 2) - 1.;
jamie@165 79 break;
jamie@185 80 case NOISE:
jamie@200 81 wavetable[i] = ((random() % 1000) / 500.0) - 1;
jamie@185 82 break;
jamie@165 83 }
jamie@165 84 }
jamie@165 85 }
jamie@165 86
jamie@165 87 void print_wavetable(void)
jamie@165 88 {
jamie@165 89 for (int i = 0; i < BLOCKSIZE; ++i)
jamie@165 90 {
jamie@165 91 printf("%f\n", wavetable[i]);
jamie@165 92 }
jamie@165 93 }
jamie@165 94
jamie@135 95 int main(void)
jamie@135 96 {
jamie@165 97 double mean = 0.0;
jamie@165 98 double f0 = 0.0;
jamie@207 99 double midicents = 0.0;
jamie@185 100 double flux = 0.0;
jamie@165 101 double centroid = 0.0;
jamie@192 102 double lowest = 0.0;
jamie@174 103 double spectrum[BLOCKSIZE] = {0};
jamie@174 104 double windowed[BLOCKSIZE] = {0};
jamie@174 105 double peaks[BLOCKSIZE] = {0};
jamie@174 106 double harmonics[BLOCKSIZE] = {0};
jamie@185 107 double subframes[BLOCKSIZE] = {0};
jamie@185 108 double difference[HALF_BLOCKSIZE] = {0};
jamie@165 109 double *window = NULL;
jamie@174 110 double mfccs[MFCC_FREQ_BANDS] = {0};
jamie@172 111 double argd[4] = {0};
jamie@165 112 double samplerate = 44100.0;
jamie@135 113 int n;
jamie@192 114 int rv = XTRACT_SUCCESS;
jamie@135 115 xtract_mel_filter mel_filters;
jamie@120 116
jamie@207 117 // fill_wavetable(344.53125f, NOISE); // 344.53125f = 128 samples @ 44100 Hz
jamie@207 118 fill_wavetable(344.53125f, SINE); // 344.53125f = 128 samples @ 44100 Hz
jamie@207 119
jamie@200 120 /*
jamie@185 121 print_wavetable();
jamie@200 122 */
jamie@165 123
jamie@165 124 /* get the F0 */
jamie@165 125 xtract[XTRACT_WAVELET_F0](wavetable, BLOCKSIZE, &samplerate, &f0);
jamie@165 126 printf("\nF0: %f\n", f0);
jamie@135 127
jamie@207 128 /* get the F0 as a MIDI note */
jamie@207 129 xtract[XTRACT_MIDICENT](NULL, 0, &f0, &midicents);
jamie@207 130 printf("\nMIDI cents: %f\n", midicents);
jamie@207 131
jamie@135 132 /* get the mean of the input */
jamie@165 133 xtract[XTRACT_MEAN](wavetable, BLOCKSIZE, NULL, &mean);
jamie@165 134 printf("\nInput mean = %.2f\n\n", mean); /* We expect this to be zero for a square wave */
jamie@165 135
jamie@192 136 /* get the lowest value in the input */
jamie@192 137 argd[0] = -.5;
jamie@192 138 rv = xtract[XTRACT_LOWEST_VALUE](wavetable, BLOCKSIZE, argd, &lowest);
jamie@192 139
jamie@192 140 if (rv == XTRACT_SUCCESS)
jamie@192 141 {
jamie@192 142 printf("\nLowest value = %.6f\n\n", lowest);
jamie@192 143 }
jamie@192 144 else
jamie@192 145 {
jamie@192 146 printf("\nUnable to get lowest value, all values below threshold?\n\n");
jamie@192 147 }
jamie@200 148
jamie@165 149 /* create the window function */
jamie@165 150 window = xtract_init_window(BLOCKSIZE, XTRACT_HANN);
jamie@165 151 xtract_windowed(wavetable, BLOCKSIZE, window, windowed);
jamie@174 152 xtract_free_window(window);
jamie@135 153
jamie@135 154 /* get the spectrum */
jamie@146 155 argd[0] = SAMPLERATE / (double)BLOCKSIZE;
jamie@146 156 argd[1] = XTRACT_MAGNITUDE_SPECTRUM;
jamie@177 157 argd[2] = 0.f; /* DC component - we expect this to zero for square wave */
jamie@146 158 argd[3] = 0.f; /* No Normalisation */
jamie@29 159
jamie@135 160 xtract_init_fft(BLOCKSIZE, XTRACT_SPECTRUM);
jamie@165 161 xtract[XTRACT_SPECTRUM](windowed, BLOCKSIZE, &argd[0], spectrum);
jamie@185 162 xtract_free_fft();
jamie@165 163
jamie@165 164 xtract[XTRACT_SPECTRAL_CENTROID](spectrum, BLOCKSIZE, NULL, &centroid);
jamie@165 165 printf("\nSpectral Centroid: %f\n", centroid);
jamie@165 166
jamie@165 167 argd[1] = 10.0; /* peak threshold as % of maximum peak */
jamie@165 168 xtract[XTRACT_PEAK_SPECTRUM](spectrum, BLOCKSIZE / 2, argd, peaks);
jamie@165 169
jamie@165 170 argd[0] = f0;
jamie@165 171 argd[1] = .3; /* harmonic threshold */
jamie@165 172 xtract[XTRACT_HARMONIC_SPECTRUM](peaks, BLOCKSIZE, argd, harmonics);
jamie@120 173
jamie@135 174 /* print the spectral bins */
jamie@165 175 printf("\nSpectrum:\n");
jamie@165 176 for(n = 0; n < (BLOCKSIZE >> 1); ++n)
jamie@165 177 {
jamie@165 178 printf("freq: %.1f\tamp: %.6f", spectrum[n + (BLOCKSIZE >> 1)], spectrum[n]);
jamie@165 179 if (peaks[n + (BLOCKSIZE >> 1)] != 0.f)
jamie@165 180 {
jamie@165 181 printf("\tpeak:: freq: %.1f\tamp: %.6f\n", peaks[n + (BLOCKSIZE >> 1)], peaks[n]);
jamie@165 182 }
jamie@165 183 else
jamie@165 184 {
jamie@165 185 printf("\n");
jamie@165 186 }
jamie@120 187 }
jamie@120 188 printf("\n");
jamie@120 189
jamie@135 190 /* compute the MFCCs */
jamie@135 191 mel_filters.n_filters = MFCC_FREQ_BANDS;
jamie@155 192 mel_filters.filters = (double **)malloc(MFCC_FREQ_BANDS * sizeof(double *));
jamie@135 193 for(n = 0; n < MFCC_FREQ_BANDS; ++n)
jamie@135 194 {
jamie@155 195 mel_filters.filters[n] = (double *)malloc(BLOCKSIZE * sizeof(double));
jamie@135 196 }
jamie@135 197
jamie@135 198 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 199 xtract_mfcc(spectrum, BLOCKSIZE >> 1, &mel_filters, mfccs);
jamie@135 200
jamie@135 201 /* print the MFCCs */
jamie@135 202 printf("MFCCs:\n");
jamie@135 203 for(n = 0; n < MFCC_FREQ_BANDS; ++n)
jamie@135 204 {
jamie@135 205 printf("band: %d\t", n);
jamie@135 206 if(n < 10) {
jamie@135 207 printf("\t");
jamie@135 208 }
jamie@135 209 printf("coeff: %f\n", mfccs[n]);
jamie@135 210 }
jamie@135 211
jamie@185 212 /* compute Spectral Flux */
jamie@185 213 argd[0] = SAMPLERATE / HALF_BLOCKSIZE;
jamie@185 214 argd[1] = XTRACT_MAGNITUDE_SPECTRUM;
jamie@185 215 argd[2] = 0.f; /* DC component */
jamie@185 216 argd[3] = 0.f; /* No Normalisation */
jamie@185 217
jamie@185 218 xtract_init_fft(HALF_BLOCKSIZE, XTRACT_SPECTRUM);
jamie@185 219 xtract_features_from_subframes(wavetable, BLOCKSIZE, XTRACT_SPECTRUM, argd, subframes);
jamie@185 220 xtract_difference_vector(subframes, BLOCKSIZE, NULL, difference);
jamie@185 221
jamie@185 222 argd[0] = 1.0; /* norm order */
jamie@185 223 argd[1] = XTRACT_POSITIVE_SLOPE; /* positive slope */
jamie@185 224
jamie@185 225 xtract_flux(difference, HALF_BLOCKSIZE, argd, &flux);
jamie@185 226
jamie@185 227 printf("Flux: %f\n", flux);
jamie@185 228
jamie@135 229 /* cleanup */
jamie@135 230 for(n = 0; n < MFCC_FREQ_BANDS; ++n)
jamie@135 231 {
jamie@135 232 free(mel_filters.filters[n]);
jamie@135 233 }
jamie@135 234 free(mel_filters.filters);
jamie@135 235
jamie@29 236 return 0;
jamie@135 237
jamie@29 238 }