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