• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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 
18 #include "sles_allinclusive.h"
19 #include "math.h"
20 #include "utils/RefBase.h"
21 #include "utils/String16.h"
22 
23 #include <audio_effects/effect_bassboost.h>
24 #include <audio_effects/effect_equalizer.h>
25 #include <audio_effects/effect_environmentalreverb.h>
26 #include <audio_effects/effect_presetreverb.h>
27 #include <audio_effects/effect_virtualizer.h>
28 
29 #include <audio_effects/effect_aec.h>
30 #include <audio_effects/effect_agc.h>
31 #include <audio_effects/effect_ns.h>
32 
33 #include <system/audio.h>
34 
35 static const int EQUALIZER_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t)
36         + EFFECT_STRING_LEN_MAX;
37 
38 static const int BASSBOOST_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
39 
40 static const int VIRTUALIZER_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
41 
42 static const int ENVREVERB_PARAM_SIZE_MAX_SINGLE = sizeof(effect_param_t) + 2 * sizeof(int32_t);
43 
44 static const int ENVREVERB_PARAM_SIZE_MAX_ALL = sizeof(effect_param_t) + sizeof(int32_t)
45         + sizeof(s_reverb_settings);
46 
47 static const int PRESETREVERB_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
48 
KEY_FROM_GUID(SLInterfaceID pUuid)49 static inline SLuint32 KEY_FROM_GUID(SLInterfaceID pUuid) {
50     return pUuid->time_low;
51 }
52 
53 
54 //-----------------------------------------------------------------------------
55 static
eq_paramSize(int32_t param)56 uint32_t eq_paramSize(int32_t param) {
57     uint32_t size;
58 
59     switch (param) {
60     case EQ_PARAM_NUM_BANDS:
61     case EQ_PARAM_LEVEL_RANGE:
62     case EQ_PARAM_CUR_PRESET:
63     case EQ_PARAM_GET_NUM_OF_PRESETS:
64         size = sizeof(int32_t);
65         break;
66     case EQ_PARAM_BAND_LEVEL:
67     case EQ_PARAM_CENTER_FREQ:
68     case EQ_PARAM_BAND_FREQ_RANGE:
69     case EQ_PARAM_GET_BAND:
70     case EQ_PARAM_GET_PRESET_NAME:
71         size = 2 * sizeof(int32_t);
72         break;
73     default:
74         size = 2 * sizeof(int32_t);
75         SL_LOGE("Trying to use an unknown EQ parameter %d", param);
76         break;
77     }
78     return size;
79 }
80 
81 static
eq_valueSize(int32_t param)82 uint32_t eq_valueSize(int32_t param) {
83     uint32_t size;
84 
85     switch (param) {
86     case EQ_PARAM_NUM_BANDS:
87     case EQ_PARAM_CUR_PRESET:
88     case EQ_PARAM_GET_NUM_OF_PRESETS:
89     case EQ_PARAM_BAND_LEVEL:
90     case EQ_PARAM_GET_BAND:
91         size = sizeof(int16_t);
92         break;
93     case EQ_PARAM_LEVEL_RANGE:
94         size = 2 * sizeof(int16_t);
95         break;
96     case EQ_PARAM_CENTER_FREQ:
97         size = sizeof(int32_t);
98         break;
99     case EQ_PARAM_BAND_FREQ_RANGE:
100         size = 2 * sizeof(int32_t);
101         break;
102     case EQ_PARAM_GET_PRESET_NAME:
103         size = EFFECT_STRING_LEN_MAX;
104         break;
105     default:
106         size = sizeof(int32_t);
107         SL_LOGE("Trying to access an unknown EQ parameter %d", param);
108         break;
109     }
110     return size;
111 }
112 
113 //-----------------------------------------------------------------------------
114 /**
115  * returns the size in bytes of the value of each bass boost parameter
116  */
117 static
bb_valueSize(int32_t param)118 uint32_t bb_valueSize(int32_t param) {
119     uint32_t size;
120 
121     switch (param) {
122     case BASSBOOST_PARAM_STRENGTH_SUPPORTED:
123         size = sizeof(int32_t);
124         break;
125     case BASSBOOST_PARAM_STRENGTH:
126         size = sizeof(int16_t);
127         break;
128     default:
129         size = sizeof(int32_t);
130         SL_LOGE("Trying to access an unknown BassBoost parameter %d", param);
131         break;
132     }
133 
134     return size;
135 }
136 
137 //-----------------------------------------------------------------------------
138 /**
139  * returns the size in bytes of the value of each virtualizer parameter
140  */
141 static
virt_valueSize(int32_t param)142 uint32_t virt_valueSize(int32_t param) {
143     uint32_t size;
144 
145     switch (param) {
146     case VIRTUALIZER_PARAM_STRENGTH_SUPPORTED:
147         size = sizeof(int32_t);
148         break;
149     case VIRTUALIZER_PARAM_STRENGTH:
150         size = sizeof(int16_t);
151         break;
152     default:
153         size = sizeof(int32_t);
154         SL_LOGE("Trying to access an unknown Virtualizer parameter %d", param);
155         break;
156     }
157 
158     return size;
159 }
160 
161 //-----------------------------------------------------------------------------
162 /**
163  * returns the size in bytes of the value of each environmental reverb parameter
164  */
165 static
erev_valueSize(int32_t param)166 uint32_t erev_valueSize(int32_t param) {
167     uint32_t size;
168 
169     switch (param) {
170     case REVERB_PARAM_ROOM_LEVEL:
171     case REVERB_PARAM_ROOM_HF_LEVEL:
172     case REVERB_PARAM_REFLECTIONS_LEVEL:
173     case REVERB_PARAM_REVERB_LEVEL:
174         size = sizeof(int16_t); // millibel
175         break;
176     case REVERB_PARAM_DECAY_TIME:
177     case REVERB_PARAM_REFLECTIONS_DELAY:
178     case REVERB_PARAM_REVERB_DELAY:
179         size = sizeof(uint32_t); // milliseconds
180         break;
181     case REVERB_PARAM_DECAY_HF_RATIO:
182     case REVERB_PARAM_DIFFUSION:
183     case REVERB_PARAM_DENSITY:
184         size = sizeof(int16_t); // permille
185         break;
186     case REVERB_PARAM_PROPERTIES:
187         size = sizeof(s_reverb_settings); // struct of all reverb properties
188         break;
189     default:
190         size = sizeof(int32_t);
191         SL_LOGE("Trying to access an unknown Environmental Reverb parameter %d", param);
192         break;
193     }
194 
195     return size;
196 }
197 
198 //-----------------------------------------------------------------------------
android_eq_getParam(android::sp<android::AudioEffect> pFx,int32_t param,int32_t param2,void * pValue)199 android::status_t android_eq_getParam(android::sp<android::AudioEffect> pFx,
200         int32_t param, int32_t param2, void *pValue)
201 {
202      android::status_t status;
203      uint32_t buf32[(EQUALIZER_PARAM_SIZE_MAX - 1) / sizeof(uint32_t) + 1];
204      effect_param_t *p = (effect_param_t *)buf32;
205 
206      p->psize = eq_paramSize(param);
207      *(int32_t *)p->data = param;
208      if (p->psize == 2 * sizeof(int32_t)) {
209          *((int32_t *)p->data + 1) = param2;
210      }
211      p->vsize = eq_valueSize(param);
212      status = pFx->getParameter(p);
213      if (android::NO_ERROR == status) {
214          status = p->status;
215          if (android::NO_ERROR == status) {
216              memcpy(pValue, p->data + p->psize, p->vsize);
217          }
218      }
219 
220      return status;
221  }
222 
223 
224 //-----------------------------------------------------------------------------
android_eq_setParam(android::sp<android::AudioEffect> pFx,int32_t param,int32_t param2,void * pValue)225 android::status_t android_eq_setParam(android::sp<android::AudioEffect> pFx,
226         int32_t param, int32_t param2, void *pValue)
227 {
228     android::status_t status;
229     uint32_t buf32[(EQUALIZER_PARAM_SIZE_MAX - 1) / sizeof(uint32_t) + 1];
230     effect_param_t *p = (effect_param_t *)buf32;
231 
232     p->psize = eq_paramSize(param);
233     *(int32_t *)p->data = param;
234     if (p->psize == 2 * sizeof(int32_t)) {
235         *((int32_t *)p->data + 1) = param2;
236     }
237     p->vsize = eq_valueSize(param);
238     memcpy(p->data + p->psize, pValue, p->vsize);
239     status = pFx->setParameter(p);
240     if (android::NO_ERROR == status) {
241         status = p->status;
242     }
243 
244     return status;
245 }
246 
247 //-----------------------------------------------------------------------------
android_bb_setParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)248 android::status_t android_bb_setParam(android::sp<android::AudioEffect> pFx,
249         int32_t param, void *pValue) {
250 
251     return android_fx_setParam(pFx, param, BASSBOOST_PARAM_SIZE_MAX,
252             pValue, bb_valueSize(param));
253 }
254 
255 //-----------------------------------------------------------------------------
android_bb_getParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)256 android::status_t android_bb_getParam(android::sp<android::AudioEffect> pFx,
257         int32_t param, void *pValue) {
258 
259     return android_fx_getParam(pFx, param, BASSBOOST_PARAM_SIZE_MAX,
260             pValue, bb_valueSize(param));
261 }
262 
263 //-----------------------------------------------------------------------------
android_bb_init(audio_session_t sessionId,IBassBoost * ibb)264 void android_bb_init(audio_session_t sessionId, IBassBoost* ibb) {
265     SL_LOGV("session %d", sessionId);
266 
267     if (!android_fx_initEffectObj(sessionId, ibb->mBassBoostEffect,
268             &ibb->mBassBoostDescriptor.type))
269     {
270         SL_LOGE("BassBoost effect initialization failed");
271         return;
272     }
273 
274     // initialize strength
275     int16_t strength;
276     if (android::NO_ERROR == android_bb_getParam(ibb->mBassBoostEffect,
277             BASSBOOST_PARAM_STRENGTH, &strength)) {
278         ibb->mStrength = (SLpermille) strength;
279     }
280 }
281 
282 
283 //-----------------------------------------------------------------------------
android_eq_init(audio_session_t sessionId,IEqualizer * ieq)284 void android_eq_init(audio_session_t sessionId, IEqualizer* ieq) {
285     SL_LOGV("android_eq_init on session %d", sessionId);
286 
287     if (!android_fx_initEffectObj(sessionId, ieq->mEqEffect, &ieq->mEqDescriptor.type)) {
288         SL_LOGE("Equalizer effect initialization failed");
289         return;
290     }
291 
292     // initialize number of bands, band level range, and number of presets
293     uint16_t num = 0;
294     if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect, EQ_PARAM_NUM_BANDS, 0, &num)) {
295         ieq->mNumBands = num;
296     }
297     int16_t range[2] = {0, 0};
298     if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect, EQ_PARAM_LEVEL_RANGE, 0, range)) {
299         ieq->mBandLevelRangeMin = range[0];
300         ieq->mBandLevelRangeMax = range[1];
301     }
302 
303     SL_LOGV(" EQ init: num bands = %u, band range=[%d %d]mB", num, range[0], range[1]);
304 
305     // FIXME don't store presets names, they can be queried each time they're needed
306     // initialize preset number and names, store in IEngine
307     uint16_t numPresets = 0;
308     if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect,
309             EQ_PARAM_GET_NUM_OF_PRESETS, 0, &numPresets)) {
310         ieq->mThis->mEngine->mEqNumPresets = numPresets;
311         ieq->mNumPresets = numPresets;
312     }
313 
314     object_lock_exclusive(&ieq->mThis->mEngine->mObject);
315     char name[EFFECT_STRING_LEN_MAX];
316     if ((0 < numPresets) && (NULL == ieq->mThis->mEngine->mEqPresetNames)) {
317         ieq->mThis->mEngine->mEqPresetNames = (char **)new char *[numPresets];
318         for(uint32_t i = 0 ; i < numPresets ; i++) {
319             if (android::NO_ERROR == android_eq_getParam(ieq->mEqEffect,
320                     EQ_PARAM_GET_PRESET_NAME, i, name)) {
321                 ieq->mThis->mEngine->mEqPresetNames[i] = new char[strlen(name) + 1];
322                 strcpy(ieq->mThis->mEngine->mEqPresetNames[i], name);
323                 SL_LOGV(" EQ init: presets = %u is %s", i, ieq->mThis->mEngine->mEqPresetNames[i]);
324             }
325         }
326     }
327     object_unlock_exclusive(&ieq->mThis->mEngine->mObject);
328 
329 }
330 
331 
332 //-----------------------------------------------------------------------------
android_virt_init(audio_session_t sessionId,IVirtualizer * ivi)333 void android_virt_init(audio_session_t sessionId, IVirtualizer* ivi) {
334     SL_LOGV("android_virt_init on session %d", sessionId);
335 
336     if (!android_fx_initEffectObj(sessionId, ivi->mVirtualizerEffect,
337             &ivi->mVirtualizerDescriptor.type)) {
338         SL_LOGE("Virtualizer effect initialization failed");
339         return;
340     }
341 
342     // initialize strength
343     int16_t strength;
344     if (android::NO_ERROR == android_virt_getParam(ivi->mVirtualizerEffect,
345             VIRTUALIZER_PARAM_STRENGTH, &strength)) {
346         ivi->mStrength = (SLpermille) strength;
347     }
348 }
349 
350 //-----------------------------------------------------------------------------
android_virt_setParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)351 android::status_t android_virt_setParam(android::sp<android::AudioEffect> pFx,
352         int32_t param, void *pValue) {
353 
354     return android_fx_setParam(pFx, param, VIRTUALIZER_PARAM_SIZE_MAX,
355             pValue, virt_valueSize(param));
356 }
357 
358 //-----------------------------------------------------------------------------
android_virt_getParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)359 android::status_t android_virt_getParam(android::sp<android::AudioEffect> pFx,
360         int32_t param, void *pValue) {
361 
362     return android_fx_getParam(pFx, param, VIRTUALIZER_PARAM_SIZE_MAX,
363             pValue, virt_valueSize(param));
364 }
365 
366 
367 //-----------------------------------------------------------------------------
android_prev_init(IPresetReverb * ipr)368 void android_prev_init(IPresetReverb* ipr) {
369     SL_LOGV("session is implicitly %d (aux effect)", AUDIO_SESSION_OUTPUT_MIX);
370 
371     if (!android_fx_initEffectObj(AUDIO_SESSION_OUTPUT_MIX /*sessionId*/,
372             ipr->mPresetReverbEffect, &ipr->mPresetReverbDescriptor.type)) {
373         SL_LOGE("PresetReverb effect initialization failed");
374         return;
375     }
376 
377     // initialize preset
378     uint16_t preset;
379     if (android::NO_ERROR == android_prev_getPreset(ipr->mPresetReverbEffect, &preset)) {
380         ipr->mPreset = preset;
381         // enable the effect if it has a preset loaded
382         ipr->mPresetReverbEffect->setEnabled(SL_REVERBPRESET_NONE != preset);
383     }
384 }
385 
386 //-----------------------------------------------------------------------------
android_prev_setPreset(android::sp<android::AudioEffect> pFx,uint16_t preset)387 android::status_t android_prev_setPreset(android::sp<android::AudioEffect> pFx, uint16_t preset) {
388     android::status_t status = android_fx_setParam(pFx, REVERB_PARAM_PRESET,
389             PRESETREVERB_PARAM_SIZE_MAX, &preset, sizeof(uint16_t));
390     // enable the effect if the preset is different from SL_REVERBPRESET_NONE
391     pFx->setEnabled(SL_REVERBPRESET_NONE != preset);
392     return status;
393 }
394 
395 //-----------------------------------------------------------------------------
android_prev_getPreset(android::sp<android::AudioEffect> pFx,uint16_t * preset)396 android::status_t android_prev_getPreset(android::sp<android::AudioEffect> pFx, uint16_t* preset) {
397     return android_fx_getParam(pFx, REVERB_PARAM_PRESET, PRESETREVERB_PARAM_SIZE_MAX, preset,
398             sizeof(uint16_t));
399 }
400 
401 
402 //-----------------------------------------------------------------------------
android_erev_init(IEnvironmentalReverb * ier)403 void android_erev_init(IEnvironmentalReverb* ier) {
404     SL_LOGV("session is implicitly %d (aux effect)", AUDIO_SESSION_OUTPUT_MIX);
405 
406     if (!android_fx_initEffectObj(AUDIO_SESSION_OUTPUT_MIX /*sessionId*/,
407             ier->mEnvironmentalReverbEffect, &ier->mEnvironmentalReverbDescriptor.type)) {
408         SL_LOGE("EnvironmentalReverb effect initialization failed");
409         return;
410     }
411 
412     // enable env reverb: other SL ES effects have an explicit SetEnabled() function, and the
413     //  preset reverb state depends on the selected preset.
414     ier->mEnvironmentalReverbEffect->setEnabled(true);
415 
416     // initialize reverb properties
417     SLEnvironmentalReverbSettings properties;
418     if (android::NO_ERROR == android_erev_getParam(ier->mEnvironmentalReverbEffect,
419             REVERB_PARAM_PROPERTIES, &properties)) {
420         ier->mProperties = properties;
421     }
422 }
423 
424 //-----------------------------------------------------------------------------
android_erev_setParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)425 android::status_t android_erev_setParam(android::sp<android::AudioEffect> pFx,
426         int32_t param, void *pValue) {
427 
428     // given the size difference between a single reverb property and the whole set of reverb
429     // properties, select which max size to pass to avoid allocating too much memory
430     if (param == REVERB_PARAM_PROPERTIES) {
431         return android_fx_setParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_ALL,
432                 pValue, erev_valueSize(param));
433     } else {
434         return android_fx_setParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_SINGLE,
435                 pValue, erev_valueSize(param));
436     }
437 }
438 
439 //-----------------------------------------------------------------------------
android_erev_getParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)440 android::status_t android_erev_getParam(android::sp<android::AudioEffect> pFx,
441         int32_t param, void *pValue) {
442 
443     // given the size difference between a single reverb property and the whole set of reverb
444     // properties, select which max size to pass to avoid allocating too much memory
445     if (param == REVERB_PARAM_PROPERTIES) {
446         return android_fx_getParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_ALL,
447                 pValue, erev_valueSize(param));
448     } else {
449         return android_fx_getParam(pFx, param, ENVREVERB_PARAM_SIZE_MAX_SINGLE,
450                 pValue, erev_valueSize(param));
451     }
452 }
453 
454 //-----------------------------------------------------------------------------
android_aec_init(audio_session_t sessionId,IAndroidAcousticEchoCancellation * iaec)455 void android_aec_init(audio_session_t sessionId, IAndroidAcousticEchoCancellation* iaec) {
456     SL_LOGV("android_aec_init on session %d", sessionId);
457 
458     if (!android_fx_initEffectObj(sessionId, iaec->mAECEffect,
459             &iaec->mAECDescriptor.type)) {
460         SL_LOGE("AEC effect initialization failed");
461         return;
462     }
463 }
464 
465 //-----------------------------------------------------------------------------
android_agc_init(audio_session_t sessionId,IAndroidAutomaticGainControl * iagc)466 void android_agc_init(audio_session_t sessionId, IAndroidAutomaticGainControl* iagc) {
467     SL_LOGV("android_agc_init on session %d", sessionId);
468 
469     if (!android_fx_initEffectObj(sessionId, iagc->mAGCEffect,
470             &iagc->mAGCDescriptor.type)) {
471         SL_LOGE("AGC effect initialization failed");
472         return;
473     }
474 }
475 
476 //-----------------------------------------------------------------------------
android_ns_init(audio_session_t sessionId,IAndroidNoiseSuppression * ins)477 void android_ns_init(audio_session_t sessionId, IAndroidNoiseSuppression* ins) {
478     SL_LOGV("android_ns_init on session %d", sessionId);
479 
480     if (!android_fx_initEffectObj(sessionId, ins->mNSEffect,
481             &ins->mNSDescriptor.type)) {
482         SL_LOGE("NS effect initialization failed");
483         return;
484     }
485 }
486 
487 //-----------------------------------------------------------------------------
488 /**
489  * pre-condition:
490  *    ap != NULL
491  *    for media players:
492  *      ap->mAPlayer != 0
493  *      ap->mAudioTrack == 0
494  *    for buffer queue players:
495  *      ap->mAPlayer == 0
496  *      ap->mAudioTrack != 0 is optional; if no track yet then the setting is deferred
497  */
android_fxSend_attach(CAudioPlayer * ap,bool attach,android::sp<android::AudioEffect> pFx,SLmillibel sendLevel)498 android::status_t android_fxSend_attach(CAudioPlayer* ap, bool attach,
499         android::sp<android::AudioEffect> pFx, SLmillibel sendLevel) {
500 
501     if (pFx == 0) {
502         return android::INVALID_OPERATION;
503     }
504 
505     // There are 3 cases:
506     //  mAPlayer != 0 && mAudioTrack == 0 means playing decoded audio
507     //  mAPlayer == 0 && mAudioTrack != 0 means playing PCM audio
508     //  mAPlayer == 0 && mAudioTrack == 0 means player not fully configured yet
509     // The asserts document and verify this.
510     if (ap->mAPlayer != 0) {
511         assert(ap->mAudioTrack == 0);
512         if (attach) {
513             ap->mAPlayer->attachAuxEffect(pFx->id());
514             ap->mAPlayer->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) );
515         } else {
516             ap->mAPlayer->attachAuxEffect(0);
517         }
518         return android::NO_ERROR;
519     }
520 
521     if (ap->mAudioTrack == 0) {
522         // the player doesn't have an AudioTrack at the moment, so store this info to use it
523         // when the AudioTrack becomes available
524         if (attach) {
525             ap->mAuxEffect = pFx;
526         } else {
527             ap->mAuxEffect.clear();
528         }
529         // we keep track of the send level, independently of the current audio player level
530         ap->mAuxSendLevel = sendLevel - ap->mVolume.mLevel;
531         return android::NO_ERROR;
532     }
533 
534     if (attach) {
535         android::status_t status = ap->mAudioTrack->attachAuxEffect(pFx->id());
536         //SL_LOGV("attachAuxEffect(%d) returned %d", pFx->id(), status);
537         if (android::NO_ERROR == status) {
538             status =
539                 ap->mAudioTrack->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) );
540         }
541         return status;
542     } else {
543         return ap->mAudioTrack->attachAuxEffect(0);
544     }
545 }
546 
547 //-----------------------------------------------------------------------------
548 /**
549  * pre-condition:
550  *    ap != NULL
551  *    ap->mOutputMix != NULL
552  */
android_fxSend_attachToAux(CAudioPlayer * ap,SLInterfaceID pUuid,SLboolean attach,SLmillibel sendLevel)553 SLresult android_fxSend_attachToAux(CAudioPlayer* ap, SLInterfaceID pUuid, SLboolean attach,
554         SLmillibel sendLevel) {
555     COutputMix *outputMix = CAudioPlayer_GetOutputMix(ap);
556     ssize_t index = outputMix->mAndroidEffect.mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
557 
558     if (0 > index) {
559         SL_LOGE("invalid effect ID: no such effect attached to the OutputMix");
560         return SL_RESULT_PARAMETER_INVALID;
561     }
562 
563     android::sp<android::AudioEffect> pFx =
564                           outputMix->mAndroidEffect.mEffects->valueAt(index);
565     if (pFx == 0) {
566         return SL_RESULT_RESOURCE_ERROR;
567     }
568     if (android::NO_ERROR == android_fxSend_attach( ap, (bool) attach, pFx, sendLevel) ) {
569         return SL_RESULT_SUCCESS;
570     } else {
571         return SL_RESULT_RESOURCE_ERROR;
572     }
573 
574 }
575 
576 //-----------------------------------------------------------------------------
577 /**
578  * pre-condition:
579  *    ap != NULL
580  *    for media players:
581  *      ap->mAPlayer != 0
582  *      ap->mAudioTrack == 0
583  *    for buffer queue players:
584  *      ap->mAPlayer == 0
585  *      ap->mAudioTrack != 0 is optional; if no track yet then the setting is deferred
586  */
android_fxSend_setSendLevel(CAudioPlayer * ap,SLmillibel sendLevel)587 android::status_t android_fxSend_setSendLevel(CAudioPlayer* ap, SLmillibel sendLevel) {
588     // we keep track of the send level, independently of the current audio player level
589     ap->mAuxSendLevel = sendLevel - ap->mVolume.mLevel;
590 
591     if (ap->mAPlayer != 0) {
592         assert(ap->mAudioTrack == 0);
593         ap->mAPlayer->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) );
594         return android::NO_ERROR;
595     }
596 
597     if (ap->mAudioTrack == 0) {
598         return android::NO_ERROR;
599     }
600 
601     return ap->mAudioTrack->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) );
602 }
603 
604 //-----------------------------------------------------------------------------
android_fx_setParam(android::sp<android::AudioEffect> pFx,int32_t param,uint32_t paramSizeMax,void * pValue,uint32_t valueSize)605 android::status_t android_fx_setParam(android::sp<android::AudioEffect> pFx,
606         int32_t param, uint32_t paramSizeMax, void *pValue, uint32_t valueSize)
607 {
608 
609     android::status_t status;
610     uint32_t buf32[(paramSizeMax - 1) / sizeof(uint32_t) + 1];
611     effect_param_t *p = (effect_param_t *)buf32;
612 
613     p->psize = sizeof(int32_t);
614     *(int32_t *)p->data = param;
615     p->vsize = valueSize;
616     memcpy(p->data + p->psize, pValue, p->vsize);
617     status = pFx->setParameter(p);
618     if (android::NO_ERROR == status) {
619         status = p->status;
620     }
621     return status;
622 }
623 
624 
625 //-----------------------------------------------------------------------------
android_fx_getParam(android::sp<android::AudioEffect> pFx,int32_t param,uint32_t paramSizeMax,void * pValue,uint32_t valueSize)626 android::status_t android_fx_getParam(android::sp<android::AudioEffect> pFx,
627         int32_t param, uint32_t paramSizeMax, void *pValue, uint32_t valueSize)
628 {
629     android::status_t status;
630     uint32_t buf32[(paramSizeMax - 1) / sizeof(uint32_t) + 1];
631     effect_param_t *p = (effect_param_t *)buf32;
632 
633     p->psize = sizeof(int32_t);
634     *(int32_t *)p->data = param;
635     p->vsize = valueSize;
636     status = pFx->getParameter(p);
637     if (android::NO_ERROR == status) {
638         status = p->status;
639         if (android::NO_ERROR == status) {
640             memcpy(pValue, p->data + p->psize, p->vsize);
641         }
642     }
643 
644     return status;
645 }
646 
647 
648 //-----------------------------------------------------------------------------
android_fx_statusToResult(android::status_t status)649 SLresult android_fx_statusToResult(android::status_t status) {
650 
651     if ((android::INVALID_OPERATION == status) || (android::DEAD_OBJECT == status)) {
652         return SL_RESULT_CONTROL_LOST;
653     } else {
654         return SL_RESULT_SUCCESS;
655     }
656 }
657 
658 
659 //-----------------------------------------------------------------------------
android_fx_initEffectObj(audio_session_t sessionId,android::sp<android::AudioEffect> & effect,const effect_uuid_t * type)660 bool android_fx_initEffectObj(audio_session_t sessionId, android::sp<android::AudioEffect>& effect,
661         const effect_uuid_t *type) {
662     //SL_LOGV("android_fx_initEffectObj on session %d", sessionId);
663 
664     effect = new android::AudioEffect(type, android::String16(), EFFECT_UUID_NULL,
665             0,// priority
666             0,// effect callback
667             0,// callback data
668             sessionId,// session ID
669             0 );// output
670 
671     android::status_t status = effect->initCheck();
672     if (android::NO_ERROR != status) {
673         effect.clear();
674         SL_LOGE("Effect initCheck() returned %d", status);
675         return false;
676     }
677 
678     return true;
679 }
680 
681 
682 //-----------------------------------------------------------------------------
android_fx_initEffectDescriptor(const SLInterfaceID effectId,effect_descriptor_t * fxDescrLoc)683 bool android_fx_initEffectDescriptor(const SLInterfaceID effectId,
684         effect_descriptor_t* fxDescrLoc) {
685     uint32_t numEffects = 0;
686     effect_descriptor_t descriptor;
687     bool foundEffect = false;
688 
689     // any effects?
690     android::status_t res = android::AudioEffect::queryNumberEffects(&numEffects);
691     if (android::NO_ERROR != res) {
692         SL_LOGE("unable to find any effects.");
693         goto effectError;
694     }
695 
696     // request effect in the effects?
697     for (uint32_t i=0 ; i < numEffects ; i++) {
698         res = android::AudioEffect::queryEffect(i, &descriptor);
699         if ((android::NO_ERROR == res) &&
700                 (0 == memcmp(effectId, &descriptor.type, sizeof(effect_uuid_t)))) {
701             SL_LOGV("found effect %d %s", i, descriptor.name);
702             foundEffect = true;
703             break;
704         }
705     }
706     if (foundEffect) {
707         memcpy(fxDescrLoc, &descriptor, sizeof(effect_descriptor_t));
708     } else {
709         SL_LOGE("unable to find an implementation for the requested effect.");
710         goto effectError;
711     }
712 
713     return true;
714 
715 effectError:
716     // the requested effect wasn't found
717     memset(fxDescrLoc, 0, sizeof(effect_descriptor_t));
718 
719     return false;
720 }
721 
722 //-----------------------------------------------------------------------------
android_genericFx_queryNumEffects(SLuint32 * pNumSupportedAudioEffects)723 SLresult android_genericFx_queryNumEffects(SLuint32 *pNumSupportedAudioEffects) {
724 
725     if (NULL == pNumSupportedAudioEffects) {
726         return SL_RESULT_PARAMETER_INVALID;
727     }
728 
729     android::status_t status =
730             android::AudioEffect::queryNumberEffects((uint32_t*)pNumSupportedAudioEffects);
731 
732     SLresult result = SL_RESULT_SUCCESS;
733     switch (status) {
734         case android::NO_ERROR:
735             result = SL_RESULT_SUCCESS;
736             break;
737         case android::PERMISSION_DENIED:
738             result = SL_RESULT_PERMISSION_DENIED;
739             break;
740         case android::NO_INIT:
741             result = SL_RESULT_RESOURCE_ERROR;
742             break;
743         case android::BAD_VALUE:
744             result = SL_RESULT_PARAMETER_INVALID;
745             break;
746         default:
747             result = SL_RESULT_INTERNAL_ERROR;
748             SL_LOGE("received invalid status %d from AudioEffect::queryNumberEffects()", status);
749             break;
750     }
751     return result;
752 }
753 
754 
755 //-----------------------------------------------------------------------------
android_genericFx_queryEffect(SLuint32 index,effect_descriptor_t * pDescriptor)756 SLresult android_genericFx_queryEffect(SLuint32 index, effect_descriptor_t* pDescriptor) {
757 
758     if (NULL == pDescriptor) {
759         return SL_RESULT_PARAMETER_INVALID;
760     }
761 
762     android::status_t status =
763                 android::AudioEffect::queryEffect(index, pDescriptor);
764 
765     SLresult result = SL_RESULT_SUCCESS;
766     if (android::NO_ERROR != status) {
767         switch (status) {
768         case android::PERMISSION_DENIED:
769             result = SL_RESULT_PERMISSION_DENIED;
770             break;
771         case android::NO_INIT:
772         case android::INVALID_OPERATION:
773             result = SL_RESULT_RESOURCE_ERROR;
774             break;
775         case android::BAD_VALUE:
776             result = SL_RESULT_PARAMETER_INVALID;
777             break;
778         default:
779             result = SL_RESULT_INTERNAL_ERROR;
780             SL_LOGE("received invalid status %d from AudioEffect::queryNumberEffects()", status);
781             break;
782         }
783         // an error occurred, reset the effect descriptor
784         memset(pDescriptor, 0, sizeof(effect_descriptor_t));
785     }
786 
787     return result;
788 }
789 
790 
791 //-----------------------------------------------------------------------------
android_genericFx_createEffect(IAndroidEffect * iae,SLInterfaceID pUuid,audio_session_t sessionId)792 SLresult android_genericFx_createEffect(IAndroidEffect* iae, SLInterfaceID pUuid,
793         audio_session_t sessionId)
794 {
795 
796     SLresult result = SL_RESULT_SUCCESS;
797 
798     // does this effect already exist?
799     if (0 <= iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid))) {
800         return result;
801     }
802 
803     // create new effect
804     android::sp<android::AudioEffect> pFx = new android::AudioEffect(
805             NULL, // not using type to create effect
806             android::String16(),
807             (const effect_uuid_t*)pUuid,
808             0,// priority
809             0,// effect callback
810             0,// callback data
811             sessionId,
812             0 );// output
813 
814     // verify effect was successfully created before storing it
815     android::status_t status = pFx->initCheck();
816     if (android::NO_ERROR != status) {
817         SL_LOGE("AudioEffect initCheck() returned %d, effect will not be stored", status);
818         result = SL_RESULT_RESOURCE_ERROR;
819     } else {
820         SL_LOGV("AudioEffect successfully created on session %d", sessionId);
821         iae->mEffects->add(KEY_FROM_GUID(pUuid), pFx);
822     }
823 
824     return result;
825 }
826 
827 
828 //-----------------------------------------------------------------------------
android_genericFx_releaseEffect(IAndroidEffect * iae,SLInterfaceID pUuid)829 SLresult android_genericFx_releaseEffect(IAndroidEffect* iae, SLInterfaceID pUuid) {
830 
831     ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
832 
833     if (0 > index) {
834         return SL_RESULT_PARAMETER_INVALID;
835     } else {
836         iae->mEffects->removeItem(index);
837         return SL_RESULT_SUCCESS;
838     }
839 }
840 
841 
842 //-----------------------------------------------------------------------------
android_genericFx_setEnabled(IAndroidEffect * iae,SLInterfaceID pUuid,SLboolean enabled)843 SLresult android_genericFx_setEnabled(IAndroidEffect* iae, SLInterfaceID pUuid, SLboolean enabled) {
844 
845     ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
846 
847     if (0 > index) {
848         return SL_RESULT_PARAMETER_INVALID;
849     } else {
850         android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index);
851         android::status_t status = pFx->setEnabled(SL_BOOLEAN_TRUE == enabled);
852         return android_fx_statusToResult(status);
853     }
854 }
855 
856 
857 //-----------------------------------------------------------------------------
android_genericFx_isEnabled(IAndroidEffect * iae,SLInterfaceID pUuid,SLboolean * pEnabled)858 SLresult android_genericFx_isEnabled(IAndroidEffect* iae, SLInterfaceID pUuid, SLboolean *pEnabled)
859 {
860     ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
861 
862     if (0 > index) {
863         return SL_RESULT_PARAMETER_INVALID;
864     } else {
865         android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index);
866         *pEnabled = (SLboolean) pFx->getEnabled();
867         return SL_RESULT_SUCCESS;
868     }
869 }
870 
871 
872 //-----------------------------------------------------------------------------
android_genericFx_sendCommand(IAndroidEffect * iae,SLInterfaceID pUuid,SLuint32 command,SLuint32 commandSize,void * pCommandData,SLuint32 * replySize,void * pReplyData)873 SLresult android_genericFx_sendCommand(IAndroidEffect* iae, SLInterfaceID pUuid,
874         SLuint32 command, SLuint32 commandSize, void* pCommandData,
875         SLuint32 *replySize, void *pReplyData) {
876 
877     ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid));
878 
879     if (0 > index) {
880         return SL_RESULT_PARAMETER_INVALID;
881     } else {
882         android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index);
883         android::status_t status = pFx->command(
884                 (uint32_t) command,
885                 (uint32_t) commandSize,
886                 pCommandData,
887                 (uint32_t*)replySize,
888                 pReplyData);
889         if (android::BAD_VALUE == status) {
890                 return SL_RESULT_PARAMETER_INVALID;
891         } else {
892             return SL_RESULT_SUCCESS;
893         }
894     }
895 }
896 
897 //-----------------------------------------------------------------------------
898 /**
899  * returns true if the given effect id is present in the AndroidEffect interface
900  */
android_genericFx_hasEffect(IAndroidEffect * iae,SLInterfaceID pUuid)901 bool android_genericFx_hasEffect(IAndroidEffect* iae, SLInterfaceID pUuid) {
902     return( 0 <= iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid)));
903 }
904 
905 //-----------------------------------------------------------------------------
906 static const int AEC_PARAM_SIZE_MAX = sizeof(effect_param_t) + (2 * sizeof(int32_t));
907 /**
908  * returns the size in bytes of the value of each acoustic echo cancellation parameter
909  */
aec_valueSize(int32_t param)910 uint32_t aec_valueSize(int32_t param) {
911     uint32_t size;
912     switch (param) {
913     case AEC_PARAM_ECHO_DELAY:
914         size = sizeof(int32_t);
915         break;
916     default:
917         size = sizeof(int32_t);
918         SL_LOGE("Trying to access an unknown Acoustic Echo Cancellation parameter %d", param);
919         break;
920     }
921 
922     return size;
923 }
924 
android_aec_setParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)925 android::status_t android_aec_setParam(android::sp<android::AudioEffect> pFx,
926         int32_t param, void *pValue) {
927     return android_fx_setParam(pFx, param, AEC_PARAM_SIZE_MAX,
928             pValue, aec_valueSize(param));
929 }
930 
android_aec_getParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)931 android::status_t android_aec_getParam(android::sp<android::AudioEffect> pFx,
932         int32_t param, void *pValue) {
933     return android_fx_getParam(pFx, param, AEC_PARAM_SIZE_MAX,
934             pValue, aec_valueSize(param));
935 }
936 
937 //-----------------------------------------------------------------------------
938 static const int AGC_PARAM_SIZE_MAX = sizeof(effect_param_t) + (2 * sizeof(int16_t)) + sizeof(bool);
939 /**
940  * returns the size in bytes of the value of each automatic gain control parameter
941  */
agc_valueSize(int32_t param)942 uint32_t agc_valueSize(int32_t param) {
943     uint32_t size;
944     switch (param) {
945     case AGC_PARAM_TARGET_LEVEL:
946     case AGC_PARAM_COMP_GAIN:
947         size = sizeof(int16_t);
948         break;
949     case AGC_PARAM_LIMITER_ENA:
950         size = sizeof(bool);
951         break;
952     default:
953         size = sizeof(int32_t);
954         SL_LOGE("Trying to access an unknown Automatic Gain Control parameter %d", param);
955         break;
956     }
957 
958     return size;
959 }
960 
android_agc_setParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)961 android::status_t android_agc_setParam(android::sp<android::AudioEffect> pFx,
962         int32_t param, void *pValue) {
963     return android_fx_setParam(pFx, param, AGC_PARAM_SIZE_MAX,
964             pValue, agc_valueSize(param));
965 }
966 
android_agc_getParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)967 android::status_t android_agc_getParam(android::sp<android::AudioEffect> pFx,
968         int32_t param, void *pValue) {
969     return android_fx_getParam(pFx, param, AGC_PARAM_SIZE_MAX,
970             pValue, agc_valueSize(param));
971 }
972 
973 //-----------------------------------------------------------------------------
974 static const int NS_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t);
975 /**
976  * returns the size in bytes of the value of each noise suppression parameter
977  */
ns_valueSize(int32_t param)978 uint32_t ns_valueSize(int32_t param) {
979     uint32_t size;
980     switch (param) {
981     case NS_PARAM_LEVEL:
982         size = sizeof(int32_t);
983         break;
984     default:
985         size = sizeof(int32_t);
986         SL_LOGE("Trying to access an unknown Noise suppression parameter %d", param);
987         break;
988     }
989 
990     return size;
991 }
992 
android_ns_setParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)993 android::status_t android_ns_setParam(android::sp<android::AudioEffect> pFx,
994         int32_t param, void *pValue)
995 {
996     return android_fx_setParam(pFx, param, NS_PARAM_SIZE_MAX,
997             pValue, ns_valueSize(param));
998 }
999 
android_ns_getParam(android::sp<android::AudioEffect> pFx,int32_t param,void * pValue)1000 android::status_t android_ns_getParam(android::sp<android::AudioEffect> pFx,
1001         int32_t param, void *pValue)
1002 {
1003     return android_fx_getParam(pFx, param, NS_PARAM_SIZE_MAX,
1004             pValue, ns_valueSize(param));
1005 }
1006