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 <system/audio_effects/effect_bassboost.h> 24 #include <system/audio_effects/effect_equalizer.h> 25 #include <system/audio_effects/effect_environmentalreverb.h> 26 #include <system/audio_effects/effect_presetreverb.h> 27 #include <system/audio_effects/effect_virtualizer.h> 28 29 #include <system/audio_effects/effect_aec.h> 30 #include <system/audio_effects/effect_agc.h> 31 #include <system/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 49 static inline SLuint32 KEY_FROM_GUID(SLInterfaceID pUuid) { 50 return pUuid->time_low; 51 } 52 53 54 //----------------------------------------------------------------------------- 55 static 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 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 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 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 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 //----------------------------------------------------------------------------- 199 android::status_t android_eq_getParam(const 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 //----------------------------------------------------------------------------- 225 android::status_t android_eq_setParam(const 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 //----------------------------------------------------------------------------- 248 android::status_t android_bb_setParam(const 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 //----------------------------------------------------------------------------- 256 android::status_t android_bb_getParam(const 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 //----------------------------------------------------------------------------- 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 //----------------------------------------------------------------------------- 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 //----------------------------------------------------------------------------- 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 //----------------------------------------------------------------------------- 351 android::status_t android_virt_setParam(const 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 //----------------------------------------------------------------------------- 359 android::status_t android_virt_getParam(const 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 //----------------------------------------------------------------------------- 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 //----------------------------------------------------------------------------- 387 android::status_t android_prev_setPreset(const 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 //----------------------------------------------------------------------------- 396 android::status_t android_prev_getPreset(const 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 //----------------------------------------------------------------------------- 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 //----------------------------------------------------------------------------- 425 android::status_t android_erev_setParam(const 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 //----------------------------------------------------------------------------- 440 android::status_t android_erev_getParam(const 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 void android_aec_init(audio_session_t sessionId, IAndroidAcousticEchoCancellation* iaec) { 456 SL_LOGV("android_aec_init on session %d", sessionId); 457 458 if (!android_fx_initEffectObj(sessionId, iaec->mAECEffect, 459 &iaec->mAECDescriptor.type)) { 460 SL_LOGE("AEC effect initialization failed"); 461 return; 462 } 463 } 464 465 //----------------------------------------------------------------------------- 466 void android_agc_init(audio_session_t sessionId, IAndroidAutomaticGainControl* iagc) { 467 SL_LOGV("android_agc_init on session %d", sessionId); 468 469 if (!android_fx_initEffectObj(sessionId, iagc->mAGCEffect, 470 &iagc->mAGCDescriptor.type)) { 471 SL_LOGE("AGC effect initialization failed"); 472 return; 473 } 474 } 475 476 //----------------------------------------------------------------------------- 477 void android_ns_init(audio_session_t sessionId, IAndroidNoiseSuppression* ins) { 478 SL_LOGV("android_ns_init on session %d", sessionId); 479 480 if (!android_fx_initEffectObj(sessionId, ins->mNSEffect, 481 &ins->mNSDescriptor.type)) { 482 SL_LOGE("NS effect initialization failed"); 483 return; 484 } 485 } 486 487 //----------------------------------------------------------------------------- 488 /** 489 * pre-condition: 490 * ap != NULL 491 * for media players: 492 * ap->mAPlayer != 0 493 * ap->mTrackPlayer->mAudioTrack == 0 494 * for buffer queue players: 495 * ap->mAPlayer == 0 496 * ap->mTrackPlayer->mAudioTrack != 0 is optional; if no track yet then the setting is deferred 497 */ 498 android::status_t android_fxSend_attach(CAudioPlayer* ap, bool attach, 499 const android::sp<android::AudioEffect>& pFx, SLmillibel sendLevel) { 500 501 if (pFx == 0) { 502 return android::INVALID_OPERATION; 503 } 504 505 // There are 3 cases: 506 // mAPlayer != 0 && mAudioTrack == 0 means playing decoded audio 507 // mAPlayer == 0 && mAudioTrack != 0 means playing PCM audio 508 // mAPlayer == 0 && mAudioTrack == 0 means player not fully configured yet 509 // The asserts document and verify this. 510 if (ap->mAPlayer != 0) { 511 assert(ap->mTrackPlayer->mAudioTrack == 0); 512 if (attach) { 513 ap->mAPlayer->attachAuxEffect(pFx->id()); 514 ap->mAPlayer->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) ); 515 } else { 516 ap->mAPlayer->attachAuxEffect(0); 517 } 518 return android::NO_ERROR; 519 } 520 521 if (ap->mTrackPlayer->mAudioTrack == 0) { 522 // the player doesn't have an AudioTrack at the moment, so store this info to use it 523 // when the AudioTrack becomes available 524 if (attach) { 525 ap->mAuxEffect = pFx; 526 } else { 527 ap->mAuxEffect.clear(); 528 } 529 // we keep track of the send level, independently of the current audio player level 530 ap->mAuxSendLevel = sendLevel - ap->mVolume.mLevel; 531 return android::NO_ERROR; 532 } 533 534 if (attach) { 535 android::status_t status = ap->mTrackPlayer->mAudioTrack->attachAuxEffect(pFx->id()); 536 //SL_LOGV("attachAuxEffect(%d) returned %d", pFx->id(), status); 537 if (android::NO_ERROR == status) { 538 status = 539 ap->mTrackPlayer->mAudioTrack->setAuxEffectSendLevel( 540 sles_to_android_amplification(sendLevel) ); 541 } 542 return status; 543 } else { 544 return ap->mTrackPlayer->mAudioTrack->attachAuxEffect(0); 545 } 546 } 547 548 //----------------------------------------------------------------------------- 549 /** 550 * pre-condition: 551 * ap != NULL 552 * ap->mOutputMix != NULL 553 */ 554 SLresult android_fxSend_attachToAux(CAudioPlayer* ap, SLInterfaceID pUuid, SLboolean attach, 555 SLmillibel sendLevel) { 556 COutputMix *outputMix = CAudioPlayer_GetOutputMix(ap); 557 ssize_t index = outputMix->mAndroidEffect.mEffects->indexOfKey(KEY_FROM_GUID(pUuid)); 558 559 if (0 > index) { 560 SL_LOGE("invalid effect ID: no such effect attached to the OutputMix"); 561 return SL_RESULT_PARAMETER_INVALID; 562 } 563 564 android::sp<android::AudioEffect> pFx = 565 outputMix->mAndroidEffect.mEffects->valueAt(index); 566 if (pFx == 0) { 567 return SL_RESULT_RESOURCE_ERROR; 568 } 569 if (android::NO_ERROR == android_fxSend_attach( ap, (bool) attach, pFx, sendLevel) ) { 570 return SL_RESULT_SUCCESS; 571 } else { 572 return SL_RESULT_RESOURCE_ERROR; 573 } 574 575 } 576 577 //----------------------------------------------------------------------------- 578 /** 579 * pre-condition: 580 * ap != NULL 581 * for media players: 582 * ap->mAPlayer != 0 583 * ap->mTrackPlayer->mAudioTrack == 0 584 * for buffer queue players: 585 * ap->mAPlayer == 0 586 * ap->mTrackPlayer->mAudioTrack != 0 is optional; if no track yet then the setting is deferred 587 */ 588 android::status_t android_fxSend_setSendLevel(CAudioPlayer* ap, SLmillibel sendLevel) { 589 // we keep track of the send level, independently of the current audio player level 590 ap->mAuxSendLevel = sendLevel - ap->mVolume.mLevel; 591 592 if (ap->mAPlayer != 0) { 593 assert(ap->mTrackPlayer->mAudioTrack == 0); 594 ap->mAPlayer->setAuxEffectSendLevel( sles_to_android_amplification(sendLevel) ); 595 return android::NO_ERROR; 596 } 597 598 if (ap->mTrackPlayer->mAudioTrack == 0) { 599 return android::NO_ERROR; 600 } 601 602 return ap->mTrackPlayer->mAudioTrack->setAuxEffectSendLevel( 603 sles_to_android_amplification(sendLevel) ); 604 } 605 606 //----------------------------------------------------------------------------- 607 android::status_t android_fx_setParam(const android::sp<android::AudioEffect>& pFx, 608 int32_t param, uint32_t paramSizeMax, void *pValue, uint32_t valueSize) 609 { 610 611 android::status_t status; 612 uint32_t buf32[(paramSizeMax - 1) / sizeof(uint32_t) + 1]; 613 effect_param_t *p = (effect_param_t *)buf32; 614 615 p->psize = sizeof(int32_t); 616 *(int32_t *)p->data = param; 617 p->vsize = valueSize; 618 memcpy(p->data + p->psize, pValue, p->vsize); 619 status = pFx->setParameter(p); 620 if (android::NO_ERROR == status) { 621 status = p->status; 622 } 623 return status; 624 } 625 626 627 //----------------------------------------------------------------------------- 628 android::status_t android_fx_getParam(const android::sp<android::AudioEffect>& pFx, 629 int32_t param, uint32_t paramSizeMax, void *pValue, uint32_t valueSize) 630 { 631 android::status_t status; 632 uint32_t buf32[(paramSizeMax - 1) / sizeof(uint32_t) + 1]; 633 effect_param_t *p = (effect_param_t *)buf32; 634 635 p->psize = sizeof(int32_t); 636 *(int32_t *)p->data = param; 637 p->vsize = valueSize; 638 status = pFx->getParameter(p); 639 if (android::NO_ERROR == status) { 640 status = p->status; 641 if (android::NO_ERROR == status) { 642 memcpy(pValue, p->data + p->psize, p->vsize); 643 } 644 } 645 646 return status; 647 } 648 649 650 //----------------------------------------------------------------------------- 651 SLresult android_fx_statusToResult(android::status_t status) { 652 653 if ((android::INVALID_OPERATION == status) || (android::DEAD_OBJECT == status)) { 654 return SL_RESULT_CONTROL_LOST; 655 } else { 656 return SL_RESULT_SUCCESS; 657 } 658 } 659 660 661 //----------------------------------------------------------------------------- 662 bool android_fx_initEffectObj(audio_session_t sessionId, android::sp<android::AudioEffect>& effect, 663 const effect_uuid_t *type) { 664 //SL_LOGV("android_fx_initEffectObj on session %d", sessionId); 665 666 effect = new android::AudioEffect(type, android::String16(), EFFECT_UUID_NULL, 667 0,// priority 668 0,// effect callback 669 0,// callback data 670 sessionId,// session ID 671 0 );// output 672 673 android::status_t status = effect->initCheck(); 674 if (android::NO_ERROR != status) { 675 effect.clear(); 676 SL_LOGE("Effect initCheck() returned %d", status); 677 return false; 678 } 679 680 return true; 681 } 682 683 684 //----------------------------------------------------------------------------- 685 bool android_fx_initEffectDescriptor(const SLInterfaceID effectId, 686 effect_descriptor_t* fxDescrLoc) { 687 uint32_t numEffects = 0; 688 effect_descriptor_t descriptor; 689 bool foundEffect = false; 690 691 // any effects? 692 android::status_t res = android::AudioEffect::queryNumberEffects(&numEffects); 693 if (android::NO_ERROR != res) { 694 SL_LOGE("unable to find any effects."); 695 goto effectError; 696 } 697 698 // request effect in the effects? 699 for (uint32_t i=0 ; i < numEffects ; i++) { 700 res = android::AudioEffect::queryEffect(i, &descriptor); 701 if ((android::NO_ERROR == res) && 702 (0 == memcmp(effectId, &descriptor.type, sizeof(effect_uuid_t)))) { 703 SL_LOGV("found effect %d %s", i, descriptor.name); 704 foundEffect = true; 705 break; 706 } 707 } 708 if (foundEffect) { 709 memcpy(fxDescrLoc, &descriptor, sizeof(effect_descriptor_t)); 710 } else { 711 SL_LOGE("unable to find an implementation for the requested effect."); 712 goto effectError; 713 } 714 715 return true; 716 717 effectError: 718 // the requested effect wasn't found 719 memset(fxDescrLoc, 0, sizeof(effect_descriptor_t)); 720 721 return false; 722 } 723 724 //----------------------------------------------------------------------------- 725 SLresult android_genericFx_queryNumEffects(SLuint32 *pNumSupportedAudioEffects) { 726 727 if (NULL == pNumSupportedAudioEffects) { 728 return SL_RESULT_PARAMETER_INVALID; 729 } 730 731 android::status_t status = 732 android::AudioEffect::queryNumberEffects((uint32_t*)pNumSupportedAudioEffects); 733 734 SLresult result = SL_RESULT_SUCCESS; 735 switch (status) { 736 case android::NO_ERROR: 737 result = SL_RESULT_SUCCESS; 738 break; 739 case android::PERMISSION_DENIED: 740 result = SL_RESULT_PERMISSION_DENIED; 741 break; 742 case android::NO_INIT: 743 result = SL_RESULT_RESOURCE_ERROR; 744 break; 745 case android::BAD_VALUE: 746 result = SL_RESULT_PARAMETER_INVALID; 747 break; 748 default: 749 result = SL_RESULT_INTERNAL_ERROR; 750 SL_LOGE("received invalid status %d from AudioEffect::queryNumberEffects()", status); 751 break; 752 } 753 return result; 754 } 755 756 757 //----------------------------------------------------------------------------- 758 SLresult android_genericFx_queryEffect(SLuint32 index, effect_descriptor_t* pDescriptor) { 759 760 if (NULL == pDescriptor) { 761 return SL_RESULT_PARAMETER_INVALID; 762 } 763 764 android::status_t status = 765 android::AudioEffect::queryEffect(index, pDescriptor); 766 767 SLresult result = SL_RESULT_SUCCESS; 768 if (android::NO_ERROR != status) { 769 switch (status) { 770 case android::PERMISSION_DENIED: 771 result = SL_RESULT_PERMISSION_DENIED; 772 break; 773 case android::NO_INIT: 774 case android::INVALID_OPERATION: 775 result = SL_RESULT_RESOURCE_ERROR; 776 break; 777 case android::BAD_VALUE: 778 result = SL_RESULT_PARAMETER_INVALID; 779 break; 780 default: 781 result = SL_RESULT_INTERNAL_ERROR; 782 SL_LOGE("received invalid status %d from AudioEffect::queryNumberEffects()", status); 783 break; 784 } 785 // an error occurred, reset the effect descriptor 786 memset(pDescriptor, 0, sizeof(effect_descriptor_t)); 787 } 788 789 return result; 790 } 791 792 793 //----------------------------------------------------------------------------- 794 SLresult android_genericFx_createEffect(IAndroidEffect* iae, SLInterfaceID pUuid, 795 audio_session_t sessionId) 796 { 797 798 SLresult result = SL_RESULT_SUCCESS; 799 800 // does this effect already exist? 801 if (0 <= iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid))) { 802 return result; 803 } 804 805 // create new effect 806 android::sp<android::AudioEffect> pFx = new android::AudioEffect( 807 NULL, // not using type to create effect 808 android::String16(), 809 (const effect_uuid_t*)pUuid, 810 0,// priority 811 0,// effect callback 812 0,// callback data 813 sessionId, 814 0 );// output 815 816 // verify effect was successfully created before storing it 817 android::status_t status = pFx->initCheck(); 818 if (android::NO_ERROR != status) { 819 SL_LOGE("AudioEffect initCheck() returned %d, effect will not be stored", status); 820 result = SL_RESULT_RESOURCE_ERROR; 821 } else { 822 SL_LOGV("AudioEffect successfully created on session %d", sessionId); 823 iae->mEffects->add(KEY_FROM_GUID(pUuid), pFx); 824 } 825 826 return result; 827 } 828 829 830 //----------------------------------------------------------------------------- 831 SLresult android_genericFx_releaseEffect(IAndroidEffect* iae, SLInterfaceID pUuid) { 832 833 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid)); 834 835 if (0 > index) { 836 return SL_RESULT_PARAMETER_INVALID; 837 } else { 838 iae->mEffects->removeItem(index); 839 return SL_RESULT_SUCCESS; 840 } 841 } 842 843 844 //----------------------------------------------------------------------------- 845 SLresult android_genericFx_setEnabled(IAndroidEffect* iae, SLInterfaceID pUuid, SLboolean enabled) { 846 847 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid)); 848 849 if (0 > index) { 850 return SL_RESULT_PARAMETER_INVALID; 851 } else { 852 android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index); 853 android::status_t status = pFx->setEnabled(SL_BOOLEAN_TRUE == enabled); 854 return android_fx_statusToResult(status); 855 } 856 } 857 858 859 //----------------------------------------------------------------------------- 860 SLresult android_genericFx_isEnabled(IAndroidEffect* iae, SLInterfaceID pUuid, SLboolean *pEnabled) 861 { 862 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid)); 863 864 if (0 > index) { 865 return SL_RESULT_PARAMETER_INVALID; 866 } else { 867 android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index); 868 *pEnabled = (SLboolean) pFx->getEnabled(); 869 return SL_RESULT_SUCCESS; 870 } 871 } 872 873 874 //----------------------------------------------------------------------------- 875 SLresult android_genericFx_sendCommand(IAndroidEffect* iae, SLInterfaceID pUuid, 876 SLuint32 command, SLuint32 commandSize, void* pCommandData, 877 SLuint32 *replySize, void *pReplyData) { 878 879 ssize_t index = iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid)); 880 881 if (0 > index) { 882 return SL_RESULT_PARAMETER_INVALID; 883 } else { 884 android::sp<android::AudioEffect> pFx = iae->mEffects->valueAt(index); 885 android::status_t status = pFx->command( 886 (uint32_t) command, 887 (uint32_t) commandSize, 888 pCommandData, 889 (uint32_t*)replySize, 890 pReplyData); 891 if (android::BAD_VALUE == status) { 892 return SL_RESULT_PARAMETER_INVALID; 893 } else { 894 return SL_RESULT_SUCCESS; 895 } 896 } 897 } 898 899 //----------------------------------------------------------------------------- 900 /** 901 * returns true if the given effect id is present in the AndroidEffect interface 902 */ 903 bool android_genericFx_hasEffect(IAndroidEffect* iae, SLInterfaceID pUuid) { 904 return( 0 <= iae->mEffects->indexOfKey(KEY_FROM_GUID(pUuid))); 905 } 906 907 //----------------------------------------------------------------------------- 908 static const int AEC_PARAM_SIZE_MAX = sizeof(effect_param_t) + (2 * sizeof(int32_t)); 909 /** 910 * returns the size in bytes of the value of each acoustic echo cancellation parameter 911 */ 912 uint32_t aec_valueSize(int32_t param) { 913 uint32_t size; 914 switch (param) { 915 case AEC_PARAM_ECHO_DELAY: 916 size = sizeof(int32_t); 917 break; 918 default: 919 size = sizeof(int32_t); 920 SL_LOGE("Trying to access an unknown Acoustic Echo Cancellation parameter %d", param); 921 break; 922 } 923 924 return size; 925 } 926 927 android::status_t android_aec_setParam(const android::sp<android::AudioEffect>& pFx, 928 int32_t param, void *pValue) { 929 return android_fx_setParam(pFx, param, AEC_PARAM_SIZE_MAX, 930 pValue, aec_valueSize(param)); 931 } 932 933 android::status_t android_aec_getParam(const android::sp<android::AudioEffect>& pFx, 934 int32_t param, void *pValue) { 935 return android_fx_getParam(pFx, param, AEC_PARAM_SIZE_MAX, 936 pValue, aec_valueSize(param)); 937 } 938 939 //----------------------------------------------------------------------------- 940 static const int AGC_PARAM_SIZE_MAX = sizeof(effect_param_t) + (2 * sizeof(int16_t)) + sizeof(bool); 941 /** 942 * returns the size in bytes of the value of each automatic gain control parameter 943 */ 944 uint32_t agc_valueSize(int32_t param) { 945 uint32_t size; 946 switch (param) { 947 case AGC_PARAM_TARGET_LEVEL: 948 case AGC_PARAM_COMP_GAIN: 949 size = sizeof(int16_t); 950 break; 951 case AGC_PARAM_LIMITER_ENA: 952 size = sizeof(bool); 953 break; 954 default: 955 size = sizeof(int32_t); 956 SL_LOGE("Trying to access an unknown Automatic Gain Control parameter %d", param); 957 break; 958 } 959 960 return size; 961 } 962 963 android::status_t android_agc_setParam(const android::sp<android::AudioEffect>& pFx, 964 int32_t param, void *pValue) { 965 return android_fx_setParam(pFx, param, AGC_PARAM_SIZE_MAX, 966 pValue, agc_valueSize(param)); 967 } 968 969 android::status_t android_agc_getParam(const android::sp<android::AudioEffect>& pFx, 970 int32_t param, void *pValue) { 971 return android_fx_getParam(pFx, param, AGC_PARAM_SIZE_MAX, 972 pValue, agc_valueSize(param)); 973 } 974 975 //----------------------------------------------------------------------------- 976 static const int NS_PARAM_SIZE_MAX = sizeof(effect_param_t) + 2 * sizeof(int32_t); 977 /** 978 * returns the size in bytes of the value of each noise suppression parameter 979 */ 980 uint32_t ns_valueSize(int32_t param) { 981 uint32_t size; 982 switch (param) { 983 case NS_PARAM_LEVEL: 984 size = sizeof(int32_t); 985 break; 986 default: 987 size = sizeof(int32_t); 988 SL_LOGE("Trying to access an unknown Noise suppression parameter %d", param); 989 break; 990 } 991 992 return size; 993 } 994 995 android::status_t android_ns_setParam(const android::sp<android::AudioEffect>& pFx, 996 int32_t param, void *pValue) 997 { 998 return android_fx_setParam(pFx, param, NS_PARAM_SIZE_MAX, 999 pValue, ns_valueSize(param)); 1000 } 1001 1002 android::status_t android_ns_getParam(const android::sp<android::AudioEffect>& pFx, 1003 int32_t param, void *pValue) 1004 { 1005 return android_fx_getParam(pFx, param, NS_PARAM_SIZE_MAX, 1006 pValue, ns_valueSize(param)); 1007 } 1008