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