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