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