-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathaudio_processing_module.cpp
More file actions
142 lines (114 loc) · 4.59 KB
/
audio_processing_module.cpp
File metadata and controls
142 lines (114 loc) · 4.59 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
/*
* 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 "livekit/audio_processing_module.h"
#include <stdexcept>
#include "audio_frame.pb.h"
#include "ffi.pb.h"
#include "ffi_client.h"
namespace livekit {
AudioProcessingModule::AudioProcessingModule()
: AudioProcessingModule(Options{}) {}
AudioProcessingModule::AudioProcessingModule(const Options &options) {
proto::FfiRequest req;
auto *msg = req.mutable_new_apm();
msg->set_echo_canceller_enabled(options.echo_cancellation);
msg->set_noise_suppression_enabled(options.noise_suppression);
msg->set_high_pass_filter_enabled(options.high_pass_filter);
msg->set_gain_controller_enabled(options.auto_gain_control);
proto::FfiResponse resp = FfiClient::instance().sendRequest(req);
if (!resp.has_new_apm()) {
throw std::runtime_error(
"AudioProcessingModule: failed to create APM - no response");
}
const auto &apm_info = resp.new_apm().apm();
handle_ = FfiHandle(static_cast<uintptr_t>(apm_info.handle().id()));
if (!handle_.valid()) {
throw std::runtime_error(
"AudioProcessingModule: failed to create APM - invalid handle");
}
}
void AudioProcessingModule::processStream(AudioFrame &frame) {
if (!handle_.valid()) {
throw std::runtime_error("AudioProcessingModule: invalid handle");
}
if (frame.data().empty()) {
return;
}
proto::FfiRequest req;
auto *msg = req.mutable_apm_process_stream();
msg->set_apm_handle(static_cast<std::uint64_t>(handle_.get()));
msg->set_data_ptr(reinterpret_cast<std::uint64_t>(frame.data().data()));
msg->set_size(
static_cast<std::uint32_t>(frame.data().size() * sizeof(std::int16_t)));
msg->set_sample_rate(static_cast<std::uint32_t>(frame.sample_rate()));
msg->set_num_channels(static_cast<std::uint32_t>(frame.num_channels()));
proto::FfiResponse resp = FfiClient::instance().sendRequest(req);
if (!resp.has_apm_process_stream()) {
throw std::runtime_error(
"AudioProcessingModule::processStream: unexpected response");
}
const auto &result = resp.apm_process_stream();
if (result.has_error()) {
throw std::runtime_error("AudioProcessingModule::processStream: " +
result.error());
}
}
void AudioProcessingModule::processReverseStream(AudioFrame &frame) {
if (!handle_.valid()) {
throw std::runtime_error("AudioProcessingModule: invalid handle");
}
if (frame.data().empty()) {
return;
}
proto::FfiRequest req;
auto *msg = req.mutable_apm_process_reverse_stream();
msg->set_apm_handle(static_cast<std::uint64_t>(handle_.get()));
msg->set_data_ptr(reinterpret_cast<std::uint64_t>(frame.data().data()));
msg->set_size(
static_cast<std::uint32_t>(frame.data().size() * sizeof(std::int16_t)));
msg->set_sample_rate(static_cast<std::uint32_t>(frame.sample_rate()));
msg->set_num_channels(static_cast<std::uint32_t>(frame.num_channels()));
proto::FfiResponse resp = FfiClient::instance().sendRequest(req);
if (!resp.has_apm_process_reverse_stream()) {
throw std::runtime_error(
"AudioProcessingModule::processReverseStream: unexpected response");
}
const auto &result = resp.apm_process_reverse_stream();
if (result.has_error()) {
throw std::runtime_error("AudioProcessingModule::processReverseStream: " +
result.error());
}
}
void AudioProcessingModule::setStreamDelayMs(int delay_ms) {
if (!handle_.valid()) {
throw std::runtime_error("AudioProcessingModule: invalid handle");
}
proto::FfiRequest req;
auto *msg = req.mutable_apm_set_stream_delay();
msg->set_apm_handle(static_cast<std::uint64_t>(handle_.get()));
msg->set_delay_ms(delay_ms);
proto::FfiResponse resp = FfiClient::instance().sendRequest(req);
if (!resp.has_apm_set_stream_delay()) {
throw std::runtime_error(
"AudioProcessingModule::setStreamDelayMs: unexpected response");
}
const auto &result = resp.apm_set_stream_delay();
if (result.has_error()) {
throw std::runtime_error("AudioProcessingModule::setStreamDelayMs: " +
result.error());
}
}
} // namespace livekit