| 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 | : AudioProcessingModule(config.ToWebrtcConfig()) {} |
| 30 | |
| 31 | AudioProcessingModule::AudioProcessingModule( |
| 32 | const webrtc::AudioProcessing::Config& config) { |
| 33 | apm_ = webrtc::BuiltinAudioProcessingBuilder() |
| 34 | .Build(webrtc::CreateEnvironment()); |
| 35 | |
| 36 | apm_->ApplyConfig(config); |
| 37 | apm_->Initialize(); |
| 38 | } |
| 39 | |
| 40 | int AudioProcessingModule::process_stream(const int16_t* src, |
| 41 | size_t src_len, |
| 42 | int16_t* dst, |
| 43 | size_t dst_len, |
| 44 | int sample_rate, |
| 45 | int num_channels) { |
| 46 | webrtc::StreamConfig stream_cfg(sample_rate, num_channels); |
| 47 | return apm_->ProcessStream(src, stream_cfg, stream_cfg, dst); |
| 48 | } |
| 49 | |
| 50 | int AudioProcessingModule::process_reverse_stream(const int16_t* src, |
| 51 | size_t src_len, |
| 52 | int16_t* dst, |
| 53 | size_t dst_len, |
| 54 | int sample_rate, |
| 55 | int num_channels) { |
| 56 | webrtc::StreamConfig stream_cfg(sample_rate, num_channels); |
| 57 | return apm_->ProcessReverseStream(src, stream_cfg, stream_cfg, dst); |
| 58 | } |
| 59 | |
| 60 | int AudioProcessingModule::set_stream_delay_ms(int delay_ms) { |
| 61 | return apm_->set_stream_delay_ms(delay_ms); |
| 62 | } |
| 63 | |
| 64 | std::unique_ptr<AudioProcessingModule> create_apm( |
| 65 | bool echo_canceller_enabled, |
| 66 | bool gain_controller_enabled, |
| 67 | bool high_pass_filter_enabled, |
| 68 | bool noise_suppression_enabled) { |
| 69 | AudioProcessingConfig config; |
| 70 | config.echo_canceller_enabled = echo_canceller_enabled; |
| 71 | config.gain_controller_enabled = gain_controller_enabled; |
| 72 | config.high_pass_filter_enabled = high_pass_filter_enabled; |
| 73 | config.noise_suppression_enabled = noise_suppression_enabled; |
| 74 | return std::make_unique<AudioProcessingModule>(config); |
| 75 | } |
| 76 | |
| 77 | std::unique_ptr<AudioProcessingModule> create_gain_apm( |
| 78 | float initial_gain_db, |
| 79 | float fixed_gain_db, |
| 80 | float max_gain_change_db_per_second) { |
| 81 | webrtc::AudioProcessing::Config config; |
| 82 | config.gain_controller2.enabled = true; |
| 83 | config.gain_controller2.adaptive_digital.enabled = true; |
| 84 | config.gain_controller2.adaptive_digital.initial_gain_db = initial_gain_db; |
| 85 | config.gain_controller2.adaptive_digital.max_gain_change_db_per_second = |
| 86 | max_gain_change_db_per_second; |
| 87 | config.gain_controller2.fixed_digital.gain_db = fixed_gain_db; |
| 88 | return std::make_unique<AudioProcessingModule>(config); |
| 89 | } |
| 90 | |
| 91 | } // namespace livekit_ffi |