jamie@254: /*
jamie@254: * Copyright (C) 2012 Jamie Bullock
jamie@254: *
jamie@254: * Permission is hereby granted, free of charge, to any person obtaining a copy
jamie@254: * of this software and associated documentation files (the "Software"), to
jamie@254: * deal in the Software without restriction, including without limitation the
jamie@254: * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
jamie@254: * sell copies of the Software, and to permit persons to whom the Software is
jamie@254: * furnished to do so, subject to the following conditions:
jamie@254: *
jamie@254: * The above copyright notice and this permission notice shall be included in
jamie@254: * all copies or substantial portions of the Software.
jamie@254: *
jamie@254: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jamie@254: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jamie@254: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jamie@254: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jamie@254: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
jamie@254: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
jamie@254: * IN THE SOFTWARE.
jamie@254: *
jamie@254: */
jamie@254:
jamie@254: /** \mainpage
jamie@254: *
jamie@254: * 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@254: * 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@254: *
jamie@254: * 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@254: *
jamie@254: * 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@254: *
jamie@254: * All feature extraction functions follow the same prototype:
jamie@254: *
jamie@254: * **int xtract_function_name(const double *data, const int N, const void *argv, double *result);**
jamie@254: *
jamie@254: * \param const double *data points to an array of doubles representing the input data
jamie@254: * \param const int N represents the number of elementes from *data to be considered in the calculation
jamie@254: * \param const void *argv represents an arbitrary list of arguments. Used to pass in values required by the feature calculation
jamie@254: * \param double *result points to an array of doubles, or a single double represnting the result of the calculation
jamie@254: *
jamie@254: *
jamie@254: * It is up to the calling function to allocate enough memory for the *data, *argv, and *result, and to free it when required. Some feature extraction functions may also require an _init() function to be called in order to perform some initialisation. The struct xtract_function_descriptor_t is used to give an indication of recommended default values, and argc for the *argv array.
jamie@254: *
jamie@254: * LibXtract can be downloaded from http://www.sf.net/projects/libxtract
jamie@254: *
jamie@254: */
jamie@254:
jamie@254: #ifndef XTRACT_H
jamie@254: #define XTRACT_H
jamie@254:
jamie@254: #ifdef __cplusplus
jamie@254: extern "C" {
jamie@254: #endif
jamie@254:
jamie@254: /**
jamie@254: * \file libxtract.h
jamie@254: * \brief main header file and API definition
jamie@254: */
jamie@254:
jamie@254: #include "xtract_scalar.h"
jamie@254: #include "xtract_vector.h"
jamie@254: #include "xtract_delta.h"
jamie@254: #include "xtract_types.h"
jamie@254: #include "xtract_macros.h"
jamie@254: #include "xtract_helper.h"
jamie@254:
jamie@254: /** \defgroup libxtract API
jamie@254: *
jamie@254: * Defines a very simple API that provides access to the functions in the library
jamie@254: * @{
jamie@254: */
jamie@254:
jamie@254: #define XTRACT_FEATURES 62
jamie@254:
jamie@254: /** \brief Enumeration of features, elements are used as indixes to an array of pointers to feature extracton functions */
jamie@254: enum xtract_features_ {
jamie@254: XTRACT_MEAN,
jamie@254: XTRACT_VARIANCE,
jamie@254: XTRACT_STANDARD_DEVIATION,
jamie@254: XTRACT_AVERAGE_DEVIATION,
jamie@254: XTRACT_SKEWNESS,
jamie@254: XTRACT_KURTOSIS,
jamie@254: XTRACT_SPECTRAL_MEAN,
jamie@254: XTRACT_SPECTRAL_VARIANCE,
jamie@254: XTRACT_SPECTRAL_STANDARD_DEVIATION,
jamie@254: /*XTRACT_SPECTRAL_AVERAGE_DEVIATION, */
jamie@254: XTRACT_SPECTRAL_SKEWNESS,
jamie@254: XTRACT_SPECTRAL_KURTOSIS,
jamie@254: XTRACT_SPECTRAL_CENTROID,
jamie@254: XTRACT_IRREGULARITY_K,
jamie@254: XTRACT_IRREGULARITY_J,
jamie@254: XTRACT_TRISTIMULUS_1,
jamie@254: XTRACT_TRISTIMULUS_2,
jamie@254: XTRACT_TRISTIMULUS_3,
jamie@254: XTRACT_SMOOTHNESS,
jamie@254: XTRACT_SPREAD,
jamie@254: XTRACT_ZCR,
jamie@254: XTRACT_ROLLOFF,
jamie@254: XTRACT_LOUDNESS,
jamie@254: XTRACT_FLATNESS,
jamie@254: XTRACT_FLATNESS_DB,
jamie@254: XTRACT_TONALITY,
jamie@254: XTRACT_CREST,
jamie@254: XTRACT_NOISINESS,
jamie@254: XTRACT_RMS_AMPLITUDE,
jamie@254: XTRACT_SPECTRAL_INHARMONICITY,
jamie@254: XTRACT_POWER,
jamie@254: XTRACT_ODD_EVEN_RATIO,
jamie@254: XTRACT_SHARPNESS,
jamie@254: XTRACT_SPECTRAL_SLOPE,
jamie@254: XTRACT_LOWEST_VALUE,
jamie@254: XTRACT_HIGHEST_VALUE,
jamie@254: XTRACT_SUM,
jamie@254: XTRACT_NONZERO_COUNT,
jamie@254: XTRACT_HPS,
jamie@254: XTRACT_F0,
jamie@254: XTRACT_FAILSAFE_F0,
jamie@254: XTRACT_WAVELET_F0,
jamie@254: XTRACT_MIDICENT,
jamie@254: XTRACT_LNORM,
jamie@254: XTRACT_FLUX,
jamie@254: XTRACT_ATTACK_TIME,
jamie@254: XTRACT_DECAY_TIME,
jamie@254: XTRACT_DIFFERENCE_VECTOR,
jamie@254: XTRACT_AUTOCORRELATION,
jamie@254: XTRACT_AMDF,
jamie@254: XTRACT_ASDF,
jamie@254: XTRACT_BARK_COEFFICIENTS,
jamie@254: XTRACT_PEAK_SPECTRUM,
jamie@254: XTRACT_SPECTRUM,
jamie@254: XTRACT_AUTOCORRELATION_FFT,
jamie@254: XTRACT_MFCC,
jamie@254: XTRACT_DCT,
jamie@254: XTRACT_HARMONIC_SPECTRUM,
jamie@254: XTRACT_LPC,
jamie@254: XTRACT_LPCC,
jamie@254: XTRACT_SUBBANDS,
jamie@254: /* Helper functions */
jamie@254: XTRACT_WINDOWED,
jamie@254: XTRACT_SMOOTHED
jamie@254: };
jamie@254:
jamie@254: /** \brief Enumeration of feature initialisation functions */
jamie@254: enum xtract_feature_init_ {
jamie@254: XTRACT_INIT_MFCC = 100,
jamie@254: XTRACT_INIT_BARK,
jamie@254: XTRACT_INIT_WINDOWED
jamie@254: };
jamie@254:
jamie@254: /** \brief Enumeration of feature types */
jamie@254: enum xtract_feature_types_ {
jamie@254: XTRACT_SCALAR,
jamie@254: XTRACT_VECTOR,
jamie@254: XTRACT_DELTA
jamie@254: };
jamie@254:
jamie@254: /** \brief Enumeration of mfcc types */
jamie@254: enum xtract_mfcc_types_ {
jamie@254: XTRACT_EQUAL_GAIN,
jamie@254: XTRACT_EQUAL_AREA
jamie@254: };
jamie@254:
jamie@254: enum xtract_lnorm_filter_types_ {
jamie@254: XTRACT_NO_LNORM_FILTER,
jamie@254: XTRACT_POSITIVE_SLOPE,
jamie@254: XTRACT_NEGATIVE_SLOPE
jamie@254: };
jamie@254:
jamie@254: /** \brief Enumeration of return codes */
jamie@254: enum xtract_return_codes_ {
jamie@254: XTRACT_SUCCESS,
jamie@254: XTRACT_MALLOC_FAILED,
jamie@254: XTRACT_BAD_ARGV,
jamie@254: XTRACT_BAD_VECTOR_SIZE,
jamie@254: XTRACT_BAD_STATE,
jamie@254: XTRACT_DENORMAL_FOUND,
jamie@254: XTRACT_NO_RESULT, /* This usually occurs when the correct calculation cannot take place because required data is missing or would result in a NaN or infinity/-infinity. Under these curcumstances 0.f is usually given by *result */
jamie@254: XTRACT_FEATURE_NOT_IMPLEMENTED,
jamie@254: XTRACT_ARGUMENT_ERROR
jamie@254: };
jamie@254:
jamie@254: /** \brief Enumeration of spectrum types */
jamie@254: enum xtract_spectrum_ {
jamie@254: XTRACT_MAGNITUDE_SPECTRUM,
jamie@254: XTRACT_LOG_MAGNITUDE_SPECTRUM,
jamie@254: XTRACT_POWER_SPECTRUM,
jamie@254: XTRACT_LOG_POWER_SPECTRUM
jamie@254: };
jamie@254:
jamie@254: /** \brief Subband scales */
jamie@254: enum xtract_subband_scales_ {
jamie@254: XTRACT_OCTAVE_SUBBANDS,
jamie@254: XTRACT_LINEAR_SUBBANDS
jamie@254: };
jamie@254:
jamie@254: /** \brief Enumeration of data types*/
jamie@254: typedef enum type_ {
jamie@254: XTRACT_FLOAT,
jamie@254: XTRACT_FLOATARRAY,
jamie@254: XTRACT_INT,
jamie@254: XTRACT_MEL_FILTER
jamie@254: } xtract_type_t;
jamie@254:
jamie@254: /** \brief Enumeration of units*/
jamie@254: typedef enum unit_ {
jamie@254: /* NONE, ANY */
jamie@254: XTRACT_HERTZ = 2,
jamie@254: XTRACT_ANY_AMPLITUDE_HERTZ,
jamie@254: XTRACT_DBFS,
jamie@254: XTRACT_DBFS_HERTZ,
jamie@254: XTRACT_PERCENT,
jamie@254: XTRACT_BINS,
jamie@254: XTRACT_SONE,
jamie@254: XTRACT_MIDI_CENT
jamie@254: } xtract_unit_t;
jamie@254:
jamie@254: /** \brief Boolean */
jamie@254: typedef enum {
jamie@254: XTRACT_FALSE,
jamie@254: XTRACT_TRUE
jamie@254: } xtract_bool_t;
jamie@254:
jamie@254: /** \brief Window types */
jamie@254: enum xtract_window_types_ {
jamie@254: XTRACT_GAUSS,
jamie@254: XTRACT_HAMMING,
jamie@254: XTRACT_HANN,
jamie@254: XTRACT_BARTLETT,
jamie@254: XTRACT_TRIANGULAR,
jamie@254: XTRACT_BARTLETT_HANN,
jamie@254: XTRACT_BLACKMAN,
jamie@254: XTRACT_KAISER,
jamie@254: XTRACT_BLACKMAN_HARRIS
jamie@254: };
jamie@254:
jamie@254: /** \brief Enumeration of vector format types*/
jamie@254: typedef enum xtract_vector_ {
jamie@254: /* N/2 magnitude/log-magnitude/power/log-power coeffs and N/2 frequencies */
jamie@254: XTRACT_SPECTRAL,
jamie@254: /* N spectral amplitudes */
jamie@254: XTRACT_SPECTRAL_MAGNITUDES,
jamie@254: /* N/2 magnitude/log-magnitude/power/log-power peak coeffs and N/2
jamie@254: * frequencies */
jamie@254: XTRACT_SPECTRAL_PEAKS,
jamie@254: /* N spectral peak amplitudes */
jamie@254: XTRACT_SPECTRAL_PEAKS_MAGNITUDES,
jamie@254: /* N spectral peak frequencies */
jamie@254: XTRACT_SPECTRAL_PEAKS_FREQUENCIES,
jamie@254: /* N/2 magnitude/log-magnitude/power/log-power harmonic peak coeffs and N/2
jamie@254: * frequencies */
jamie@254: XTRACT_SPECTRAL_HARMONICS,
jamie@254: /* N spectral harmonic amplitudes */
jamie@254: XTRACT_SPECTRAL_HARMONICS_MAGNITUDES,
jamie@254: /* N spectral harmonic frequencies */
jamie@254: XTRACT_SPECTRAL_HARMONICS_FREQUENCIES,
jamie@254: XTRACT_AUTOCORRELATION_COEFFS,
jamie@254: XTRACT_ARBITRARY_SERIES,
jamie@254: XTRACT_AUDIO_SAMPLES,
jamie@254: XTRACT_MEL_COEFFS,
jamie@254: XTRACT_LPC_COEFFS,
jamie@254: XTRACT_LPCC_COEFFS,
jamie@254: XTRACT_BARK_COEFFS,
jamie@254: XTRACT_SUBFRAMES,
jamie@254: XTRACT_NO_DATA
jamie@254: } xtract_vector_t;
jamie@254:
jamie@254: /** \brief Data structure containing useful information about functions provided by LibXtract. */
jamie@254: typedef struct _xtract_function_descriptor {
jamie@254:
jamie@254: int id;
jamie@254:
jamie@254: struct {
jamie@254: char name[XTRACT_MAX_NAME_LENGTH];
jamie@254: char p_name[XTRACT_MAX_NAME_LENGTH]; /* pretty name */
jamie@254: char desc[XTRACT_MAX_DESC_LENGTH];
jamie@254: char p_desc[XTRACT_MAX_DESC_LENGTH]; /* pretty description */
jamie@254: char author[XTRACT_MAX_AUTHOR_LENGTH];
jamie@254: int year;
jamie@254: } algo;
jamie@254:
jamie@254: struct {
jamie@254: xtract_vector_t format;
jamie@254: xtract_unit_t unit;
jamie@254: } data;
jamie@254:
jamie@254: int argc;
jamie@254:
jamie@254: struct {
jamie@254: xtract_type_t type; /* type of the array/value pointed to by argv */
jamie@254: double min[XTRACT_MAXARGS];
jamie@254: double max[XTRACT_MAXARGS];
jamie@254: double def[XTRACT_MAXARGS]; /* defaults */
jamie@254: xtract_unit_t unit[XTRACT_MAXARGS];
jamie@254: int donor[XTRACT_MAXARGS]; /* suggested donor functions for argv */
jamie@254: } argv;
jamie@254:
jamie@254: xtract_bool_t is_scalar;
jamie@254: xtract_bool_t is_delta; /* features in xtract_delta.h can be scalar or vector */
jamie@254:
jamie@254: /* The result.<> entries in descritors.c need to be checked */
jamie@254: union {
jamie@254:
jamie@254: struct {
jamie@254: double min;
jamie@254: double max;
jamie@254: xtract_unit_t unit;
jamie@254: } scalar;
jamie@254:
jamie@254: struct {
jamie@254: xtract_vector_t format;
jamie@254: xtract_unit_t unit;
jamie@254: } vector;
jamie@254:
jamie@254: } result;
jamie@254:
jamie@254: } xtract_function_descriptor_t;
jamie@254:
jamie@254: /**
jamie@254: *
jamie@254: * \brief An array of pointers to functions that perform the extraction
jamie@254: *
jamie@254: * \param *data: a pointer to the start of the input data (usually the first element in an array)
jamie@254: *
jamie@254: * \param N: the number of elements to be processed
jamie@254: *
jamie@254: * \param *argv: an abitrary number of additional arguments, used to pass additional parameters to the function being called. All arguments are compulsary!
jamie@254: *
jamie@254: * \param *result: a pointer to the first element in the result
jamie@254: *
jamie@254: * Each function will iterate over N array elements, the first of which is
jamie@254: * 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@254: *
jamie@254: * For scalar and delta features, *result will point to a single value.
jamie@254: *
jamie@254: * For vector features it will point to the first element in an array.
jamie@254: *
jamie@254: * Memory for this array must be allocated and freed by the calling
jamie@254: * function.
jamie@254: *
jamie@254: * All functions return an integer error code as descibed in the enumeration
jamie@254: * return_codes_
jamie@254: *
jamie@254: * The preprocessor macro: XTRACT must be defined before this can be used
jamie@254: *
jamie@254: * example:
jamie@254: * \verbatim
jamie@254: #include
jamie@254: #define XTRACT
jamie@254: #include "libxtract.h"
jamie@254:
jamie@254: main () {
jamie@254: double values[] = {1.0, 2.0, 3.0, 4.0, 5.0};
jamie@254: int N = 5;
jamie@254: double mean;
jamie@254:
jamie@254: xtract[MEAN]((void *)values, N, NULL, &mean);
jamie@254:
jamie@254: printf("Mean = %.2f\n", mean);
jamie@254: }
jamie@254: \endverbatim
jamie@254: * The calling function may additionally make some tests against the value returned by xtract
jamie@254: *
jamie@254: */
jamie@254: #ifdef XTRACT_H
jamie@254: extern int(*xtract[XTRACT_FEATURES])(const double *data, const int N, const void *argv, double *result);
jamie@254:
jamie@254: #endif
jamie@254:
jamie@254: /** \brief A function to initialise wavelet f0 detector state */
jamie@254: int xtract_init_wavelet_f0_state(void);
jamie@254:
jamie@254: /** \brief A structure to store a set of n_filters Mel filters */
jamie@254: typedef struct xtract_mel_filter_ {
jamie@254: int n_filters;
jamie@254: double **filters;
jamie@254: } xtract_mel_filter;
jamie@254:
jamie@254: /** \brief A function to initialise a mel filter bank
jamie@254: *
jamie@254: * 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@254: */
jamie@254: int xtract_init_mfcc(int N, double nyquist, int style, double freq_min, double freq_max, int freq_bands, double **fft_tables);
jamie@254:
jamie@254: /** \brief A function to initialise bark filter bounds
jamie@254: *
jamie@254: * 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@254: *
jamie@254: * \param N: the audio block size
jamie@254: * \param sr: The sample audio sample rate
jamie@254: * \param *band_limits: a pointer to an array of BARK_BANDS ints
jamie@254: */
jamie@254: int xtract_init_bark(int N, double sr, int *band_limits);
jamie@254:
jamie@254: /** \brief An initialisation function for functions using FFT
jamie@254: *
jamie@254: * This function initialises global data structures used by functions requiring FFT functionality. It can be called multiple times with different feature names. Calling it more than once with the same feature name is not a valid operation and will result in a memory leak.
jamie@254: *
jamie@254: * \param N: the size of the FFT
jamie@254: * \param feature_name: the name of the feature the FFT is being used for,
jamie@254: * e.g. XTRACT_DCT
jamie@254: *
jamie@254: */
jamie@254: int xtract_init_fft(int N, int feature_name);
jamie@254:
jamie@254: /** \brief Free memory used for fft plans
jamie@254: *
jamie@254: * This function should be used to explicitly free memory allocated for ffts by xtract_init_fft(). It is primarily intended for use if a new FFT needs to be taken with a different blocksize. If only one fft size is required then there is no need to call this function since it will be called when the program exits.
jamie@254: * */
jamie@254: void xtract_free_fft(void);
jamie@254:
jamie@254: /** \brief Make a window of a given type and return a pointer to it
jamie@254: *
jamie@254: * \param N: the size of the window
jamie@254: * \param type: the type of the window as given in the enumeration window_types_
jamie@254: *
jamie@254: */
jamie@254: double *xtract_init_window(const int N, const int type);
jamie@254:
jamie@254: /** \brief Free a window as allocated by xtract_make_window()
jamie@254: *
jamie@254: * \param *window: a pointer to an array of doubles as allocated by xtract_make_window()
jamie@254: *
jamie@254: */
jamie@254: void xtract_free_window(double *window);
jamie@254:
jamie@254: /* \brief A function to build an array of function descriptors */
jamie@254: xtract_function_descriptor_t *xtract_make_descriptors();
jamie@254:
jamie@254: /* \brief A function to free an array of function descriptors */
jamie@254: int xtract_free_descriptors(xtract_function_descriptor_t *fd);
jamie@254: /* Free functions */
jamie@254:
jamie@254: /** @} */
jamie@254:
jamie@254: #ifdef __cplusplus
jamie@254: }
jamie@254: #endif
jamie@254:
jamie@254: #endif