14 typedef struct af_data_s
16 void* audio
; // data buffer
17 int len
; // buffer length
18 int rate
; // sample rate
19 int nch
; // number of channels
21 int bps
; // bytes per sample
24 // Fraction, used to calculate buffer lengths
31 int af_gcd(register int a
, register int b
);
32 void af_frac_cancel(frac_t
*f
);
33 void af_frac_mul(frac_t
*out
, const frac_t
*in
);
35 // Flags used for defining the behavior of an audio filter
36 #define AF_FLAGS_REENTRANT 0x00000000
37 #define AF_FLAGS_NOT_REENTRANT 0x00000001
39 /* Audio filter information not specific for current instance, but for
41 typedef struct af_info_s
48 int (*open
)(struct af_instance_s
* vf
);
51 // Linked list of audio filters
52 typedef struct af_instance_s
55 int (*control
)(struct af_instance_s
* af
, int cmd
, void* arg
);
56 void (*uninit
)(struct af_instance_s
* af
);
57 af_data_t
* (*play
)(struct af_instance_s
* af
, af_data_t
* data
);
58 void* setup
; // setup data for this specific instance and filter
59 af_data_t
* data
; // configuration for outgoing data stream
60 struct af_instance_s
* next
;
61 struct af_instance_s
* prev
;
62 double delay
; // Delay caused by the filter [ms]
63 frac_t mul
; /* length multiplier: how much does this instance change
64 the length of the buffer. */
67 // Initialization flags
68 extern int* af_cpu_speed
;
70 #define AF_INIT_AUTO 0x00000000
71 #define AF_INIT_SLOW 0x00000001
72 #define AF_INIT_FAST 0x00000002
73 #define AF_INIT_FORCE 0x00000003
74 #define AF_INIT_TYPE_MASK 0x00000003
76 #define AF_INIT_INT 0x00000000
77 #define AF_INIT_FLOAT 0x00000004
78 #define AF_INIT_FORMAT_MASK 0x00000004
82 #if defined(HAVE_SSE) || defined(HAVE_3DNOW)
83 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_FAST)
85 #define AF_INIT_TYPE (af_cpu_speed?*af_cpu_speed:AF_INIT_SLOW)
89 // Configuration switches
90 typedef struct af_cfg_s
{
91 int force
; // Initialization type
92 char** list
; /* list of names of filters that are added to filter
93 list during first initialization of stream */
96 // Current audio stream
97 typedef struct af_stream_s
99 // The first and last filter in the list
100 af_instance_t
* first
;
102 // Storage for input and output data formats
105 // Configuration for this stream
109 /*********************************************
117 #define AF_UNKNOWN -1
123 /*********************************************
128 * \defgroup af_chain Audio filter chain functions
130 * \param s filter chain
134 * \brief Initialize the stream "s".
135 * \return 0 on success, -1 on failure
137 * This function creates a new filter list if necessary, according
138 * to the values set in input and output. Input and output should contain
139 * the format of the current movie and the format of the preferred output
141 * Filters to convert to the preferred output format are inserted
142 * automatically, except when they are set to 0.
143 * The function is reentrant i.e. if called with an already initialized
144 * stream the stream will be reinitialized.
146 int af_init(af_stream_t
* s
);
149 * \brief Uninit and remove all filters from audio filter chain
151 void af_uninit(af_stream_t
* s
);
154 * \brief This function adds the filter "name" to the stream s.
155 * \param name name of filter to add
156 * \return pointer to the new filter, NULL if insert failed
158 * The filter will be inserted somewhere nice in the
159 * list of filters (i.e. at the beginning unless the
160 * first filter is the format filter (why??).
162 af_instance_t
* af_add(af_stream_t
* s
, char* name
);
165 * \brief Uninit and remove the filter "af"
166 * \param af filter to remove
168 void af_remove(af_stream_t
* s
, af_instance_t
* af
);
171 * \brief find filter in chain by name
172 * \param name name of the filter to find
173 * \return first filter with right name or NULL if not found
175 * This function is used for finding already initialized filters
177 af_instance_t
* af_get(af_stream_t
* s
, char* name
);
180 * \brief filter data chunk through the filters in the list
181 * \param data data to play
182 * \return resulting data
185 af_data_t
* af_play(af_stream_t
* s
, af_data_t
* data
);
188 * \brief send control to all filters, starting with the last until
189 * one accepts the command with AF_OK.
190 * \param cmd filter control command
191 * \param arg argument for filter command
192 * \return the accepting filter or NULL if none was found
194 af_instance_t
*af_control_any_rev (af_stream_t
* s
, int cmd
, void* arg
);
197 * \brief Calculate how long the output from the filters will be for a given
199 * \param len input lenght for which to calculate output length
200 * \return calculated output length, will always be >= the resulting
201 * length when actually calling af_play.
203 int af_outputlen(af_stream_t
* s
, int len
);
206 * \brief Calculate how long the input to the filters should be to produce a
207 * certain output length
208 * \param len wanted output length
209 * \return input length required to produce the output length "len". Possibly
210 * smaller to avoid overflow of output buffer
212 int af_inputlen(af_stream_t
* s
, int len
);
215 * \brief calculate required input length for desired output size
216 * \param len desired minimum output length
217 * \param max_outsize maximum output length
218 * \param max_insize maximum input length
219 * \return input length or -1 on error
221 Calculate how long the input IN to the filters should be to produce
222 a certain output length OUT but with the following three constraints:
223 1. IN <= max_insize, where max_insize is the maximum possible input
225 2. OUT <= max_outsize, where max_outsize is the maximum possible
227 3. If possible OUT >= len.
229 int af_calc_insize_constrained(af_stream_t
* s
, int len
,
230 int max_outsize
,int max_insize
);
233 * \brief Calculate the total delay caused by the filters
234 * \return delay in seconds
236 double af_calc_delay(af_stream_t
* s
);
238 /** \} */ // end of af_chain group
240 // Helper functions and macros used inside the audio filters
243 * \defgroup af_filter Audio filter helper functions
247 /* Helper function called by the macro with the same name only to be
248 called from inside filters */
249 int af_resize_local_buffer(af_instance_t
* af
, af_data_t
* data
);
251 /* Helper function used to calculate the exact buffer length needed
252 when buffers are resized. The returned length is >= than what is
254 int af_lencalc(frac_t mul
, af_data_t
* data
);
257 * \brief convert dB to gain value
258 * \param n number of values to convert
259 * \param in [in] values in dB, <= -200 will become 0 gain
260 * \param out [out] gain values
261 * \param k input values are divided by this
262 * \param mi minimum dB value, input will be clamped to this
263 * \param ma maximum dB value, input will be clamped to this
264 * \return AF_ERROR on error, AF_OK otherwise
266 int af_from_dB(int n
, float* in
, float* out
, float k
, float mi
, float ma
);
269 * \brief convert gain value to dB
270 * \param n number of values to convert
271 * \param in [in] gain values, 0 wil become -200 dB
272 * \param out [out] values in dB
273 * \param k output values will be multiplied by this
274 * \return AF_ERROR on error, AF_OK otherwise
276 int af_to_dB(int n
, float* in
, float* out
, float k
);
279 * \brief convert milliseconds to sample time
280 * \param n number of values to convert
281 * \param in [in] values in milliseconds
282 * \param out [out] sample time values
283 * \param rate sample rate
284 * \param mi minimum ms value, input will be clamped to this
285 * \param ma maximum ms value, input will be clamped to this
286 * \return AF_ERROR on error, AF_OK otherwise
288 int af_from_ms(int n
, float* in
, int* out
, int rate
, float mi
, float ma
);
291 * \brief convert sample time to milliseconds
292 * \param n number of values to convert
293 * \param in [in] sample time values
294 * \param out [out] values in milliseconds
295 * \param rate sample rate
296 * \return AF_ERROR on error, AF_OK otherwise
298 int af_to_ms(int n
, int* in
, float* out
, int rate
);
301 * \brief test if output format matches
302 * \param af audio filter
303 * \param out needed format, will be overwritten by available
304 * format if they do not match
305 * \return AF_FALSE if formats do not match, AF_OK if they match
307 * compares the format, bps, rate and nch values of af->data with out
309 int af_test_output(struct af_instance_s
* af
, af_data_t
* out
);
312 * \brief soft clipping function using sin()
313 * \param a input value
314 * \return clipped value
316 float af_softclip(float a
);
318 /** \} */ // end of af_filter group, but more functions of this group below
320 /** Print a list of all available audio filters */
324 * \brief fill the missing parameters in the af_data_t structure
325 * \param data structure to fill
328 * Currently only sets bps based on format
330 void af_fix_parameters(af_data_t
*data
);
332 /** Memory reallocation macro: if a local buffer is used (i.e. if the
333 filter doesn't operate on the incoming buffer this macro must be
334 called to ensure the buffer is big enough.
337 #define RESIZE_LOCAL_BUFFER(a,d)\
338 ((a->data->len < af_lencalc(a->mul,d))?af_resize_local_buffer(a,d):AF_OK)
340 /* Some other useful macro definitions*/
342 #define min(a,b)(((a)>(b))?(b):(a))
346 #define max(a,b)(((a)>(b))?(a):(b))
350 #define clamp(a,min,max) (((a)>(max))?(max):(((a)<(min))?(min):(a)))
354 #define sign(a) (((a)>0)?(1):(-1))
358 #define lrnd(a,b) ((b)((a)>=0.0?(a)+0.5:(a)-0.5))
363 typedef struct af_msg_cfg_s
365 int level
; /* Message level for debug and error messages max = 2
366 min = -2 default = 0 */
367 FILE* err
; // Stream to print error messages to
368 FILE* msg
; // Stream to print information messages to
371 extern af_msg_cfg_t af_msg_cfg
; // Message
373 //! \addtogroup af_filter
375 #define AF_MSG_FATAL -3 ///< Fatal error exit immediately
376 #define AF_MSG_ERROR -2 ///< Error return gracefully
377 #define AF_MSG_WARN -1 ///< Print warning but do not exit (can be suppressed)
378 #define AF_MSG_INFO 0 ///< Important information
379 #define AF_MSG_VERBOSE 1 ///< Print this if verbose is enabled
380 #define AF_MSG_DEBUG0 2 ///< Print if very verbose
381 #define AF_MSG_DEBUG1 3 ///< Print if very very verbose
383 //! Macro for printing error messages
385 #define af_msg(lev, args... ) \
386 (((lev)<AF_MSG_WARN)?(fprintf(af_msg_cfg.err?af_msg_cfg.err:stderr, ## args )): \
387 (((lev)<=af_msg_cfg.level)?(fprintf(af_msg_cfg.msg?af_msg_cfg.msg:stdout, ## args )):0))
391 #endif /* __aop_h__ */