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