Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 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 | |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 17 | #define LOG_TAG "AudioGain" |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 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 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 27 | #include <algorithm> |
| 28 | |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 29 | #include <android-base/stringprintf.h> |
| 30 | #include <media/AudioGain.h> |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 31 | #include <utils/Log.h> |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 32 | |
| 33 | #include <math.h> |
| 34 | |
| 35 | namespace android { |
| 36 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 37 | AudioGain::AudioGain(int index, bool useInChannelMask) |
| 38 | { |
| 39 | mIndex = index; |
| 40 | mUseInChannelMask = useInChannelMask; |
| 41 | memset(&mGain, 0, sizeof(struct audio_gain)); |
| 42 | } |
| 43 | |
| 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 | |
| 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 | |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 103 | void AudioGain::dump(std::string *dst, int spaces, int index) const |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 104 | { |
jiabin | 9ccb28b | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 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)); |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 114 | } |
| 115 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 116 | 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 | |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 132 | 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 | |
| 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; |
Mikhail Naganov | 5577303 | 2020-10-01 15:08:13 -0700 | [diff] [blame] | 155 | 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); |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 161 | 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 | |
jiabin | 49e69a1 | 2019-10-15 16:04:13 -0700 | [diff] [blame] | 170 | bool 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 | |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 178 | status_t AudioGains::writeToParcel(android::Parcel *parcel) const { |
| 179 | status_t status = NO_ERROR; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 180 | if ((status = parcel->writeVectorSize(*this)) != NO_ERROR) return status; |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 181 | for (const auto &audioGain : *this) { |
| 182 | if ((status = parcel->writeParcelable(*audioGain)) != NO_ERROR) { |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | return status; |
| 187 | } |
| 188 | |
| 189 | status_t AudioGains::readFromParcel(const android::Parcel *parcel) { |
| 190 | status_t status = NO_ERROR; |
jiabin | 17058fa | 2019-10-08 17:33:38 -0700 | [diff] [blame] | 191 | 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) { |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 196 | this->clear(); |
| 197 | break; |
| 198 | } |
jiabin | 84e43c7 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 199 | } |
| 200 | return status; |
| 201 | } |
| 202 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 203 | } // namespace android |