jamie@1
|
1 /* libxtract feature extraction library
|
jamie@1
|
2 *
|
jamie@1
|
3 * Copyright (C) 2006 Jamie Bullock
|
jamie@1
|
4 *
|
jamie@1
|
5 * This program is free software; you can redistribute it and/or modify
|
jamie@1
|
6 * it under the terms of the GNU General Public License as published by
|
jamie@1
|
7 * the Free Software Foundation; either version 2 of the License, or
|
jamie@1
|
8 * (at your option) any later version.
|
jamie@1
|
9 *
|
jamie@1
|
10 * This program is distributed in the hope that it will be useful,
|
jamie@1
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jamie@1
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jamie@1
|
13 * GNU General Public License for more details.
|
jamie@1
|
14 *
|
jamie@1
|
15 * You should have received a copy of the GNU General Public License
|
jamie@1
|
16 * along with this program; if not, write to the Free Software
|
jamie@1
|
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
|
jamie@1
|
18 * USA.
|
jamie@1
|
19 */
|
jamie@1
|
20
|
jamie@20
|
21 /** \mainpage
|
jamie@20
|
22 *
|
jamie@20
|
23 * LibXtract is a simple, portable, lightweight library of audio feature extraction functions. The purpose of the library is to provide a relatively exhaustive set of feature extraction primatives that are designed to be 'cascaded' to create a extraction hierarchies.
|
jamie@20
|
24 * For example, 'variance', 'average deviation', 'skewness' and 'kurtosis', all require the 'mean' of the input vector to be precomputed. However, rather than compute the 'mean' 'inside' each function, it is expected that the 'mean' will be passed in as an argument. This means that if the user wishes to use all of these features, the mean is calculated only once, and then passed to any functions that require it.
|
jamie@20
|
25 *
|
jamie@20
|
26 * This philosophy of 'cascading' features is followed throughout the library, for example with features that operate on the magnitude spectrum of a signal vector (e.g. 'irregularity'), the magnitude spectrum is not calculated 'inside' the respective function, instead, a pointer to the first element in an array containing the magnitude spectrum is passed in as an argument.
|
jamie@20
|
27 *
|
jamie@20
|
28 * Hopefully this not only makes the library more efficient when computing large numbers of features, but also makes it more flexible because extraction functions can be combined arbitrarily (one can take the irregularility of the Mel Frequency Cepstral Coefficients for example).
|
jamie@20
|
29 *
|
jamie@20
|
30 */
|
jamie@20
|
31
|
jamie@1
|
32 #ifndef XTRACT_H
|
jamie@1
|
33 #define XTRACT_H
|
jamie@1
|
34
|
jamie@1
|
35 #ifdef __cplusplus
|
jamie@1
|
36 extern "C" {
|
jamie@1
|
37 #endif
|
jamie@1
|
38
|
jamie@21
|
39 /**
|
jamie@21
|
40 * \file libxtract.h
|
jamie@21
|
41 * \brief main header file and API definition
|
jamie@1
|
42 */
|
jamie@1
|
43
|
jamie@1
|
44 #include "xtract_scalar.h"
|
jamie@1
|
45 #include "xtract_vector.h"
|
jamie@1
|
46 #include "xtract_delta.h"
|
jamie@1
|
47 #include "xtract_types.h"
|
jamie@1
|
48 #include "xtract_macros.h"
|
jamie@1
|
49
|
jamie@20
|
50 /** \defgroup libxtract API
|
jamie@20
|
51 *
|
jamie@20
|
52 * Defines a very simple API that provides access to the functions in the library
|
jamie@20
|
53 * @{
|
jamie@20
|
54 */
|
jamie@20
|
55
|
jamie@59
|
56 #define XTRACT_FEATURES 54
|
jamie@30
|
57
|
jamie@1
|
58 /** \brief Enumeration of features, elements are used as indixes to an array of pointers to feature extracton functions */
|
jamie@56
|
59 enum xtract_features_ {
|
jamie@56
|
60 XTRACT_MEAN,
|
jamie@56
|
61 XTRACT_VARIANCE,
|
jamie@56
|
62 XTRACT_STANDARD_DEVIATION,
|
jamie@56
|
63 XTRACT_AVERAGE_DEVIATION,
|
jamie@56
|
64 XTRACT_SKEWNESS,
|
jamie@56
|
65 XTRACT_KURTOSIS,
|
jamie@56
|
66 XTRACT_SPECTRAL_MEAN,
|
jamie@56
|
67 XTRACT_SPECTRAL_VARIANCE,
|
jamie@56
|
68 XTRACT_SPECTRAL_STANDARD_DEVIATION,
|
jamie@56
|
69 XTRACT_SPECTRAL_AVERAGE_DEVIATION,
|
jamie@56
|
70 XTRACT_SPECTRAL_SKEWNESS,
|
jamie@56
|
71 XTRACT_SPECTRAL_KURTOSIS,
|
jamie@56
|
72 XTRACT_SPECTRAL_CENTROID,
|
jamie@56
|
73 XTRACT_IRREGULARITY_K,
|
jamie@56
|
74 XTRACT_IRREGULARITY_J,
|
jamie@56
|
75 XTRACT_TRISTIMULUS_1,
|
jamie@56
|
76 XTRACT_TRISTIMULUS_2,
|
jamie@56
|
77 XTRACT_TRISTIMULUS_3,
|
jamie@56
|
78 XTRACT_SMOOTHNESS,
|
jamie@56
|
79 XTRACT_SPREAD,
|
jamie@56
|
80 XTRACT_ZCR,
|
jamie@56
|
81 XTRACT_ROLLOFF,
|
jamie@56
|
82 XTRACT_LOUDNESS,
|
jamie@56
|
83 XTRACT_FLATNESS,
|
jamie@56
|
84 XTRACT_TONALITY,
|
jamie@56
|
85 XTRACT_CREST,
|
jamie@56
|
86 XTRACT_NOISINESS,
|
jamie@56
|
87 XTRACT_RMS_AMPLITUDE,
|
jamie@56
|
88 XTRACT_SPECTRAL_INHARMONICITY,
|
jamie@56
|
89 XTRACT_POWER,
|
jamie@56
|
90 XTRACT_ODD_EVEN_RATIO,
|
jamie@56
|
91 XTRACT_SHARPNESS,
|
jamie@56
|
92 XTRACT_SPECTRAL_SLOPE,
|
jamie@56
|
93 XTRACT_LOWEST_VALUE,
|
jamie@56
|
94 XTRACT_HIGHEST_VALUE,
|
jamie@56
|
95 XTRACT_SUM,
|
jamie@59
|
96 XTRACT_NONZERO_COUNT,
|
jamie@56
|
97 XTRACT_HPS,
|
jamie@56
|
98 XTRACT_F0,
|
jamie@56
|
99 XTRACT_FAILSAFE_F0,
|
jamie@56
|
100 XTRACT_FLUX,
|
jamie@56
|
101 XTRACT_ATTACK_TIME,
|
jamie@56
|
102 XTRACT_DECAY_TIME,
|
jamie@56
|
103 XTRACT_DELTA_FEATURE,
|
jamie@56
|
104 XTRACT_AUTOCORRELATION,
|
jamie@56
|
105 XTRACT_AMDF,
|
jamie@56
|
106 XTRACT_ASDF,
|
jamie@56
|
107 XTRACT_BARK_COEFFICIENTS,
|
jamie@56
|
108 XTRACT_PEAK_SPECTRUM,
|
jamie@56
|
109 XTRACT_SPECTRUM,
|
jamie@56
|
110 XTRACT_AUTOCORRELATION_FFT,
|
jamie@56
|
111 XTRACT_MFCC,
|
jamie@56
|
112 XTRACT_DCT,
|
jamie@56
|
113 XTRACT_HARMONIC_SPECTRUM
|
jamie@1
|
114 };
|
jamie@1
|
115
|
jamie@55
|
116 /** \brief Enumeration of feature initialisation functions */
|
jamie@56
|
117 enum xtract_feature_init_ {
|
jamie@56
|
118 XTRACT_INIT_MFCC = 100,
|
jamie@56
|
119 XTRACT_INIT_BARK
|
jamie@55
|
120 };
|
jamie@55
|
121
|
jamie@1
|
122 /** \brief Enumeration of feature types */
|
jamie@56
|
123 enum xtract_feature_types_ {
|
jamie@56
|
124 XTRACT_SCALAR,
|
jamie@56
|
125 XTRACT_VECTOR,
|
jamie@56
|
126 XTRACT_DELTA
|
jamie@1
|
127 };
|
jamie@1
|
128
|
jamie@1
|
129 /** \brief Enumeration of mfcc types */
|
jamie@56
|
130 enum xtract_mfcc_types_ {
|
jamie@56
|
131 XTRACT_EQUAL_GAIN,
|
jamie@56
|
132 XTRACT_EQUAL_AREA
|
jamie@1
|
133 };
|
jamie@1
|
134
|
jamie@1
|
135 /** \brief Enumeration of return codes */
|
jamie@56
|
136 enum xtract_return_codes_ {
|
jamie@56
|
137 XTRACT_SUCCESS,
|
jamie@56
|
138 XTRACT_MALLOC_FAILED,
|
jamie@56
|
139 XTRACT_BAD_ARGV,
|
jamie@56
|
140 XTRACT_BAD_VECTOR_SIZE,
|
jamie@56
|
141 XTRACT_NO_RESULT,
|
jamie@56
|
142 XTRACT_FEATURE_NOT_IMPLEMENTED
|
jamie@1
|
143 };
|
jamie@1
|
144
|
jamie@54
|
145 /** \brief Enumeration of spectrum types */
|
jamie@56
|
146 enum xtract_spectrum_ {
|
jamie@56
|
147 XTRACT_MAGNITUDE_SPECTRUM,
|
jamie@56
|
148 XTRACT_LOG_MAGNITUDE_SPECTRUM,
|
jamie@56
|
149 XTRACT_POWER_SPECTRUM,
|
jamie@56
|
150 XTRACT_LOG_POWER_SPECTRUM
|
jamie@54
|
151 };
|
jamie@54
|
152
|
jamie@50
|
153 /** \brief Enumeration of data types*/
|
jamie@50
|
154 typedef enum type_ {
|
jamie@56
|
155 XTRACT_FLOAT,
|
jamie@56
|
156 XTRACT_FLOATARRAY,
|
jamie@56
|
157 XTRACT_INT,
|
jamie@56
|
158 XTRACT_MEL_FILTER
|
jamie@56
|
159 } xtract_type_t;
|
jamie@50
|
160
|
jamie@50
|
161 /** \brief Enumeration of units*/
|
jamie@50
|
162 typedef enum unit_ {
|
jamie@55
|
163 /* NONE, ANY */
|
jamie@56
|
164 XTRACT_HERTZ = 2,
|
jamie@56
|
165 XTRACT_ANY_AMPLITUDE_HERTZ,
|
jamie@56
|
166 XTRACT_DBFS,
|
jamie@56
|
167 XTRACT_DBFS_HERTZ,
|
jamie@56
|
168 XTRACT_PERCENT,
|
jamie@56
|
169 XTRACT_SONE
|
jamie@56
|
170 } xtract_unit_t;
|
jamie@50
|
171
|
jamie@50
|
172 /** \brief Boolean */
|
jamie@50
|
173 typedef enum {
|
jamie@56
|
174 XTRACT_FALSE,
|
jamie@56
|
175 XTRACT_TRUE
|
jamie@56
|
176 } xtract_bool_t;
|
jamie@50
|
177
|
jamie@50
|
178 /** \brief Enumeration of vector format types*/
|
jamie@56
|
179 typedef enum xtract_vector_ {
|
jamie@54
|
180 /* N/2 magnitude/log-magnitude/power/log-power coeffs and N/2 frequencies */
|
jamie@56
|
181 XTRACT_SPECTRAL,
|
jamie@54
|
182 /* N spectral amplitudes */
|
jamie@56
|
183 XTRACT_SPECTRAL_MAGNITUDES,
|
jamie@54
|
184 /* N/2 magnitude/log-magnitude/power/log-power peak coeffs and N/2
|
jamie@54
|
185 * frequencies */
|
jamie@56
|
186 XTRACT_SPECTRAL_PEAKS,
|
jamie@54
|
187 /* N spectral peak amplitudes */
|
jamie@59
|
188 XTRACT_SPECTRAL_PEAKS_MAGNITUDES,
|
jamie@59
|
189 /* N spectral peak frequencies */
|
jamie@59
|
190 XTRACT_SPECTRAL_PEAKS_FREQUENCIES,
|
jamie@54
|
191 /* N/2 magnitude/log-magnitude/power/log-power harmonic peak coeffs and N/2
|
jamie@54
|
192 * frequencies */
|
jamie@56
|
193 XTRACT_SPECTRAL_HARMONICS,
|
jamie@54
|
194 /* N spectral harmonic amplitudes */
|
jamie@56
|
195 XTRACT_SPECTRAL_HARMONICS_MAGNITUDES,
|
jamie@54
|
196 /* N spectral harmonic frequencies */
|
jamie@56
|
197 XTRACT_SPECTRAL_HARMONICS_FREQUENCIES,
|
jamie@56
|
198 XTRACT_ARBITRARY_SERIES,
|
jamie@56
|
199 XTRACT_AUDIO_SAMPLES,
|
jamie@56
|
200 XTRACT_MEL_COEFFS,
|
jamie@56
|
201 XTRACT_BARK_COEFFS,
|
jamie@56
|
202 XTRACT_NO_DATA
|
jamie@56
|
203 } xtract_vector_t;
|
jamie@50
|
204
|
jamie@50
|
205 /** \brief Data structure containing useful information about functions provided by LibXtract. */
|
jamie@56
|
206 typedef struct _xtract_function_descriptor {
|
jamie@50
|
207
|
jamie@50
|
208 struct {
|
jamie@56
|
209 char name[XTRACT_MAX_NAME_LENGTH];
|
jamie@56
|
210 char p_name[XTRACT_MAX_NAME_LENGTH]; /* pretty name */
|
jamie@56
|
211 char desc[XTRACT_MAX_DESC_LENGTH];
|
jamie@56
|
212 char p_desc[XTRACT_MAX_DESC_LENGTH]; /* pretty description */
|
jamie@56
|
213 char author[XTRACT_MAX_AUTHOR_LENGTH];
|
jamie@50
|
214 int year;
|
jamie@50
|
215 } algo;
|
jamie@50
|
216
|
jamie@50
|
217 struct {
|
jamie@56
|
218 xtract_vector_t format;
|
jamie@56
|
219 xtract_unit_t unit;
|
jamie@50
|
220 } data;
|
jamie@50
|
221
|
jamie@51
|
222 int argc;
|
jamie@50
|
223
|
jamie@50
|
224 struct {
|
jamie@56
|
225 xtract_type_t type; /* type of the array/value pointed to by argv */
|
jamie@56
|
226 float min[XTRACT_MAXARGS];
|
jamie@56
|
227 float max[XTRACT_MAXARGS];
|
jamie@56
|
228 float def[XTRACT_MAXARGS]; /* defaults */
|
jamie@56
|
229 xtract_unit_t unit[XTRACT_MAXARGS];
|
jamie@56
|
230 int donor[XTRACT_MAXARGS]; /* suggested donor functions for argv */
|
jamie@50
|
231 } argv;
|
jamie@50
|
232
|
jamie@56
|
233 xtract_bool_t is_scalar;
|
jamie@50
|
234
|
jamie@55
|
235 /* The result.<> entries in descritors.c need to be checked */
|
jamie@50
|
236 union {
|
jamie@50
|
237
|
jamie@50
|
238 struct {
|
jamie@50
|
239 float min;
|
jamie@50
|
240 float max;
|
jamie@56
|
241 xtract_unit_t unit;
|
jamie@50
|
242 } scalar;
|
jamie@50
|
243
|
jamie@50
|
244 struct {
|
jamie@56
|
245 xtract_vector_t format;
|
jamie@56
|
246 xtract_unit_t unit;
|
jamie@50
|
247 } vector;
|
jamie@50
|
248
|
jamie@50
|
249 } result;
|
jamie@50
|
250
|
jamie@56
|
251 } xtract_function_descriptor_t;
|
jamie@50
|
252
|
jamie@1
|
253 /**
|
jamie@1
|
254 *
|
jamie@2
|
255 * \brief An array of pointers to functions that perform the extraction
|
jamie@1
|
256 *
|
jamie@2
|
257 * \param *data: a pointer to the start of the input data (usually the first element in an array)
|
jamie@1
|
258 *
|
jamie@2
|
259 * \param N: the number of elements to be processed
|
jamie@1
|
260 *
|
jamie@2
|
261 * \param *argv: an abitrary number of additional arguments, used to pass additional parameters to the function being called
|
jamie@1
|
262 *
|
jamie@2
|
263 * \param *result: a pointer to the first element in the result
|
jamie@1
|
264 *
|
jamie@1
|
265 * Each function will iterate over N array elements, the first of which is
|
jamie@2
|
266 * pointed to by *data. It is up to the calling function to ensure that the array is in the format expected by the function being called.
|
jamie@1
|
267 *
|
jamie@1
|
268 * For scalar and delta features, *result will point to a single value.
|
jamie@1
|
269 *
|
jamie@1
|
270 * For vector features it will point to the first element in an array.
|
jamie@1
|
271 *
|
jamie@1
|
272 * Memory for this array must be allocated and freed by the calling
|
jamie@1
|
273 * function.
|
jamie@1
|
274 *
|
jamie@21
|
275 * All functions return an integer error code as descibed in the enumeration
|
jamie@21
|
276 * return_codes_
|
jamie@2
|
277 *
|
jamie@21
|
278 * The preprocessor macro: XTRACT must be defined before this can be used
|
jamie@9
|
279 *
|
jamie@2
|
280 * example:<br>
|
jamie@21
|
281 * \verbatim
|
jamie@21
|
282 #include <stdio.h>
|
jamie@21
|
283 #define XTRACT
|
jamie@21
|
284 #include "libxtract.h"
|
jamie@21
|
285
|
jamie@21
|
286 main () {
|
jamie@21
|
287 float values[] = {1.0, 2.0, 3.0, 4.0, 5.0};
|
jamie@21
|
288 int N = 5;
|
jamie@21
|
289 float mean;
|
jamie@21
|
290
|
jamie@21
|
291 xtract[MEAN]((void *)values, N, NULL, &mean);
|
jamie@21
|
292
|
jamie@21
|
293 printf("Mean = %.2f\n", mean);
|
jamie@21
|
294 }
|
jamie@21
|
295 \endverbatim
|
jamie@9
|
296 * The calling function may additionally make some tests against the value returned by xtract
|
jamie@9
|
297 *
|
jamie@2
|
298 */
|
jamie@56
|
299 #ifdef XTRACT_H
|
jamie@43
|
300 extern int(*xtract[XTRACT_FEATURES])(const float *data, const int N, const void *argv, float *result);
|
jamie@1
|
301
|
jamie@28
|
302 #endif
|
jamie@26
|
303
|
jamie@2
|
304 /** \brief A structure to store a set of n_filters Mel filters */
|
jamie@1
|
305 typedef struct xtract_mel_filter_ {
|
jamie@1
|
306 int n_filters;
|
jamie@1
|
307 float **filters;
|
jamie@1
|
308 } xtract_mel_filter;
|
jamie@1
|
309
|
jamie@2
|
310 /** \brief A function to initialise a mel filter bank
|
jamie@2
|
311 *
|
jamie@2
|
312 * It is up to the caller to pass in a pointer to memory allocated for freq_bands arrays of length N. This function populates these arrays with magnitude coefficients representing the mel filterbank on a linear scale
|
jamie@2
|
313 */
|
jamie@56
|
314 int xtract_init_mfcc(int N, float nyquist, int style, float freq_min, float freq_max, int freq_bands, float **fft_tables);
|
jamie@1
|
315
|
jamie@2
|
316 /** \brief A function to initialise bark filter bounds
|
jamie@2
|
317 *
|
jamie@2
|
318 * A pointer to an array of BARK_BANDS ints most be passed in, and is populated with BARK_BANDS fft bin numbers representing the limits of each band
|
jamie@59
|
319 *
|
jamie@59
|
320 * \param N: the audio block size
|
jamie@59
|
321 * \param sr: The sample audio sample rate
|
jamie@59
|
322 * \param *band_limits: a pointer to an array of BARK_BANDS ints
|
jamie@2
|
323 */
|
jamie@59
|
324 int xtract_init_bark(int N, float sr, int *band_limits);
|
jamie@1
|
325
|
jamie@50
|
326 /* \brief A function to build an array of function descriptors */
|
jamie@50
|
327 void *xtract_make_descriptors();
|
jamie@1
|
328
|
jamie@50
|
329 /* \brief A function to free an array of function descriptors */
|
jamie@50
|
330 int xtract_free_descriptors(void *fd);
|
jamie@1
|
331 /* Free functions */
|
jamie@1
|
332
|
jamie@20
|
333 /** @} */
|
jamie@20
|
334
|
jamie@1
|
335 #ifdef __cplusplus
|
jamie@1
|
336 }
|
jamie@1
|
337 #endif
|
jamie@1
|
338
|
jamie@1
|
339 #endif
|