• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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 #include <memory.h>
18 
19 #define LOG_TAG "Equalizer_HAL"
20 #include <android/log.h>
21 
22 #include "EqualizerEffect.h"
23 
24 namespace android {
25 namespace hardware {
26 namespace audio {
27 namespace effect {
28 namespace V2_0 {
29 namespace implementation {
30 
EqualizerEffect(effect_handle_t handle)31 EqualizerEffect::EqualizerEffect(effect_handle_t handle)
32         : mEffect(new Effect(handle)) {
33 }
34 
~EqualizerEffect()35 EqualizerEffect::~EqualizerEffect() {}
36 
propertiesFromHal(const t_equalizer_settings & halProperties,IEqualizerEffect::AllProperties * properties)37 void EqualizerEffect::propertiesFromHal(
38         const t_equalizer_settings& halProperties,
39         IEqualizerEffect::AllProperties* properties) {
40     properties->curPreset = halProperties.curPreset;
41     // t_equalizer_settings incorrectly defines bandLevels as uint16_t,
42     // whereas the actual type of values used by effects is int16_t.
43     const int16_t* signedBandLevels =
44             reinterpret_cast<const int16_t*>(&halProperties.bandLevels[0]);
45     properties->bandLevels.setToExternal(
46             const_cast<int16_t*>(signedBandLevels), halProperties.numBands);
47 }
48 
propertiesToHal(const IEqualizerEffect::AllProperties & properties,t_equalizer_settings ** halProperties)49 std::vector<uint8_t> EqualizerEffect::propertiesToHal(
50         const IEqualizerEffect::AllProperties& properties,
51         t_equalizer_settings** halProperties) {
52     size_t bandsSize = properties.bandLevels.size() * sizeof(uint16_t);
53     std::vector<uint8_t> halBuffer(sizeof(t_equalizer_settings) + bandsSize, 0);
54     *halProperties = reinterpret_cast<t_equalizer_settings*>(&halBuffer[0]);
55     (*halProperties)->curPreset = properties.curPreset;
56     (*halProperties)->numBands = properties.bandLevels.size();
57     memcpy((*halProperties)->bandLevels, &properties.bandLevels[0], bandsSize);
58     return halBuffer;
59 }
60 
61 // Methods from ::android::hardware::audio::effect::V2_0::IEffect follow.
init()62 Return<Result> EqualizerEffect::init() {
63     return mEffect->init();
64 }
65 
setConfig(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)66 Return<Result> EqualizerEffect::setConfig(
67         const EffectConfig& config,
68         const sp<IEffectBufferProviderCallback>& inputBufferProvider,
69         const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
70     return mEffect->setConfig(config, inputBufferProvider, outputBufferProvider);
71 }
72 
reset()73 Return<Result> EqualizerEffect::reset() {
74     return mEffect->reset();
75 }
76 
enable()77 Return<Result> EqualizerEffect::enable() {
78     return mEffect->enable();
79 }
80 
disable()81 Return<Result> EqualizerEffect::disable() {
82     return mEffect->disable();
83 }
84 
setDevice(AudioDevice device)85 Return<Result> EqualizerEffect::setDevice(AudioDevice device) {
86     return mEffect->setDevice(device);
87 }
88 
setAndGetVolume(const hidl_vec<uint32_t> & volumes,setAndGetVolume_cb _hidl_cb)89 Return<void> EqualizerEffect::setAndGetVolume(
90         const hidl_vec<uint32_t>& volumes, setAndGetVolume_cb _hidl_cb) {
91     return mEffect->setAndGetVolume(volumes, _hidl_cb);
92 }
93 
volumeChangeNotification(const hidl_vec<uint32_t> & volumes)94 Return<Result> EqualizerEffect::volumeChangeNotification(
95         const hidl_vec<uint32_t>& volumes) {
96     return mEffect->volumeChangeNotification(volumes);
97 }
98 
setAudioMode(AudioMode mode)99 Return<Result> EqualizerEffect::setAudioMode(AudioMode mode) {
100     return mEffect->setAudioMode(mode);
101 }
102 
setConfigReverse(const EffectConfig & config,const sp<IEffectBufferProviderCallback> & inputBufferProvider,const sp<IEffectBufferProviderCallback> & outputBufferProvider)103 Return<Result> EqualizerEffect::setConfigReverse(
104         const EffectConfig& config,
105         const sp<IEffectBufferProviderCallback>& inputBufferProvider,
106         const sp<IEffectBufferProviderCallback>& outputBufferProvider) {
107     return mEffect->setConfigReverse(config, inputBufferProvider, outputBufferProvider);
108 }
109 
setInputDevice(AudioDevice device)110 Return<Result> EqualizerEffect::setInputDevice(AudioDevice device) {
111     return mEffect->setInputDevice(device);
112 }
113 
getConfig(getConfig_cb _hidl_cb)114 Return<void> EqualizerEffect::getConfig(getConfig_cb _hidl_cb) {
115     return mEffect->getConfig(_hidl_cb);
116 }
117 
getConfigReverse(getConfigReverse_cb _hidl_cb)118 Return<void> EqualizerEffect::getConfigReverse(getConfigReverse_cb _hidl_cb) {
119     return mEffect->getConfigReverse(_hidl_cb);
120 }
121 
getSupportedAuxChannelsConfigs(uint32_t maxConfigs,getSupportedAuxChannelsConfigs_cb _hidl_cb)122 Return<void> EqualizerEffect::getSupportedAuxChannelsConfigs(
123         uint32_t maxConfigs, getSupportedAuxChannelsConfigs_cb _hidl_cb) {
124     return mEffect->getSupportedAuxChannelsConfigs(maxConfigs, _hidl_cb);
125 }
126 
getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb)127 Return<void> EqualizerEffect::getAuxChannelsConfig(getAuxChannelsConfig_cb _hidl_cb) {
128     return mEffect->getAuxChannelsConfig(_hidl_cb);
129 }
130 
setAuxChannelsConfig(const EffectAuxChannelsConfig & config)131 Return<Result> EqualizerEffect::setAuxChannelsConfig(
132         const EffectAuxChannelsConfig& config) {
133     return mEffect->setAuxChannelsConfig(config);
134 }
135 
setAudioSource(AudioSource source)136 Return<Result> EqualizerEffect::setAudioSource(AudioSource source) {
137     return mEffect->setAudioSource(source);
138 }
139 
offload(const EffectOffloadParameter & param)140 Return<Result> EqualizerEffect::offload(const EffectOffloadParameter& param) {
141     return mEffect->offload(param);
142 }
143 
getDescriptor(getDescriptor_cb _hidl_cb)144 Return<void> EqualizerEffect::getDescriptor(getDescriptor_cb _hidl_cb) {
145     return mEffect->getDescriptor(_hidl_cb);
146 }
147 
prepareForProcessing(prepareForProcessing_cb _hidl_cb)148 Return<void> EqualizerEffect::prepareForProcessing(
149         prepareForProcessing_cb _hidl_cb) {
150     return mEffect->prepareForProcessing(_hidl_cb);
151 }
152 
setProcessBuffers(const AudioBuffer & inBuffer,const AudioBuffer & outBuffer)153 Return<Result> EqualizerEffect::setProcessBuffers(
154         const AudioBuffer& inBuffer, const AudioBuffer& outBuffer) {
155     return mEffect->setProcessBuffers(inBuffer, outBuffer);
156 }
157 
command(uint32_t commandId,const hidl_vec<uint8_t> & data,uint32_t resultMaxSize,command_cb _hidl_cb)158 Return<void> EqualizerEffect::command(
159         uint32_t commandId,
160         const hidl_vec<uint8_t>& data,
161         uint32_t resultMaxSize,
162         command_cb _hidl_cb) {
163     return mEffect->command(commandId, data, resultMaxSize, _hidl_cb);
164 }
165 
setParameter(const hidl_vec<uint8_t> & parameter,const hidl_vec<uint8_t> & value)166 Return<Result> EqualizerEffect::setParameter(
167         const hidl_vec<uint8_t>& parameter, const hidl_vec<uint8_t>& value) {
168     return mEffect->setParameter(parameter, value);
169 }
170 
getParameter(const hidl_vec<uint8_t> & parameter,uint32_t valueMaxSize,getParameter_cb _hidl_cb)171 Return<void> EqualizerEffect::getParameter(
172         const hidl_vec<uint8_t>& parameter,
173         uint32_t valueMaxSize,
174         getParameter_cb _hidl_cb) {
175     return mEffect->getParameter(parameter, valueMaxSize, _hidl_cb);
176 }
177 
getSupportedConfigsForFeature(uint32_t featureId,uint32_t maxConfigs,uint32_t configSize,getSupportedConfigsForFeature_cb _hidl_cb)178 Return<void> EqualizerEffect::getSupportedConfigsForFeature(
179         uint32_t featureId,
180         uint32_t maxConfigs,
181         uint32_t configSize,
182         getSupportedConfigsForFeature_cb _hidl_cb) {
183     return mEffect->getSupportedConfigsForFeature(featureId, maxConfigs, configSize, _hidl_cb);
184 }
185 
getCurrentConfigForFeature(uint32_t featureId,uint32_t configSize,getCurrentConfigForFeature_cb _hidl_cb)186 Return<void> EqualizerEffect::getCurrentConfigForFeature(
187         uint32_t featureId,
188         uint32_t configSize,
189         getCurrentConfigForFeature_cb _hidl_cb) {
190     return mEffect->getCurrentConfigForFeature(featureId, configSize, _hidl_cb);
191 }
192 
setCurrentConfigForFeature(uint32_t featureId,const hidl_vec<uint8_t> & configData)193 Return<Result> EqualizerEffect::setCurrentConfigForFeature(
194         uint32_t featureId, const hidl_vec<uint8_t>& configData) {
195     return mEffect->setCurrentConfigForFeature(featureId, configData);
196 }
197 
close()198 Return<Result> EqualizerEffect::close() {
199     return mEffect->close();
200 }
201 
202 // Methods from ::android::hardware::audio::effect::V2_0::IEqualizerEffect follow.
getNumBands(getNumBands_cb _hidl_cb)203 Return<void> EqualizerEffect::getNumBands(getNumBands_cb _hidl_cb)  {
204     return mEffect->getIntegerParam(EQ_PARAM_NUM_BANDS, _hidl_cb);
205 }
206 
getLevelRange(getLevelRange_cb _hidl_cb)207 Return<void> EqualizerEffect::getLevelRange(getLevelRange_cb _hidl_cb)  {
208     int16_t halLevels[2] = { 0, 0 };
209     Result retval = mEffect->getParam(EQ_PARAM_LEVEL_RANGE, halLevels);
210     _hidl_cb(retval, halLevels[0], halLevels[1]);
211     return Void();
212 }
213 
setBandLevel(uint16_t band,int16_t level)214 Return<Result> EqualizerEffect::setBandLevel(uint16_t band, int16_t level)  {
215     return mEffect->setParam(EQ_PARAM_BAND_LEVEL, band, level);
216 }
217 
getBandLevel(uint16_t band,getBandLevel_cb _hidl_cb)218 Return<void> EqualizerEffect::getBandLevel(uint16_t band, getBandLevel_cb _hidl_cb)  {
219     int16_t halLevel = 0;
220     Result retval = mEffect->getParam(EQ_PARAM_BAND_LEVEL, band, halLevel);
221     _hidl_cb(retval, halLevel);
222     return Void();
223 }
224 
getBandCenterFrequency(uint16_t band,getBandCenterFrequency_cb _hidl_cb)225 Return<void> EqualizerEffect::getBandCenterFrequency(
226         uint16_t band, getBandCenterFrequency_cb _hidl_cb)  {
227     uint32_t halFreq = 0;
228     Result retval = mEffect->getParam(EQ_PARAM_CENTER_FREQ, band, halFreq);
229     _hidl_cb(retval, halFreq);
230     return Void();
231 }
232 
getBandFrequencyRange(uint16_t band,getBandFrequencyRange_cb _hidl_cb)233 Return<void> EqualizerEffect::getBandFrequencyRange(
234         uint16_t band, getBandFrequencyRange_cb _hidl_cb)  {
235     uint32_t halFreqs[2] = { 0, 0 };
236     Result retval = mEffect->getParam(EQ_PARAM_BAND_FREQ_RANGE, band, halFreqs);
237     _hidl_cb(retval, halFreqs[0], halFreqs[1]);
238     return Void();
239 }
240 
getBandForFrequency(uint32_t freq,getBandForFrequency_cb _hidl_cb)241 Return<void> EqualizerEffect::getBandForFrequency(uint32_t freq, getBandForFrequency_cb _hidl_cb)  {
242     uint16_t halBand = 0;
243     Result retval = mEffect->getParam(EQ_PARAM_GET_BAND, freq, halBand);
244     _hidl_cb(retval, halBand);
245     return Void();
246 }
247 
getPresetNames(getPresetNames_cb _hidl_cb)248 Return<void> EqualizerEffect::getPresetNames(getPresetNames_cb _hidl_cb)  {
249     uint16_t halPresetCount = 0;
250     Result retval = mEffect->getParam(EQ_PARAM_GET_NUM_OF_PRESETS, halPresetCount);
251     hidl_vec<hidl_string> presetNames;
252     if (retval == Result::OK) {
253         presetNames.resize(halPresetCount);
254         for (uint16_t i = 0; i < halPresetCount; ++i) {
255             char halPresetName[EFFECT_STRING_LEN_MAX];
256             retval = mEffect->getParam(EQ_PARAM_GET_PRESET_NAME, i, halPresetName);
257             if (retval == Result::OK) {
258                 presetNames[i] = halPresetName;
259             } else {
260                 presetNames.resize(i);
261             }
262         }
263     }
264     _hidl_cb(retval, presetNames);
265     return Void();
266 }
267 
setCurrentPreset(uint16_t preset)268 Return<Result> EqualizerEffect::setCurrentPreset(uint16_t preset)  {
269     return mEffect->setParam(EQ_PARAM_CUR_PRESET, preset);
270 }
271 
getCurrentPreset(getCurrentPreset_cb _hidl_cb)272 Return<void> EqualizerEffect::getCurrentPreset(getCurrentPreset_cb _hidl_cb)  {
273     return mEffect->getIntegerParam(EQ_PARAM_CUR_PRESET, _hidl_cb);
274 }
275 
setAllProperties(const IEqualizerEffect::AllProperties & properties)276 Return<Result> EqualizerEffect::setAllProperties(
277         const IEqualizerEffect::AllProperties& properties)  {
278     t_equalizer_settings *halPropertiesPtr = nullptr;
279     std::vector<uint8_t> halBuffer = propertiesToHal(properties, &halPropertiesPtr);
280     uint32_t paramId = EQ_PARAM_PROPERTIES;
281     return mEffect->setParameterImpl(
282             sizeof(paramId), &paramId, halBuffer.size(), halPropertiesPtr);
283 }
284 
getAllProperties(getAllProperties_cb _hidl_cb)285 Return<void> EqualizerEffect::getAllProperties(getAllProperties_cb _hidl_cb)  {
286     uint16_t numBands = 0;
287     Result retval = mEffect->getParam(EQ_PARAM_NUM_BANDS, numBands);
288     AllProperties properties;
289     if (retval != Result::OK) {
290         _hidl_cb(retval, properties);
291         return Void();
292     }
293     size_t valueSize = sizeof(t_equalizer_settings) + sizeof(int16_t) * numBands;
294     uint32_t paramId = EQ_PARAM_PROPERTIES;
295     retval = mEffect->getParameterImpl(
296             sizeof(paramId), &paramId, valueSize,
297             [&] (uint32_t, const void* valueData) {
298                 const t_equalizer_settings* halProperties =
299                         reinterpret_cast<const t_equalizer_settings*>(valueData);
300                 propertiesFromHal(*halProperties, &properties);
301             });
302     _hidl_cb(retval, properties);
303     return Void();
304 }
305 
306 } // namespace implementation
307 }  // namespace V2_0
308 }  // namespace effect
309 }  // namespace audio
310 }  // namespace hardware
311 }  // namespace android
312