1 /*
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
17 #define LOG_TAG "AudioGain"
18 //#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
27 #include <algorithm>
28
29 #include <android-base/stringprintf.h>
30 #include <media/AudioGain.h>
31 #include <utils/Log.h>
32
33 #include <math.h>
34
35 namespace android {
36
AudioGain(int index,bool useInChannelMask)37 AudioGain::AudioGain(int index, bool useInChannelMask)
38 {
39 mIndex = index;
40 mUseInChannelMask = useInChannelMask;
41 memset(&mGain, 0, sizeof(struct audio_gain));
42 }
43
getDefaultConfig(struct audio_gain_config * config)44 void 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
checkConfig(const struct audio_gain_config * config)67 status_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
dump(std::string * dst,int spaces,int index) const103 void AudioGain::dump(std::string *dst, int spaces, int index) const
104 {
105 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));
114 }
115
equals(const sp<AudioGain> & other) const116 bool 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
writeToParcel(android::Parcel * parcel) const132 status_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
readFromParcel(const android::Parcel * parcel)149 status_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;
155 if ((status = parcel->readUint32(&mGain.mode)) != NO_ERROR) return status;
156 if ((status = parcel->readUint32(&mGain.channel_mask)) != NO_ERROR) return status;
157 if ((status = parcel->readInt32(&mGain.min_value)) != NO_ERROR) return status;
158 if ((status = parcel->readInt32(&mGain.max_value)) != NO_ERROR) return status;
159 if ((status = parcel->readInt32(&mGain.default_value)) != NO_ERROR) return status;
160 if ((status = parcel->readUint32(&mGain.step_value)) != NO_ERROR) return status;
161 if ((status = parcel->readUint32(&mGain.min_ramp_ms)) != NO_ERROR) return status;
162 status = parcel->readUint32(&mGain.max_ramp_ms);
163 return status;
164 }
165
equals(const AudioGains & other) const166 bool AudioGains::equals(const AudioGains &other) const
167 {
168 return std::equal(begin(), end(), other.begin(), other.end(),
169 [](const sp<AudioGain>& left, const sp<AudioGain>& right) {
170 return left->equals(right);
171 });
172 }
173
writeToParcel(android::Parcel * parcel) const174 status_t AudioGains::writeToParcel(android::Parcel *parcel) const {
175 status_t status = NO_ERROR;
176 if ((status = parcel->writeVectorSize(*this)) != NO_ERROR) return status;
177 for (const auto &audioGain : *this) {
178 if ((status = parcel->writeParcelable(*audioGain)) != NO_ERROR) {
179 break;
180 }
181 }
182 return status;
183 }
184
readFromParcel(const android::Parcel * parcel)185 status_t AudioGains::readFromParcel(const android::Parcel *parcel) {
186 status_t status = NO_ERROR;
187 this->clear();
188 if ((status = parcel->resizeOutVector(this)) != NO_ERROR) return status;
189 for (size_t i = 0; i < this->size(); i++) {
190 this->at(i) = new AudioGain(0, false);
191 if ((status = parcel->readParcelable(this->at(i).get())) != NO_ERROR) {
192 this->clear();
193 break;
194 }
195 }
196 return status;
197 }
198
199 } // namespace android
200