blob: 759140ef01e2223d573fb8747ee5d29753ae81bb [file] [log] [blame]
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
jiabin41502b22019-07-31 09:59:09 -070017#define LOG_TAG "AudioGain"
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080018//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
jiabin0da24a32019-10-15 16:04:13 -070027#include <algorithm>
28
jiabin41502b22019-07-31 09:59:09 -070029#include <android-base/stringprintf.h>
30#include <media/AudioGain.h>
François Gaffiead3183e2015-03-18 16:55:35 +010031#include <utils/Log.h>
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080032
33#include <math.h>
34
35namespace android {
36
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080037AudioGain::AudioGain(int index, bool useInChannelMask)
38{
39 mIndex = index;
40 mUseInChannelMask = useInChannelMask;
41 memset(&mGain, 0, sizeof(struct audio_gain));
42}
43
44void AudioGain::getDefaultConfig(struct audio_gain_config *config)
45{
46 config->index = mIndex;
47 config->mode = mGain.mode;
48 config->channel_mask = mGain.channel_mask;
49 if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
50 config->values[0] = mGain.default_value;
51 } else {
52 uint32_t numValues;
53 if (mUseInChannelMask) {
54 numValues = audio_channel_count_from_in_mask(mGain.channel_mask);
55 } else {
56 numValues = audio_channel_count_from_out_mask(mGain.channel_mask);
57 }
58 for (size_t i = 0; i < numValues; i++) {
59 config->values[i] = mGain.default_value;
60 }
61 }
62 if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
63 config->ramp_duration_ms = mGain.min_ramp_ms;
64 }
65}
66
67status_t AudioGain::checkConfig(const struct audio_gain_config *config)
68{
69 if ((config->mode & ~mGain.mode) != 0) {
70 return BAD_VALUE;
71 }
72 if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
73 if ((config->values[0] < mGain.min_value) ||
74 (config->values[0] > mGain.max_value)) {
75 return BAD_VALUE;
76 }
77 } else {
78 if ((config->channel_mask & ~mGain.channel_mask) != 0) {
79 return BAD_VALUE;
80 }
81 uint32_t numValues;
82 if (mUseInChannelMask) {
83 numValues = audio_channel_count_from_in_mask(config->channel_mask);
84 } else {
85 numValues = audio_channel_count_from_out_mask(config->channel_mask);
86 }
87 for (size_t i = 0; i < numValues; i++) {
88 if ((config->values[i] < mGain.min_value) ||
89 (config->values[i] > mGain.max_value)) {
90 return BAD_VALUE;
91 }
92 }
93 }
94 if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
95 if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
96 (config->ramp_duration_ms > mGain.max_ramp_ms)) {
97 return BAD_VALUE;
98 }
99 }
100 return NO_ERROR;
101}
102
jiabin41502b22019-07-31 09:59:09 -0700103void AudioGain::dump(std::string *dst, int spaces, int index) const
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800104{
jiabin41502b22019-07-31 09:59:09 -0700105 dst->append(base::StringPrintf("%*sGain %d:\n", spaces, "", index+1));
106 dst->append(base::StringPrintf("%*s- mode: %08x\n", spaces, "", mGain.mode));
107 dst->append(base::StringPrintf("%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask));
108 dst->append(base::StringPrintf("%*s- min_value: %d mB\n", spaces, "", mGain.min_value));
109 dst->append(base::StringPrintf("%*s- max_value: %d mB\n", spaces, "", mGain.max_value));
110 dst->append(base::StringPrintf("%*s- default_value: %d mB\n", spaces, "", mGain.default_value));
111 dst->append(base::StringPrintf("%*s- step_value: %d mB\n", spaces, "", mGain.step_value));
112 dst->append(base::StringPrintf("%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms));
113 dst->append(base::StringPrintf("%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms));
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800114}
115
jiabin0da24a32019-10-15 16:04:13 -0700116bool AudioGain::equals(const sp<AudioGain>& other) const
117{
118 return other != nullptr &&
119 mUseInChannelMask == other->mUseInChannelMask &&
120 mUseForVolume == other->mUseForVolume &&
121 // Compare audio gain
122 mGain.mode == other->mGain.mode &&
123 mGain.channel_mask == other->mGain.channel_mask &&
124 mGain.min_value == other->mGain.min_value &&
125 mGain.max_value == other->mGain.max_value &&
126 mGain.default_value == other->mGain.default_value &&
127 mGain.step_value == other->mGain.step_value &&
128 mGain.min_ramp_ms == other->mGain.min_ramp_ms &&
129 mGain.max_ramp_ms == other->mGain.max_ramp_ms;
130}
131
jiabin4ad3d032019-07-31 17:23:17 -0700132status_t AudioGain::writeToParcel(android::Parcel *parcel) const
133{
134 status_t status = NO_ERROR;
135 if ((status = parcel->writeInt32(mIndex)) != NO_ERROR) return status;
136 if ((status = parcel->writeBool(mUseInChannelMask)) != NO_ERROR) return status;
137 if ((status = parcel->writeBool(mUseForVolume)) != NO_ERROR) return status;
138 if ((status = parcel->writeUint32(mGain.mode)) != NO_ERROR) return status;
139 if ((status = parcel->writeUint32(mGain.channel_mask)) != NO_ERROR) return status;
140 if ((status = parcel->writeInt32(mGain.min_value)) != NO_ERROR) return status;
141 if ((status = parcel->writeInt32(mGain.max_value)) != NO_ERROR) return status;
142 if ((status = parcel->writeInt32(mGain.default_value)) != NO_ERROR) return status;
143 if ((status = parcel->writeUint32(mGain.step_value)) != NO_ERROR) return status;
144 if ((status = parcel->writeUint32(mGain.min_ramp_ms)) != NO_ERROR) return status;
145 status = parcel->writeUint32(mGain.max_ramp_ms);
146 return status;
147}
148
149status_t AudioGain::readFromParcel(const android::Parcel *parcel)
150{
151 status_t status = NO_ERROR;
152 if ((status = parcel->readInt32(&mIndex)) != NO_ERROR) return status;
153 if ((status = parcel->readBool(&mUseInChannelMask)) != NO_ERROR) return status;
154 if ((status = parcel->readBool(&mUseForVolume)) != NO_ERROR) return status;
Mikhail Naganove3b59ac2020-10-01 15:08:13 -0700155 uint32_t rawGainMode;
156 if ((status = parcel->readUint32(&rawGainMode)) != NO_ERROR) return status;
157 mGain.mode = static_cast<audio_gain_mode_t>(rawGainMode);
158 uint32_t rawChannelMask;
159 if ((status = parcel->readUint32(&rawChannelMask)) != NO_ERROR) return status;
160 mGain.channel_mask = static_cast<audio_channel_mask_t>(rawChannelMask);
jiabin4ad3d032019-07-31 17:23:17 -0700161 if ((status = parcel->readInt32(&mGain.min_value)) != NO_ERROR) return status;
162 if ((status = parcel->readInt32(&mGain.max_value)) != NO_ERROR) return status;
163 if ((status = parcel->readInt32(&mGain.default_value)) != NO_ERROR) return status;
164 if ((status = parcel->readUint32(&mGain.step_value)) != NO_ERROR) return status;
165 if ((status = parcel->readUint32(&mGain.min_ramp_ms)) != NO_ERROR) return status;
166 status = parcel->readUint32(&mGain.max_ramp_ms);
167 return status;
168}
169
jiabin0da24a32019-10-15 16:04:13 -0700170bool AudioGains::equals(const AudioGains &other) const
171{
172 return std::equal(begin(), end(), other.begin(), other.end(),
173 [](const sp<AudioGain>& left, const sp<AudioGain>& right) {
174 return left->equals(right);
175 });
176}
177
jiabin4ad3d032019-07-31 17:23:17 -0700178status_t AudioGains::writeToParcel(android::Parcel *parcel) const {
179 status_t status = NO_ERROR;
jiabind19519a2019-10-08 17:33:38 -0700180 if ((status = parcel->writeVectorSize(*this)) != NO_ERROR) return status;
jiabin4ad3d032019-07-31 17:23:17 -0700181 for (const auto &audioGain : *this) {
182 if ((status = parcel->writeParcelable(*audioGain)) != NO_ERROR) {
183 break;
184 }
185 }
186 return status;
187}
188
189status_t AudioGains::readFromParcel(const android::Parcel *parcel) {
190 status_t status = NO_ERROR;
jiabind19519a2019-10-08 17:33:38 -0700191 this->clear();
192 if ((status = parcel->resizeOutVector(this)) != NO_ERROR) return status;
193 for (size_t i = 0; i < this->size(); i++) {
194 this->at(i) = new AudioGain(0, false);
195 if ((status = parcel->readParcelable(this->at(i).get())) != NO_ERROR) {
jiabin4ad3d032019-07-31 17:23:17 -0700196 this->clear();
197 break;
198 }
jiabin4ad3d032019-07-31 17:23:17 -0700199 }
200 return status;
201}
202
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800203} // namespace android