blob: 9d1d6db2c1fe19cb2f0627d1f4f3d4b673400a5b [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
jiabin41502b22019-07-31 09:59:09 -070027#include <android-base/stringprintf.h>
28#include <media/AudioGain.h>
François Gaffiead3183e2015-03-18 16:55:35 +010029#include <utils/Log.h>
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080030
31#include <math.h>
32
33namespace android {
34
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -080035AudioGain::AudioGain(int index, bool useInChannelMask)
36{
37 mIndex = index;
38 mUseInChannelMask = useInChannelMask;
39 memset(&mGain, 0, sizeof(struct audio_gain));
40}
41
42void AudioGain::getDefaultConfig(struct audio_gain_config *config)
43{
44 config->index = mIndex;
45 config->mode = mGain.mode;
46 config->channel_mask = mGain.channel_mask;
47 if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
48 config->values[0] = mGain.default_value;
49 } else {
50 uint32_t numValues;
51 if (mUseInChannelMask) {
52 numValues = audio_channel_count_from_in_mask(mGain.channel_mask);
53 } else {
54 numValues = audio_channel_count_from_out_mask(mGain.channel_mask);
55 }
56 for (size_t i = 0; i < numValues; i++) {
57 config->values[i] = mGain.default_value;
58 }
59 }
60 if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
61 config->ramp_duration_ms = mGain.min_ramp_ms;
62 }
63}
64
65status_t AudioGain::checkConfig(const struct audio_gain_config *config)
66{
67 if ((config->mode & ~mGain.mode) != 0) {
68 return BAD_VALUE;
69 }
70 if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
71 if ((config->values[0] < mGain.min_value) ||
72 (config->values[0] > mGain.max_value)) {
73 return BAD_VALUE;
74 }
75 } else {
76 if ((config->channel_mask & ~mGain.channel_mask) != 0) {
77 return BAD_VALUE;
78 }
79 uint32_t numValues;
80 if (mUseInChannelMask) {
81 numValues = audio_channel_count_from_in_mask(config->channel_mask);
82 } else {
83 numValues = audio_channel_count_from_out_mask(config->channel_mask);
84 }
85 for (size_t i = 0; i < numValues; i++) {
86 if ((config->values[i] < mGain.min_value) ||
87 (config->values[i] > mGain.max_value)) {
88 return BAD_VALUE;
89 }
90 }
91 }
92 if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
93 if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
94 (config->ramp_duration_ms > mGain.max_ramp_ms)) {
95 return BAD_VALUE;
96 }
97 }
98 return NO_ERROR;
99}
100
jiabin41502b22019-07-31 09:59:09 -0700101void AudioGain::dump(std::string *dst, int spaces, int index) const
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800102{
jiabin41502b22019-07-31 09:59:09 -0700103 dst->append(base::StringPrintf("%*sGain %d:\n", spaces, "", index+1));
104 dst->append(base::StringPrintf("%*s- mode: %08x\n", spaces, "", mGain.mode));
105 dst->append(base::StringPrintf("%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask));
106 dst->append(base::StringPrintf("%*s- min_value: %d mB\n", spaces, "", mGain.min_value));
107 dst->append(base::StringPrintf("%*s- max_value: %d mB\n", spaces, "", mGain.max_value));
108 dst->append(base::StringPrintf("%*s- default_value: %d mB\n", spaces, "", mGain.default_value));
109 dst->append(base::StringPrintf("%*s- step_value: %d mB\n", spaces, "", mGain.step_value));
110 dst->append(base::StringPrintf("%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms));
111 dst->append(base::StringPrintf("%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms));
Jean-Michel Trivi56ec4ff2015-01-23 16:45:18 -0800112}
113
jiabin4ad3d032019-07-31 17:23:17 -0700114status_t AudioGain::writeToParcel(android::Parcel *parcel) const
115{
116 status_t status = NO_ERROR;
117 if ((status = parcel->writeInt32(mIndex)) != NO_ERROR) return status;
118 if ((status = parcel->writeBool(mUseInChannelMask)) != NO_ERROR) return status;
119 if ((status = parcel->writeBool(mUseForVolume)) != NO_ERROR) return status;
120 if ((status = parcel->writeUint32(mGain.mode)) != NO_ERROR) return status;
121 if ((status = parcel->writeUint32(mGain.channel_mask)) != NO_ERROR) return status;
122 if ((status = parcel->writeInt32(mGain.min_value)) != NO_ERROR) return status;
123 if ((status = parcel->writeInt32(mGain.max_value)) != NO_ERROR) return status;
124 if ((status = parcel->writeInt32(mGain.default_value)) != NO_ERROR) return status;
125 if ((status = parcel->writeUint32(mGain.step_value)) != NO_ERROR) return status;
126 if ((status = parcel->writeUint32(mGain.min_ramp_ms)) != NO_ERROR) return status;
127 status = parcel->writeUint32(mGain.max_ramp_ms);
128 return status;
129}
130
131status_t AudioGain::readFromParcel(const android::Parcel *parcel)
132{
133 status_t status = NO_ERROR;
134 if ((status = parcel->readInt32(&mIndex)) != NO_ERROR) return status;
135 if ((status = parcel->readBool(&mUseInChannelMask)) != NO_ERROR) return status;
136 if ((status = parcel->readBool(&mUseForVolume)) != NO_ERROR) return status;
137 if ((status = parcel->readUint32(&mGain.mode)) != NO_ERROR) return status;
138 if ((status = parcel->readUint32(&mGain.channel_mask)) != NO_ERROR) return status;
139 if ((status = parcel->readInt32(&mGain.min_value)) != NO_ERROR) return status;
140 if ((status = parcel->readInt32(&mGain.max_value)) != NO_ERROR) return status;
141 if ((status = parcel->readInt32(&mGain.default_value)) != NO_ERROR) return status;
142 if ((status = parcel->readUint32(&mGain.step_value)) != NO_ERROR) return status;
143 if ((status = parcel->readUint32(&mGain.min_ramp_ms)) != NO_ERROR) return status;
144 status = parcel->readUint32(&mGain.max_ramp_ms);
145 return status;
146}
147
148status_t AudioGains::writeToParcel(android::Parcel *parcel) const {
149 status_t status = NO_ERROR;
150 if ((status = parcel->writeUint64(this->size())) != NO_ERROR) return status;
151 for (const auto &audioGain : *this) {
152 if ((status = parcel->writeParcelable(*audioGain)) != NO_ERROR) {
153 break;
154 }
155 }
156 return status;
157}
158
159status_t AudioGains::readFromParcel(const android::Parcel *parcel) {
160 status_t status = NO_ERROR;
161 uint64_t count;
162 if ((status = parcel->readUint64(&count)) != NO_ERROR) return status;
163 for (uint64_t i = 0; i < count; i++) {
164 sp<AudioGain> audioGain = new AudioGain(0, false);
165 if ((status = parcel->readParcelable(audioGain.get())) != NO_ERROR) {
166 this->clear();
167 break;
168 }
169 this->push_back(audioGain);
170 }
171 return status;
172}
173
Mikhail Naganov1b2a7942017-12-08 10:18:09 -0800174} // namespace android