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@30
|
108 MAGNITUDE_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@50
|
138 /** \brief Enumeration of data types*/
|
jamie@50
|
139 typedef enum type_ {
|
jamie@50
|
140 FLOAT,
|
jamie@50
|
141 INT,
|
jamie@51
|
142 MEL_FILTER
|
jamie@50
|
143 } t_type;
|
jamie@50
|
144
|
jamie@50
|
145 /** \brief Enumeration of units*/
|
jamie@50
|
146 typedef enum unit_ {
|
jamie@50
|
147 HERTZ,
|
jamie@50
|
148 DBFS
|
jamie@50
|
149 } t_unit;
|
jamie@50
|
150
|
jamie@50
|
151 /** \brief Boolean */
|
jamie@50
|
152 typedef enum {
|
jamie@50
|
153 FALSE,
|
jamie@50
|
154 TRUE
|
jamie@50
|
155 } t_bool;
|
jamie@50
|
156
|
jamie@50
|
157 /** \brief Enumeration of vector format types*/
|
jamie@50
|
158 typedef enum vector_ {
|
jamie@50
|
159 MAGNITUDES,
|
jamie@50
|
160 FREQUENCIES,
|
jamie@50
|
161 FREQUENCIES_AND_MAGNITUDES,
|
jamie@50
|
162 BARK_COEFFS,
|
jamie@50
|
163 MEL_COEFFS,
|
jamie@50
|
164 SAMPLES,
|
jamie@50
|
165 } t_vector;
|
jamie@50
|
166
|
jamie@50
|
167 /** \brief Data structure containing useful information about functions provided by LibXtract. */
|
jamie@50
|
168 typedef struct _function_descriptor {
|
jamie@50
|
169
|
jamie@50
|
170 struct {
|
jamie@50
|
171 char name[MAX_NAME_LENGTH];
|
jamie@51
|
172 char p_name[MAX_NAME_LENGTH]; /* pretty name */
|
jamie@51
|
173 char desc[MAX_DESC_LENGTH];
|
jamie@51
|
174 char p_desc[MAX_DESC_LENGTH]; /* pretty description */
|
jamie@50
|
175 char author[MAX_AUTHOR_LENGTH];
|
jamie@50
|
176 int year;
|
jamie@50
|
177 } algo;
|
jamie@50
|
178
|
jamie@50
|
179 struct {
|
jamie@50
|
180 t_vector format;
|
jamie@50
|
181 t_unit unit;
|
jamie@50
|
182 } data;
|
jamie@50
|
183
|
jamie@51
|
184 int argc;
|
jamie@50
|
185
|
jamie@50
|
186 struct {
|
jamie@51
|
187 t_type type; /* type of the array/value pointed to by argv */
|
jamie@50
|
188 float min[MAXARGS];
|
jamie@50
|
189 float max[MAXARGS];
|
jamie@51
|
190 float def[MAXARGS]; /* defaults */
|
jamie@50
|
191 t_unit unit[MAXARGS];
|
jamie@51
|
192 char donor[MAXARGS]; /* suggested donor functions for argv */
|
jamie@50
|
193 } argv;
|
jamie@50
|
194
|
jamie@50
|
195 t_bool is_scalar;
|
jamie@50
|
196
|
jamie@50
|
197 union {
|
jamie@50
|
198
|
jamie@50
|
199 struct {
|
jamie@50
|
200 float min;
|
jamie@50
|
201 float max;
|
jamie@50
|
202 t_unit unit;
|
jamie@50
|
203 } scalar;
|
jamie@50
|
204
|
jamie@50
|
205 struct {
|
jamie@50
|
206 t_vector format;
|
jamie@50
|
207 t_unit unit;
|
jamie@50
|
208 } vector;
|
jamie@50
|
209
|
jamie@50
|
210 } result;
|
jamie@50
|
211
|
jamie@50
|
212 } t_function_descriptor;
|
jamie@50
|
213
|
jamie@1
|
214 /**
|
jamie@1
|
215 *
|
jamie@2
|
216 * \brief An array of pointers to functions that perform the extraction
|
jamie@1
|
217 *
|
jamie@2
|
218 * \param *data: a pointer to the start of the input data (usually the first element in an array)
|
jamie@1
|
219 *
|
jamie@2
|
220 * \param N: the number of elements to be processed
|
jamie@1
|
221 *
|
jamie@2
|
222 * \param *argv: an abitrary number of additional arguments, used to pass additional parameters to the function being called
|
jamie@1
|
223 *
|
jamie@2
|
224 * \param *result: a pointer to the first element in the result
|
jamie@1
|
225 *
|
jamie@1
|
226 * Each function will iterate over N array elements, the first of which is
|
jamie@2
|
227 * 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
|
228 *
|
jamie@1
|
229 * For scalar and delta features, *result will point to a single value.
|
jamie@1
|
230 *
|
jamie@1
|
231 * For vector features it will point to the first element in an array.
|
jamie@1
|
232 *
|
jamie@1
|
233 * Memory for this array must be allocated and freed by the calling
|
jamie@1
|
234 * function.
|
jamie@1
|
235 *
|
jamie@21
|
236 * All functions return an integer error code as descibed in the enumeration
|
jamie@21
|
237 * return_codes_
|
jamie@2
|
238 *
|
jamie@21
|
239 * The preprocessor macro: XTRACT must be defined before this can be used
|
jamie@9
|
240 *
|
jamie@2
|
241 * example:<br>
|
jamie@21
|
242 * \verbatim
|
jamie@21
|
243 #include <stdio.h>
|
jamie@21
|
244 #define XTRACT
|
jamie@21
|
245 #include "libxtract.h"
|
jamie@21
|
246
|
jamie@21
|
247 main () {
|
jamie@21
|
248 float values[] = {1.0, 2.0, 3.0, 4.0, 5.0};
|
jamie@21
|
249 int N = 5;
|
jamie@21
|
250 float mean;
|
jamie@21
|
251
|
jamie@21
|
252 xtract[MEAN]((void *)values, N, NULL, &mean);
|
jamie@21
|
253
|
jamie@21
|
254 printf("Mean = %.2f\n", mean);
|
jamie@21
|
255 }
|
jamie@21
|
256 \endverbatim
|
jamie@9
|
257 * The calling function may additionally make some tests against the value returned by xtract
|
jamie@9
|
258 *
|
jamie@2
|
259 */
|
jamie@9
|
260 #ifdef XTRACT
|
jamie@43
|
261 extern int(*xtract[XTRACT_FEATURES])(const float *data, const int N, const void *argv, float *result);
|
jamie@1
|
262
|
jamie@50
|
263 /** \brief An array of pointers to function descriptors
|
jamie@26
|
264 *
|
jamie@50
|
265 * 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
|
266 */
|
jamie@50
|
267 //extern t_function_descriptor *xtract_help[XTRACT_FEATURES];
|
jamie@38
|
268
|
jamie@28
|
269 #endif
|
jamie@26
|
270
|
jamie@2
|
271 /** \brief A structure to store a set of n_filters Mel filters */
|
jamie@1
|
272 typedef struct xtract_mel_filter_ {
|
jamie@1
|
273 int n_filters;
|
jamie@1
|
274 float **filters;
|
jamie@1
|
275 } xtract_mel_filter;
|
jamie@1
|
276
|
jamie@2
|
277 /** \brief A function to initialise a mel filter bank
|
jamie@2
|
278 *
|
jamie@2
|
279 * 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
|
280 */
|
jamie@1
|
281 int xtract_init_mfcc(int N, float nyquist, int style, float freq_max, float freq_min, int freq_bands, float **fft_tables);
|
jamie@1
|
282
|
jamie@2
|
283 /** \brief A function to initialise bark filter bounds
|
jamie@2
|
284 *
|
jamie@2
|
285 * 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
|
286 */
|
jamie@1
|
287 int xtract_init_bark(int N, float nyquist, int *band_limits);
|
jamie@1
|
288
|
jamie@50
|
289 /* \brief A function to build an array of function descriptors */
|
jamie@50
|
290 void *xtract_make_descriptors();
|
jamie@1
|
291
|
jamie@50
|
292 /* \brief A function to free an array of function descriptors */
|
jamie@50
|
293 int xtract_free_descriptors(void *fd);
|
jamie@1
|
294 /* Free functions */
|
jamie@1
|
295
|
jamie@20
|
296 /** @} */
|
jamie@20
|
297
|
jamie@1
|
298 #ifdef __cplusplus
|
jamie@1
|
299 }
|
jamie@1
|
300 #endif
|
jamie@1
|
301
|
jamie@1
|
302 #endif
|