Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
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 Library
6 * test/openavx_audio_test.c - Unit and Verification Suite for Audio Engine
7 */
8  
9 #define _POSIX_C_SOURCE 200809L
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <stdbool.h>
14 #include <string.h>
15 #include <math.h>
16 #include <assert.h>
17 #include "openavx.h"
18 #include "avx_audio.h"
19 #include "avx_simd.h"
20  
21 #ifndef M_PI
22 #define M_PI 3.14159265358979323846
23 #endif
24  
25 #define ASSERT_TRUE(cond) do { \
26 if (!(cond)) { \
27 fprintf(stderr, "[FAIL] %s:%d: Assertion '%s' failed\n", __FILE__, __LINE__, #cond); \
28 exit(1); \
29 } \
30 } while(0)
31  
32 #define ASSERT_NEAR(a, b, eps) do { \
33 float _diff = fabsf((float)(a) - (float)(b)); \
34 if (_diff > (eps)) { \
35 fprintf(stderr, "[FAIL] %s:%d: Assertion '%s' =~ '%s' failed (diff=%f, eps=%f, a=%f, b=%f)\n", \
36 __FILE__, __LINE__, #a, #b, _diff, (float)(eps), (float)(a), (float)(b)); \
37 exit(1); \
38 } \
39 } while(0)
40  
41 /* ========================================================================= */
42 /* TEST 1: 5.1 DOWNMIXING TO STEREO */
43 /* ========================================================================= */
44  
45 static void test_downmix_5_1(void) {
46 printf("[*] Testing 5.1 -> Stereo Downmixing...\n");
47 int n = 1024;
48 float* fl = (float*)calloc(n, sizeof(float));
49 float* fr = (float*)calloc(n, sizeof(float));
50 float* c = (float*)calloc(n, sizeof(float));
51 float* lfe = (float*)calloc(n, sizeof(float));
52 float* bl = (float*)calloc(n, sizeof(float));
53 float* br = (float*)calloc(n, sizeof(float));
54  
55 float* out_l = (float*)malloc(n * sizeof(float));
56 float* out_r = (float*)malloc(n * sizeof(float));
57 const float* const in[6] = { fl, fr, c, lfe, bl, br };
58  
59 /* 1A: Center-only signal (should appear symmetrically in L and R with -3dB attenuation) */
60 for (int i = 0; i < n; ++i) c[i] = 1.0f;
61 avx_audio_downmix_5_1_to_stereo(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
62 for (int i = 0; i < n; ++i) {
63 ASSERT_NEAR(out_l[i], 0.70710678f, 1e-4f);
64 ASSERT_NEAR(out_r[i], 0.70710678f, 1e-4f);
65 }
66 memset(c, 0, n * sizeof(float));
67  
68 /* 1B: LFE-only signal (should be muted in ITU-R BS.775 stereo downmix) */
69 for (int i = 0; i < n; ++i) lfe[i] = 1.0f;
70 avx_audio_downmix_5_1_to_stereo(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
71 for (int i = 0; i < n; ++i) {
72 ASSERT_NEAR(out_l[i], 0.0f, 1e-6f);
73 ASSERT_NEAR(out_r[i], 0.0f, 1e-6f);
74 }
75 memset(lfe, 0, n * sizeof(float));
76  
77 /* 1C: Left surround only (should only appear in Left output) */
78 for (int i = 0; i < n; ++i) bl[i] = 0.8f;
79 avx_audio_downmix_5_1_to_stereo(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
80 for (int i = 0; i < n; ++i) {
81 ASSERT_NEAR(out_l[i], 0.8f * 0.70710678f, 1e-4f);
82 ASSERT_NEAR(out_r[i], 0.0f, 1e-6f);
83 }
84 memset(bl, 0, n * sizeof(float));
85  
86 /* 1D: Full-scale coherent signal across all channels with Peak Normalization */
87 for (int i = 0; i < n; ++i) {
88 fl[i] = 1.0f;
89 fr[i] = 1.0f;
90 c[i] = 1.0f;
91 bl[i] = 1.0f;
92 br[i] = 1.0f;
93 }
94 avx_audio_downmix_5_1_to_stereo(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_PEAK);
95 for (int i = 0; i < n; ++i) {
96 ASSERT_TRUE(out_l[i] <= 1.0001f);
97 ASSERT_TRUE(out_r[i] <= 1.0001f);
98 ASSERT_NEAR(out_l[i], 1.0f, 1e-4f);
99 ASSERT_NEAR(out_r[i], 1.0f, 1e-4f);
100 }
101  
102 /* 1E: Attenuation (-3dB) Normalization */
103 avx_audio_downmix_5_1_to_stereo(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_ATTEN_3DB);
104 for (int i = 0; i < n; ++i) {
105 ASSERT_TRUE(out_l[i] <= 1.0f);
106 ASSERT_TRUE(out_r[i] <= 1.0f);
107 ASSERT_NEAR(out_l[i], 1.0f, 1e-4f); /* Full-scale inputs normalized by sum */
108 }
109  
110 free(fl); free(fr); free(c); free(lfe); free(bl); free(br);
111 free(out_l); free(out_r);
112 printf(" [+] 5.1 Downmix: PASSED\n");
113 }
114  
115 /* ========================================================================= */
116 /* TEST 2: 7.1 DOWNMIXING TO STEREO */
117 /* ========================================================================= */
118  
119 static void test_downmix_7_1(void) {
120 printf("[*] Testing 7.1 -> Stereo Downmixing...\n");
121 int n = 512;
122 float* ch[8];
123 for (int i = 0; i < 8; ++i) ch[i] = (float*)calloc(n, sizeof(float));
124 float* out_l = (float*)malloc(n * sizeof(float));
125 float* out_r = (float*)malloc(n * sizeof(float));
126  
127 /* Side Left active */
128 for (int i = 0; i < n; ++i) ch[6][i] = 0.5f; /* SL */
129 avx_audio_downmix_7_1_to_stereo((const float* const*)ch, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
130 for (int i = 0; i < n; ++i) {
131 ASSERT_NEAR(out_l[i], 0.5f * 0.70710678f, 1e-4f);
132 ASSERT_NEAR(out_r[i], 0.0f, 1e-6f);
133 }
134  
135 /* Side Right active */
136 memset(ch[6], 0, n * sizeof(float));
137 for (int i = 0; i < n; ++i) ch[7][i] = 0.5f; /* SR */
138 avx_audio_downmix_7_1_to_stereo((const float* const*)ch, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
139 for (int i = 0; i < n; ++i) {
140 ASSERT_NEAR(out_l[i], 0.0f, 1e-6f);
141 ASSERT_NEAR(out_r[i], 0.5f * 0.70710678f, 1e-4f);
142 }
143  
144 for (int i = 0; i < 8; ++i) free(ch[i]);
145 free(out_l); free(out_r);
146 printf(" [+] 7.1 Downmix: PASSED\n");
147 }
148  
149 /* ========================================================================= */
150 /* TEST 3: QUAD & STEREO TO MONO */
151 /* ========================================================================= */
152  
153 static void test_quad_and_mono(void) {
154 printf("[*] Testing Quad & Stereo-to-Mono Downmixing...\n");
155 int n = 256;
156 float* q[4];
157 for (int i = 0; i < 4; ++i) q[i] = (float*)calloc(n, sizeof(float));
158 float* out_l = (float*)malloc(n * sizeof(float));
159 float* out_r = (float*)malloc(n * sizeof(float));
160  
161 for (int i = 0; i < n; ++i) {
162 q[0][i] = 0.6f; /* FL */
163 q[1][i] = 0.4f; /* FR */
164 q[2][i] = 0.2f; /* BL */
165 q[3][i] = 0.2f; /* BR */
166 }
167  
168 avx_audio_downmix_quad_to_stereo((const float* const*)q, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
169 for (int i = 0; i < n; ++i) {
170 ASSERT_NEAR(out_l[i], 0.6f + 0.2f * 0.70710678f, 1e-4f);
171 ASSERT_NEAR(out_r[i], 0.4f + 0.2f * 0.70710678f, 1e-4f);
172 }
173  
174 /* Test Stereo to Mono */
175 float* mono = (float*)malloc(n * sizeof(float));
176 avx_audio_downmix_stereo_to_mono(out_l, out_r, mono, n, AVX_AUDIO_NORMALIZE_ATTEN_3DB);
177 for (int i = 0; i < n; ++i) {
178 float expected = 0.5f * (out_l[i] + out_r[i]);
179 ASSERT_NEAR(mono[i], expected, 1e-4f);
180 }
181  
182 for (int i = 0; i < 4; ++i) free(q[i]);
183 free(out_l); free(out_r); free(mono);
184 printf(" [+] Quad & Stereo-to-Mono: PASSED\n");
185 }
186  
187 /* ========================================================================= */
188 /* TEST 4: ARBITRARY MATRIX MIXING */
189 /* ========================================================================= */
190  
191 static void test_matrix_mix(void) {
192 printf("[*] Testing Arbitrary Matrix Mixing...\n");
193 int n = 128;
194 float* in_ch[3];
195 for (int i = 0; i < 3; ++i) in_ch[i] = (float*)malloc(n * sizeof(float));
196 for (int i = 0; i < n; ++i) {
197 in_ch[0][i] = 1.0f;
198 in_ch[1][i] = 2.0f;
199 in_ch[2][i] = 3.0f;
200 }
201  
202 float* out_ch[2];
203 for (int i = 0; i < 2; ++i) out_ch[i] = (float*)malloc(n * sizeof(float));
204  
205 /* 2x3 matrix */
206 const float matrix[6] = {
207 0.5f, 0.25f, 0.1f, /* Out 0 = 0.5*1 + 0.25*2 + 0.1*3 = 0.5 + 0.5 + 0.3 = 1.3 */
208 0.0f, 1.0f, 0.5f /* Out 1 = 0 + 1.0*2 + 0.5*3 = 2.0 + 1.5 = 3.5 */
209 };
210  
211 avx_audio_matrix_mix((const float* const*)in_ch, 3, out_ch, 2, matrix, n, AVX_AUDIO_NORMALIZE_PEAK);
212 /* Peak is 3.5, normalized to 1.0 */
213 for (int i = 0; i < n; ++i) {
214 ASSERT_NEAR(out_ch[1][i], 1.0f, 1e-4f);
215 ASSERT_NEAR(out_ch[0][i], 1.3f / 3.5f, 1e-4f);
216 }
217  
218 for (int i = 0; i < 3; ++i) free(in_ch[i]);
219 for (int i = 0; i < 2; ++i) free(out_ch[i]);
220 printf(" [+] Matrix Mix: PASSED\n");
221 }
222  
223 /* ========================================================================= */
224 /* TEST 5: OPENAVX AUDIO FRAME DOWNMIX HELPER */
225 /* ========================================================================= */
226  
227 static void test_frame_downmix(void) {
228 printf("[*] Testing oavx_audio_frame_t Frame Downmix Helper...\n");
229 int samples = 480;
230 int channels = 6;
231 float* data = (float*)calloc((size_t)channels * samples, sizeof(float));
232 /* Fill channel 0 (FL) and 1 (FR) */
233 for (int i = 0; i < samples; ++i) {
234 data[0 * samples + i] = 0.5f;
235 data[1 * samples + i] = 0.25f;
236 }
237  
238 oavx_audio_frame_t src;
239 memset(&src, 0, sizeof(src));
240 src.sample_rate = 48000;
241 src.channels = channels;
242 src.frames = samples;
243 src.format = OAVX_AUDIO_FMT_FLT_PLANAR;
244 src.pts_ns = 12345678LL;
245 src.planes[0] = data;
246 src.planes[1] = data + samples;
247 src.stride_bytes = samples * sizeof(float);
248 src.metadata = "test_meta";
249 src.metadata_len = strlen("test_meta");
250  
251 oavx_audio_frame_t dst;
252 memset(&dst, 0, sizeof(dst));
253 bool ok = avx_audio_frame_downmix_to_stereo(&src, &dst, AVX_AUDIO_NORMALIZE_NONE);
254 ASSERT_TRUE(ok);
255 ASSERT_TRUE(dst.channels == 2);
256 ASSERT_TRUE(dst.frames == (uint32_t)samples);
257 ASSERT_TRUE(dst.sample_rate == 48000);
258 ASSERT_TRUE(dst.pts_ns == 12345678LL);
259  
260 const float* out_l = (const float*)dst.planes[0];
261 const float* out_r = (const float*)dst.planes[1];
262 for (int i = 0; i < samples; ++i) {
263 ASSERT_NEAR(out_l[i], 0.5f, 1e-5f);
264 ASSERT_NEAR(out_r[i], 0.25f, 1e-5f);
265 }
266  
267 avx_audio_frame_free_data(&dst);
268 ASSERT_TRUE(dst.planes[0] == NULL);
269 free(data);
270 printf(" [+] Frame Downmix Helper: PASSED\n");
271 }
272  
273 /* ========================================================================= */
274 /* TEST 6: STREAMING RESAMPLER EXACT SAMPLE ACCURACY */
275 /* ========================================================================= */
276  
277 static void test_resampler_exact_count(void) {
278 printf("[*] Testing Streaming Resampler Continuity & Exact Sample Counts...\n");
279  
280 const int chunk_sizes[] = { 128, 256, 512, 1000 };
281 const avx_audio_resample_mode_e modes[] = {
282 AVX_AUDIO_RESAMPLE_LINEAR,
283 AVX_AUDIO_RESAMPLE_CUBIC,
284 AVX_AUDIO_RESAMPLE_SINC
285 };
286 const char* mode_names[] = { "Linear", "Cubic", "Sinc" };
287  
288 /* 6A: 44.1 kHz -> 48 kHz (exact 48000 output samples for 44100 input samples) */
289 for (int m = 0; m < 3; ++m) {
290 for (int cs = 0; cs < 4; ++cs) {
291 int chunk = chunk_sizes[cs];
292 int in_rate = 44100;
293 int out_rate = 48000;
294 int total_in = in_rate;
295  
296 avx_resampler_t* r = avx_resampler_create(in_rate, out_rate, 2, modes[m]);
297 ASSERT_TRUE(r != NULL);
298  
299 float* in_l = (float*)malloc(chunk * sizeof(float));
300 float* in_r = (float*)malloc(chunk * sizeof(float));
301 float* out_l = (float*)malloc(2048 * sizeof(float));
302 float* out_r = (float*)malloc(2048 * sizeof(float));
303 const float* in_ch[2] = { in_l, in_r };
304 float* out_ch[2] = { out_l, out_r };
305  
306 int total_out = 0;
307 int processed_in = 0;
308  
309 while (processed_in < total_in) {
310 int this_chunk = chunk;
311 if (processed_in + this_chunk > total_in) {
312 this_chunk = total_in - processed_in;
313 }
314 for (int i = 0; i < this_chunk; ++i) {
315 float t = (float)(processed_in + i) / (float)in_rate;
316 in_l[i] = sinf(2.0f * (float)M_PI * 440.0f * t);
317 in_r[i] = cosf(2.0f * (float)M_PI * 440.0f * t);
318 }
319  
320 int n = avx_resampler_process(r, in_ch, this_chunk, out_ch, 2048);
321 total_out += n;
322 processed_in += this_chunk;
323 }
324  
325 int flushed = avx_resampler_flush(r, out_ch, 2048);
326 total_out += flushed;
327  
328 ASSERT_TRUE(total_out == out_rate);
329  
330 free(in_l); free(in_r); free(out_l); free(out_r);
331 avx_resampler_destroy(r);
332 }
333 printf(" [+] 44.1k -> 48k Exact 48,000 Samples (%s across all chunk sizes): PASSED\n", mode_names[m]);
334 }
335  
336 /* 6B: 48 kHz -> 44.1 kHz (exact 44100 output samples for 48000 input samples) */
337 for (int m = 0; m < 3; ++m) {
338 avx_resampler_t* r = avx_resampler_create(48000, 44100, 1, modes[m]);
339 ASSERT_TRUE(r != NULL);
340  
341 int chunk = 512;
342 int total_in = 48000;
343 int processed_in = 0;
344 int total_out = 0;
345  
346 float* in = (float*)malloc(chunk * sizeof(float));
347 float* out = (float*)malloc(2048 * sizeof(float));
348 const float* in_ch[1] = { in };
349 float* out_ch[1] = { out };
350  
351 while (processed_in < total_in) {
352 int this_chunk = chunk;
353 if (processed_in + this_chunk > total_in) {
354 this_chunk = total_in - processed_in;
355 }
356 for (int i = 0; i < this_chunk; ++i) {
357 in[i] = 0.5f;
358 }
359 int n = avx_resampler_process(r, in_ch, this_chunk, out_ch, 2048);
360 total_out += n;
361 processed_in += this_chunk;
362 }
363  
364 int flushed = avx_resampler_flush(r, out_ch, 2048);
365 total_out += flushed;
366  
367 ASSERT_TRUE(total_out == 44100);
368  
369 free(in); free(out);
370 avx_resampler_destroy(r);
371 printf(" [+] 48k -> 44.1k Exact 44,100 Samples (%s): PASSED\n", mode_names[m]);
372 }
373 }
374  
375 /* ========================================================================= */
376 /* TEST 7: RESAMPLER SPECTRAL PURITY & SNR */
377 /* ========================================================================= */
378  
379 static void test_resampler_snr(void) {
380 printf("[*] Testing Resampler Numerical SNR (Signal-to-Noise Ratio)...\n");
381  
382 int in_rate = 44100;
383 int out_rate = 48000;
384 float f0 = 1000.0f; /* 1 kHz test tone */
385  
386 float* in_buf = (float*)malloc(in_rate * sizeof(float));
387 for (int i = 0; i < in_rate; ++i) {
388 float t = (float)i / (float)in_rate;
389 in_buf[i] = sinf(2.0f * (float)M_PI * f0 * t);
390 }
391  
392 const avx_audio_resample_mode_e modes[] = {
393 AVX_AUDIO_RESAMPLE_LINEAR,
394 AVX_AUDIO_RESAMPLE_CUBIC,
395 AVX_AUDIO_RESAMPLE_SINC
396 };
397 const float min_snr[] = { 45.0f, 70.0f, 70.0f };
398 const char* mode_names[] = { "Linear", "Cubic", "Windowed Sinc" };
399  
400 for (int m = 0; m < 3; ++m) {
401 float* out_buf = (float*)malloc((out_rate + 256) * sizeof(float));
402  
403 int written = avx_audio_resample_simple(in_buf, in_rate, in_rate, out_buf, out_rate + 256, out_rate, modes[m]);
404 ASSERT_TRUE(written >= out_rate);
405  
406 /* Compute SNR against theoretical 1 kHz sine at out_rate */
407 /* Ignore 100 samples at edges to avoid boundary transient */
408 double sig_power = 0.0;
409 double noise_power = 0.0;
410 int count = 0;
411  
412 for (int i = 100; i < out_rate - 100; ++i) {
413 float t = (float)i / (float)out_rate;
414 float ref = sinf(2.0f * (float)M_PI * f0 * t);
415 float actual = out_buf[i];
416 float err = actual - ref;
417  
418 sig_power += (double)ref * ref;
419 noise_power += (double)err * err;
420 count++;
421 }
422  
423 ASSERT_TRUE(count > 0);
424 double snr_db = 10.0 * log10(sig_power / noise_power);
425 printf(" [+] %-15s SNR: %.2f dB (Threshold: >= %.1f dB)\n", mode_names[m], snr_db, min_snr[m]);
426 ASSERT_TRUE(snr_db >= min_snr[m]);
427  
428 free(out_buf);
429 }
430  
431 free(in_buf);
432 }
433  
434 /* ========================================================================= */
435 /* TEST 8: FRAME RESAMPLING HELPER */
436 /* ========================================================================= */
437  
438 static void test_frame_resample(void) {
439 printf("[*] Testing oavx_audio_frame_t Frame Resampling Helper...\n");
440  
441 avx_resampler_t* r = avx_resampler_create(44100, 48000, 2, AVX_AUDIO_RESAMPLE_CUBIC);
442 ASSERT_TRUE(r != NULL);
443  
444 int in_samples = 441;
445 float* in_data = (float*)calloc(2 * in_samples, sizeof(float));
446 for (int i = 0; i < in_samples; ++i) {
447 in_data[i] = 0.3f;
448 in_data[in_samples + i] = 0.7f;
449 }
450  
451 oavx_audio_frame_t src;
452 memset(&src, 0, sizeof(src));
453 src.sample_rate = 44100;
454 src.channels = 2;
455 src.frames = in_samples;
456 src.format = OAVX_AUDIO_FMT_FLT_PLANAR;
457 src.pts_ns = 55555LL;
458 src.planes[0] = in_data;
459 src.planes[1] = in_data + in_samples;
460 src.stride_bytes = in_samples * sizeof(float);
461 src.metadata = NULL;
462  
463 oavx_audio_frame_t dst;
464 memset(&dst, 0, sizeof(dst));
465 bool ok = avx_audio_frame_resample(r, &src, &dst);
466 ASSERT_TRUE(ok);
467 ASSERT_TRUE(dst.sample_rate == 48000);
468 ASSERT_TRUE(dst.channels == 2);
469 ASSERT_TRUE(dst.frames > 0);
470 ASSERT_TRUE(dst.planes[0] != NULL);
471  
472 avx_audio_frame_free_data(&dst);
473 ASSERT_TRUE(dst.planes[0] == NULL);
474  
475 free(in_data);
476 avx_resampler_destroy(r);
477 printf(" [+] Frame Resampling Helper: PASSED\n");
478 }
479  
480 /* ========================================================================= */
481 /* TEST 9: ADAPTIVE AUDIO JITTER BUFFER & DRIFT SLEWING */
482 /* ========================================================================= */
483  
484 static void test_audio_jitter_buffer(void) {
485 printf("[*] Testing Adaptive Audio Jitter Buffer & Micro-Slewing...\n");
486  
487 const int rate = 48000;
488 const int chs = 2;
489 avx_audio_jitter_config_t cfg = {
490 .sample_rate = rate,
491 .channels = chs,
492 .target_cushion_ms = 15, /* 720 samples */
493 .min_cushion_ms = 5, /* 240 samples */
494 .max_cushion_ms = 35 /* 1680 samples */
495 };
496  
497 avx_audio_jitter_t* jb = avx_audio_jitter_create(&cfg);
498 ASSERT_TRUE(jb != NULL);
499 ASSERT_TRUE(avx_audio_jitter_get_depth_samples(jb) == 0);
500 ASSERT_NEAR(avx_audio_jitter_get_depth_ms(jb), 0.0f, 1e-4f);
501  
502 /* 1. Pre-roll verification: pushing less than target cushion outputs pre-roll silence */
503 const int chunk1_len = 480;
504 float* in_l = (float*)malloc(chunk1_len * sizeof(float));
505 float* in_r = (float*)malloc(chunk1_len * sizeof(float));
506 for (int i = 0; i < chunk1_len; ++i) {
507 in_l[i] = 0.5f;
508 in_r[i] = -0.5f;
509 }
510 const float* in_ch[2] = { in_l, in_r };
511 avx_audio_jitter_push(jb, in_ch, chunk1_len);
512 ASSERT_TRUE(avx_audio_jitter_get_depth_samples(jb) == 480);
513  
514 /* Pull 240 samples during pre-roll -> should deliver pre-roll silence */
515 float* out_l = (float*)malloc(480 * sizeof(float));
516 float* out_r = (float*)malloc(480 * sizeof(float));
517 float* out_ch[2] = { out_l, out_r };
518  
519 int pulled = avx_audio_jitter_pull(jb, out_ch, 240);
520 ASSERT_TRUE(pulled == 240);
521 for (int i = 0; i < 240; ++i) {
522 ASSERT_TRUE(out_l[i] == 0.0f);
523 ASSERT_TRUE(out_r[i] == 0.0f);
524 }
525 /* Depth should still be 480 because pre-roll did not consume buffered samples */
526 ASSERT_TRUE(avx_audio_jitter_get_depth_samples(jb) == 480);
527  
528 /* 2. Push second chunk to cross target cushion (480 + 480 = 960 > 720) */
529 avx_audio_jitter_push(jb, in_ch, chunk1_len);
530 ASSERT_TRUE(avx_audio_jitter_get_depth_samples(jb) == 960);
531  
532 /* Now pull 240 samples -> should deliver actual audio (0.5f / -0.5f) */
533 pulled = avx_audio_jitter_pull(jb, out_ch, 240);
534 ASSERT_TRUE(pulled == 240);
535 ASSERT_NEAR(out_l[10], 0.5f, 1e-3f);
536 ASSERT_NEAR(out_r[10], -0.5f, 1e-3f);
537 ASSERT_TRUE(avx_audio_jitter_get_depth_samples(jb) < 960);
538 printf(" [+] Pre-Roll Cushion & Priming Phase: PASSED\n");
539  
540 /* 3. Burst Jitter & Micro-Slewing Clock Steer */
541 /* Overfill buffer to 1500 samples (far above target cushion 720) */
542 for (int b = 0; b < 2; ++b) {
543 avx_audio_jitter_push(jb, in_ch, chunk1_len);
544 }
545 int initial_depth = avx_audio_jitter_get_depth_samples(jb);
546 ASSERT_TRUE(initial_depth > 1200);
547  
548 /* Pull multiple 128-sample blocks; micro-slewing at 1.0005 should gently drain depth */
549 for (int p = 0; p < 5; ++p) {
550 pulled = avx_audio_jitter_pull(jb, out_ch, 128);
551 ASSERT_TRUE(pulled == 128);
552 }
553 int steered_depth = avx_audio_jitter_get_depth_samples(jb);
554 ASSERT_TRUE(steered_depth < initial_depth);
555 printf(" [+] Micro-Slewing Clock Drift Correction: PASSED\n");
556  
557 /* 4. Smooth Cosine Concealment on Underrun */
558 /* Drain entire buffer down to starvation */
559 while (avx_audio_jitter_get_depth_samples(jb) > 0) {
560 avx_audio_jitter_pull(jb, out_ch, 128);
561 }
562 ASSERT_TRUE(avx_audio_jitter_get_depth_samples(jb) == 0);
563  
564 /* Pulling during starvation should not pop: smooth fade out to 0 */
565 pulled = avx_audio_jitter_pull(jb, out_ch, 128);
566 ASSERT_TRUE(pulled == 128);
567 ASSERT_TRUE(out_l[127] == 0.0f);
568 printf(" [+] Smooth Cosine Concealment on Underrun: PASSED\n");
569  
570 /* Cleanup */
571 free(in_l);
572 free(in_r);
573 free(out_l);
574 free(out_r);
575 avx_audio_jitter_destroy(jb);
576 printf(" [+] Adaptive Jitter Buffer Lifecycle: PASSED\n");
577 }
578  
579 /* ========================================================================= */
580 /* TEST 10: CHANNEL LAYOUTS, MASKS & 32-CHANNEL SEMANTICS */
581 /* ========================================================================= */
582  
583 static void test_channel_layouts_and_masks(void) {
584 printf("[*] Testing Channel Layouts, Semantic Masks & 32-Channel Semantics...\n");
585  
586 /* 1. Spatial Layouts: Channel Counts & Standard Masks */
587 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_MONO) == 1);
588 ASSERT_TRUE(avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_MONO) == OAVX_AUDIO_MASK_MONO);
589 ASSERT_TRUE(strcmp(avx_audio_layout_name(AVX_AUDIO_LAYOUT_MONO), "Mono (1.0)") == 0);
590 ASSERT_TRUE(strcmp(avx_audio_channel_name(0, AVX_AUDIO_LAYOUT_MONO), "Center / Mono") == 0);
591  
592 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_STEREO) == 2);
593 ASSERT_TRUE(avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_STEREO) == OAVX_AUDIO_MASK_STEREO);
594 ASSERT_TRUE(strcmp(avx_audio_channel_name(0, AVX_AUDIO_LAYOUT_STEREO), "Left") == 0);
595 ASSERT_TRUE(strcmp(avx_audio_channel_name(1, AVX_AUDIO_LAYOUT_STEREO), "Right") == 0);
596  
597 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_2_1) == 3);
598 ASSERT_TRUE(avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_2_1) == OAVX_AUDIO_MASK_2_1);
599  
600 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_QUAD) == 4);
601 ASSERT_TRUE(avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_QUAD) == OAVX_AUDIO_MASK_QUAD);
602  
603 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_5_1) == 6);
604 ASSERT_TRUE(avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_5_1) == OAVX_AUDIO_MASK_5_1);
605 ASSERT_TRUE(strcmp(avx_audio_channel_name(2, AVX_AUDIO_LAYOUT_5_1), "Center") == 0);
606 ASSERT_TRUE(strcmp(avx_audio_channel_name(3, AVX_AUDIO_LAYOUT_5_1), "LFE / Sub") == 0);
607 ASSERT_TRUE(strcmp(avx_audio_channel_name(4, AVX_AUDIO_LAYOUT_5_1), "Surround Left") == 0);
608 ASSERT_TRUE(strcmp(avx_audio_channel_name(5, AVX_AUDIO_LAYOUT_5_1), "Surround Right") == 0);
609  
610 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_7_1) == 8);
611 ASSERT_TRUE(avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_7_1) == OAVX_AUDIO_MASK_7_1);
612 ASSERT_TRUE(strcmp(avx_audio_channel_name(6, AVX_AUDIO_LAYOUT_7_1), "Side Left") == 0);
613 ASSERT_TRUE(strcmp(avx_audio_channel_name(7, AVX_AUDIO_LAYOUT_7_1), "Side Right") == 0);
614  
615 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_7_1_4) == 12);
616 ASSERT_TRUE(avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_7_1_4) == OAVX_AUDIO_MASK_7_1_4);
617 ASSERT_TRUE(strcmp(avx_audio_channel_name(8, AVX_AUDIO_LAYOUT_7_1_4), "Top Front Left") == 0);
618  
619 /* 2. Broadcast Discrete Stems: 16-channel & 32-channel */
620 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_BROADCAST_16CH) == 16);
621 ASSERT_TRUE(strcmp(avx_audio_layout_name(AVX_AUDIO_LAYOUT_BROADCAST_16CH), "Broadcast 16-Channel SDI") == 0);
622 ASSERT_TRUE(strcmp(avx_audio_channel_name(0, AVX_AUDIO_LAYOUT_BROADCAST_16CH), "Track 1") == 0);
623 ASSERT_TRUE(strcmp(avx_audio_channel_name(15, AVX_AUDIO_LAYOUT_BROADCAST_16CH), "Track 16") == 0);
624  
625 ASSERT_TRUE(avx_audio_layout_channels(AVX_AUDIO_LAYOUT_BROADCAST_32CH) == 32);
626 ASSERT_TRUE(strcmp(avx_audio_layout_name(AVX_AUDIO_LAYOUT_BROADCAST_32CH), "Broadcast 32-Channel Pro-Audio") == 0);
627 ASSERT_TRUE(strcmp(avx_audio_channel_name(31, AVX_AUDIO_LAYOUT_BROADCAST_32CH), "Track 32") == 0);
628  
629 /* 3. Automatic Deduction & Round-Tripping */
630 ASSERT_TRUE(avx_audio_layout_from_channels(1) == AVX_AUDIO_LAYOUT_MONO);
631 ASSERT_TRUE(avx_audio_layout_from_channels(2) == AVX_AUDIO_LAYOUT_STEREO);
632 ASSERT_TRUE(avx_audio_layout_from_channels(6) == AVX_AUDIO_LAYOUT_5_1);
633 ASSERT_TRUE(avx_audio_layout_from_channels(8) == AVX_AUDIO_LAYOUT_7_1);
634 ASSERT_TRUE(avx_audio_layout_from_channels(12) == AVX_AUDIO_LAYOUT_7_1_4);
635 ASSERT_TRUE(avx_audio_layout_from_channels(16) == AVX_AUDIO_LAYOUT_BROADCAST_16CH);
636 ASSERT_TRUE(avx_audio_layout_from_channels(32) == AVX_AUDIO_LAYOUT_BROADCAST_32CH);
637 ASSERT_TRUE(avx_audio_layout_from_channels(24) == AVX_AUDIO_LAYOUT_DISCRETE);
638  
639 ASSERT_TRUE(avx_audio_layout_from_mask(OAVX_AUDIO_MASK_5_1, 6) == AVX_AUDIO_LAYOUT_5_1);
640 ASSERT_TRUE(avx_audio_layout_from_mask(OAVX_AUDIO_MASK_7_1, 8) == AVX_AUDIO_LAYOUT_7_1);
641 ASSERT_TRUE(avx_audio_layout_from_mask(OAVX_AUDIO_MASK_7_1_4, 12) == AVX_AUDIO_LAYOUT_7_1_4);
642  
643 /* 4. Audio Frame 32-Channel Capacity */
644 ASSERT_TRUE(OAVX_MAX_AUDIO_CHANNELS == 32);
645 oavx_audio_frame_t f32;
646 memset(&f32, 0, sizeof(f32));
647 f32.channels = 32;
648 f32.channel_mask = avx_audio_layout_to_mask(AVX_AUDIO_LAYOUT_BROADCAST_32CH);
649 for (int i = 0; i < 32; ++i) {
650 f32.planes[i] = (void*)(uintptr_t)(0x1000 + i * 8);
651 }
652 ASSERT_TRUE(f32.planes[31] == (void*)(uintptr_t)(0x1000 + 31 * 8));
653  
654 printf(" [+] Channel Layouts, Semantic Masks & 32-Channel Semantics: PASSED\n");
655 }
656  
657 /* ========================================================================= */
658 /* TEST 11: CHANNEL ROUTING & SHUFFLING (UP TO 32 CHANNELS) */
659 /* ========================================================================= */
660  
661 static void test_audio_route_channels(void) {
662 printf("[*] Testing Audio Channel Routing & Shuffling...\n");
663 int n = 256;
664  
665 /* 1. 1:1 Identity routing with NULL map */
666 {
667 float* in[4];
668 float* out[4];
669 for (int c = 0; c < 4; ++c) {
670 in[c] = (float*)malloc(n * sizeof(float));
671 out[c] = (float*)malloc(n * sizeof(float));
672 for (int i = 0; i < n; ++i) in[c][i] = (float)(c + 1) * 10.0f + (float)i;
673 }
674  
675 avx_audio_route_channels((const float* const*)in, 4, out, 4, NULL, NULL, n);
676 for (int c = 0; c < 4; ++c) {
677 for (int i = 0; i < n; ++i) {
678 ASSERT_NEAR(out[c][i], in[c][i], 1e-6f);
679 }
680 }
681  
682 for (int c = 0; c < 4; ++c) { free(in[c]); free(out[c]); }
683 }
684  
685 /* 2. Custom shuffle, track duplication, and muting (-1) with gains */
686 {
687 float* in[4];
688 float* out[4];
689 for (int c = 0; c < 4; ++c) {
690 in[c] = (float*)malloc(n * sizeof(float));
691 out[c] = (float*)malloc(n * sizeof(float));
692 for (int i = 0; i < n; ++i) in[c][i] = 1.0f + (float)c;
693 }
694  
695 /* out[0] = in[3] * 0.5f
696 * out[1] = in[0] (gain 1.0f)
697 * out[2] = -1 (muted -> 0.0f)
698 * out[3] = in[0] * 0.25f (duplicated in[0])
699 */
700 int8_t map[4] = { 3, 0, -1, 0 };
701 float gains[4] = { 0.5f, 1.0f, 1.0f, 0.25f };
702  
703 avx_audio_route_channels((const float* const*)in, 4, out, 4, map, gains, n);
704  
705 for (int i = 0; i < n; ++i) {
706 ASSERT_NEAR(out[0][i], 4.0f * 0.5f, 1e-6f); /* in[3]=4.0 * 0.5 = 2.0 */
707 ASSERT_NEAR(out[1][i], 1.0f * 1.0f, 1e-6f); /* in[0]=1.0 * 1.0 = 1.0 */
708 ASSERT_NEAR(out[2][i], 0.0f, 1e-6f); /* muted */
709 ASSERT_NEAR(out[3][i], 1.0f * 0.25f, 1e-6f); /* in[0]=1.0 * 0.25 = 0.25 */
710 }
711  
712 for (int c = 0; c < 4; ++c) { free(in[c]); free(out[c]); }
713 }
714  
715 /* 3. In-place aliasing channel swap */
716 {
717 float* ch[2];
718 ch[0] = (float*)malloc(n * sizeof(float));
719 ch[1] = (float*)malloc(n * sizeof(float));
720 for (int i = 0; i < n; ++i) {
721 ch[0][i] = 111.0f;
722 ch[1][i] = 222.0f;
723 }
724  
725 int8_t swap_map[2] = { 1, 0 };
726 avx_audio_route_channels((const float* const*)ch, 2, ch, 2, swap_map, NULL, n);
727  
728 for (int i = 0; i < n; ++i) {
729 ASSERT_NEAR(ch[0][i], 222.0f, 1e-6f);
730 ASSERT_NEAR(ch[1][i], 111.0f, 1e-6f);
731 }
732  
733 free(ch[0]); free(ch[1]);
734 }
735  
736 /* 4. 32-Channel Broadcast Shuffler */
737 {
738 float* in32[32];
739 float* out32[32];
740 for (int c = 0; c < 32; ++c) {
741 in32[c] = (float*)malloc(n * sizeof(float));
742 out32[c] = (float*)malloc(n * sizeof(float));
743 for (int i = 0; i < n; ++i) in32[c][i] = (float)(c + 1);
744 }
745  
746 /* Reverse 32 channels */
747 int8_t rev_map[32];
748 for (int c = 0; c < 32; ++c) rev_map[c] = (int8_t)(31 - c);
749  
750 avx_audio_route_channels((const float* const*)in32, 32, out32, 32, rev_map, NULL, n);
751 for (int c = 0; c < 32; ++c) {
752 float expected = (float)(32 - c);
753 for (int i = 0; i < n; ++i) {
754 ASSERT_NEAR(out32[c][i], expected, 1e-6f);
755 }
756 }
757  
758 for (int c = 0; c < 32; ++c) { free(in32[c]); free(out32[c]); }
759 }
760  
761 /* 5. Frame-level routing helper (avx_audio_frame_route_channels) */
762 {
763 oavx_audio_frame_t src_frame;
764 memset(&src_frame, 0, sizeof(src_frame));
765 src_frame.channels = 2;
766 src_frame.frames = (uint32_t)n;
767 src_frame.sample_rate = 48000;
768 src_frame.format = OAVX_AUDIO_FMT_FLT_PLANAR;
769 float* p0 = (float*)malloc(2 * n * sizeof(float));
770 src_frame.planes[0] = p0;
771 src_frame.planes[1] = p0 + n;
772 for (int i = 0; i < n; ++i) {
773 ((float*)src_frame.planes[0])[i] = 10.0f;
774 ((float*)src_frame.planes[1])[i] = 20.0f;
775 }
776  
777 oavx_audio_frame_t dst_frame;
778 int8_t map4[4] = { 1, 0, -1, 0 };
779 bool ok = avx_audio_frame_route_channels(&src_frame, &dst_frame, map4, NULL, 4);
780 ASSERT_TRUE(ok);
781 ASSERT_TRUE(dst_frame.channels == 4);
782 ASSERT_TRUE(dst_frame.frames == (uint32_t)n);
783  
784 for (int i = 0; i < n; ++i) {
785 ASSERT_NEAR(((float*)dst_frame.planes[0])[i], 20.0f, 1e-6f);
786 ASSERT_NEAR(((float*)dst_frame.planes[1])[i], 10.0f, 1e-6f);
787 ASSERT_NEAR(((float*)dst_frame.planes[2])[i], 0.0f, 1e-6f);
788 ASSERT_NEAR(((float*)dst_frame.planes[3])[i], 10.0f, 1e-6f);
789 }
790  
791 avx_audio_frame_free_data(&dst_frame);
792 free(p0);
793 }
794  
795 printf(" [+] Audio Channel Routing & Shuffling: PASSED\n");
796 }
797  
798 /* ========================================================================= */
799 /* TEST 12: 5.1 TO LT/RT MATRIX DOWNMIXING */
800 /* ========================================================================= */
801  
802 static void test_downmix_5_1_ltrt(void) {
803 printf("[*] Testing 5.1 -> Lt/Rt Matrix Downmixing...\n");
804 int n = 1024;
805 float* fl = (float*)calloc(n, sizeof(float));
806 float* fr = (float*)calloc(n, sizeof(float));
807 float* c = (float*)calloc(n, sizeof(float));
808 float* lfe = (float*)calloc(n, sizeof(float));
809 float* bl = (float*)calloc(n, sizeof(float));
810 float* br = (float*)calloc(n, sizeof(float));
811  
812 float* out_l = (float*)malloc(n * sizeof(float));
813 float* out_r = (float*)malloc(n * sizeof(float));
814 const float* const in[6] = { fl, fr, c, lfe, bl, br };
815  
816 /* 1. Center-only: appears in-phase in both Lt and Rt at 0.7071 */
817 for (int i = 0; i < n; ++i) c[i] = 1.0f;
818 avx_audio_downmix_5_1_to_ltrt(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
819 for (int i = 0; i < n; ++i) {
820 ASSERT_NEAR(out_l[i], 0.70710678f, 1e-4f);
821 ASSERT_NEAR(out_r[i], 0.70710678f, 1e-4f);
822 /* In matrix decoder, (Lt - Rt) cancels Center completely */
823 ASSERT_NEAR(out_l[i] - out_r[i], 0.0f, 1e-5f);
824 }
825 for (int i = 0; i < n; ++i) c[i] = 0.0f;
826  
827 /* 2. Left-only / Right-only */
828 for (int i = 0; i < n; ++i) fl[i] = 1.0f;
829 avx_audio_downmix_5_1_to_ltrt(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
830 for (int i = 0; i < n; ++i) {
831 ASSERT_NEAR(out_l[i], 1.0f, 1e-4f);
832 ASSERT_NEAR(out_r[i], 0.0f, 1e-4f);
833 }
834 for (int i = 0; i < n; ++i) fl[i] = 0.0f;
835  
836 for (int i = 0; i < n; ++i) fr[i] = 1.0f;
837 avx_audio_downmix_5_1_to_ltrt(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
838 for (int i = 0; i < n; ++i) {
839 ASSERT_NEAR(out_l[i], 0.0f, 1e-4f);
840 ASSERT_NEAR(out_r[i], 1.0f, 1e-4f);
841 }
842 for (int i = 0; i < n; ++i) fr[i] = 0.0f;
843  
844 /* 3. Surround Left (BL): Lt = -0.7071, Rt = +0.7071 (180 deg phase steering) */
845 for (int i = 0; i < n; ++i) bl[i] = 1.0f;
846 avx_audio_downmix_5_1_to_ltrt(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
847 for (int i = 0; i < n; ++i) {
848 ASSERT_NEAR(out_l[i], -0.70710678f, 1e-4f);
849 ASSERT_NEAR(out_r[i], +0.70710678f, 1e-4f);
850 /* In matrix decoder sum (Lt + Rt), Surround cancels completely */
851 ASSERT_NEAR(out_l[i] + out_r[i], 0.0f, 1e-5f);
852 }
853 for (int i = 0; i < n; ++i) bl[i] = 0.0f;
854  
855 /* 4. Surround Right (BR): Lt = -0.7071, Rt = +0.7071 */
856 for (int i = 0; i < n; ++i) br[i] = 1.0f;
857 avx_audio_downmix_5_1_to_ltrt(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_NONE);
858 for (int i = 0; i < n; ++i) {
859 ASSERT_NEAR(out_l[i], -0.70710678f, 1e-4f);
860 ASSERT_NEAR(out_r[i], +0.70710678f, 1e-4f);
861 }
862 for (int i = 0; i < n; ++i) br[i] = 0.0f;
863  
864 /* 5. Normalization modes: ATTEN_3DB and PEAK */
865 for (int i = 0; i < n; ++i) {
866 fl[i] = 0.8f;
867 fr[i] = 0.8f;
868 c[i] = 0.8f;
869 bl[i] = 0.8f;
870 br[i] = 0.8f;
871 }
872 avx_audio_downmix_5_1_to_ltrt(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_ATTEN_3DB);
873 for (int i = 0; i < n; ++i) {
874 ASSERT_TRUE(out_l[i] <= 1.0f && out_l[i] >= -1.0f);
875 ASSERT_TRUE(out_r[i] <= 1.0f && out_r[i] >= -1.0f);
876 }
877  
878 avx_audio_downmix_5_1_to_ltrt(in, out_l, out_r, n, AVX_AUDIO_NORMALIZE_PEAK);
879 float max_p = 0.0f;
880 for (int i = 0; i < n; ++i) {
881 if (fabsf(out_l[i]) > max_p) max_p = fabsf(out_l[i]);
882 if (fabsf(out_r[i]) > max_p) max_p = fabsf(out_r[i]);
883 }
884 ASSERT_NEAR(max_p, 1.0f, 1e-4f);
885  
886 free(fl); free(fr); free(c); free(lfe); free(bl); free(br);
887 free(out_l); free(out_r);
888 printf(" [+] 5.1 -> Lt/Rt Matrix Downmixing: PASSED\n");
889 }
890  
891 /* ========================================================================= */
892 /* TEST 13: STEREO TO 5.1 SURROUND UPMIXER */
893 /* ========================================================================= */
894  
895 static void test_upmix_stereo_to_5_1(void) {
896 printf("[*] Testing Stereo -> 5.1 Surround Upmixer...\n");
897 int n = 1000;
898 int sample_rate = 48000;
899 float* in_l = (float*)calloc(n, sizeof(float));
900 float* in_r = (float*)calloc(n, sizeof(float));
901  
902 float* out[6];
903 for (int c = 0; c < 6; ++c) out[c] = (float*)malloc(n * sizeof(float));
904  
905 /* 1. Pure Mono Dialogue: L = 1.0, R = 1.0
906 * Should extract Center C = 0.7071
907 * Surrounds BL, BR must be 0.0 (no bleed of dialogue into rear speakers)
908 * Fronts FL and FR are attenuated by center extraction (1.0 - 0.5 * 0.7071 ~= 0.64645)
909 */
910 for (int i = 0; i < n; ++i) { in_l[i] = 1.0f; in_r[i] = 1.0f; }
911 avx_audio_upmix_cfg_t cfg;
912 avx_audio_upmix_cfg_default(&cfg);
913 cfg.ambient_delay_ms = 0; /* test direct without delay first */
914  
915 avx_audio_upmix_stereo_to_5_1(in_l, in_r, out, n, sample_rate, &cfg);
916  
917 for (int i = 0; i < n; ++i) {
918 ASSERT_NEAR(out[2][i], 0.70710678f, 1e-4f); /* Center */
919 ASSERT_NEAR(out[4][i], 0.0f, 1e-5f); /* Back Left = 0 */
920 ASSERT_NEAR(out[5][i], 0.0f, 1e-5f); /* Back Right = 0 */
921 ASSERT_NEAR(out[0][i], 1.0f - 0.5f * 0.70710678f, 1e-4f); /* FL */
922 ASSERT_NEAR(out[1][i], 1.0f - 0.5f * 0.70710678f, 1e-4f); /* FR */
923 }
924  
925 /* 2. Pure Out-of-Phase Ambience: L = 1.0, R = -1.0
926 * Center C must be 0.0 (cancels completely)
927 * Surrounds BL and BR receive maximum energy with opposite phase
928 */
929 for (int i = 0; i < n; ++i) { in_l[i] = 1.0f; in_r[i] = -1.0f; }
930 avx_audio_upmix_stereo_to_5_1(in_l, in_r, out, n, sample_rate, &cfg);
931  
932 for (int i = 0; i < n; ++i) {
933 ASSERT_NEAR(out[2][i], 0.0f, 1e-5f); /* Center = 0 */
934 ASSERT_NEAR(out[4][i], 0.70710678f, 1e-4f); /* BL */
935 ASSERT_NEAR(out[5][i], -0.70710678f, 1e-4f); /* BR */
936 ASSERT_NEAR(out[0][i], 1.0f, 1e-4f); /* FL */
937 ASSERT_NEAR(out[1][i], -1.0f, 1e-4f); /* FR */
938 }
939  
940 /* 3. Streaming Stateful Upmixer & Ambient Delay Ring Buffer
941 * Test 15ms delay at 48kHz (720 samples).
942 * Feed an impulse at sample 0 of chunk 1, verify it emerges in chunk 2 at sample 720.
943 */
944 {
945 avx_audio_upmix_cfg_default(&cfg);
946 cfg.ambient_delay_ms = 15; /* 15 ms * 48 = 720 samples */
947 avx_audio_upmixer_t* u = avx_audio_upmixer_create(sample_rate, &cfg);
948 ASSERT_TRUE(u != NULL);
949  
950 int chunk_size = 480; /* 10 ms chunks */
951 float* chk_in_l = (float*)calloc(chunk_size, sizeof(float));
952 float* chk_in_r = (float*)calloc(chunk_size, sizeof(float));
953 float* chk_out[6];
954 for (int c = 0; c < 6; ++c) chk_out[c] = (float*)calloc(chunk_size, sizeof(float));
955  
956 /* Put out-of-phase impulse at index 0 of chunk 0: L=1.0, R=-1.0 */
957 chk_in_l[0] = 1.0f;
958 chk_in_r[0] = -1.0f;
959  
960 /* Chunk 0 (samples 0..479): impulse is at t=0, so delayed surround at t=0 should be 0 */
961 avx_audio_upmixer_process(u, chk_in_l, chk_in_r, chk_out, chunk_size);
962 ASSERT_NEAR(chk_out[4][0], 0.0f, 1e-5f);
963  
964 /* Chunk 1 (samples 480..959): sample 720 corresponds to local index (720 - 480) = 240 */
965 memset(chk_in_l, 0, chunk_size * sizeof(float));
966 memset(chk_in_r, 0, chunk_size * sizeof(float));
967 avx_audio_upmixer_process(u, chk_in_l, chk_in_r, chk_out, chunk_size);
968  
969 /* At index 240, delayed impulse must emerge */
970 ASSERT_NEAR(chk_out[4][240], 0.70710678f, 1e-4f);
971 ASSERT_NEAR(chk_out[5][240], -0.70710678f, 1e-4f);
972 /* And neighbouring samples must be 0 */
973 ASSERT_NEAR(chk_out[4][239], 0.0f, 1e-5f);
974 ASSERT_NEAR(chk_out[4][241], 0.0f, 1e-5f);
975  
976 avx_audio_upmixer_destroy(u);
977 for (int c = 0; c < 6; ++c) free(chk_out[c]);
978 free(chk_in_l); free(chk_in_r);
979 }
980  
981 /* 4. LFE Butterworth Low-Pass Filter Frequency Response:
982 * - 50 Hz sine wave (well below 120 Hz cutoff) -> should pass with high amplitude (~0.5f * lfe_level)
983 * - 5000 Hz sine wave (well above 120 Hz cutoff) -> should be heavily attenuated (< 0.01)
984 */
985 {
986 int num_filt_samples = 4800; /* 100 ms */
987 float* low_l = (float*)malloc(num_filt_samples * sizeof(float));
988 float* low_r = (float*)malloc(num_filt_samples * sizeof(float));
989 float* filt_out[6];
990 for (int c = 0; c < 6; ++c) filt_out[c] = (float*)malloc(num_filt_samples * sizeof(float));
991  
992 /* 50 Hz sine wave */
993 for (int i = 0; i < num_filt_samples; ++i) {
994 float s = sinf(2.0f * (float)M_PI * 50.0f * (float)i / 48000.0f);
995 low_l[i] = s;
996 low_r[i] = s;
997 }
998  
999 avx_audio_upmix_cfg_default(&cfg);
1000 cfg.ambient_delay_ms = 0;
1001 avx_audio_upmix_stereo_to_5_1(low_l, low_r, filt_out, num_filt_samples, 48000, &cfg);
1002  
1003 /* Measure steady state peak of LFE (after filter settling at 2400 samples) */
1004 float peak_50hz = 0.0f;
1005 for (int i = 2400; i < num_filt_samples; ++i) {
1006 if (fabsf(filt_out[3][i]) > peak_50hz) peak_50hz = fabsf(filt_out[3][i]);
1007 }
1008 /* With cfg.lfe_level = 0.5f, 50 Hz should be passed near 0.5f (at least 0.45f) */
1009 ASSERT_TRUE(peak_50hz > 0.45f);
1010  
1011 /* 5000 Hz sine wave */
1012 for (int i = 0; i < num_filt_samples; ++i) {
1013 float s = sinf(2.0f * (float)M_PI * 5000.0f * (float)i / 48000.0f);
1014 low_l[i] = s;
1015 low_r[i] = s;
1016 }
1017 avx_audio_upmix_stereo_to_5_1(low_l, low_r, filt_out, num_filt_samples, 48000, &cfg);
1018  
1019 float peak_5khz = 0.0f;
1020 for (int i = 2400; i < num_filt_samples; ++i) {
1021 if (fabsf(filt_out[3][i]) > peak_5khz) peak_5khz = fabsf(filt_out[3][i]);
1022 }
1023 /* 5000 Hz is over 5 octaves above 120 Hz -> > 40 dB attenuation */
1024 ASSERT_TRUE(peak_5khz < 0.01f);
1025  
1026 for (int c = 0; c < 6; ++c) free(filt_out[c]);
1027 free(low_l); free(low_r);
1028 }
1029  
1030 for (int c = 0; c < 6; ++c) free(out[c]);
1031 free(in_l); free(in_r);
1032  
1033 printf(" [+] Stereo -> 5.1 Surround Upmixer: PASSED\n");
1034 }
1035  
1036 int main(void) {
1037 printf("=== OpenAVX Audio Engine Tests ===\n");
1038 printf("SIMD: %s\n\n", avx_simd_get_name());
1039  
1040 test_downmix_5_1();
1041 test_downmix_7_1();
1042 test_quad_and_mono();
1043 test_matrix_mix();
1044 test_frame_downmix();
1045 test_resampler_exact_count();
1046 test_resampler_snr();
1047 test_frame_resample();
1048 test_audio_jitter_buffer();
1049 test_channel_layouts_and_masks();
1050 test_audio_route_channels();
1051 test_downmix_5_1_ltrt();
1052 test_upmix_stereo_to_5_1();
1053  
1054 printf("\n[PASS] openavx_audio_test\n");
1055 return 0;
1056 }