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 LOG_TAG
16 #define LOG_TAG "AudioBlend"
17 #endif
18
19 #include <cinttypes>
20 #include "audio_common_log.h"
21 #include "audio_channel_blend.h"
22
23 namespace OHOS {
24 namespace AudioStandard {
AudioBlend()25 AudioBlend::AudioBlend()
26 {
27 blendMode_ = MODE_DEFAULT;
28 format_ = 0;
29 channels_ = 0;
30 }
31
AudioBlend(ChannelBlendMode blendMode,uint8_t format,uint8_t channel)32 AudioBlend::AudioBlend(ChannelBlendMode blendMode, uint8_t format, uint8_t channel)
33 :blendMode_(blendMode), format_(format), channels_(channel)
34 {
35 }
36
SetParams(ChannelBlendMode blendMode,uint8_t format,uint8_t channel)37 void AudioBlend::SetParams(ChannelBlendMode blendMode, uint8_t format, uint8_t channel)
38 {
39 AUDIO_DEBUG_LOG("SetParams blendMode_:%{public}d format_:%{public}d channels_:%{public}d",
40 blendMode_, format_, channels_);
41 blendMode_ = blendMode;
42 format_ = format;
43 channels_ = channel;
44 }
45
Process(uint8_t * buffer,size_t bufferSize)46 void AudioBlend::Process(uint8_t *buffer, size_t bufferSize)
47 {
48 switch (format_) {
49 case AudioSampleFormat::SAMPLE_U8:
50 ProcessWithBlendMode<uint8_t>(reinterpret_cast<uint8_t*>(buffer), bufferSize);
51 break;
52 case AudioSampleFormat::SAMPLE_S16LE:
53 ProcessWithBlendMode<int16_t>(reinterpret_cast<int16_t*>(buffer), bufferSize);
54 break;
55 case AudioSampleFormat::SAMPLE_S24LE:
56 ProcessWithBlendMode<int24_t>(reinterpret_cast<int24_t*>(buffer), bufferSize);
57 break;
58 case AudioSampleFormat::SAMPLE_S32LE:
59 ProcessWithBlendMode<int32_t>(reinterpret_cast<int32_t*>(buffer), bufferSize);
60 break;
61 default:
62 break;
63 }
64 }
65
66 template<typename T>
ProcessWithBlendMode(T * buffer,size_t bufferSize)67 void AudioBlend::ProcessWithBlendMode(T *buffer, size_t bufferSize)
68 {
69 if (channels_ == 0) {
70 return;
71 }
72
73 uint32_t frameCount = 0;
74 frameCount = bufferSize / (channels_ * (format_ + 1));
75 switch (blendMode_) {
76 case MODE_BLEND_LR:
77 ProcessBlendLRModeWithFormat<T>(buffer, frameCount, (AudioChannel)channels_);
78 break;
79 case MODE_ALL_LEFT:
80 ProcessAllLeftModeWithFormat<T>(buffer, frameCount, (AudioChannel)channels_);
81 break;
82 case MODE_ALL_RIGHT:
83 ProcessAllRightModeWithFormat<T>(buffer, frameCount, (AudioChannel)channels_);
84 break;
85 default:
86 break;
87 }
88 }
89
90 template <typename T>
BlendLR(T & left,T & right)91 void AudioBlend::BlendLR(T& left, T& right)
92 {
93 left = left / 2 + right / 2;
94 right = left;
95 }
96
97 template <>
BlendLR(int24_t & left,int24_t & right)98 void AudioBlend::BlendLR(int24_t& left, int24_t& right)
99 {
100 left.value[0] = left.value[0] / 2 + right.value[0] / 2;
101 right.value[0] = left.value[0];
102 left.value[1] = left.value[1] / 2 + right.value[1] / 2;
103 right.value[0] = left.value[0];
104 left.value[2] = left.value[2] / 2 + right.value[2] / 2;
105 right.value[2] = left.value[2];
106 }
107
108 template <typename T>
ProcessBlendLRModeWithFormat(T * buffer,size_t count,AudioChannel channel)109 void AudioBlend::ProcessBlendLRModeWithFormat(T *buffer, size_t count, AudioChannel channel)
110 {
111 for (uint32_t i = count; i > 0; i--) {
112 switch (channel) {
113 case CHANNEL_8:
114 BlendLR(buffer[CHANNEL_SEVEN], buffer[CHANNEL_EIGHT]);
115 [[fallthrough]];
116 case CHANNEL_7:
117 case CHANNEL_6:
118 BlendLR(buffer[CHANNEL_FIVE], buffer[CHANNEL_SIX]);
119 BlendLR(buffer[CHANNEL_ONE], buffer[CHANNEL_TWO]);
120 break;
121 case CHANNEL_5:
122 case CHANNEL_4:
123 BlendLR(buffer[CHANNEL_THREE], buffer[CHANNEL_FOUR]);
124 [[fallthrough]];
125 case CHANNEL_3:
126 case STEREO:
127 BlendLR(buffer[CHANNEL_ONE], buffer[CHANNEL_TWO]);
128 break;
129 default:
130 break;
131 }
132 buffer += (int8_t)channel;
133 }
134 }
135
136 template <typename T>
ProcessAllLeftModeWithFormat(T * buffer,size_t count,AudioChannel channel)137 void AudioBlend::ProcessAllLeftModeWithFormat(T *buffer, size_t count, AudioChannel channel)
138 {
139 for (uint32_t i = count; i > 0; i--) {
140 switch (channel) {
141 case CHANNEL_8:
142 buffer[CHANNEL_EIGHT] = buffer[CHANNEL_SEVEN];
143 [[fallthrough]];
144 case CHANNEL_7:
145 case CHANNEL_6:
146 buffer[CHANNEL_SIX] = buffer[CHANNEL_FIVE];
147 buffer[CHANNEL_TWO] = buffer[CHANNEL_ONE];
148 break;
149 case CHANNEL_5:
150 case CHANNEL_4:
151 buffer[CHANNEL_FOUR] = buffer[CHANNEL_THREE];
152 [[fallthrough]];
153 case CHANNEL_3:
154 case STEREO:
155 buffer[CHANNEL_TWO] = buffer[CHANNEL_ONE];
156 break;
157 default:
158 break;
159 }
160 buffer += (int8_t)channel;
161 }
162 }
163
164 template <typename T>
ProcessAllRightModeWithFormat(T * buffer,size_t count,AudioChannel channel)165 void AudioBlend::ProcessAllRightModeWithFormat(T *buffer, size_t count, AudioChannel channel)
166 {
167 for (uint32_t i = count; i > 0; i--) {
168 switch (channel) {
169 case CHANNEL_8:
170 buffer[CHANNEL_SEVEN] = buffer[CHANNEL_EIGHT];
171 [[fallthrough]];
172 case CHANNEL_7:
173 case CHANNEL_6:
174 buffer[CHANNEL_FIVE] = buffer[CHANNEL_SIX];
175 buffer[CHANNEL_ONE] = buffer[CHANNEL_TWO];
176 break;
177 case CHANNEL_5:
178 case CHANNEL_4:
179 buffer[CHANNEL_THREE] = buffer[CHANNEL_FOUR];
180 [[fallthrough]];
181 case CHANNEL_3:
182 case STEREO:
183 buffer[CHANNEL_ONE] = buffer[CHANNEL_TWO];
184 break;
185 default:
186 break;
187 }
188 buffer += (int8_t)channel;
189 }
190 }
191
192 template void AudioBlend::ProcessBlendLRModeWithFormat(uint8_t *buffer, size_t count, AudioChannel channel);
193 template void AudioBlend::ProcessBlendLRModeWithFormat(int16_t *buffer, size_t count, AudioChannel channel);
194 template void AudioBlend::ProcessBlendLRModeWithFormat(int24_t *buffer, size_t count, AudioChannel channel);
195 template void AudioBlend::ProcessBlendLRModeWithFormat(int32_t *buffer, size_t count, AudioChannel channel);
196
197 template void AudioBlend::ProcessAllLeftModeWithFormat(uint8_t *buffer, size_t count, AudioChannel channel);
198 template void AudioBlend::ProcessAllLeftModeWithFormat(int16_t *buffer, size_t count, AudioChannel channel);
199 template void AudioBlend::ProcessAllLeftModeWithFormat(int24_t *buffer, size_t count, AudioChannel channel);
200 template void AudioBlend::ProcessAllLeftModeWithFormat(int32_t *buffer, size_t count, AudioChannel channel);
201
202 template void AudioBlend::ProcessAllRightModeWithFormat(uint8_t *buffer, size_t count, AudioChannel channel);
203 template void AudioBlend::ProcessAllRightModeWithFormat(int16_t *buffer, size_t count, AudioChannel channel);
204 template void AudioBlend::ProcessAllRightModeWithFormat(int24_t *buffer, size_t count, AudioChannel channel);
205 template void AudioBlend::ProcessAllRightModeWithFormat(int32_t *buffer, size_t count, AudioChannel channel);
206 } // namespace AudioStandard
207 } // namespace OHOS
208