| 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/audio_resampler.h" |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include "audio/remix_resample.h" |
| 22 | #include "api/audio/audio_view.h" |
| 23 | #include "api/audio/audio_frame.h" |
| 24 | |
| 25 | namespace livekit_ffi { |
| 26 | |
| 27 | size_t AudioResampler::remix_and_resample(const int16_t* src, |
| 28 | size_t samples_per_channel, |
| 29 | size_t num_channels, |
| 30 | int sample_rate, |
| 31 | size_t dest_num_channels, |
| 32 | int dest_sample_rate) { |
| 33 | frame_.num_channels_ = dest_num_channels; |
| 34 | frame_.sample_rate_hz_ = dest_sample_rate; |
| 35 | frame_.samples_per_channel_ = webrtc::SampleRateToDefaultChannelSize(dest_sample_rate); |
| 36 | webrtc::InterleavedView<const int16_t> source(static_cast<const int16_t*>(src), |
| 37 | samples_per_channel, |
| 38 | num_channels); |
| 39 | webrtc::voe::RemixAndResample(source, sample_rate, &resampler_, &frame_); |
| 40 | |
| 41 | return frame_.num_channels() * frame_.samples_per_channel() * sizeof(int16_t); |
| 42 | } |
| 43 | |
| 44 | const int16_t* AudioResampler::data() const { |
| 45 | return frame_.data(); |
| 46 | } |
| 47 | |
| 48 | std::unique_ptr<AudioResampler> create_audio_resampler() { |
| 49 | return std::make_unique<AudioResampler>(); |
| 50 | } |
| 51 | |
| 52 | } // namespace livekit_ffi |