-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathtest_audio_processing_module.cpp
More file actions
927 lines (733 loc) · 31.5 KB
/
test_audio_processing_module.cpp
File metadata and controls
927 lines (733 loc) · 31.5 KB
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
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
/*
* Copyright 2025 LiveKit
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <livekit/audio_frame.h>
#include <livekit/audio_processing_module.h>
#include <livekit/livekit.h>
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
namespace livekit {
namespace test {
class AudioProcessingModuleTest : public ::testing::Test {
protected:
void SetUp() override {
livekit::initialize(livekit::LogLevel::Info, livekit::LogSink::kConsole);
}
void TearDown() override { livekit::shutdown(); }
// Helper to create a 10ms audio frame at given sample rate
static AudioFrame create10msFrame(int sample_rate, int num_channels) {
int samples_per_channel = sample_rate / 100; // 10ms
return AudioFrame::create(sample_rate, num_channels, samples_per_channel);
}
// Helper to fill frame with sine wave
static void fillWithSineWave(AudioFrame &frame, double frequency,
double amplitude = 10000.0) {
auto &data = frame.data();
int sample_rate = frame.sample_rate();
int num_channels = frame.num_channels();
int samples_per_channel = frame.samples_per_channel();
for (int i = 0; i < samples_per_channel; ++i) {
double t = static_cast<double>(i) / sample_rate;
auto sample = static_cast<std::int16_t>(
amplitude * std::sin(2.0 * M_PI * frequency * t));
for (int ch = 0; ch < num_channels; ++ch) {
data[i * num_channels + ch] = sample;
}
}
}
// Helper to fill frame with random noise
static void fillWithNoise(AudioFrame &frame, double amplitude = 5000.0,
unsigned int seed = 0) {
std::mt19937 gen(seed == 0 ? std::random_device{}() : seed);
std::uniform_real_distribution<> dis(-amplitude, amplitude);
auto &data = frame.data();
for (auto &sample : data) {
sample = static_cast<std::int16_t>(dis(gen));
}
}
// Calculate RMS (Root Mean Square) energy of audio frame
static double calculateRMS(const AudioFrame &frame) {
const auto &data = frame.data();
if (data.empty()) {
return 0.0;
}
double sum_squares = 0.0;
for (const auto &sample : data) {
sum_squares += static_cast<double>(sample) * static_cast<double>(sample);
}
return std::sqrt(sum_squares / static_cast<double>(data.size()));
}
// Calculate energy in a specific frequency band using a simple DFT approach
// This is a simplified calculation for testing purposes
static double calculateFrequencyBandEnergy(const AudioFrame &frame,
double low_freq,
double high_freq) {
const auto &data = frame.data();
int sample_rate = frame.sample_rate();
int num_channels = frame.num_channels();
int samples_per_channel = frame.samples_per_channel();
if (data.empty() || samples_per_channel == 0) {
return 0.0;
}
// Use first channel only for frequency analysis
std::vector<double> mono_data(samples_per_channel);
for (int i = 0; i < samples_per_channel; ++i) {
mono_data[i] = static_cast<double>(data[i * num_channels]);
}
// Simple DFT for the frequency range of interest
double energy = 0.0;
int low_bin =
static_cast<int>(low_freq * samples_per_channel / sample_rate);
int high_bin =
static_cast<int>(high_freq * samples_per_channel / sample_rate);
for (int k = std::max(1, low_bin);
k <= std::min(high_bin, samples_per_channel / 2); ++k) {
double real = 0.0;
double imag = 0.0;
double freq_rad = 2.0 * M_PI * k / samples_per_channel;
for (int n = 0; n < samples_per_channel; ++n) {
real += mono_data[n] * std::cos(freq_rad * n);
imag -= mono_data[n] * std::sin(freq_rad * n);
}
energy += real * real + imag * imag;
}
return std::sqrt(energy / samples_per_channel);
}
// Copy audio frame data
static std::vector<std::int16_t> copyFrameData(const AudioFrame &frame) {
return frame.data();
}
// Read a WAV file and return the raw PCM samples
// Assumes 16-bit PCM format
static bool readWavFile(const std::string &path,
std::vector<std::int16_t> &samples, int &sample_rate,
int &num_channels) {
std::ifstream file(path, std::ios::binary);
if (!file.is_open()) {
std::cerr << "Failed to open WAV file: " << path << std::endl;
return false;
}
// Read RIFF header
char riff[4];
file.read(riff, 4);
if (std::string(riff, 4) != "RIFF") {
std::cerr << "Not a valid RIFF file" << std::endl;
return false;
}
file.seekg(4, std::ios::cur); // Skip file size
char wave[4];
file.read(wave, 4);
if (std::string(wave, 4) != "WAVE") {
std::cerr << "Not a valid WAVE file" << std::endl;
return false;
}
// Find fmt chunk
while (file.good()) {
char chunk_id[4];
file.read(chunk_id, 4);
std::uint32_t chunk_size;
file.read(reinterpret_cast<char *>(&chunk_size), 4);
if (std::string(chunk_id, 4) == "fmt ") {
std::uint16_t audio_format;
file.read(reinterpret_cast<char *>(&audio_format), 2);
if (audio_format != 1) { // PCM
std::cerr << "Only PCM format supported" << std::endl;
return false;
}
std::uint16_t channels;
file.read(reinterpret_cast<char *>(&channels), 2);
num_channels = channels;
std::uint32_t rate;
file.read(reinterpret_cast<char *>(&rate), 4);
sample_rate = static_cast<int>(rate);
file.seekg(chunk_size - 8, std::ios::cur); // Skip rest of fmt chunk
} else if (std::string(chunk_id, 4) == "data") {
// Read audio data
size_t num_samples = chunk_size / sizeof(std::int16_t);
samples.resize(num_samples);
file.read(reinterpret_cast<char *>(samples.data()), chunk_size);
return true;
} else {
// Skip unknown chunk
file.seekg(chunk_size, std::ios::cur);
}
}
std::cerr << "No data chunk found" << std::endl;
return false;
}
// Scale audio samples by a factor (for simulating quiet/loud audio)
static void scaleAudio(std::vector<std::int16_t> &samples, double scale) {
for (auto &sample : samples) {
double scaled = static_cast<double>(sample) * scale;
sample = static_cast<std::int16_t>(std::clamp(scaled, -32768.0, 32767.0));
}
}
};
// ============================================================================
// Construction Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, CreateWithDefaultOptions) {
AudioProcessingModule::Options opts;
// Constructor throws on failure, so reaching this point means success
EXPECT_NO_THROW(AudioProcessingModule apm(opts));
}
TEST_F(AudioProcessingModuleTest, CreateWithAllFeaturesEnabled) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
opts.noise_suppression = true;
opts.high_pass_filter = true;
opts.auto_gain_control = true;
// Constructor throws on failure, so reaching this point means success
EXPECT_NO_THROW(AudioProcessingModule apm(opts));
}
TEST_F(AudioProcessingModuleTest, CreateWithEchoCancellationOnly) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
// Constructor throws on failure, so reaching this point means success
EXPECT_NO_THROW(AudioProcessingModule apm(opts));
}
TEST_F(AudioProcessingModuleTest, CreateWithNoiseSuppressionOnly) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
// Constructor throws on failure, so reaching this point means success
EXPECT_NO_THROW(AudioProcessingModule apm(opts));
}
TEST_F(AudioProcessingModuleTest, CreateWithAutoGainControlOnly) {
AudioProcessingModule::Options opts;
opts.auto_gain_control = true;
// Constructor throws on failure, so reaching this point means success
EXPECT_NO_THROW(AudioProcessingModule apm(opts));
}
TEST_F(AudioProcessingModuleTest, CreateWithHighPassFilterOnly) {
AudioProcessingModule::Options opts;
opts.high_pass_filter = true;
// Constructor throws on failure, so reaching this point means success
EXPECT_NO_THROW(AudioProcessingModule apm(opts));
}
// ============================================================================
// ProcessStream Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, ProcessStreamMono48kHz) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, 440.0); // 440 Hz tone
EXPECT_NO_THROW(apm.processStream(frame));
}
TEST_F(AudioProcessingModuleTest, ProcessStreamStereo48kHz) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
AudioFrame frame = create10msFrame(48000, 2);
fillWithSineWave(frame, 440.0);
EXPECT_NO_THROW(apm.processStream(frame));
}
TEST_F(AudioProcessingModuleTest, ProcessStreamMono16kHz) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
AudioFrame frame = create10msFrame(16000, 1);
fillWithSineWave(frame, 440.0);
EXPECT_NO_THROW(apm.processStream(frame));
}
TEST_F(AudioProcessingModuleTest, ProcessStreamEmptyFrame) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
AudioFrame frame; // Default empty frame
// Should not throw, just return early
EXPECT_NO_THROW(apm.processStream(frame));
}
TEST_F(AudioProcessingModuleTest, ProcessStreamWithNoisyInput) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
AudioFrame frame = create10msFrame(48000, 1);
fillWithNoise(frame);
EXPECT_NO_THROW(apm.processStream(frame));
}
// ============================================================================
// ProcessReverseStream Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, ProcessReverseStreamMono48kHz) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
AudioProcessingModule apm(opts);
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, 440.0);
EXPECT_NO_THROW(apm.processReverseStream(frame));
}
TEST_F(AudioProcessingModuleTest, ProcessReverseStreamStereo48kHz) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
AudioProcessingModule apm(opts);
AudioFrame frame = create10msFrame(48000, 2);
fillWithSineWave(frame, 440.0);
EXPECT_NO_THROW(apm.processReverseStream(frame));
}
TEST_F(AudioProcessingModuleTest, ProcessReverseStreamEmptyFrame) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
AudioProcessingModule apm(opts);
AudioFrame frame; // Default empty frame
EXPECT_NO_THROW(apm.processReverseStream(frame));
}
// ============================================================================
// SetStreamDelay Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, SetStreamDelayMs) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
AudioProcessingModule apm(opts);
EXPECT_NO_THROW(apm.setStreamDelayMs(0));
EXPECT_NO_THROW(apm.setStreamDelayMs(50));
EXPECT_NO_THROW(apm.setStreamDelayMs(100));
EXPECT_NO_THROW(apm.setStreamDelayMs(200));
}
// ============================================================================
// Echo Cancellation Workflow Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, EchoCancellationWorkflow) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
// Simulate typical AEC workflow:
// 1. Process speaker audio (reverse stream)
// 2. Process microphone audio (forward stream)
AudioFrame speakerFrame = create10msFrame(48000, 1);
fillWithSineWave(speakerFrame, 440.0);
AudioFrame micFrame = create10msFrame(48000, 1);
fillWithSineWave(micFrame, 440.0); // Simulated echo
fillWithNoise(micFrame, 1000.0); // Plus some noise
// Set estimated delay
EXPECT_NO_THROW(apm.setStreamDelayMs(50));
// Process reverse stream (speaker output)
EXPECT_NO_THROW(apm.processReverseStream(speakerFrame));
// Process forward stream (microphone input)
EXPECT_NO_THROW(apm.processStream(micFrame));
}
TEST_F(AudioProcessingModuleTest, MultipleFramesProcessing) {
AudioProcessingModule::Options opts;
opts.echo_cancellation = true;
opts.noise_suppression = true;
opts.auto_gain_control = true;
AudioProcessingModule apm(opts);
// Process multiple frames (simulating real-time audio)
for (int i = 0; i < 100; ++i) {
AudioFrame speakerFrame = create10msFrame(48000, 1);
fillWithSineWave(speakerFrame, 440.0);
AudioFrame micFrame = create10msFrame(48000, 1);
fillWithNoise(micFrame);
EXPECT_NO_THROW(apm.processReverseStream(speakerFrame));
EXPECT_NO_THROW(apm.processStream(micFrame));
}
}
// ============================================================================
// Move Semantics Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, MoveConstruction) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm1(opts);
// Move construct - should not throw
AudioProcessingModule apm2(std::move(apm1));
// The moved-to APM should be usable
AudioFrame frame = create10msFrame(48000, 1);
EXPECT_NO_THROW(apm2.processStream(frame));
}
TEST_F(AudioProcessingModuleTest, MoveAssignment) {
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm1(opts);
AudioProcessingModule apm2(opts);
// Move assign - should not throw
apm2 = std::move(apm1);
// The moved-to APM should be usable
AudioFrame frame = create10msFrame(48000, 1);
EXPECT_NO_THROW(apm2.processStream(frame));
}
// ============================================================================
// Noise Suppression Effectiveness Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, NoiseSuppressionReducesNoiseEnergy) {
// Create APM with noise suppression enabled
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
// Process multiple frames to let the noise suppressor adapt
// The noise suppressor needs several frames to estimate the noise profile
constexpr int kWarmupFrames = 50;
constexpr int kTestFrames = 50;
constexpr unsigned int kSeed = 12345; // Fixed seed for reproducibility
double total_input_energy = 0.0;
double total_output_energy = 0.0;
// Warmup phase - let the noise suppressor learn the noise characteristics
for (int i = 0; i < kWarmupFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithNoise(frame, 8000.0, kSeed + i);
apm.processStream(frame);
}
// Measurement phase - measure energy reduction
for (int i = 0; i < kTestFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithNoise(frame, 8000.0, kSeed + kWarmupFrames + i);
double input_energy = calculateRMS(frame);
total_input_energy += input_energy;
apm.processStream(frame);
double output_energy = calculateRMS(frame);
total_output_energy += output_energy;
}
double avg_input_energy = total_input_energy / kTestFrames;
double avg_output_energy = total_output_energy / kTestFrames;
// Noise suppression should reduce energy significantly
// We expect at least 50% reduction for pure noise input
std::cout << "[NoiseSuppression] Avg input energy: " << avg_input_energy
<< ", Avg output energy: " << avg_output_energy << ", Reduction: "
<< (1.0 - avg_output_energy / avg_input_energy) * 100.0 << "%"
<< std::endl;
EXPECT_LT(avg_output_energy, avg_input_energy)
<< "Noise suppression should reduce energy";
}
TEST_F(AudioProcessingModuleTest, NoiseSuppressionPreservesSpeechLikeSignal) {
// Create APM with noise suppression enabled
AudioProcessingModule::Options opts;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
// Create a more speech-like signal with multiple harmonics and varying
// amplitude Pure sine waves may be classified as tonal noise by the NS
// algorithm
constexpr int kFrames = 100;
double total_input_energy = 0.0;
double total_output_energy = 0.0;
for (int i = 0; i < kFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
auto &data = frame.data();
int sample_rate = frame.sample_rate();
int samples_per_channel = frame.samples_per_channel();
// Create speech-like signal with fundamental + harmonics (like voice)
// Vary amplitude slightly to simulate natural speech variation
double amplitude_variation =
8000.0 + 2000.0 * std::sin(2.0 * M_PI * i / 20.0);
for (int s = 0; s < samples_per_channel; ++s) {
double t = static_cast<double>(s) / sample_rate;
// Fundamental (250 Hz) + harmonics (typical of voiced speech)
double sample = amplitude_variation *
(0.5 * std::sin(2.0 * M_PI * 250.0 * t) + // Fundamental
0.3 * std::sin(2.0 * M_PI * 500.0 * t) + // 2nd harmonic
0.15 * std::sin(2.0 * M_PI * 750.0 * t) + // 3rd harmonic
0.05 * std::sin(2.0 * M_PI * 1000.0 * t) // 4th harmonic
);
data[s] =
static_cast<std::int16_t>(std::clamp(sample, -32768.0, 32767.0));
}
double input_energy = calculateRMS(frame);
total_input_energy += input_energy;
apm.processStream(frame);
double output_energy = calculateRMS(frame);
total_output_energy += output_energy;
}
double avg_input_energy = total_input_energy / kFrames;
double avg_output_energy = total_output_energy / kFrames;
double preservation_ratio = avg_output_energy / avg_input_energy;
std::cout << "[NoiseSuppression-Speech] Avg input energy: "
<< avg_input_energy << ", Avg output energy: " << avg_output_energy
<< ", Preservation: " << preservation_ratio * 100.0 << "%"
<< std::endl;
// Note: Even speech-like signals may be partially attenuated by NS
// We just verify that the output has some significant energy
// (i.e., NS doesn't completely silence the signal)
EXPECT_GT(avg_output_energy, avg_input_energy * 0.1)
<< "Speech-like signals should not be completely suppressed";
}
// ============================================================================
// High Pass Filter Effectiveness Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, HighPassFilterAttenuatesLowFrequencies) {
// Create APM with high pass filter enabled
AudioProcessingModule::Options opts;
opts.high_pass_filter = true;
AudioProcessingModule apm(opts);
// Test with a very low frequency signal (below the ~80Hz cutoff)
constexpr double kLowFrequency = 30.0; // 30 Hz - below cutoff
constexpr int kFrames = 100;
double total_input_energy = 0.0;
double total_output_energy = 0.0;
for (int i = 0; i < kFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, kLowFrequency, 10000.0);
double input_energy = calculateRMS(frame);
total_input_energy += input_energy;
apm.processStream(frame);
double output_energy = calculateRMS(frame);
total_output_energy += output_energy;
}
double avg_input_energy = total_input_energy / kFrames;
double avg_output_energy = total_output_energy / kFrames;
std::cout << "[HighPassFilter-LowFreq] Avg input energy: " << avg_input_energy
<< ", Avg output energy: " << avg_output_energy << ", Attenuation: "
<< (1.0 - avg_output_energy / avg_input_energy) * 100.0 << "%"
<< std::endl;
// Low frequencies should be significantly attenuated
EXPECT_LT(avg_output_energy, avg_input_energy * 0.8)
<< "High pass filter should attenuate low frequencies";
}
TEST_F(AudioProcessingModuleTest, HighPassFilterPassesHighFrequencies) {
// Create APM with high pass filter enabled
AudioProcessingModule::Options opts;
opts.high_pass_filter = true;
AudioProcessingModule apm(opts);
// Test with a frequency well above the cutoff
constexpr double kHighFrequency = 1000.0; // 1kHz - well above 80Hz cutoff
constexpr int kFrames = 100;
double total_input_energy = 0.0;
double total_output_energy = 0.0;
for (int i = 0; i < kFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, kHighFrequency, 10000.0);
double input_energy = calculateRMS(frame);
total_input_energy += input_energy;
apm.processStream(frame);
double output_energy = calculateRMS(frame);
total_output_energy += output_energy;
}
double avg_input_energy = total_input_energy / kFrames;
double avg_output_energy = total_output_energy / kFrames;
double pass_ratio = avg_output_energy / avg_input_energy;
std::cout << "[HighPassFilter-HighFreq] Avg input energy: "
<< avg_input_energy << ", Avg output energy: " << avg_output_energy
<< ", Pass ratio: " << pass_ratio * 100.0 << "%" << std::endl;
// High frequencies should pass through with minimal attenuation
// Allow up to 20% loss due to processing artifacts
EXPECT_GT(pass_ratio, 0.8) << "High pass filter should pass high frequencies";
}
TEST_F(AudioProcessingModuleTest, HighPassFilterCompareLowVsHighFrequency) {
// Create two APMs with high pass filter enabled
AudioProcessingModule::Options opts;
opts.high_pass_filter = true;
AudioProcessingModule apm_low(opts);
AudioProcessingModule apm_high(opts);
constexpr double kLowFrequency = 30.0; // Below cutoff
constexpr double kHighFrequency = 500.0; // Above cutoff
constexpr int kFrames = 100;
double low_freq_output_energy = 0.0;
double high_freq_output_energy = 0.0;
// Process low frequency
for (int i = 0; i < kFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, kLowFrequency, 10000.0);
apm_low.processStream(frame);
low_freq_output_energy += calculateRMS(frame);
}
// Process high frequency
for (int i = 0; i < kFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, kHighFrequency, 10000.0);
apm_high.processStream(frame);
high_freq_output_energy += calculateRMS(frame);
}
double avg_low = low_freq_output_energy / kFrames;
double avg_high = high_freq_output_energy / kFrames;
std::cout << "[HighPassFilter-Compare] Low freq (30Hz) output: " << avg_low
<< ", High freq (500Hz) output: " << avg_high << std::endl;
// High frequency output should be significantly greater than low frequency
EXPECT_GT(avg_high, avg_low * 1.5) << "High frequencies should have more "
"energy than low frequencies after HPF";
}
// ============================================================================
// Automatic Gain Control (AGC) Effectiveness Tests
// ============================================================================
TEST_F(AudioProcessingModuleTest, AGCProcessesAudioWithoutError) {
// Create APM with AGC enabled
// Note: WebRTC's AGC behavior varies by configuration. This test verifies
// that AGC processes audio correctly without errors and produces valid
// output.
AudioProcessingModule::Options opts;
opts.auto_gain_control = true;
AudioProcessingModule apm(opts);
constexpr int kFrames = 200; // 2 seconds of audio
double total_input_energy = 0.0;
double total_output_energy = 0.0;
for (int i = 0; i < kFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
auto &data = frame.data();
int sample_rate = frame.sample_rate();
int samples_per_channel = frame.samples_per_channel();
// Speech-like signal with varying amplitude
double amplitude = 2000.0 * (0.5 + 0.5 * std::sin(2.0 * M_PI * i / 50.0));
for (int s = 0; s < samples_per_channel; ++s) {
double t = static_cast<double>(s) / sample_rate;
double sample = amplitude * (0.5 * std::sin(2.0 * M_PI * 250.0 * t) +
0.3 * std::sin(2.0 * M_PI * 500.0 * t));
data[s] =
static_cast<std::int16_t>(std::clamp(sample, -32768.0, 32767.0));
}
total_input_energy += calculateRMS(frame);
apm.processStream(frame);
total_output_energy += calculateRMS(frame);
}
double avg_input = total_input_energy / kFrames;
double avg_output = total_output_energy / kFrames;
std::cout << "[AGC] Processed " << kFrames << " frames. "
<< "Avg input=" << avg_input << ", Avg output=" << avg_output
<< std::endl;
// Verify output is valid (not zero, not clipped)
EXPECT_GT(avg_output, 0.0) << "AGC output should not be zero";
EXPECT_LT(avg_output, 30000.0)
<< "AGC output should not be excessively clipped";
}
TEST_F(AudioProcessingModuleTest, AGCHandlesVaryingInputLevels) {
// Create APM with AGC enabled
// Test that AGC handles transitions between quiet and loud audio
AudioProcessingModule::Options opts;
opts.auto_gain_control = true;
AudioProcessingModule apm(opts);
constexpr int kFramesPerPhase = 100; // 1 second per phase
// Process quiet audio
double quiet_output_sum = 0.0;
for (int i = 0; i < kFramesPerPhase; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, 440.0, 1000.0); // Quiet
apm.processStream(frame);
quiet_output_sum += calculateRMS(frame);
}
// Process loud audio
double loud_output_sum = 0.0;
for (int i = 0; i < kFramesPerPhase; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithSineWave(frame, 440.0, 15000.0); // Loud
apm.processStream(frame);
loud_output_sum += calculateRMS(frame);
}
double quiet_avg = quiet_output_sum / kFramesPerPhase;
double loud_avg = loud_output_sum / kFramesPerPhase;
std::cout << "[AGC-VaryingLevels] Quiet output=" << quiet_avg
<< ", Loud output=" << loud_avg << std::endl;
// Verify outputs are valid and different levels produce different outputs
EXPECT_GT(quiet_avg, 0.0) << "Quiet output should not be zero";
EXPECT_GT(loud_avg, 0.0) << "Loud output should not be zero";
EXPECT_GT(loud_avg, quiet_avg) << "Loud output should be greater than quiet";
}
TEST_F(AudioProcessingModuleTest, AGCAttenuatesLoudSpeech) {
// Test AGC with real speech audio scaled to simulate loud input
// This verifies that AGC attenuates loud speech to prevent clipping
std::vector<std::int16_t> original_samples;
int sample_rate = 0;
int num_channels = 0;
// TODO: figure out what generates this welcome.wav file such that this test isn't skipped
std::string wav_path = std::string(LIVEKIT_ROOT_DIR) + "/data/welcome.wav";
if (!readWavFile(wav_path, original_samples, sample_rate, num_channels)) {
GTEST_SKIP() << "Could not read " << wav_path;
}
std::cout << "[AGC-LoudSpeech] Loaded " << original_samples.size()
<< " samples, " << sample_rate << " Hz, " << num_channels
<< " channels" << std::endl;
// Scale up to simulate loud input (3x original volume)
std::vector<std::int16_t> loud_samples = original_samples;
scaleAudio(loud_samples, 3.0);
double loud_input_rms = 0.0;
for (const auto &s : loud_samples) {
loud_input_rms += static_cast<double>(s) * static_cast<double>(s);
}
loud_input_rms = std::sqrt(loud_input_rms / loud_samples.size());
std::cout << "[AGC-LoudSpeech] Loud input RMS (3x): " << loud_input_rms
<< std::endl;
// Create APM with AGC enabled
AudioProcessingModule::Options opts;
opts.auto_gain_control = true;
AudioProcessingModule apm(opts);
// Process in 10ms chunks
int samples_per_frame = sample_rate / 100;
int total_frames = static_cast<int>(loud_samples.size()) / samples_per_frame;
double total_output_rms = 0.0;
int frame_count = 0;
for (int f = 0; f < total_frames; ++f) {
std::vector<std::int16_t> frame_data(
loud_samples.begin() + f * samples_per_frame,
loud_samples.begin() + (f + 1) * samples_per_frame);
AudioFrame frame(frame_data, sample_rate, num_channels, samples_per_frame);
apm.processStream(frame);
total_output_rms += calculateRMS(frame);
frame_count++;
}
double avg_output_rms = total_output_rms / frame_count;
double gain_applied =
(loud_input_rms > 0) ? (avg_output_rms / loud_input_rms) : 0.0;
std::cout << "[AGC-LoudSpeech] Input RMS=" << loud_input_rms
<< ", Output RMS=" << avg_output_rms
<< ", Effective gain=" << gain_applied << "x" << std::endl;
// Verify AGC attenuated the loud signal (gain < 1.0)
EXPECT_GT(avg_output_rms, 0.0) << "Output should not be zero";
EXPECT_LT(gain_applied, 1.0)
<< "AGC should attenuate loud audio (gain < 1.0)";
std::cout << "[AGC-LoudSpeech] SUCCESS: AGC attenuated loud speech by "
<< (1.0 - gain_applied) * 100.0 << "%" << std::endl;
}
TEST_F(AudioProcessingModuleTest, AGCWithNoiseSuppressionCombined) {
// Test combined AGC + noise suppression
AudioProcessingModule::Options opts;
opts.auto_gain_control = true;
opts.noise_suppression = true;
AudioProcessingModule apm(opts);
constexpr int kFrames = 200;
constexpr unsigned int kSeed = 54321;
// Process noise-only frames first (warmup)
for (int i = 0; i < 50; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
fillWithNoise(frame, 3000.0, kSeed + i);
apm.processStream(frame);
}
// Now process signal + noise
constexpr double kSignalAmplitude = 2000.0;
constexpr double kNoiseAmplitude = 1000.0;
constexpr double kSignalFrequency = 1000.0;
double signal_energy_sum = 0.0;
int signal_frames = 0;
for (int i = 0; i < kFrames; ++i) {
AudioFrame frame = create10msFrame(48000, 1);
// Add signal
fillWithSineWave(frame, kSignalFrequency, kSignalAmplitude);
// Add noise on top
auto &data = frame.data();
std::mt19937 gen(kSeed + 50 + i);
std::uniform_real_distribution<> dis(-kNoiseAmplitude, kNoiseAmplitude);
for (auto &sample : data) {
sample = static_cast<std::int16_t>(std::clamp(
static_cast<double>(sample) + dis(gen), -32768.0, 32767.0));
}
apm.processStream(frame);
if (i >= kFrames / 2) { // Measure second half after adaptation
signal_energy_sum += calculateRMS(frame);
signal_frames++;
}
}
double avg_output_energy = signal_energy_sum / signal_frames;
std::cout << "[AGC+NS Combined] Avg output energy: " << avg_output_energy
<< " (input signal amplitude: " << kSignalAmplitude
<< ", noise amplitude: " << kNoiseAmplitude << ")" << std::endl;
// Should have reasonable output energy (AGC boosted, NS cleaned)
EXPECT_GT(avg_output_energy, 100.0)
<< "Combined AGC+NS should produce reasonable output";
}
} // namespace test
} // namespace livekit