forked from mail2chromium/Custom-AEC-using-WebRTC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_echo_canceller.cc
102 lines (74 loc) · 3.42 KB
/
custom_echo_canceller.cc
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
#include "custom_acoustic_echo_canceller.h"
#include "rtc_base/logging.h"
#include "absl/types/optional.h"
#include "api/make_ref_counted.h"
#include "api/scoped_refptr.h"
#include "rtc_base/checks.h"
#include "modules/audio_processing/aec3/echo_canceller3.h"
#include "api/audio/echo_canceller3_config.h"
#include "api/audio/echo_canceller3_factory.h"
namespace webrtc {
// Anonymous namespace for private implementation details
namespace {
const char kLogTag[] = "CustomAEC";
const int kSampleRateHz = 48000;
const size_t kChannelNum = 1;
std::unique_ptr<EchoCanceller3> aec3;
std::unique_ptr<AudioBuffer> nearend_audio_buffer;
std::unique_ptr<AudioBuffer> farend_audio_buffer;
std::unique_ptr<AudioBuffer> aec_linear_audio;
int16_t farend_processed_data[480]; // Assuming a 10ms frame at 48kHz.
} // namespace
CustomAcousticEchoCanceller::CustomAcousticEchoCanceller() {
// webrtc::EchoCanceller3Config config;
EchoCanceller3Config config;
config.filter.export_linear_aec_output = true;
// webrtc::EchoCanceller3Factory aec_factory(config);
EchoCanceller3Factory aec_factory(config);
aec3 = aec_factory.Create(kSampleRateHz, kChannelNum, kChannelNum);
RTC_LOG(LS_INFO) << kLogTag << ": Custom Acoustic Echo Canceller object initialized";
nearend_audio_buffer = std::make_unique<AudioBuffer>(
kSampleRateHz, kChannelNum,
kSampleRateHz, kChannelNum,
kSampleRateHz, kChannelNum);
aec_linear_audio = std::make_unique<AudioBuffer>(
kSampleRateHz / 3, kChannelNum,
kSampleRateHz / 3, kChannelNum,
kSampleRateHz / 3, kChannelNum);
farend_audio_buffer = std::make_unique<AudioBuffer>(
kSampleRateHz, kChannelNum,
kSampleRateHz, kChannelNum,
kSampleRateHz, kChannelNum);
RTC_LOG(LS_INFO) << kLogTag << ": Audio buffers created for echo cancellation";
}
void CustomAcousticEchoCanceller::ProcessNearEnd(int16_t* near_end_signal) {
RTC_DCHECK(aec3);
RTC_DCHECK(near_end_signal);
RTC_LOG(LS_INFO) << kLogTag << ": Processing near-end signal for echo cancellation";
StreamConfig stream_config(kSampleRateHz, kChannelNum);
nearend_audio_buffer->CopyFrom(near_end_signal, stream_config);
aec3->AnalyzeCapture(nearend_audio_buffer);
nearend_audio_buffer->SplitIntoFrequencyBands();
aec3->SetAudioBufferDelay(0);
aec3->ProcessCapture(nearend_audio_buffer, aec_linear_audio, false);
nearend_audio_buffer->MergeFrequencyBands();
nearend_audio_buffer->CopyTo(stream_config, near_end_signal);
RTC_LOG(LS_INFO) << kLogTag << ": Near-end signal processed";
}
void CustomAcousticEchoCanceller::ProcessFarEnd(int16_t* far_end_signal) {
RTC_DCHECK(aec3);
RTC_DCHECK(far_end_signal);
RTC_LOG(LS_INFO) << kLogTag << ": Processing far-end signal for echo cancellation";
StreamConfig stream_config(kSampleRateHz, kChannelNum);
farend_audio_buffer->CopyFrom(far_end_signal, stream_config);
farend_audio_buffer->SplitIntoFrequencyBands();
aec3->AnalyzeRender(farend_audio_buffer);
farend_audio_buffer->MergeFrequencyBands();
farend_audio_buffer->CopyTo(stream_config, farend_processed_data);
RTC_LOG(LS_INFO) << kLogTag << ": Far-end signal processed";
}
CustomAcousticEchoCanceller::~CustomAcousticEchoCanceller() {
// Resources are automatically freed by the unique_ptr destructors.
RTC_LOG(LS_INFO) << kLogTag << ": Custom Acoustic Echo Canceller object destroyed";
}
} // namespace webrtc