1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "gtest/gtest.h"
#include "DynamicResampler.h"
using namespace mozilla;
TEST(TestDynamicResampler, SameRates_Float1)
{
const uint32_t in_frames = 100;
const uint32_t out_frames = 100;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
// float in_ch1[] = {.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0};
// float in_ch2[] = {.1, .2, .3, .4, .5, .6, .7, .8, .9, 1.0};
float in_ch1[in_frames] = {};
float in_ch2[in_frames] = {};
AutoTArray<const float*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
float out_ch1[out_frames] = {};
float out_ch2[out_frames] = {};
// Warm up with zeros
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_FLOAT_EQ(in_ch1[i], out_ch1[i]);
EXPECT_FLOAT_EQ(in_ch2[i], out_ch2[i]);
}
// Continue with non zero
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch1[i] = in_ch2[i] = 0.01f * i;
}
dr.AppendInput(in_buffer, in_frames);
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_FLOAT_EQ(in_ch1[i], out_ch1[i]);
EXPECT_FLOAT_EQ(in_ch2[i], out_ch2[i]);
}
// No more frames in the input buffer
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
}
TEST(TestDynamicResampler, SameRates_Short1)
{
uint32_t in_frames = 2;
uint32_t out_frames = 2;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
short in_ch1[] = {1, 2, 3};
short in_ch2[] = {4, 5, 6};
AutoTArray<const short*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
short out_ch1[3] = {};
short out_ch2[3] = {};
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_EQ(in_ch1[i], out_ch1[i]);
EXPECT_EQ(in_ch2[i], out_ch2[i]);
}
// No more frames in the input buffer
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
}
TEST(TestDynamicResampler, SameRates_Float2)
{
uint32_t in_frames = 3;
uint32_t out_frames = 2;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
float in_ch1[] = {0.1, 0.2, 0.3};
float in_ch2[] = {0.4, 0.5, 0.6};
AutoTArray<const float*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
float out_ch1[3] = {};
float out_ch2[3] = {};
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_FLOAT_EQ(in_ch1[i], out_ch1[i]);
EXPECT_FLOAT_EQ(in_ch2[i], out_ch2[i]);
}
out_frames = 1;
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_FLOAT_EQ(in_ch1[i + 2], out_ch1[i]);
EXPECT_FLOAT_EQ(in_ch2[i + 2], out_ch2[i]);
}
// No more frames, the input buffer has drained
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
}
TEST(TestDynamicResampler, SameRates_Short2)
{
uint32_t in_frames = 3;
uint32_t out_frames = 2;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
short in_ch1[] = {1, 2, 3};
short in_ch2[] = {4, 5, 6};
AutoTArray<const short*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
short out_ch1[3] = {};
short out_ch2[3] = {};
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_EQ(in_ch1[i], out_ch1[i]);
EXPECT_EQ(in_ch2[i], out_ch2[i]);
}
out_frames = 1;
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_EQ(in_ch1[i + 2], out_ch1[i]);
EXPECT_EQ(in_ch2[i + 2], out_ch2[i]);
}
// No more frames, the input buffer has drained
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
}
TEST(TestDynamicResampler, SameRates_Float3)
{
uint32_t in_frames = 2;
uint32_t out_frames = 3;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
float in_ch1[] = {0.1, 0.2, 0.3};
float in_ch2[] = {0.4, 0.5, 0.6};
AutoTArray<const float*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
float out_ch1[3] = {};
float out_ch2[3] = {};
// Not enough frames in the input buffer
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
// Add one frame
in_buffer[0] = in_ch1 + 2;
in_buffer[1] = in_ch2 + 2;
dr.AppendInput(in_buffer, 1);
out_frames = 1;
hasUnderrun = dr.Resample(out_ch1 + 2, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2 + 2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < 3; ++i) {
EXPECT_FLOAT_EQ(in_ch1[i], out_ch1[i]);
EXPECT_FLOAT_EQ(in_ch2[i], out_ch2[i]);
}
}
TEST(TestDynamicResampler, SameRates_Short3)
{
uint32_t in_frames = 2;
uint32_t out_frames = 3;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
short in_ch1[] = {1, 2, 3};
short in_ch2[] = {4, 5, 6};
AutoTArray<const short*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
short out_ch1[3] = {};
short out_ch2[3] = {};
// Not enough frames in the input buffer
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
// Add one frame
in_buffer[0] = in_ch1 + 2;
in_buffer[1] = in_ch2 + 2;
dr.AppendInput(in_buffer, 1);
out_frames = 1;
hasUnderrun = dr.Resample(out_ch1 + 2, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2 + 2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < 3; ++i) {
EXPECT_EQ(in_ch1[i], out_ch1[i]);
EXPECT_EQ(in_ch2[i], out_ch2[i]);
}
}
TEST(TestDynamicResampler, UpdateOutRate_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 20;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
float in_ch1[10] = {};
float in_ch2[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch1[i] = in_ch2[i] = 0.01f * i;
}
AutoTArray<const float*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
float out_ch1[40] = {};
float out_ch2[40] = {};
dr.AppendInputSilence(pre_buffer - in_frames);
dr.AppendInput(in_buffer, in_frames);
out_frames = 20u;
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
// Half the input pre-buffer (10) is silence, and half the output (20).
EXPECT_FLOAT_EQ(out_ch1[i], 0.0);
EXPECT_FLOAT_EQ(out_ch2[i], 0.0);
}
// Update in rate
in_rate = 26122;
dr.UpdateResampler(in_rate, channels);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
out_frames = in_frames * out_rate / in_rate;
EXPECT_EQ(out_frames, 18u);
// Even if we provide no input if we have enough buffered input, we can create
// output
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
}
TEST(TestDynamicResampler, UpdateOutRate_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 40;
uint32_t channels = 2;
uint32_t in_rate = 24000;
uint32_t out_rate = 48000;
uint32_t pre_buffer = 20;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
short in_ch1[10] = {};
short in_ch2[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch1[i] = in_ch2[i] = i;
}
AutoTArray<const short*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
short out_ch1[40] = {};
short out_ch2[40] = {};
dr.AppendInputSilence(pre_buffer - in_frames);
dr.AppendInput(in_buffer, in_frames);
out_frames = 20u;
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
// Half the input pre-buffer (10) is silence, and half the output (20).
EXPECT_EQ(out_ch1[i], 0.0);
EXPECT_EQ(out_ch2[i], 0.0);
}
// Update in rate
in_rate = 26122;
dr.UpdateResampler(in_rate, channels);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
out_frames = in_frames * out_rate / in_rate;
EXPECT_EQ(out_frames, 18u);
// Even if we provide no input if we have enough buffered input, we can create
// output
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
}
TEST(TestDynamicResampler, BigRangeInRates_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
const uint32_t in_capacity = 40;
float in_ch1[in_capacity] = {};
float in_ch2[in_capacity] = {};
for (uint32_t i = 0; i < in_capacity; ++i) {
in_ch1[i] = in_ch2[i] = 0.01f * i;
}
AutoTArray<const float*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
const uint32_t out_capacity = 1000;
float out_ch1[out_capacity] = {};
float out_ch2[out_capacity] = {};
// Downsampling at a high enough ratio happens to have enough excess
// in_frames from rounding in the out_frames calculation to cover the
// skipped input latency when switching from zero-latency 44100->44100 to a
// non-1:1 ratio.
for (uint32_t rate = 100000; rate >= 10000; rate -= 2) {
in_rate = rate;
dr.UpdateResampler(in_rate, channels);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
in_frames = 20; // more than we need
out_frames = in_frames * out_rate / in_rate;
for (uint32_t y = 0; y < 2; ++y) {
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
}
}
}
TEST(TestDynamicResampler, BigRangeInRates_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 44100;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
const uint32_t in_capacity = 40;
short in_ch1[in_capacity] = {};
short in_ch2[in_capacity] = {};
for (uint32_t i = 0; i < in_capacity; ++i) {
in_ch1[i] = in_ch2[i] = i;
}
AutoTArray<const short*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
const uint32_t out_capacity = 1000;
short out_ch1[out_capacity] = {};
short out_ch2[out_capacity] = {};
for (uint32_t rate = 100000; rate >= 10000; rate -= 2) {
in_rate = rate;
dr.UpdateResampler(in_rate, channels);
in_frames = 20; // more than we need
out_frames = in_frames * out_rate / in_rate;
for (uint32_t y = 0; y < 2; ++y) {
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
}
}
}
TEST(TestDynamicResampler, UpdateChannels_Float)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 48000;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
float in_ch1[10] = {};
float in_ch2[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch1[i] = in_ch2[i] = 0.01f * i;
}
AutoTArray<const float*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
float out_ch1[10] = {};
float out_ch2[10] = {};
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
// Add 3rd channel
dr.UpdateResampler(in_rate, 3);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), 3u);
float in_ch3[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch3[i] = 0.01f * i;
}
in_buffer.AppendElement();
in_buffer[2] = in_ch3;
float out_ch3[10] = {};
dr.AppendInput(in_buffer, in_frames);
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch3, out_frames, 2);
EXPECT_FALSE(hasUnderrun);
float in_ch4[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch3[i] = 0.01f * i;
}
in_buffer.AppendElement();
in_buffer[3] = in_ch4;
float out_ch4[10] = {};
dr.UpdateResampler(in_rate, 4);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), 4u);
dr.AppendInput(in_buffer, in_frames);
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch3, out_frames, 2);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch4, out_frames, 3);
EXPECT_FALSE(hasUnderrun);
}
TEST(TestDynamicResampler, UpdateChannels_Short)
{
uint32_t in_frames = 10;
uint32_t out_frames = 10;
uint32_t channels = 2;
uint32_t in_rate = 44100;
uint32_t out_rate = 48000;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_S16);
short in_ch1[10] = {};
short in_ch2[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch1[i] = in_ch2[i] = i;
}
AutoTArray<const short*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
short out_ch1[10] = {};
short out_ch2[10] = {};
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
// Add 3rd channel
dr.UpdateResampler(in_rate, 3);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), 3u);
short in_ch3[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch3[i] = i;
}
in_buffer.AppendElement();
in_buffer[2] = in_ch3;
short out_ch3[10] = {};
dr.AppendInput(in_buffer, in_frames);
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch3, out_frames, 2);
EXPECT_FALSE(hasUnderrun);
// Check update with AudioSegment
short in_ch4[10] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch3[i] = i;
}
in_buffer.AppendElement();
in_buffer[3] = in_ch4;
short out_ch4[10] = {};
dr.UpdateResampler(in_rate, 4);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), 4u);
dr.AppendInput(in_buffer, in_frames);
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch3, out_frames, 2);
EXPECT_FALSE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch4, out_frames, 3);
EXPECT_FALSE(hasUnderrun);
}
TEST(TestDynamicResampler, Underrun)
{
const uint32_t in_frames = 100;
const uint32_t out_frames = 200;
uint32_t channels = 2;
uint32_t in_rate = 48000;
uint32_t out_rate = 48000;
DynamicResampler dr(in_rate, out_rate);
dr.SetSampleFormat(AUDIO_FORMAT_FLOAT32);
EXPECT_EQ(dr.GetInRate(), in_rate);
EXPECT_EQ(dr.GetChannels(), channels);
float in_ch1[in_frames] = {};
float in_ch2[in_frames] = {};
AutoTArray<const float*, 2> in_buffer;
in_buffer.AppendElements(channels);
in_buffer[0] = in_ch1;
in_buffer[1] = in_ch2;
float out_ch1[out_frames] = {};
float out_ch2[out_frames] = {};
for (uint32_t i = 0; i < in_frames; ++i) {
in_ch1[i] = 0.01f * i;
in_ch2[i] = -0.01f * i;
}
dr.AppendInput(in_buffer, in_frames);
bool hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
for (uint32_t i = 0; i < in_frames; ++i) {
EXPECT_EQ(out_ch1[i], in_ch1[i]);
EXPECT_EQ(out_ch2[i], in_ch2[i]);
}
for (uint32_t i = in_frames; i < out_frames; ++i) {
EXPECT_EQ(out_ch1[i], 0.0f) << "for i=" << i;
EXPECT_EQ(out_ch2[i], 0.0f) << "for i=" << i;
}
// No more frames in the input buffer
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_EQ(out_ch1[i], 0.0f) << "for i=" << i;
EXPECT_EQ(out_ch2[i], 0.0f) << "for i=" << i;
}
// Now try with resampling.
dr.UpdateResampler(in_rate * 2, channels);
dr.AppendInput(in_buffer, in_frames);
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
// There is some buffering in the resampler, which is why the below is not
// exact.
for (uint32_t i = 0; i < 50; ++i) {
EXPECT_GT(out_ch1[i], 0.0f) << "for i=" << i;
EXPECT_LT(out_ch2[i], 0.0f) << "for i=" << i;
}
for (uint32_t i = 50; i < 54; ++i) {
EXPECT_NE(out_ch1[i], 0.0f) << "for i=" << i;
EXPECT_NE(out_ch2[i], 0.0f) << "for i=" << i;
}
for (uint32_t i = 54; i < out_frames; ++i) {
EXPECT_EQ(out_ch1[i], 0.0f) << "for i=" << i;
EXPECT_EQ(out_ch2[i], 0.0f) << "for i=" << i;
}
// No more frames in the input buffer
hasUnderrun = dr.Resample(out_ch1, out_frames, 0);
EXPECT_TRUE(hasUnderrun);
hasUnderrun = dr.Resample(out_ch2, out_frames, 1);
EXPECT_TRUE(hasUnderrun);
for (uint32_t i = 0; i < out_frames; ++i) {
EXPECT_EQ(out_ch1[i], 0.0f) << "for i=" << i;
EXPECT_EQ(out_ch2[i], 0.0f) << "for i=" << i;
}
}
|