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@52
|
56 #define XTRACT_FEATURES 53
|
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@1
|
59 enum features_ {
|
jamie@1
|
60 MEAN,
|
jamie@1
|
61 VARIANCE,
|
jamie@1
|
62 STANDARD_DEVIATION,
|
jamie@1
|
63 AVERAGE_DEVIATION,
|
jamie@1
|
64 SKEWNESS,
|
jamie@1
|
65 KURTOSIS,
|
jamie@52
|
66 SPECTRAL_MEAN,
|
jamie@52
|
67 SPECTRAL_VARIANCE,
|
jamie@52
|
68 SPECTRAL_STANDARD_DEVIATION,
|
jamie@52
|
69 SPECTRAL_AVERAGE_DEVIATION,
|
jamie@52
|
70 SPECTRAL_SKEWNESS,
|
jamie@52
|
71 SPECTRAL_KURTOSIS,
|
jamie@52
|
72 SPECTRAL_CENTROID,
|
jamie@1
|
73 IRREGULARITY_K,
|
jamie@1
|
74 IRREGULARITY_J,
|
jamie@1
|
75 TRISTIMULUS_1,
|
jamie@1
|
76 TRISTIMULUS_2,
|
jamie@1
|
77 TRISTIMULUS_3,
|
jamie@1
|
78 SMOOTHNESS,
|
jamie@1
|
79 SPREAD,
|
jamie@1
|
80 ZCR,
|
jamie@1
|
81 ROLLOFF,
|
jamie@1
|
82 LOUDNESS,
|
jamie@1
|
83 FLATNESS,
|
jamie@1
|
84 TONALITY,
|
jamie@1
|
85 CREST,
|
jamie@1
|
86 NOISINESS,
|
jamie@1
|
87 RMS_AMPLITUDE,
|
jamie@52
|
88 SPECTRAL_INHARMONICITY,
|
jamie@1
|
89 POWER,
|
jamie@1
|
90 ODD_EVEN_RATIO,
|
jamie@1
|
91 SHARPNESS,
|
jamie@52
|
92 SPECTRAL_SLOPE,
|
jamie@45
|
93 LOWEST_VALUE,
|
jamie@45
|
94 HIGHEST_VALUE,
|
jamie@45
|
95 SUM,
|
jamie@1
|
96 HPS,
|
jamie@9
|
97 F0,
|
jamie@43
|
98 FAILSAFE_F0,
|
jamie@1
|
99 FLUX,
|
jamie@1
|
100 ATTACK_TIME,
|
jamie@1
|
101 DECAY_TIME,
|
jamie@30
|
102 DELTA_FEATURE,
|
jamie@30
|
103 AUTOCORRELATION,
|
jamie@30
|
104 AMDF,
|
jamie@30
|
105 ASDF,
|
jamie@30
|
106 BARK_COEFFICIENTS,
|
jamie@52
|
107 PEAK_SPECTRUM,
|
jamie@54
|
108 SPECTRUM,
|
jamie@30
|
109 AUTOCORRELATION_FFT,
|
jamie@30
|
110 MFCC,
|
jamie@38
|
111 DCT,
|
jamie@52
|
112 HARMONIC_SPECTRUM
|
jamie@1
|
113 };
|
jamie@1
|
114
|
jamie@1
|
115 /** \brief Enumeration of feature types */
|
jamie@1
|
116 enum feature_types_ {
|
jamie@1
|
117 SCALAR,
|
jamie@1
|
118 VECTOR,
|
jamie@1
|
119 DELTA
|
jamie@1
|
120 };
|
jamie@1
|
121
|
jamie@1
|
122 /** \brief Enumeration of mfcc types */
|
jamie@1
|
123 enum mfcc_types_ {
|
jamie@1
|
124 EQUAL_GAIN,
|
jamie@1
|
125 EQUAL_AREA
|
jamie@1
|
126 };
|
jamie@1
|
127
|
jamie@1
|
128 /** \brief Enumeration of return codes */
|
jamie@1
|
129 enum return_codes_ {
|
jamie@1
|
130 SUCCESS,
|
jamie@1
|
131 MALLOC_FAILED,
|
jamie@1
|
132 BAD_ARGV,
|
jamie@12
|
133 BAD_VECTOR_SIZE,
|
jamie@38
|
134 NO_RESULT,
|
jamie@38
|
135 FEATURE_NOT_IMPLEMENTED
|
jamie@1
|
136 };
|
jamie@1
|
137
|
jamie@54
|
138 /** \brief Enumeration of spectrum types */
|
jamie@54
|
139 enum spectrum_ {
|
jamie@54
|
140 MAGNITUDE_SPECTRUM,
|
jamie@54
|
141 LOG_MAGNITUDE_SPECTRUM,
|
jamie@54
|
142 POWER_SPECTRUM,
|
jamie@54
|
143 LOG_POWER_SPECTRUM
|
jamie@54
|
144 };
|
jamie@54
|
145
|
jamie@50
|
146 /** \brief Enumeration of data types*/
|
jamie@50
|
147 typedef enum type_ {
|
jamie@50
|
148 FLOAT,
|
jamie@50
|
149 INT,
|
jamie@51
|
150 MEL_FILTER
|
jamie@50
|
151 } t_type;
|
jamie@50
|
152
|
jamie@50
|
153 /** \brief Enumeration of units*/
|
jamie@50
|
154 typedef enum unit_ {
|
jamie@50
|
155 HERTZ,
|
jamie@50
|
156 DBFS
|
jamie@50
|
157 } t_unit;
|
jamie@50
|
158
|
jamie@50
|
159 /** \brief Boolean */
|
jamie@50
|
160 typedef enum {
|
jamie@50
|
161 FALSE,
|
jamie@50
|
162 TRUE
|
jamie@50
|
163 } t_bool;
|
jamie@50
|
164
|
jamie@50
|
165 /** \brief Enumeration of vector format types*/
|
jamie@50
|
166 typedef enum vector_ {
|
jamie@54
|
167 /* N/2 magnitude/log-magnitude/power/log-power coeffs and N/2 frequencies */
|
jamie@54
|
168 SPECTRAL,
|
jamie@54
|
169 /* N spectral amplitudes */
|
jamie@54
|
170 SPECTRAL_MAGNITUDES,
|
jamie@54
|
171 /* N/2 magnitude/log-magnitude/power/log-power peak coeffs and N/2
|
jamie@54
|
172 * frequencies */
|
jamie@54
|
173 SPECTRAL_PEAKS,
|
jamie@54
|
174 /* N spectral peak amplitudes */
|
jamie@54
|
175 SPECTRAL_PEAK_MAGNITUDES,
|
jamie@54
|
176 /* N/2 magnitude/log-magnitude/power/log-power harmonic peak coeffs and N/2
|
jamie@54
|
177 * frequencies */
|
jamie@54
|
178 SPECTRAL_HARMONICS,
|
jamie@54
|
179 /* N spectral harmonic amplitudes */
|
jamie@54
|
180 SPECTRAL_HARMONICS_MAGNITUDES,
|
jamie@54
|
181 /* N spectral harmonic frequencies */
|
jamie@54
|
182 SPECTRAL_HARMONICS_FREQUENCIES,
|
jamie@54
|
183 ARBITRARY_SERIES,
|
jamie@54
|
184 AUDIO_SAMPLES,
|
jamie@54
|
185 MEL_COEFFS,
|
jamie@50
|
186 BARK_COEFFS,
|
jamie@54
|
187 NO_DATA
|
jamie@50
|
188 } t_vector;
|
jamie@50
|
189
|
jamie@50
|
190 /** \brief Data structure containing useful information about functions provided by LibXtract. */
|
jamie@50
|
191 typedef struct _function_descriptor {
|
jamie@50
|
192
|
jamie@50
|
193 struct {
|
jamie@50
|
194 char name[MAX_NAME_LENGTH];
|
jamie@51
|
195 char p_name[MAX_NAME_LENGTH]; /* pretty name */
|
jamie@51
|
196 char desc[MAX_DESC_LENGTH];
|
jamie@51
|
197 char p_desc[MAX_DESC_LENGTH]; /* pretty description */
|
jamie@50
|
198 char author[MAX_AUTHOR_LENGTH];
|
jamie@50
|
199 int year;
|
jamie@50
|
200 } algo;
|
jamie@50
|
201
|
jamie@50
|
202 struct {
|
jamie@50
|
203 t_vector format;
|
jamie@50
|
204 t_unit unit;
|
jamie@50
|
205 } data;
|
jamie@50
|
206
|
jamie@51
|
207 int argc;
|
jamie@50
|
208
|
jamie@50
|
209 struct {
|
jamie@51
|
210 t_type type; /* type of the array/value pointed to by argv */
|
jamie@50
|
211 float min[MAXARGS];
|
jamie@50
|
212 float max[MAXARGS];
|
jamie@51
|
213 float def[MAXARGS]; /* defaults */
|
jamie@50
|
214 t_unit unit[MAXARGS];
|
jamie@51
|
215 char donor[MAXARGS]; /* suggested donor functions for argv */
|
jamie@50
|
216 } argv;
|
jamie@50
|
217
|
jamie@50
|
218 t_bool is_scalar;
|
jamie@50
|
219
|
jamie@50
|
220 union {
|
jamie@50
|
221
|
jamie@50
|
222 struct {
|
jamie@50
|
223 float min;
|
jamie@50
|
224 float max;
|
jamie@50
|
225 t_unit unit;
|
jamie@50
|
226 } scalar;
|
jamie@50
|
227
|
jamie@50
|
228 struct {
|
jamie@50
|
229 t_vector format;
|
jamie@50
|
230 t_unit unit;
|
jamie@50
|
231 } vector;
|
jamie@50
|
232
|
jamie@50
|
233 } result;
|
jamie@50
|
234
|
jamie@50
|
235 } t_function_descriptor;
|
jamie@50
|
236
|
jamie@1
|
237 /**
|
jamie@1
|
238 *
|
jamie@2
|
239 * \brief An array of pointers to functions that perform the extraction
|
jamie@1
|
240 *
|
jamie@2
|
241 * \param *data: a pointer to the start of the input data (usually the first element in an array)
|
jamie@1
|
242 *
|
jamie@2
|
243 * \param N: the number of elements to be processed
|
jamie@1
|
244 *
|
jamie@2
|
245 * \param *argv: an abitrary number of additional arguments, used to pass additional parameters to the function being called
|
jamie@1
|
246 *
|
jamie@2
|
247 * \param *result: a pointer to the first element in the result
|
jamie@1
|
248 *
|
jamie@1
|
249 * Each function will iterate over N array elements, the first of which is
|
jamie@2
|
250 * 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
|
251 *
|
jamie@1
|
252 * For scalar and delta features, *result will point to a single value.
|
jamie@1
|
253 *
|
jamie@1
|
254 * For vector features it will point to the first element in an array.
|
jamie@1
|
255 *
|
jamie@1
|
256 * Memory for this array must be allocated and freed by the calling
|
jamie@1
|
257 * function.
|
jamie@1
|
258 *
|
jamie@21
|
259 * All functions return an integer error code as descibed in the enumeration
|
jamie@21
|
260 * return_codes_
|
jamie@2
|
261 *
|
jamie@21
|
262 * The preprocessor macro: XTRACT must be defined before this can be used
|
jamie@9
|
263 *
|
jamie@2
|
264 * example:<br>
|
jamie@21
|
265 * \verbatim
|
jamie@21
|
266 #include <stdio.h>
|
jamie@21
|
267 #define XTRACT
|
jamie@21
|
268 #include "libxtract.h"
|
jamie@21
|
269
|
jamie@21
|
270 main () {
|
jamie@21
|
271 float values[] = {1.0, 2.0, 3.0, 4.0, 5.0};
|
jamie@21
|
272 int N = 5;
|
jamie@21
|
273 float mean;
|
jamie@21
|
274
|
jamie@21
|
275 xtract[MEAN]((void *)values, N, NULL, &mean);
|
jamie@21
|
276
|
jamie@21
|
277 printf("Mean = %.2f\n", mean);
|
jamie@21
|
278 }
|
jamie@21
|
279 \endverbatim
|
jamie@9
|
280 * The calling function may additionally make some tests against the value returned by xtract
|
jamie@9
|
281 *
|
jamie@2
|
282 */
|
jamie@9
|
283 #ifdef XTRACT
|
jamie@43
|
284 extern int(*xtract[XTRACT_FEATURES])(const float *data, const int N, const void *argv, float *result);
|
jamie@1
|
285
|
jamie@50
|
286 /** \brief An array of pointers to function descriptors
|
jamie@26
|
287 *
|
jamie@50
|
288 * Defined in libxtract.c. This is an array of pointers to function descriptors designed to be queried for useful information such as the expected input and output units of a function, or the number of arguments it takes.
|
jamie@26
|
289 */
|
jamie@50
|
290 //extern t_function_descriptor *xtract_help[XTRACT_FEATURES];
|
jamie@38
|
291
|
jamie@28
|
292 #endif
|
jamie@26
|
293
|
jamie@2
|
294 /** \brief A structure to store a set of n_filters Mel filters */
|
jamie@1
|
295 typedef struct xtract_mel_filter_ {
|
jamie@1
|
296 int n_filters;
|
jamie@1
|
297 float **filters;
|
jamie@1
|
298 } xtract_mel_filter;
|
jamie@1
|
299
|
jamie@2
|
300 /** \brief A function to initialise a mel filter bank
|
jamie@2
|
301 *
|
jamie@2
|
302 * 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
|
303 */
|
jamie@1
|
304 int xtract_init_mfcc(int N, float nyquist, int style, float freq_max, float freq_min, int freq_bands, float **fft_tables);
|
jamie@1
|
305
|
jamie@2
|
306 /** \brief A function to initialise bark filter bounds
|
jamie@2
|
307 *
|
jamie@2
|
308 * 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@2
|
309 */
|
jamie@1
|
310 int xtract_init_bark(int N, float nyquist, int *band_limits);
|
jamie@1
|
311
|
jamie@50
|
312 /* \brief A function to build an array of function descriptors */
|
jamie@50
|
313 void *xtract_make_descriptors();
|
jamie@1
|
314
|
jamie@50
|
315 /* \brief A function to free an array of function descriptors */
|
jamie@50
|
316 int xtract_free_descriptors(void *fd);
|
jamie@1
|
317 /* Free functions */
|
jamie@1
|
318
|
jamie@20
|
319 /** @} */
|
jamie@20
|
320
|
jamie@1
|
321 #ifdef __cplusplus
|
jamie@1
|
322 }
|
jamie@1
|
323 #endif
|
jamie@1
|
324
|
jamie@1
|
325 #endif
|