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