1 /*
2 * Copyright (c) 2022 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 #include <chrono>
17 #include <sstream>
18 #include <ostream>
19 #include "audio_utils.h"
20 #include "audio_log.h"
21 #include "parameter.h"
22
23 namespace OHOS {
24 namespace AudioStandard {
GetNowTimeMs()25 int64_t GetNowTimeMs()
26 {
27 std::chrono::milliseconds nowMs =
28 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch());
29 return nowMs.count();
30 }
31
GetNowTimeUs()32 int64_t GetNowTimeUs()
33 {
34 std::chrono::microseconds nowUs =
35 std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch());
36 return nowUs.count();
37 }
38
AdjustStereoToMonoForPCM8Bit(int8_t * data,uint64_t len)39 void AdjustStereoToMonoForPCM8Bit(int8_t *data, uint64_t len)
40 {
41 for (unsigned i = len >> 1; i > 0; i--) {
42 // the number 2 is the count of stereo audio channels
43 data[0] = data[0] / 2 + data[1] / 2;
44 data[1] = data[0];
45 data += 2;
46 }
47 }
48
AdjustStereoToMonoForPCM16Bit(int16_t * data,uint64_t len)49 void AdjustStereoToMonoForPCM16Bit(int16_t *data, uint64_t len)
50 {
51 for (unsigned i = len >> 1; i > 0; i--) {
52 // the number 2 is the count of stereo audio channels
53 data[0] = data[0] / 2 + data[1] / 2;
54 data[1] = data[0];
55 data += 2;
56 }
57 }
58
AdjustStereoToMonoForPCM24Bit(int8_t * data,uint64_t len)59 void AdjustStereoToMonoForPCM24Bit(int8_t *data, uint64_t len)
60 {
61 // int8_t is used for reading data of PCM24BIT here
62 // 24 / 8 = 3, so we need repeat the calculation three times in each loop
63 for (unsigned i = len >> 1; i > 0; i--) {
64 // the number 2 is the count of stereo audio channels, 2 * 3 = 6
65 data[0] = data[0] / 2 + data[3] / 2;
66 data[3] = data[0];
67 data[1] = data[1] / 2 + data[4] / 2;
68 data[4] = data[1];
69 data[2] = data[2] / 2 + data[5] / 2;
70 data[5] = data[2];
71 data += 6;
72 }
73 }
74
AdjustStereoToMonoForPCM32Bit(int32_t * data,uint64_t len)75 void AdjustStereoToMonoForPCM32Bit(int32_t *data, uint64_t len)
76 {
77 for (unsigned i = len >> 1; i > 0; i--) {
78 // the number 2 is the count of stereo audio channels
79 data[0] = data[0] / 2 + data[1] / 2;
80 data[1] = data[0];
81 data += 2;
82 }
83 }
84
AdjustAudioBalanceForPCM8Bit(int8_t * data,uint64_t len,float left,float right)85 void AdjustAudioBalanceForPCM8Bit(int8_t *data, uint64_t len, float left, float right)
86 {
87 for (unsigned i = len >> 1; i > 0; i--) {
88 // the number 2 is the count of stereo audio channels
89 data[0] *= left;
90 data[1] *= right;
91 data += 2;
92 }
93 }
94
AdjustAudioBalanceForPCM16Bit(int16_t * data,uint64_t len,float left,float right)95 void AdjustAudioBalanceForPCM16Bit(int16_t *data, uint64_t len, float left, float right)
96 {
97 for (unsigned i = len >> 1; i > 0; i--) {
98 // the number 2 is the count of stereo audio channels
99 data[0] *= left;
100 data[1] *= right;
101 data += 2;
102 }
103 }
104
AdjustAudioBalanceForPCM24Bit(int8_t * data,uint64_t len,float left,float right)105 void AdjustAudioBalanceForPCM24Bit(int8_t *data, uint64_t len, float left, float right)
106 {
107 // int8_t is used for reading data of PCM24BIT here
108 // 24 / 8 = 3, so we need repeat the calculation three times in each loop
109 for (unsigned i = len >> 1; i > 0; i--) {
110 // the number 2 is the count of stereo audio channels, 2 * 3 = 6
111 data[0] *= left;
112 data[1] *= left;
113 data[2] *= left;
114 data[3] *= right;
115 data[4] *= right;
116 data[5] *= right;
117 data += 6;
118 }
119 }
120
AdjustAudioBalanceForPCM32Bit(int32_t * data,uint64_t len,float left,float right)121 void AdjustAudioBalanceForPCM32Bit(int32_t *data, uint64_t len, float left, float right)
122 {
123 for (unsigned i = len >> 1; i > 0; i--) {
124 // the number 2 is the count of stereo audio channels
125 data[0] *= left;
126 data[1] *= right;
127 data += 2;
128 }
129 }
130
131 template <typename T>
GetSysPara(const char * key,T & value)132 bool GetSysPara(const char *key, T &value)
133 {
134 if (key == nullptr) {
135 AUDIO_ERR_LOG("GetSysPara: key is nullptr");
136 return false;
137 }
138 char paraValue[20] = {0}; // 20 for system parameter
139 auto res = GetParameter(key, "-1", paraValue, sizeof(paraValue));
140 if (res <= 0) {
141 AUDIO_WARNING_LOG("GetSysPara fail, key:%{public}s res:%{public}d", key, res);
142 return false;
143 }
144 AUDIO_INFO_LOG("GetSysPara: key:%{public}s value:%{public}s", key, paraValue);
145 std::stringstream valueStr;
146 valueStr << paraValue;
147 valueStr >> value;
148 return true;
149 }
150
151 template bool GetSysPara(const char *key, int32_t &value);
152 template bool GetSysPara(const char *key, uint32_t &value);
153 template bool GetSysPara(const char *key, int64_t &value);
154 template bool GetSysPara(const char *key, std::string &value);
155 } // namespace AudioStandard
156 } // namespace OHOS
157