1 /*
2 * Copyright (c) 2023-2024 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 "VolumeTools"
17 #endif
18
19 #include <cmath>
20
21 #include "volume_tools.h"
22 #include "volume_tools_c.h"
23 #include "audio_errors.h"
24 #include "audio_service_log.h"
25
26 namespace {
27 static const int32_t UINT8_SHIFT = 0x80;
28 static const int32_t INT24_SHIFT = 8;
29 static const int32_t INT24_MAX_VALUE = 8388607;
30 static const uint32_t SHIFT_EIGHT = 8;
31 static const uint32_t SHIFT_SIXTEEN = 16;
32 static const uint32_t ARRAY_INDEX_TWO = 2;
33 static const size_t MIN_FRAME_SIZE = 1;
34 static const size_t MAX_FRAME_SIZE = 100000; // max to about 2s for 48khz
35 }
36 namespace OHOS {
37 namespace AudioStandard {
IsVolumeValid(float volFloat)38 bool VolumeTools::IsVolumeValid(float volFloat)
39 {
40 return volFloat >= 0.0 && volFloat <= 1.0;
41 }
42
IsVolumeValid(int32_t volInt)43 bool VolumeTools::IsVolumeValid(int32_t volInt)
44 {
45 return volInt >= INT32_VOLUME_MIN && volInt <= INT32_VOLUME_MAX;
46 }
IsVolumeValid(ChannelVolumes vols)47 bool VolumeTools::IsVolumeValid(ChannelVolumes vols)
48 {
49 if (vols.channel > CHANNEL_16 || vols.channel < MONO) {
50 return false;
51 }
52 for (size_t i = 0; i < vols.channel; i++) {
53 if (!IsVolumeValid(vols.volStart[i]) || !IsVolumeValid(vols.volEnd[i])) {
54 return false;
55 }
56 }
57
58 return true;
59 }
60
GetInt32Vol(float volFloat)61 int32_t VolumeTools::GetInt32Vol(float volFloat)
62 {
63 if (IsVolumeValid(volFloat)) {
64 return volFloat * INT32_VOLUME_MAX;
65 }
66 if (volFloat < 0.0) {
67 return INT32_VOLUME_MIN;
68 }
69 return INT32_VOLUME_MAX;
70 }
71
GetChannelVolumes(AudioChannel channel, int32_t volStart, int32_t volEnd)72 ChannelVolumes VolumeTools::GetChannelVolumes(AudioChannel channel, int32_t volStart, int32_t volEnd)
73 {
74 ChannelVolumes vols = {};
75 if (!IsVolumeValid(volStart) || !IsVolumeValid(volEnd) || channel > CHANNEL_16 || channel < MONO) {
76 AUDIO_ERR_LOG("GetChannelVolumes failed with invalid vol:%{public}d %{public}d channel: %{public}d", volStart,
77 volEnd, channel);
78 return vols;
79 }
80 for (size_t i = 0; i < channel; i++) {
81 vols.volStart[i] = volStart;
82 vols.volEnd[i] = volEnd;
83 }
84 vols.channel = channel;
85 return vols;
86 }
87
GetChannelVolumes(AudioChannel channel, float volStart, float volEnd)88 ChannelVolumes VolumeTools::GetChannelVolumes(AudioChannel channel, float volStart, float volEnd)
89 {
90 ChannelVolumes vols = {};
91 if (!IsVolumeValid(volStart) || !IsVolumeValid(volEnd) || channel > CHANNEL_16 || channel < MONO) {
92 AUDIO_ERR_LOG("GetChannelVolumes failed with invalid vol:%{public}f %{public}f channel: %{public}d", volStart,
93 volEnd, channel);
94 return vols;
95 }
96 for (size_t i = 0; i < channel; i++) {
97 vols.volStart[i] = GetInt32Vol(volStart);
98 vols.volEnd[i] = GetInt32Vol(volEnd);
99 }
100 vols.channel = channel;
101 return vols;
102 }
103
GetByteSize(AudioSampleFormat format)104 size_t GetByteSize(AudioSampleFormat format)
105 {
106 size_t bitWidthSize = 0;
107 switch (format) {
108 case SAMPLE_U8:
109 bitWidthSize = 1; // size is 1
110 break;
111 case SAMPLE_S16LE:
112 bitWidthSize = 2; // size is 2
113 break;
114 case SAMPLE_S24LE:
115 bitWidthSize = 3; // size is 3
116 break;
117 case SAMPLE_S32LE:
118 bitWidthSize = 4; // size is 4
119 break;
120 case SAMPLE_F32LE:
121 bitWidthSize = 4; // size is 4
122 break;
123 default:
124 bitWidthSize = 2; // default size is 2
125 break;
126 }
127 return bitWidthSize;
128 }
129
ReadInt24LE(const uint8_t *p)130 static inline uint32_t ReadInt24LE(const uint8_t *p)
131 {
132 return ((uint32_t) p[ARRAY_INDEX_TWO] << SHIFT_SIXTEEN) | ((uint32_t) p[1] << SHIFT_EIGHT) | ((uint32_t) p[0]);
133 }
134
WriteInt24LE(uint8_t *p, uint32_t u)135 static inline void WriteInt24LE(uint8_t *p, uint32_t u)
136 {
137 p[ARRAY_INDEX_TWO] = (uint8_t) (u >> SHIFT_SIXTEEN);
138 p[1] = (uint8_t) (u >> SHIFT_EIGHT);
139 p[0] = (uint8_t) u;
140 }
141
VolumeFlatten(int32_t vol)142 inline int32_t VolumeFlatten(int32_t vol)
143 {
144 return vol < INT32_VOLUME_MIN ? 0 : (vol > INT32_VOLUME_MAX ? INT32_VOLUME_MAX : vol);
145 }
146
ProcessOneFrame(uint8_t *ptr, AudioSampleFormat format, int32_t vol)147 void ProcessOneFrame(uint8_t *ptr, AudioSampleFormat format, int32_t vol)
148 {
149 int64_t temp = 0;
150 int16_t *raw16 = nullptr;
151 int32_t *raw32 = nullptr;
152 float *rawFloat = nullptr;
153 switch (format) {
154 case SAMPLE_U8:
155 temp = *ptr - UINT8_SHIFT;
156 temp = (temp * vol) >> VOLUME_SHIFT;
157 temp = temp < INT8_MIN ? INT8_MIN : (temp > INT8_MAX ? INT8_MAX : temp);
158 *ptr = static_cast<uint8_t>(temp + UINT8_SHIFT);
159 break;
160 case SAMPLE_S16LE:
161 raw16 = reinterpret_cast<int16_t *>(ptr);
162 temp = (*raw16 * static_cast<int64_t>(vol)) >> VOLUME_SHIFT;
163 *raw16 = temp > INT16_MAX ? INT16_MAX : (temp < INT16_MIN ? INT16_MIN : temp);
164 break;
165 case SAMPLE_S24LE:
166 temp = static_cast<int32_t>(ReadInt24LE(ptr) << INT24_SHIFT) * static_cast<int64_t>(vol) >> VOLUME_SHIFT;
167 WriteInt24LE(ptr, (static_cast<uint32_t>(temp) >> INT24_SHIFT));
168 break;
169 case SAMPLE_S32LE:
170 raw32 = reinterpret_cast<int32_t *>(ptr);
171 // int32_t * int16_t, max result is int48_t
172 temp = (*raw32 * static_cast<int64_t>(vol)) >> VOLUME_SHIFT;
173 *raw32 = temp > INT32_MAX ? INT32_MAX : (temp < INT32_MIN ? INT32_MIN : temp);
174 break;
175 case SAMPLE_F32LE:
176 rawFloat = reinterpret_cast<float *>(ptr);
177 *rawFloat = *rawFloat * (static_cast<float>(vol) / INT32_VOLUME_MAX);
178 break;
179 default:
180 AUDIO_ERR_LOG("ProcessOneFrame with invalid format");
181 break;
182 }
183 }
184
185 // |---------frame1--------|---------frame2--------|---------frame3--------|
186 // |ch1-ch2-ch3-ch4-ch5-ch6|ch1-ch2-ch3-ch4-ch5-ch6|ch1-ch2-ch3-ch4-ch5-ch6|
Process(const BufferDesc &buffer, AudioSampleFormat format, ChannelVolumes vols)187 int32_t VolumeTools::Process(const BufferDesc &buffer, AudioSampleFormat format, ChannelVolumes vols)
188 {
189 // parms check
190 if (format > SAMPLE_F32LE || !IsVolumeValid(vols)) {
191 AUDIO_ERR_LOG("Process failed with invalid params");
192 return ERR_INVALID_PARAM;
193 }
194 size_t byteSizePerData = GetByteSize(format);
195 size_t byteSizePerFrame = byteSizePerData * vols.channel;
196 if (buffer.buffer == nullptr || buffer.bufLength % byteSizePerFrame != 0) {
197 AUDIO_ERR_LOG("Process failed with invalid buffer, size is %{public}zu", buffer.bufLength);
198 return ERR_INVALID_PARAM;
199 }
200
201 size_t frameSize = buffer.bufLength / byteSizePerFrame;
202 if (frameSize < MIN_FRAME_SIZE) {
203 AUDIO_ERR_LOG("Process failed with invalid frameSize, size is %{public}zu", frameSize);
204 return ERR_INVALID_PARAM;
205 }
206
207 float volStep[CHANNEL_MAX] = {};
208 for (size_t channelIdx = 0; channelIdx < vols.channel; channelIdx++) {
209 if (vols.volEnd[channelIdx] == vols.volStart[channelIdx] || frameSize == MIN_FRAME_SIZE) {
210 volStep[channelIdx] = 0.0;
211 } else {
212 volStep[channelIdx] = (static_cast<float>(vols.volEnd[channelIdx] - vols.volStart[channelIdx])) /
213 (frameSize - MIN_FRAME_SIZE);
214 }
215 }
216 for (size_t frameIndex = 0; frameIndex < frameSize; frameIndex++) {
217 for (size_t channelIdx = 0; channelIdx < vols.channel; channelIdx++) {
218 int32_t vol = volStep[channelIdx] * frameIndex + vols.volStart[channelIdx];
219 vol = VolumeFlatten(vol);
220 uint8_t *samplePtr = buffer.buffer + frameIndex * byteSizePerFrame + channelIdx * byteSizePerData;
221 ProcessOneFrame(samplePtr, format, vol);
222 }
223 }
224
225 return SUCCESS;
226 }
227
GetVolDb(AudioSampleFormat format, int32_t vol)228 double VolumeTools::GetVolDb(AudioSampleFormat format, int32_t vol)
229 {
230 double volume = static_cast<double>(vol);
231 switch (format) {
232 case SAMPLE_U8:
233 volume = volume / INT8_MAX;
234 break;
235 case SAMPLE_S16LE:
236 volume = volume / INT16_MAX;
237 break;
238 case SAMPLE_S24LE:
239 volume = volume / INT24_MAX_VALUE;
240 break;
241 case SAMPLE_S32LE:
242 volume = volume / INT32_MAX;
243 break;
244 case SAMPLE_F32LE:
245 volume = volume / INT32_MAX;
246 break;
247 default:
248 break;
249 }
250 return std::log10(volume);
251 }
252
CountU8Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)253 static void CountU8Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)
254 {
255 size_t byteSizePerData = 1; // 1 for unsigned 8bit
256 size_t byteSizePerFrame = byteSizePerData * channel;
257 if (buffer.buffer == nullptr || byteSizePerFrame == 0 || buffer.bufLength % byteSizePerFrame != 0) {
258 AUDIO_ERR_LOG("invalid buffer, size is %{public}zu", buffer.bufLength);
259 return;
260 }
261 size_t frameSize = buffer.bufLength / byteSizePerFrame;
262 if (frameSize <= MIN_FRAME_SIZE || frameSize >= MAX_FRAME_SIZE) {
263 AUDIO_ERR_LOG("invalid frameSize, size is %{public}zu", frameSize);
264 return;
265 }
266
267 // reset maps
268 for (size_t index = 0; index < channel; index++) {
269 volMaps.volStart[index] = 0;
270 volMaps.volEnd[index] = 0;
271 }
272 uint8_t *raw8 = buffer.buffer;
273 for (size_t frameIndex = 0; frameIndex < frameSize; frameIndex++) {
274 for (size_t channelIdx = 0; channelIdx < channel; channelIdx++) {
275 volMaps.volStart[channelIdx] += (*raw8 >= UINT8_SHIFT ? *raw8 - UINT8_SHIFT : UINT8_SHIFT - *raw8);
276 raw8++;
277 }
278 }
279 // Calculate the average value
280 for (size_t index = 0; index < channel; index++) {
281 volMaps.volStart[index] /= static_cast<int32_t>(frameSize);
282 }
283 return;
284 }
285
CountS16Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)286 static void CountS16Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)
287 {
288 size_t byteSizePerData = 2; // 2 for signed 16bit
289 size_t byteSizePerFrame = byteSizePerData * channel;
290 if (buffer.buffer == nullptr || byteSizePerFrame == 0 || buffer.bufLength % byteSizePerFrame != 0) {
291 AUDIO_ERR_LOG("invalid buffer, size is %{public}zu", buffer.bufLength);
292 return;
293 }
294 size_t frameSize = buffer.bufLength / byteSizePerFrame;
295 if (frameSize <= MIN_FRAME_SIZE || frameSize >= MAX_FRAME_SIZE) {
296 AUDIO_ERR_LOG("invalid frameSize, size is %{public}zu", frameSize);
297 return;
298 }
299
300 // reset maps
301 for (size_t index = 0; index < channel; index++) {
302 volMaps.volStart[index] = 0;
303 volMaps.volEnd[index] = 0;
304 }
305 int16_t *raw16 = reinterpret_cast<int16_t *>(buffer.buffer);
306 for (size_t frameIndex = 0; frameIndex < frameSize; frameIndex++) {
307 for (size_t channelIdx = 0; channelIdx < channel; channelIdx++) {
308 volMaps.volStart[channelIdx] += (*raw16 >= 0 ? *raw16: (-*raw16));
309 raw16++;
310 }
311 }
312 // Calculate the average value
313 for (size_t index = 0; index < channel; index++) {
314 volMaps.volStart[index] /= static_cast<int32_t>(frameSize);
315 }
316 return;
317 }
318
CountS24Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)319 static void CountS24Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)
320 {
321 size_t byteSizePerData = 3; // 3 for 24bit
322 size_t byteSizePerFrame = byteSizePerData * channel;
323 if (buffer.buffer == nullptr || byteSizePerFrame == 0 || buffer.bufLength % byteSizePerFrame != 0) {
324 AUDIO_ERR_LOG("invalid buffer, size is %{public}zu", buffer.bufLength);
325 return;
326 }
327 size_t frameSize = buffer.bufLength / byteSizePerFrame;
328 if (frameSize <= MIN_FRAME_SIZE || frameSize >= MAX_FRAME_SIZE) {
329 AUDIO_ERR_LOG("invalid frameSize, size is %{public}zu", frameSize);
330 return;
331 }
332
333 // reset maps
334 for (size_t index = 0; index < channel; index++) {
335 volMaps.volStart[index] = 0;
336 volMaps.volEnd[index] = 0;
337 }
338 uint8_t *raw8 = buffer.buffer;
339 for (size_t frameIndex = 0; frameIndex < frameSize; frameIndex++) {
340 for (size_t channelIdx = 0; channelIdx < channel; channelIdx++) {
341 int32_t sample = static_cast<int32_t>(ReadInt24LE(raw8));
342 volMaps.volStart[channelIdx] += (sample >= 0 ? sample: (-sample));
343 raw8 += byteSizePerData;
344 }
345 }
346 // Calculate the average value
347 for (size_t index = 0; index < channel; index++) {
348 volMaps.volStart[index] /= static_cast<int32_t>(frameSize);
349 }
350 return;
351 }
352
CountS32Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)353 static void CountS32Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)
354 {
355 size_t byteSizePerData = 4; // 4 for signed 32bit
356 size_t byteSizePerFrame = byteSizePerData * channel;
357 if (buffer.buffer == nullptr || byteSizePerFrame == 0 || buffer.bufLength % byteSizePerFrame != 0) {
358 AUDIO_ERR_LOG("invalid buffer, size is %{public}zu", buffer.bufLength);
359 return;
360 }
361 size_t frameSize = buffer.bufLength / byteSizePerFrame;
362 if (frameSize <= MIN_FRAME_SIZE || frameSize >= MAX_FRAME_SIZE) {
363 AUDIO_ERR_LOG("invalid frameSize, size is %{public}zu", frameSize);
364 return;
365 }
366
367 // reset maps
368 int64_t volSums[CHANNEL_MAX] = {0};
369 for (size_t index = 0; index < CHANNEL_MAX; index++) {
370 volSums[index] = 0;
371 }
372 int32_t *raw32 = reinterpret_cast<int32_t *>(buffer.buffer);
373 for (size_t frameIndex = 0; frameIndex < frameSize; frameIndex++) {
374 for (size_t channelIdx = 0; channelIdx < channel; channelIdx++) {
375 volSums[channelIdx] += (*raw32 >= 0 ? *raw32: (-*raw32));
376 raw32++;
377 }
378 }
379 // Calculate the average value
380 for (size_t index = 0; index < channel; index++) {
381 volSums[index] /= static_cast<int32_t>(frameSize);
382 volMaps.volStart[index] = volSums[index];
383 }
384 return;
385 }
386
CountF32Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)387 static void CountF32Volume(const BufferDesc &buffer, AudioChannel channel, ChannelVolumes &volMaps)
388 {
389 size_t byteSizePerData = 4; // 4 for 32bit
390 size_t byteSizePerFrame = byteSizePerData * channel;
391 if (buffer.buffer == nullptr || byteSizePerFrame == 0 || buffer.bufLength % byteSizePerFrame != 0) {
392 AUDIO_ERR_LOG("invalid buffer, size is %{public}zu", buffer.bufLength);
393 return;
394 }
395 size_t frameSize = buffer.bufLength / byteSizePerFrame;
396 if (frameSize <= MIN_FRAME_SIZE || frameSize >= MAX_FRAME_SIZE) {
397 AUDIO_ERR_LOG("invalid frameSize, size is %{public}zu", frameSize);
398 return;
399 }
400
401 // reset maps
402 double volSums[CHANNEL_MAX] = {0};
403 for (size_t index = 0; index < CHANNEL_MAX; index++) {
404 volSums[index] = 0.0;
405 }
406 float *raw32 = reinterpret_cast<float *>(buffer.buffer);
407 for (size_t frameIndex = 0; frameIndex < frameSize; frameIndex++) {
408 for (size_t channelIdx = 0; channelIdx < channel; channelIdx++) {
409 volSums[channelIdx] += (*raw32 >= 0 ? *raw32: (-*raw32));
410 raw32++;
411 }
412 }
413 // Calculate the average value
414 for (size_t index = 0; index < channel; index++) {
415 volSums[index] /= frameSize;
416 volMaps.volStart[index] = static_cast<int32_t>(volSums[index]);
417 }
418 return;
419 }
420
CountVolumeLevel(const BufferDesc &buffer, AudioSampleFormat format, AudioChannel channel)421 ChannelVolumes VolumeTools::CountVolumeLevel(const BufferDesc &buffer, AudioSampleFormat format, AudioChannel channel)
422 {
423 ChannelVolumes channelVols = {};
424 channelVols.channel = channel;
425 if (format > SAMPLE_F32LE || channel > CHANNEL_16) {
426 AUDIO_ERR_LOG("failed with invalid params");
427 return channelVols;
428 }
429 switch (format) {
430 case SAMPLE_U8:
431 CountU8Volume(buffer, channel, channelVols);
432 break;
433 case SAMPLE_S16LE:
434 CountS16Volume(buffer, channel, channelVols);
435 break;
436 case SAMPLE_S24LE:
437 CountS24Volume(buffer, channel, channelVols);
438 break;
439 case SAMPLE_S32LE:
440 CountS32Volume(buffer, channel, channelVols);
441 break;
442 case SAMPLE_F32LE:
443 CountF32Volume(buffer, channel, channelVols);
444 break;
445 default:
446 break;
447 }
448
449 return channelVols;
450 }
451 } // namespace AudioStandard
452 } // namespace OHOS
453
454 #ifdef __cplusplus
455 extern "C" {
456 #endif
457 using namespace OHOS::AudioStandard;
458
ProcessVol(uint8_t *buffer, size_t length, AudioRawFormat rawformat, float volStart, float volEnd)459 int32_t ProcessVol(uint8_t *buffer, size_t length, AudioRawFormat rawformat, float volStart, float volEnd)
460 {
461 BufferDesc desc = {0};
462 desc.buffer = buffer;
463 desc.bufLength = length;
464 desc.dataLength = length;
465 ChannelVolumes mapVols = VolumeTools::GetChannelVolumes(static_cast<AudioChannel>(rawformat.channels), volStart,
466 volEnd);
467 return VolumeTools::Process(desc, static_cast<AudioSampleFormat>(rawformat.format), mapVols);
468 }
469
470 #ifdef __cplusplus
471 }
472 #endif