• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <math.h>
28 
29 #include <algorithm>
30 
31 #include <android-base/stringprintf.h>
32 #include <media/AudioGain.h>
33 #include <utils/Log.h>
34 
35 namespace android {
36 
AudioGain(int index,bool isInput)37 AudioGain::AudioGain(int index, bool isInput)
38         : mIndex(index), mIsInput(isInput) {}
39 
getDefaultConfig(struct audio_gain_config * config)40 void AudioGain::getDefaultConfig(struct audio_gain_config *config)
41 {
42     config->index = mIndex;
43     config->mode = mGain.mode;
44     config->channel_mask = mGain.channel_mask;
45     if ((mGain.mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
46         config->values[0] = mGain.default_value;
47     } else {
48         const uint32_t numValues = mIsInput ?
49                 audio_channel_count_from_in_mask(mGain.channel_mask) :
50                 audio_channel_count_from_out_mask(mGain.channel_mask);
51         for (size_t i = 0; i < numValues; i++) {
52             config->values[i] = mGain.default_value;
53         }
54     }
55     if ((mGain.mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
56         config->ramp_duration_ms = mGain.min_ramp_ms;
57     }
58 }
59 
checkConfig(const struct audio_gain_config * config)60 status_t AudioGain::checkConfig(const struct audio_gain_config *config)
61 {
62     if ((config->mode & ~mGain.mode) != 0) {
63         return BAD_VALUE;
64     }
65     if ((config->mode & AUDIO_GAIN_MODE_JOINT) == AUDIO_GAIN_MODE_JOINT) {
66         if ((config->values[0] < mGain.min_value) ||
67                     (config->values[0] > mGain.max_value)) {
68             return BAD_VALUE;
69         }
70     } else {
71         if ((config->channel_mask & ~mGain.channel_mask) != 0) {
72             return BAD_VALUE;
73         }
74         const uint32_t numValues = mIsInput ?
75                 audio_channel_count_from_in_mask(config->channel_mask) :
76                 audio_channel_count_from_out_mask(config->channel_mask);
77         for (size_t i = 0; i < numValues; i++) {
78             if ((config->values[i] < mGain.min_value) ||
79                     (config->values[i] > mGain.max_value)) {
80                 return BAD_VALUE;
81             }
82         }
83     }
84     if ((config->mode & AUDIO_GAIN_MODE_RAMP) == AUDIO_GAIN_MODE_RAMP) {
85         if ((config->ramp_duration_ms < mGain.min_ramp_ms) ||
86                     (config->ramp_duration_ms > mGain.max_ramp_ms)) {
87             return BAD_VALUE;
88         }
89     }
90     return NO_ERROR;
91 }
92 
dump(std::string * dst,int spaces,int index) const93 void AudioGain::dump(std::string *dst, int spaces, int index) const
94 {
95     dst->append(base::StringPrintf("%*sGain %d:\n", spaces, "", index+1));
96     dst->append(base::StringPrintf("%*s- mode: %08x\n", spaces, "", mGain.mode));
97     dst->append(base::StringPrintf("%*s- channel_mask: %08x\n", spaces, "", mGain.channel_mask));
98     dst->append(base::StringPrintf("%*s- min_value: %d mB\n", spaces, "", mGain.min_value));
99     dst->append(base::StringPrintf("%*s- max_value: %d mB\n", spaces, "", mGain.max_value));
100     dst->append(base::StringPrintf("%*s- default_value: %d mB\n", spaces, "", mGain.default_value));
101     dst->append(base::StringPrintf("%*s- step_value: %d mB\n", spaces, "", mGain.step_value));
102     dst->append(base::StringPrintf("%*s- min_ramp_ms: %d ms\n", spaces, "", mGain.min_ramp_ms));
103     dst->append(base::StringPrintf("%*s- max_ramp_ms: %d ms\n", spaces, "", mGain.max_ramp_ms));
104 }
105 
equals(const sp<AudioGain> & other) const106 bool AudioGain::equals(const sp<AudioGain>& other) const
107 {
108     return other != nullptr &&
109            mIsInput == other->mIsInput &&
110            mUseForVolume == other->mUseForVolume &&
111            // Compare audio gain
112            mGain.mode == other->mGain.mode &&
113            mGain.channel_mask == other->mGain.channel_mask &&
114            mGain.min_value == other->mGain.min_value &&
115            mGain.max_value == other->mGain.max_value &&
116            mGain.default_value == other->mGain.default_value &&
117            mGain.step_value == other->mGain.step_value &&
118            mGain.min_ramp_ms == other->mGain.min_ramp_ms &&
119            mGain.max_ramp_ms == other->mGain.max_ramp_ms;
120 }
121 
toParcelable() const122 ConversionResult<AudioGain::Aidl> AudioGain::toParcelable() const {
123     media::audio::common::AudioGain aidl = VALUE_OR_RETURN(
124             legacy2aidl_audio_gain_AudioGain(mGain, mIsInput));
125     aidl.useForVolume = mUseForVolume;
126     media::AudioGainSys aidlSys;
127     aidlSys.index = VALUE_OR_RETURN(convertIntegral<int32_t>(mIndex));
128     aidlSys.isInput = mIsInput;
129     return std::make_pair(aidl, aidlSys);
130 }
131 
fromParcelable(const AudioGain::Aidl & aidl)132 ConversionResult<sp<AudioGain>> AudioGain::fromParcelable(const AudioGain::Aidl& aidl) {
133     const media::audio::common::AudioGain& hal = aidl.first;
134     const media::AudioGainSys& sys = aidl.second;
135     auto index = VALUE_OR_RETURN(convertIntegral<int>(sys.index));
136     sp<AudioGain> legacy = sp<AudioGain>::make(index, sys.isInput);
137     legacy->mGain = VALUE_OR_RETURN(aidl2legacy_AudioGain_audio_gain(hal, sys.isInput));
138     legacy->mUseForVolume = hal.useForVolume;
139     return legacy;
140 }
141 
equals(const AudioGains & other) const142 bool AudioGains::equals(const AudioGains &other) const
143 {
144     return std::equal(begin(), end(), other.begin(), other.end(),
145                       [](const sp<AudioGain>& left, const sp<AudioGain>& right) {
146                           return left->equals(right);
147                       });
148 }
149 
150 ConversionResult<sp<AudioGain>>
aidl2legacy_AudioGain(const AudioGain::Aidl & aidl)151 aidl2legacy_AudioGain(const AudioGain::Aidl& aidl) {
152     return AudioGain::fromParcelable(aidl);
153 }
154 
155 ConversionResult<AudioGain::Aidl>
legacy2aidl_AudioGain(const sp<AudioGain> & legacy)156 legacy2aidl_AudioGain(const sp<AudioGain>& legacy) {
157     return legacy->toParcelable();
158 }
159 
160 ConversionResult<AudioGains>
aidl2legacy_AudioGains(const AudioGains::Aidl & aidl)161 aidl2legacy_AudioGains(const AudioGains::Aidl& aidl) {
162     return convertContainers<AudioGains>(aidl.first, aidl.second,
163             [](const media::audio::common::AudioGain& g,
164                const media::AudioGainSys& gs) {
165                 return aidl2legacy_AudioGain(std::make_pair(g, gs));
166             });
167 }
168 
169 ConversionResult<AudioGains::Aidl>
legacy2aidl_AudioGains(const AudioGains & legacy)170 legacy2aidl_AudioGains(const AudioGains& legacy) {
171     return convertContainerSplit<
172             std::vector<media::audio::common::AudioGain>,
173             std::vector<media::AudioGainSys>>(legacy, legacy2aidl_AudioGain);
174 }
175 
176 } // namespace android
177