| 1 | /* |
| 2 | * Copyright 2025 LiveKit, Inc. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "livekit/apm.h" |
| 18 | |
| 19 | #include "api/audio/builtin_audio_processing_builder.h" |
| 20 | #include "api/environment/environment_factory.h" |
| 21 | |
| 22 | #include <iostream> |
| 23 | #include <memory> |
| 24 | |
| 25 | namespace livekit_ffi { |
| 26 | |
| 27 | AudioProcessingModule::AudioProcessingModule( |
| 28 | const AudioProcessingConfig& config) { |
| 29 | apm_ = webrtc::BuiltinAudioProcessingBuilder() |
| 30 | .Build(webrtc::CreateEnvironment()); |
| 31 | |
| 32 | apm_->ApplyConfig(config.ToWebrtcConfig()); |
| 33 | apm_->Initialize(); |
| 34 | } |
| 35 | |
| 36 | int AudioProcessingModule::process_stream(const int16_t* src, |
| 37 | size_t src_len, |
| 38 | int16_t* dst, |
| 39 | size_t dst_len, |
| 40 | int sample_rate, |
| 41 | int num_channels) { |
| 42 | webrtc::StreamConfig stream_cfg(sample_rate, num_channels); |
| 43 | return apm_->ProcessStream(src, stream_cfg, stream_cfg, dst); |
| 44 | } |
| 45 | |
| 46 | int AudioProcessingModule::process_reverse_stream(const int16_t* src, |
| 47 | size_t src_len, |
| 48 | int16_t* dst, |
| 49 | size_t dst_len, |
| 50 | int sample_rate, |
| 51 | int num_channels) { |
| 52 | webrtc::StreamConfig stream_cfg(sample_rate, num_channels); |
| 53 | return apm_->ProcessReverseStream(src, stream_cfg, stream_cfg, dst); |
| 54 | } |
| 55 | |
| 56 | int AudioProcessingModule::set_stream_delay_ms(int delay_ms) { |
| 57 | return apm_->set_stream_delay_ms(delay_ms); |
| 58 | } |
| 59 | |
| 60 | std::unique_ptr<AudioProcessingModule> create_apm( |
| 61 | bool echo_canceller_enabled, |
| 62 | bool gain_controller_enabled, |
| 63 | bool high_pass_filter_enabled, |
| 64 | bool noise_suppression_enabled) { |
| 65 | AudioProcessingConfig config; |
| 66 | config.echo_canceller_enabled = echo_canceller_enabled; |
| 67 | config.gain_controller_enabled = gain_controller_enabled; |
| 68 | config.high_pass_filter_enabled = high_pass_filter_enabled; |
| 69 | config.noise_suppression_enabled = noise_suppression_enabled; |
| 70 | return std::make_unique<AudioProcessingModule>(config); |
| 71 | } |
| 72 | |
| 73 | } // namespace livekit_ffi |