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
16 #ifndef VOLUME_TOOLS_H
17 #define VOLUME_TOOLS_H
18 #include "audio_log_utils.h"
19 #include "ring_buffer_wrapper.h"
20
21 namespace OHOS {
22 namespace AudioStandard {
23
IsVolumeSame(const float & x,const float & y,const float & epsilon)24 static inline bool IsVolumeSame(const float& x, const float& y, const float& epsilon)
25 {
26 return (std::abs((x) - (y)) <= std::abs(epsilon));
27 }
28
29 class VolumeTools {
30 public:
31 static double GetVolDb(AudioSampleFormat format, int32_t vol);
32 static bool IsVolumeValid(float volFloat); // 0.0 <= volFloat <= 1.0
33 static bool IsVolumeValid(int32_t volInt); // 0 <= volInt <= 65536
34 static bool IsVolumeValid(ChannelVolumes vols);
35 static size_t GetByteSize(AudioSampleFormat format);
36 static int32_t GetInt32Vol(float volFloat);
37 static ChannelVolumes GetChannelVolumes(AudioChannel channel, int32_t volStart, int32_t volEnd);
38 static ChannelVolumes GetChannelVolumes(AudioChannel channel, float volStart, float volEnd);
39
40 // Data size should be rounded to each sample size
41 // There will be significant sound quality loss when process uint8_t samples.
42 static int32_t Process(const BufferDesc &buffer, AudioSampleFormat format, ChannelVolumes vols);
43 static int32_t Process(const RingBufferWrapper& ringBufferDesc, AudioSampleFormat format, ChannelVolumes vols);
44
45 // will count volume for each channel, vol sum will be kept in volStart
46 static ChannelVolumes CountVolumeLevel(const BufferDesc &buffer, AudioSampleFormat format, AudioChannel channel,
47 size_t split = 1);
48 static void DfxOperation(const BufferDesc &buffer, AudioStreamInfo streamInfo, std::string logTag,
49 int64_t &volumeDataCount, size_t split = 1);
50
51 static void CalcMuteFrame(BufferDesc &buffer, AudioStreamInfo streamInfo, std::string logTag,
52 int64_t &volumeDataCount, int64_t &muteFrameCnt, size_t split = 1);
53 static bool IsZeroVolume(float volume);
54 };
55 } // namespace AudioStandard
56 } // namespace OHOS
57 #endif // VOLUME_TOOLS_H