• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 #ifndef AUDIO_CONVERTER_3DA
16 #define AUDIO_CONVERTER_3DA
17 
18 #include <cstdint>
19 #include <cstring>
20 #include <string>
21 #include <dlfcn.h>
22 #include <unistd.h>
23 #include "audio_effect.h"
24 #include "audio_service_log.h"
25 
26 namespace OHOS {
27 namespace AudioStandard {
28 
29 class LibLoader {
30 public:
31     LibLoader() = default;
32     ~LibLoader();
33     bool Init();
34     uint32_t GetLatency();
35     bool AddAlgoHandle(Library lib);
36     void SetIOBufferConfig(bool isInput, uint32_t sampleRate, uint8_t format, uint32_t channels,
37         uint64_t channelLayout);
38     int32_t ApplyAlgo(AudioBuffer &inputBuffer, AudioBuffer &outputBuffer);
39     bool FlushAlgo();
40 
41 private:
42     bool LoadLibrary(const std::string &relativePath) noexcept;
43     std::unique_ptr<AudioEffectLibEntry> libEntry_;
44     uint32_t latency_;
45     AudioEffectHandle handle_;
46     AudioEffectConfig ioBufferConfig_;
47     void *libHandle_;
48 };
49 
50 class AudioSpatialChannelConverter {
51 public:
52     AudioSpatialChannelConverter() = default;
53     bool Init(const AudioStreamParams info, const ConverterConfig cfg);
54     bool GetInputBufferSize(size_t &bufferSize);
55     size_t GetMetaSize();
56     bool CheckInputValid(const BufferDesc bufDesc);
57     bool AllocateMem();
58     bool Flush();
59     uint32_t GetLatency();
60     void Process(const BufferDesc bufDesc);
61     void ConverterChannels(uint8_t &channel, uint64_t &channelLayout);
62     void GetOutputBufferStream(uint8_t *&buffer, uint32_t &bufferLen);
63 
64 private:
65     size_t GetPcmLength(int32_t channels, int8_t bps);
66 
67     std::unique_ptr<uint8_t[]> outPcmBuf_;
68 
69     int32_t inChannel_;
70     int32_t outChannel_;
71     int32_t sampleRate_;
72 
73     uint8_t bps_;
74     uint8_t encoding_;
75 
76     uint64_t outChannelLayout_;
77 
78     bool loadSuccess_;
79 
80     LibLoader externalLoader_;
81 };
82 
83 } // namespace AudioStandard
84 } // namespace OHOS
85 #endif // AUDIO_CONVERTER_3DA