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 | 41502b2 | 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 | 41502b2 | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
| 28 | #include <media/AudioGain.h> |
François Gaffie | ad3183e | 2015-03-18 16:55:35 +0100 | [diff] [blame] | 29 | #include <utils/Log.h> |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 30 | |
| 31 | #include <math.h> |
| 32 | |
| 33 | namespace android { |
| 34 | |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 35 | AudioGain::AudioGain(int index, bool useInChannelMask) |
| 36 | { |
| 37 | mIndex = index; |
| 38 | mUseInChannelMask = useInChannelMask; |
| 39 | memset(&mGain, 0, sizeof(struct audio_gain)); |
| 40 | } |
| 41 | |
| 42 | void 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 | |
| 65 | status_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 | |
jiabin | 41502b2 | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 101 | void AudioGain::dump(std::string *dst, int spaces, int index) const |
Jean-Michel Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 102 | { |
jiabin | 41502b2 | 2019-07-31 09:59:09 -0700 | [diff] [blame] | 103 | 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 Trivi | 56ec4ff | 2015-01-23 16:45:18 -0800 | [diff] [blame] | 112 | } |
| 113 | |
jiabin | 4ad3d03 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 114 | status_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 | |
| 131 | status_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 | |
| 148 | status_t AudioGains::writeToParcel(android::Parcel *parcel) const { |
| 149 | status_t status = NO_ERROR; |
jiabin | d19519a | 2019-10-08 17:33:38 -0700 | [diff] [blame^] | 150 | if ((status = parcel->writeVectorSize(*this)) != NO_ERROR) return status; |
jiabin | 4ad3d03 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 151 | for (const auto &audioGain : *this) { |
| 152 | if ((status = parcel->writeParcelable(*audioGain)) != NO_ERROR) { |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | return status; |
| 157 | } |
| 158 | |
| 159 | status_t AudioGains::readFromParcel(const android::Parcel *parcel) { |
| 160 | status_t status = NO_ERROR; |
jiabin | d19519a | 2019-10-08 17:33:38 -0700 | [diff] [blame^] | 161 | this->clear(); |
| 162 | if ((status = parcel->resizeOutVector(this)) != NO_ERROR) return status; |
| 163 | for (size_t i = 0; i < this->size(); i++) { |
| 164 | this->at(i) = new AudioGain(0, false); |
| 165 | if ((status = parcel->readParcelable(this->at(i).get())) != NO_ERROR) { |
jiabin | 4ad3d03 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 166 | this->clear(); |
| 167 | break; |
| 168 | } |
jiabin | 4ad3d03 | 2019-07-31 17:23:17 -0700 | [diff] [blame] | 169 | } |
| 170 | return status; |
| 171 | } |
| 172 | |
Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 173 | } // namespace android |