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