Raw ↗
+ +
65338e2 fix(windows): add win32 compat headers and export audio and simd APIs
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * include/avx_audio.h - Audio resampling and channel downmixing
7 */
8  
9 #ifndef AVX_AUDIO_H
10 #define AVX_AUDIO_H
11  
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include "openavx.h"
16  
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20  
21 /* Standard Channel Layout Identifiers */
22 typedef enum avx_audio_layout_e {
23 AVX_AUDIO_LAYOUT_UNKNOWN = 0,
24 AVX_AUDIO_LAYOUT_MONO = 1, /* [0] = Center/Mono (M) */
25 AVX_AUDIO_LAYOUT_STEREO = 2, /* [0] = Left (L), [1] = Right (R) */
26 AVX_AUDIO_LAYOUT_2_1 = 3, /* [0] = L, [1] = R, [2] = LFE */
27 AVX_AUDIO_LAYOUT_QUAD = 4, /* [0] = FL, [1] = FR, [2] = BL, [3] = BR */
28 AVX_AUDIO_LAYOUT_5_1 = 6, /* [0] = FL, [1] = FR, [2] = C, [3] = LFE, [4] = BL, [5] = BR (ITU-R BS.775) */
29 AVX_AUDIO_LAYOUT_7_1 = 8, /* [0] = FL, [1] = FR, [2] = C, [3] = LFE, [4] = BL, [5] = BR, [6] = SL, [7] = SR */
30 AVX_AUDIO_LAYOUT_7_1_4 = 12, /* 7.1 + 4 Height [TFL, TFR, TBL, TBR] */
31 AVX_AUDIO_LAYOUT_DISCRETE = 100, /* Generic discrete tracks without fixed spatial positioning */
32 AVX_AUDIO_LAYOUT_BROADCAST_16CH = 116, /* SDI 16-channel broadcast matrix */
33 AVX_AUDIO_LAYOUT_BROADCAST_32CH = 132 /* 32-channel pro-audio / Dante matrix */
34 } avx_audio_layout_e;
35  
36 /* Channel Layout & Semantic Metadata Helpers */
37 OPENAVX_API oavx_audio_channel_mask_t avx_audio_layout_to_mask(avx_audio_layout_e layout);
38 OPENAVX_API avx_audio_layout_e avx_audio_layout_from_channels(uint16_t channels);
39 OPENAVX_API avx_audio_layout_e avx_audio_layout_from_mask(oavx_audio_channel_mask_t mask, uint16_t channels);
40 OPENAVX_API const char* avx_audio_layout_name(avx_audio_layout_e layout);
41 OPENAVX_API const char* avx_audio_channel_name(uint16_t channel_idx, avx_audio_layout_e layout);
42 OPENAVX_API uint16_t avx_audio_layout_channels(avx_audio_layout_e layout);
43  
44 /* Normalization mode for downmixing */
45 typedef enum avx_audio_normalize_e {
46 AVX_AUDIO_NORMALIZE_NONE = 0, /* Direct weighted sum; hard-clips exceeding [-1.0, 1.0] */
47 AVX_AUDIO_NORMALIZE_PEAK = 1, /* Computes peak amplitude; normalizes frame if peak > 1.0 */
48 AVX_AUDIO_NORMALIZE_ATTEN_3DB = 2 /* Scales by coefficient sum to guarantee no clipping */
49 } avx_audio_normalize_e;
50  
51 /* Resampling interpolation algorithm */
52 typedef enum avx_audio_resample_mode_e {
53 AVX_AUDIO_RESAMPLE_LINEAR = 0, /* Fast 2-point linear interpolation */
54 AVX_AUDIO_RESAMPLE_CUBIC = 1, /* 4-point Catmull-Rom cubic Hermite spline (broadcast quality) */
55 AVX_AUDIO_RESAMPLE_SINC = 2 /* Windowed Sinc filter with Blackman window (studio grade) */
56 } avx_audio_resample_mode_e;
57  
58 /* Opaque streaming resampler context */
59 typedef struct avx_resampler_t avx_resampler_t;
60  
61 /* ========================================================================= */
62 /* 1. MULTI-CHANNEL DOWNMIXING ROUTINES */
63 /* ========================================================================= */
64  
65 /**
66 * Downmix 5.1 Surround to Stereo (ITU-R BS.775 / ATSC A/52 standard).
67 * in_channels: array of 6 float pointers [FL, FR, C, LFE, BL, BR].
68 */
69 OPENAVX_API void avx_audio_downmix_5_1_to_stereo(
70 const float* const in_channels[6],
71 float* out_left,
72 float* out_right,
73 int frames,
74 avx_audio_normalize_e normalize_mode
75 );
76  
77 /**
78 * Downmix 7.1 Surround to Stereo.
79 * in_channels: array of 8 float pointers [FL, FR, C, LFE, BL, BR, SL, SR].
80 */
81 OPENAVX_API void avx_audio_downmix_7_1_to_stereo(
82 const float* const in_channels[8],
83 float* out_left,
84 float* out_right,
85 int frames,
86 avx_audio_normalize_e normalize_mode
87 );
88  
89 /**
90 * Downmix Quadraphonic 4.0 to Stereo.
91 * in_channels: array of 4 float pointers [FL, FR, BL, BR].
92 */
93 OPENAVX_API void avx_audio_downmix_quad_to_stereo(
94 const float* const in_channels[4],
95 float* out_left,
96 float* out_right,
97 int frames,
98 avx_audio_normalize_e normalize_mode
99 );
100  
101 /**
102 * Downmix Stereo to Mono.
103 */
104 OPENAVX_API void avx_audio_downmix_stereo_to_mono(
105 const float* in_left,
106 const float* in_right,
107 float* out_mono,
108 int frames,
109 avx_audio_normalize_e normalize_mode
110 );
111  
112 /**
113 * Arbitrary M x N channel matrix remixing.
114 * matrix is out_channel_count x in_channel_count row-major float matrix.
115 */
116 OPENAVX_API void avx_audio_matrix_mix(
117 const float* const* in_channels,
118 int in_channel_count,
119 float* const* out_channels,
120 int out_channel_count,
121 const float* matrix,
122 int frames,
123 avx_audio_normalize_e normalize_mode
124 );
125  
126 /**
127 * High-level OpenAVX audio frame downmix helper.
128 * If src_frame has > 2 channels, downmixes to stereo in dst_frame.
129 * Allocates dst_frame->planes[0]; must be freed with avx_audio_frame_free_data().
130 */
131 OPENAVX_API bool avx_audio_frame_downmix_to_stereo(
132 const oavx_audio_frame_t* src_frame,
133 oavx_audio_frame_t* dst_frame,
134 avx_audio_normalize_e normalize_mode
135 );
136  
137 /**
138 * Frees memory allocated by avx_audio_frame_downmix_to_stereo or avx_audio_frame_resample.
139 */
140 OPENAVX_API void avx_audio_frame_free_data(oavx_audio_frame_t* frame);
141  
142 /* ========================================================================= */
143 /* 2. BROADCAST SAMPLE RATE CONVERSION (RESAMPLING) */
144 /* ========================================================================= */
145  
146 /**
147 * Creates a stateful streaming audio resampler.
148 * Uses exact rational phase fraction stepping based on gcd(in_rate, out_rate).
149 */
150 OPENAVX_API avx_resampler_t* avx_resampler_create(
151 int in_rate,
152 int out_rate,
153 int channels,
154 avx_audio_resample_mode_e mode
155 );
156  
157 /**
158 * Processes incoming planar audio chunk through the resampler.
159 * in_channels: array of channels planar float pointers.
160 * out_channels: array of channels planar float output buffers.
161 * Returns the exact number of output samples generated in this call.
162 */
163 OPENAVX_API int avx_resampler_process(
164 avx_resampler_t* resampler,
165 const float* const* in_channels,
166 int in_samples,
167 float* const* out_channels,
168 int max_out_samples
169 );
170  
171 /**
172 * Flushes any remaining samples buffered in the resampler at end of stream.
173 * Returns the number of flushed samples generated.
174 */
175 OPENAVX_API int avx_resampler_flush(
176 avx_resampler_t* resampler,
177 float* const* out_channels,
178 int max_out_samples
179 );
180  
181 /**
182 * Resets the resampler's internal FIFO buffers and phase accumulator.
183 */
184 OPENAVX_API void avx_resampler_reset(avx_resampler_t* resampler);
185  
186 /**
187 * Destroys a resampler instance and frees all associated buffers.
188 */
189 OPENAVX_API void avx_resampler_destroy(avx_resampler_t* resampler);
190  
191 /**
192 * High-level OpenAVX audio frame resampling helper.
193 * Resamples src_frame into dst_frame using the provided resampler instance.
194 * Allocates dst_frame->planes[0]; must be freed with avx_audio_frame_free_data().
195 */
196 OPENAVX_API bool avx_audio_frame_resample(
197 avx_resampler_t* resampler,
198 const oavx_audio_frame_t* src_frame,
199 oavx_audio_frame_t* dst_frame
200 );
201  
202 /**
203 * Simple one-shot resample for contiguous single-channel buffers.
204 * Returns number of output samples written, or -1 on error.
205 */
206 OPENAVX_API int avx_audio_resample_simple(
207 const float* in_data,
208 int in_samples,
209 int in_rate,
210 float* out_data,
211 int max_out_samples,
212 int out_rate,
213 avx_audio_resample_mode_e mode
214 );
215  
216 /* ========================================================================= */
217 /* 3. ADAPTIVE AUDIO JITTER BUFFER & CLOCK SLEWING */
218 /* ========================================================================= */
219  
220 typedef struct avx_audio_jitter_t avx_audio_jitter_t;
221  
222 typedef struct avx_audio_jitter_config_t {
223 int sample_rate; /* Nominal sample rate (e.g. 48000) */
224 int channels; /* Channel count (e.g. 2, max 8) */
225 int target_cushion_ms; /* Base cushion setpoint in ms (default: 15 ms) */
226 int min_cushion_ms; /* Minimum allowable cushion on clean LAN (default: 5 ms) */
227 int max_cushion_ms; /* Maximum allowable cushion on congested links (default: 40 ms) */
228 } avx_audio_jitter_config_t;
229  
230 /**
231 * Creates an adaptive audio jitter buffer instance.
232 */
233 OPENAVX_API avx_audio_jitter_t* avx_audio_jitter_create(const avx_audio_jitter_config_t* cfg);
234  
235 /**
236 * Destroys an adaptive audio jitter buffer instance and releases memory.
237 */
238 OPENAVX_API void avx_audio_jitter_destroy(avx_audio_jitter_t* jb);
239  
240 /**
241 * Pushes incoming bursty/network audio chunk into the elastic planar ring buffer.
242 */
243 OPENAVX_API void avx_audio_jitter_push(avx_audio_jitter_t* jb, const float* const* channels, int num_samples);
244  
245 /**
246 * Pulls steady, clock-steered audio samples for mixer/soundcard playback.
247 * Automatically applies pre-roll buffering, micro-slewing drift correction,
248 * and smooth cosine cross-fades on starvation.
249 * Returns the actual number of samples generated.
250 */
251 OPENAVX_API int avx_audio_jitter_pull(avx_audio_jitter_t* jb, float* const* out_channels, int requested_samples);
252  
253 /**
254 * Returns current buffered audio depth in samples.
255 */
256 OPENAVX_API int avx_audio_jitter_get_depth_samples(const avx_audio_jitter_t* jb);
257  
258 /**
259 * Returns current buffered audio depth in milliseconds.
260 */
261 OPENAVX_API float avx_audio_jitter_get_depth_ms(const avx_audio_jitter_t* jb);
262  
263 /**
264 * Resets the jitter buffer, clearing buffered samples and re-arming pre-roll.
265 */
266 OPENAVX_API void avx_audio_jitter_reset(avx_audio_jitter_t* jb);
267  
268 /* ========================================================================= */
269 /* 4. AUDIO MATRIXING, ROUTING & UPMIXING TOOLS */
270 /* ========================================================================= */
271  
272 /**
273 * Universal Audio Channel Router and Shuffler.
274 * Maps, shuffles, mutes, and scales audio channels across arbitrary formats (up to 32ch).
275 *
276 * in_planes: array of in_channels planar float pointers.
277 * out_planes: array of out_channels planar float pointers.
278 * channel_map: array of length out_channels specifying the input channel index for each output channel.
279 * channel_map[i] = src_idx (0 <= src_idx < in_channels) maps input to output[i].
280 * channel_map[i] = -1 mutes output[i] (fills with silence).
281 * If channel_map is NULL, performs 1:1 mapping (identity up to min(in_channels, out_channels)).
282 * channel_gains: optional array of length out_channels with linear gain multiplier per output channel.
283 * If NULL, unity gain (1.0f) is applied to all channels.
284 * frames: number of samples per channel to process.
285 */
286 OPENAVX_API void avx_audio_route_channels(
287 const float* const* in_planes, int in_channels,
288 float* const* out_planes, int out_channels,
289 const int8_t* channel_map,
290 const float* channel_gains,
291 int frames
292 );
293  
294 /**
295 * High-level OpenAVX audio frame routing helper.
296 * Routes channels from src_frame into dst_frame according to channel_map and gains.
297 * Allocates dst_frame->planes[0]; must be freed with avx_audio_frame_free_data().
298 */
299 OPENAVX_API bool avx_audio_frame_route_channels(
300 const oavx_audio_frame_t* src_frame,
301 oavx_audio_frame_t* dst_frame,
302 const int8_t* channel_map,
303 const float* channel_gains,
304 uint16_t out_channels
305 );
306  
307 /**
308 * Broadcast Lt/Rt (Left total / Right total) Matrix Downmixer.
309 * Produces matrix-encoded stereo from 5.1 surround compatible with Dolby Pro Logic decoders.
310 * Formula:
311 * Lt = FL + 0.7071 * C - 0.7071 * BL - 0.7071 * BR
312 * Rt = FR + 0.7071 * C + 0.7071 * BL + 0.7071 * BR
313 *
314 * in_channels: array of 6 float pointers [FL, FR, C, LFE, BL, BR]. LFE is muted.
315 * out_left, out_right: destination stereo buffers.
316 * frames: number of samples to process.
317 * normalize_mode: normalization mode (NONE, PEAK, ATTEN_3DB).
318 */
319 OPENAVX_API void avx_audio_downmix_5_1_to_ltrt(
320 const float* const in_channels[6],
321 float* out_left,
322 float* out_right,
323 int frames,
324 avx_audio_normalize_e normalize_mode
325 );
326  
327 /**
328 * Configuration parameters for Stereo to 5.1 Surround Upmixer.
329 */
330 typedef struct avx_audio_upmix_cfg_t {
331 float center_level; /* Center/dialog level multiplier (default: 0.7071f) */
332 float surround_level; /* Surround/ambience level multiplier (default: 0.7071f) */
333 float lfe_level; /* Subwoofer/LFE low-pass level multiplier (default: 0.5000f) */
334 int ambient_delay_ms; /* Surround decorrelation delay in ms (default: 15 ms, range 0..50) */
335 avx_audio_normalize_e normalize_mode; /* Output normalization mode (default: NONE) */
336 } avx_audio_upmix_cfg_t;
337  
338 /**
339 * Initializes upmix configuration with broadcast-standard defaults.
340 */
341 OPENAVX_API void avx_audio_upmix_cfg_default(avx_audio_upmix_cfg_t* cfg);
342  
343 /**
344 * Opaque streaming stateful upmixer context.
345 * Maintains surround delay ring buffers and 2nd-order Butterworth LFE filter state.
346 */
347 typedef struct avx_audio_upmixer_t avx_audio_upmixer_t;
348  
349 /**
350 * Creates a stateful stereo to 5.1 surround upmixer.
351 */
352 OPENAVX_API avx_audio_upmixer_t* avx_audio_upmixer_create(
353 int sample_rate,
354 const avx_audio_upmix_cfg_t* cfg
355 );
356  
357 /**
358 * Destroys a stateful upmixer and frees internal delay/filter buffers.
359 */
360 OPENAVX_API void avx_audio_upmixer_destroy(avx_audio_upmixer_t* upmixer);
361  
362 /**
363 * Resets upmixer internal state (clears delay buffer and filter memory).
364 */
365 OPENAVX_API void avx_audio_upmixer_reset(avx_audio_upmixer_t* upmixer);
366  
367 /**
368 * Processes incoming stereo audio chunk through the stateful upmixer.
369 * out_channels: array of 6 float pointers [FL, FR, C, LFE, BL, BR].
370 */
371 OPENAVX_API void avx_audio_upmixer_process(
372 avx_audio_upmixer_t* upmixer,
373 const float* in_left,
374 const float* in_right,
375 float* const out_channels[6],
376 int frames
377 );
378  
379 /**
380 * One-shot Stereo to 5.1 Surround Upmixer.
381 * Convenient standalone helper for single-buffer processing.
382 *
383 * in_left, in_right: input stereo channel buffers.
384 * out_channels: array of 6 output pointers [FL, FR, C, LFE, BL, BR].
385 * frames: number of samples to process.
386 * sample_rate: audio sample rate (e.g. 48000).
387 * cfg: optional configuration. If NULL, default parameters are used.
388 */
389 OPENAVX_API void avx_audio_upmix_stereo_to_5_1(
390 const float* in_left,
391 const float* in_right,
392 float* const out_channels[6],
393 int frames,
394 int sample_rate,
395 const avx_audio_upmix_cfg_t* cfg
396 );
397  
398 #ifdef __cplusplus
399 }
400 #endif
401  
402 #endif /* AVX_AUDIO_H */