1 /*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "audio_resample.h"
16 #include "audio_errors.h"
17 #include "audio_service_log.h"
18 #include "audio_utils.h"
19 #include "audio_proresampler.h"
20 #include <cinttypes>
21
22 namespace OHOS {
23 namespace AudioStandard {
AudioResample(uint32_t channels,uint32_t inRate,uint32_t outRate,int32_t quantity)24 AudioResample::AudioResample(uint32_t channels, uint32_t inRate, uint32_t outRate, int32_t quantity)
25 : resampler_(std::make_unique<HPAE::ProResampler>(inRate, outRate, channels, quantity))
26 {
27 }
28
IsResampleInit() const29 bool AudioResample::IsResampleInit() const noexcept
30 {
31 if (resampler_) {
32 return true;
33 }
34 return false;
35 }
36
~AudioResample()37 AudioResample::~AudioResample()
38 {
39 if (!resampler_) {
40 return;
41 }
42 resampler_->Reset();
43 resampler_ = nullptr;
44 }
45
ProcessFloatResample(const std::vector<float> & input,std::vector<float> & output)46 int32_t AudioResample::ProcessFloatResample(const std::vector<float> &input, std::vector<float> &output)
47 {
48 if (!resampler_) {
49 return ERR_INVALID_PARAM;
50 }
51 Trace trace("AudioResample::ProcessFloatResample");
52 if (resampler_->GetChannels() <= 0) {
53 return ERR_INVALID_PARAM;
54 }
55 uint32_t inSize = input.size() / resampler_->GetChannels();
56 uint32_t outSize = output.size() / resampler_->GetChannels();
57 return resampler_->Process(input.data(), inSize, output.data(), outSize);
58 }
59 } // namespace AudioStandard
60 } // namespace OHOS
61