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