1 /*
2  * Copyright (c) 2023 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_CHANNEL_BLEND_H
16 #define AUDIO_CHANNEL_BLEND_H
17 
18 #include <cstdint>
19 #include <string>
20 #include "audio_info.h"
21 
22 namespace OHOS {
23 namespace AudioStandard {
24 static constexpr int32_t CHANNEL_ONE = 0;
25 static constexpr int32_t CHANNEL_TWO = 1;
26 static constexpr int32_t CHANNEL_THREE = 2;
27 static constexpr int32_t CHANNEL_FOUR = 3;
28 static constexpr int32_t CHANNEL_FIVE = 4;
29 static constexpr int32_t CHANNEL_SIX = 5;
30 static constexpr int32_t CHANNEL_SEVEN = 6;
31 static constexpr int32_t CHANNEL_EIGHT = 7;
32 
33 typedef struct {
34     int8_t value[3];
35 } __attribute__((__packed__)) int24_t;
36 
37 class AudioBlend {
38 public:
39     AudioBlend();
40     AudioBlend(ChannelBlendMode blendMode, uint8_t format, uint8_t channel);
41     void Process(uint8_t *buffer, size_t bufferSize);
42     void SetParams(ChannelBlendMode blendMode, uint8_t format, uint8_t channel);
43 
44 private:
45     template <typename T>
46     void BlendLR(T& left, T& right);
47     template <>
48     void BlendLR(int24_t& left, int24_t& right);
49     template <typename T>
50     void ProcessBlendLRModeWithFormat(T *buffer, size_t count, AudioChannel channel);
51     template <typename T>
52     void ProcessAllLeftModeWithFormat(T *buffer, size_t count, AudioChannel channel);
53     template <typename T>
54     void ProcessAllRightModeWithFormat(T *buffer, size_t count, AudioChannel channel);
55     template<typename T>
56     void ProcessWithBlendMode(T *buffer, size_t bufferSize);
57 
58     ChannelBlendMode blendMode_;
59     uint8_t format_;
60     uint8_t channels_;
61 };
62 } // namespace AudioStandard
63 } // namespace OHOS
64 #endif // AUDIO_CHANNEL_BLEND_H
65