1 /* 2 ** 3 ** Copyright 2007, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #define LOG_TAG "AudioMixer" 19 //#define LOG_NDEBUG 0 20 21 #include "Configuration.h" 22 #include <stdint.h> 23 #include <string.h> 24 #include <stdlib.h> 25 #include <math.h> 26 #include <sys/types.h> 27 28 #include <utils/Errors.h> 29 #include <utils/Log.h> 30 31 #include <cutils/bitops.h> 32 #include <cutils/compiler.h> 33 #include <utils/Debug.h> 34 35 #include <system/audio.h> 36 37 #include <audio_utils/primitives.h> 38 #include <audio_utils/format.h> 39 #include <common_time/local_clock.h> 40 #include <common_time/cc_helper.h> 41 42 #include <media/EffectsFactoryApi.h> 43 #include <audio_effects/effect_downmix.h> 44 45 #include "AudioMixerOps.h" 46 #include "AudioMixer.h" 47 48 // The FCC_2 macro refers to the Fixed Channel Count of 2 for the legacy integer mixer. 49 #ifndef FCC_2 50 #define FCC_2 2 51 #endif 52 53 // Look for MONO_HACK for any Mono hack involving legacy mono channel to 54 // stereo channel conversion. 55 56 /* VERY_VERY_VERBOSE_LOGGING will show exactly which process hook and track hook is 57 * being used. This is a considerable amount of log spam, so don't enable unless you 58 * are verifying the hook based code. 59 */ 60 //#define VERY_VERY_VERBOSE_LOGGING 61 #ifdef VERY_VERY_VERBOSE_LOGGING 62 #define ALOGVV ALOGV 63 //define ALOGVV printf // for test-mixer.cpp 64 #else 65 #define ALOGVV(a...) do { } while (0) 66 #endif 67 68 #ifndef ARRAY_SIZE 69 #define ARRAY_SIZE(x) (sizeof(x)/sizeof((x)[0])) 70 #endif 71 72 // Set kUseNewMixer to true to use the new mixer engine. Otherwise the 73 // original code will be used. This is false for now. 74 static const bool kUseNewMixer = false; 75 76 // Set kUseFloat to true to allow floating input into the mixer engine. 77 // If kUseNewMixer is false, this is ignored or may be overridden internally 78 // because of downmix/upmix support. 79 static const bool kUseFloat = true; 80 81 // Set to default copy buffer size in frames for input processing. 82 static const size_t kCopyBufferFrameCount = 256; 83 84 namespace android { 85 86 // ---------------------------------------------------------------------------- 87 88 template <typename T> 89 T min(const T& a, const T& b) 90 { 91 return a < b ? a : b; 92 } 93 94 AudioMixer::CopyBufferProvider::CopyBufferProvider(size_t inputFrameSize, 95 size_t outputFrameSize, size_t bufferFrameCount) : 96 mInputFrameSize(inputFrameSize), 97 mOutputFrameSize(outputFrameSize), 98 mLocalBufferFrameCount(bufferFrameCount), 99 mLocalBufferData(NULL), 100 mConsumed(0) 101 { 102 ALOGV("CopyBufferProvider(%p)(%zu, %zu, %zu)", this, 103 inputFrameSize, outputFrameSize, bufferFrameCount); 104 LOG_ALWAYS_FATAL_IF(inputFrameSize < outputFrameSize && bufferFrameCount == 0, 105 "Requires local buffer if inputFrameSize(%zu) < outputFrameSize(%zu)", 106 inputFrameSize, outputFrameSize); 107 if (mLocalBufferFrameCount) { 108 (void)posix_memalign(&mLocalBufferData, 32, mLocalBufferFrameCount * mOutputFrameSize); 109 } 110 mBuffer.frameCount = 0; 111 } 112 113 AudioMixer::CopyBufferProvider::~CopyBufferProvider() 114 { 115 ALOGV("~CopyBufferProvider(%p)", this); 116 if (mBuffer.frameCount != 0) { 117 mTrackBufferProvider->releaseBuffer(&mBuffer); 118 } 119 free(mLocalBufferData); 120 } 121 122 status_t AudioMixer::CopyBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer, 123 int64_t pts) 124 { 125 //ALOGV("CopyBufferProvider(%p)::getNextBuffer(%p (%zu), %lld)", 126 // this, pBuffer, pBuffer->frameCount, pts); 127 if (mLocalBufferFrameCount == 0) { 128 status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts); 129 if (res == OK) { 130 copyFrames(pBuffer->raw, pBuffer->raw, pBuffer->frameCount); 131 } 132 return res; 133 } 134 if (mBuffer.frameCount == 0) { 135 mBuffer.frameCount = pBuffer->frameCount; 136 status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer, pts); 137 // At one time an upstream buffer provider had 138 // res == OK and mBuffer.frameCount == 0, doesn't seem to happen now 7/18/2014. 139 // 140 // By API spec, if res != OK, then mBuffer.frameCount == 0. 141 // but there may be improper implementations. 142 ALOG_ASSERT(res == OK || mBuffer.frameCount == 0); 143 if (res != OK || mBuffer.frameCount == 0) { // not needed by API spec, but to be safe. 144 pBuffer->raw = NULL; 145 pBuffer->frameCount = 0; 146 return res; 147 } 148 mConsumed = 0; 149 } 150 ALOG_ASSERT(mConsumed < mBuffer.frameCount); 151 size_t count = min(mLocalBufferFrameCount, mBuffer.frameCount - mConsumed); 152 count = min(count, pBuffer->frameCount); 153 pBuffer->raw = mLocalBufferData; 154 pBuffer->frameCount = count; 155 copyFrames(pBuffer->raw, (uint8_t*)mBuffer.raw + mConsumed * mInputFrameSize, 156 pBuffer->frameCount); 157 return OK; 158 } 159 160 void AudioMixer::CopyBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) 161 { 162 //ALOGV("CopyBufferProvider(%p)::releaseBuffer(%p(%zu))", 163 // this, pBuffer, pBuffer->frameCount); 164 if (mLocalBufferFrameCount == 0) { 165 mTrackBufferProvider->releaseBuffer(pBuffer); 166 return; 167 } 168 // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount"); 169 mConsumed += pBuffer->frameCount; // TODO: update for efficiency to reuse existing content 170 if (mConsumed != 0 && mConsumed >= mBuffer.frameCount) { 171 mTrackBufferProvider->releaseBuffer(&mBuffer); 172 ALOG_ASSERT(mBuffer.frameCount == 0); 173 } 174 pBuffer->raw = NULL; 175 pBuffer->frameCount = 0; 176 } 177 178 void AudioMixer::CopyBufferProvider::reset() 179 { 180 if (mBuffer.frameCount != 0) { 181 mTrackBufferProvider->releaseBuffer(&mBuffer); 182 } 183 mConsumed = 0; 184 } 185 186 AudioMixer::DownmixerBufferProvider::DownmixerBufferProvider( 187 audio_channel_mask_t inputChannelMask, 188 audio_channel_mask_t outputChannelMask, audio_format_t format, 189 uint32_t sampleRate, int32_t sessionId, size_t bufferFrameCount) : 190 CopyBufferProvider( 191 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(inputChannelMask), 192 audio_bytes_per_sample(format) * audio_channel_count_from_out_mask(outputChannelMask), 193 bufferFrameCount) // set bufferFrameCount to 0 to do in-place 194 { 195 ALOGV("DownmixerBufferProvider(%p)(%#x, %#x, %#x %u %d)", 196 this, inputChannelMask, outputChannelMask, format, 197 sampleRate, sessionId); 198 if (!sIsMultichannelCapable 199 || EffectCreate(&sDwnmFxDesc.uuid, 200 sessionId, 201 SESSION_ID_INVALID_AND_IGNORED, 202 &mDownmixHandle) != 0) { 203 ALOGE("DownmixerBufferProvider() error creating downmixer effect"); 204 mDownmixHandle = NULL; 205 return; 206 } 207 // channel input configuration will be overridden per-track 208 mDownmixConfig.inputCfg.channels = inputChannelMask; // FIXME: Should be bits 209 mDownmixConfig.outputCfg.channels = outputChannelMask; // FIXME: should be bits 210 mDownmixConfig.inputCfg.format = format; 211 mDownmixConfig.outputCfg.format = format; 212 mDownmixConfig.inputCfg.samplingRate = sampleRate; 213 mDownmixConfig.outputCfg.samplingRate = sampleRate; 214 mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; 215 mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; 216 // input and output buffer provider, and frame count will not be used as the downmix effect 217 // process() function is called directly (see DownmixerBufferProvider::getNextBuffer()) 218 mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | 219 EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE; 220 mDownmixConfig.outputCfg.mask = mDownmixConfig.inputCfg.mask; 221 222 int cmdStatus; 223 uint32_t replySize = sizeof(int); 224 225 // Configure downmixer 226 status_t status = (*mDownmixHandle)->command(mDownmixHandle, 227 EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/, 228 &mDownmixConfig /*pCmdData*/, 229 &replySize, &cmdStatus /*pReplyData*/); 230 if (status != 0 || cmdStatus != 0) { 231 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while configuring downmixer", 232 status, cmdStatus); 233 EffectRelease(mDownmixHandle); 234 mDownmixHandle = NULL; 235 return; 236 } 237 238 // Enable downmixer 239 replySize = sizeof(int); 240 status = (*mDownmixHandle)->command(mDownmixHandle, 241 EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/, 242 &replySize, &cmdStatus /*pReplyData*/); 243 if (status != 0 || cmdStatus != 0) { 244 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while enabling downmixer", 245 status, cmdStatus); 246 EffectRelease(mDownmixHandle); 247 mDownmixHandle = NULL; 248 return; 249 } 250 251 // Set downmix type 252 // parameter size rounded for padding on 32bit boundary 253 const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int); 254 const int downmixParamSize = 255 sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t); 256 effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize); 257 param->psize = sizeof(downmix_params_t); 258 const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE; 259 memcpy(param->data, &downmixParam, param->psize); 260 const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD; 261 param->vsize = sizeof(downmix_type_t); 262 memcpy(param->data + psizePadded, &downmixType, param->vsize); 263 replySize = sizeof(int); 264 status = (*mDownmixHandle)->command(mDownmixHandle, 265 EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize /* cmdSize */, 266 param /*pCmdData*/, &replySize, &cmdStatus /*pReplyData*/); 267 free(param); 268 if (status != 0 || cmdStatus != 0) { 269 ALOGE("DownmixerBufferProvider() error %d cmdStatus %d while setting downmix type", 270 status, cmdStatus); 271 EffectRelease(mDownmixHandle); 272 mDownmixHandle = NULL; 273 return; 274 } 275 ALOGV("DownmixerBufferProvider() downmix type set to %d", (int) downmixType); 276 } 277 278 AudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider() 279 { 280 ALOGV("~DownmixerBufferProvider (%p)", this); 281 EffectRelease(mDownmixHandle); 282 mDownmixHandle = NULL; 283 } 284 285 void AudioMixer::DownmixerBufferProvider::copyFrames(void *dst, const void *src, size_t frames) 286 { 287 mDownmixConfig.inputCfg.buffer.frameCount = frames; 288 mDownmixConfig.inputCfg.buffer.raw = const_cast<void *>(src); 289 mDownmixConfig.outputCfg.buffer.frameCount = frames; 290 mDownmixConfig.outputCfg.buffer.raw = dst; 291 // may be in-place if src == dst. 292 status_t res = (*mDownmixHandle)->process(mDownmixHandle, 293 &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer); 294 ALOGE_IF(res != OK, "DownmixBufferProvider error %d", res); 295 } 296 297 /* call once in a pthread_once handler. */ 298 /*static*/ status_t AudioMixer::DownmixerBufferProvider::init() 299 { 300 // find multichannel downmix effect if we have to play multichannel content 301 uint32_t numEffects = 0; 302 int ret = EffectQueryNumberEffects(&numEffects); 303 if (ret != 0) { 304 ALOGE("AudioMixer() error %d querying number of effects", ret); 305 return NO_INIT; 306 } 307 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects); 308 309 for (uint32_t i = 0 ; i < numEffects ; i++) { 310 if (EffectQueryEffect(i, &sDwnmFxDesc) == 0) { 311 ALOGV("effect %d is called %s", i, sDwnmFxDesc.name); 312 if (memcmp(&sDwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) { 313 ALOGI("found effect \"%s\" from %s", 314 sDwnmFxDesc.name, sDwnmFxDesc.implementor); 315 sIsMultichannelCapable = true; 316 break; 317 } 318 } 319 } 320 ALOGW_IF(!sIsMultichannelCapable, "unable to find downmix effect"); 321 return NO_INIT; 322 } 323 324 /*static*/ bool AudioMixer::DownmixerBufferProvider::sIsMultichannelCapable = false; 325 /*static*/ effect_descriptor_t AudioMixer::DownmixerBufferProvider::sDwnmFxDesc; 326 327 AudioMixer::RemixBufferProvider::RemixBufferProvider(audio_channel_mask_t inputChannelMask, 328 audio_channel_mask_t outputChannelMask, audio_format_t format, 329 size_t bufferFrameCount) : 330 CopyBufferProvider( 331 audio_bytes_per_sample(format) 332 * audio_channel_count_from_out_mask(inputChannelMask), 333 audio_bytes_per_sample(format) 334 * audio_channel_count_from_out_mask(outputChannelMask), 335 bufferFrameCount), 336 mFormat(format), 337 mSampleSize(audio_bytes_per_sample(format)), 338 mInputChannels(audio_channel_count_from_out_mask(inputChannelMask)), 339 mOutputChannels(audio_channel_count_from_out_mask(outputChannelMask)) 340 { 341 ALOGV("RemixBufferProvider(%p)(%#x, %#x, %#x) %zu %zu", 342 this, format, inputChannelMask, outputChannelMask, 343 mInputChannels, mOutputChannels); 344 // TODO: consider channel representation in index array formulation 345 // We ignore channel representation, and just use the bits. 346 memcpy_by_index_array_initialization(mIdxAry, ARRAY_SIZE(mIdxAry), 347 audio_channel_mask_get_bits(outputChannelMask), 348 audio_channel_mask_get_bits(inputChannelMask)); 349 } 350 351 void AudioMixer::RemixBufferProvider::copyFrames(void *dst, const void *src, size_t frames) 352 { 353 memcpy_by_index_array(dst, mOutputChannels, 354 src, mInputChannels, mIdxAry, mSampleSize, frames); 355 } 356 357 AudioMixer::ReformatBufferProvider::ReformatBufferProvider(int32_t channels, 358 audio_format_t inputFormat, audio_format_t outputFormat, 359 size_t bufferFrameCount) : 360 CopyBufferProvider( 361 channels * audio_bytes_per_sample(inputFormat), 362 channels * audio_bytes_per_sample(outputFormat), 363 bufferFrameCount), 364 mChannels(channels), 365 mInputFormat(inputFormat), 366 mOutputFormat(outputFormat) 367 { 368 ALOGV("ReformatBufferProvider(%p)(%d, %#x, %#x)", this, channels, inputFormat, outputFormat); 369 } 370 371 void AudioMixer::ReformatBufferProvider::copyFrames(void *dst, const void *src, size_t frames) 372 { 373 memcpy_by_audio_format(dst, mOutputFormat, src, mInputFormat, frames * mChannels); 374 } 375 376 // ---------------------------------------------------------------------------- 377 378 // Ensure mConfiguredNames bitmask is initialized properly on all architectures. 379 // The value of 1 << x is undefined in C when x >= 32. 380 381 AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks) 382 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1), 383 mSampleRate(sampleRate) 384 { 385 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u", 386 maxNumTracks, MAX_NUM_TRACKS); 387 388 // AudioMixer is not yet capable of more than 32 active track inputs 389 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS); 390 391 pthread_once(&sOnceControl, &sInitRoutine); 392 393 mState.enabledTracks= 0; 394 mState.needsChanged = 0; 395 mState.frameCount = frameCount; 396 mState.hook = process__nop; 397 mState.outputTemp = NULL; 398 mState.resampleTemp = NULL; 399 mState.mLog = &mDummyLog; 400 // mState.reserved 401 402 // FIXME Most of the following initialization is probably redundant since 403 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0 404 // and mTrackNames is initially 0. However, leave it here until that's verified. 405 track_t* t = mState.tracks; 406 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 407 t->resampler = NULL; 408 t->downmixerBufferProvider = NULL; 409 t->mReformatBufferProvider = NULL; 410 t++; 411 } 412 413 } 414 415 AudioMixer::~AudioMixer() 416 { 417 track_t* t = mState.tracks; 418 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 419 delete t->resampler; 420 delete t->downmixerBufferProvider; 421 delete t->mReformatBufferProvider; 422 t++; 423 } 424 delete [] mState.outputTemp; 425 delete [] mState.resampleTemp; 426 } 427 428 void AudioMixer::setLog(NBLog::Writer *log) 429 { 430 mState.mLog = log; 431 } 432 433 int AudioMixer::getTrackName(audio_channel_mask_t channelMask, 434 audio_format_t format, int sessionId) 435 { 436 if (!isValidPcmTrackFormat(format)) { 437 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format); 438 return -1; 439 } 440 uint32_t names = (~mTrackNames) & mConfiguredNames; 441 if (names != 0) { 442 int n = __builtin_ctz(names); 443 ALOGV("add track (%d)", n); 444 // assume default parameters for the track, except where noted below 445 track_t* t = &mState.tracks[n]; 446 t->needs = 0; 447 448 // Integer volume. 449 // Currently integer volume is kept for the legacy integer mixer. 450 // Will be removed when the legacy mixer path is removed. 451 t->volume[0] = UNITY_GAIN_INT; 452 t->volume[1] = UNITY_GAIN_INT; 453 t->prevVolume[0] = UNITY_GAIN_INT << 16; 454 t->prevVolume[1] = UNITY_GAIN_INT << 16; 455 t->volumeInc[0] = 0; 456 t->volumeInc[1] = 0; 457 t->auxLevel = 0; 458 t->auxInc = 0; 459 t->prevAuxLevel = 0; 460 461 // Floating point volume. 462 t->mVolume[0] = UNITY_GAIN_FLOAT; 463 t->mVolume[1] = UNITY_GAIN_FLOAT; 464 t->mPrevVolume[0] = UNITY_GAIN_FLOAT; 465 t->mPrevVolume[1] = UNITY_GAIN_FLOAT; 466 t->mVolumeInc[0] = 0.; 467 t->mVolumeInc[1] = 0.; 468 t->mAuxLevel = 0.; 469 t->mAuxInc = 0.; 470 t->mPrevAuxLevel = 0.; 471 472 // no initialization needed 473 // t->frameCount 474 t->channelCount = audio_channel_count_from_out_mask(channelMask); 475 t->enabled = false; 476 ALOGV_IF(audio_channel_mask_get_bits(channelMask) != AUDIO_CHANNEL_OUT_STEREO, 477 "Non-stereo channel mask: %d\n", channelMask); 478 t->channelMask = channelMask; 479 t->sessionId = sessionId; 480 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name) 481 t->bufferProvider = NULL; 482 t->buffer.raw = NULL; 483 // no initialization needed 484 // t->buffer.frameCount 485 t->hook = NULL; 486 t->in = NULL; 487 t->resampler = NULL; 488 t->sampleRate = mSampleRate; 489 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name) 490 t->mainBuffer = NULL; 491 t->auxBuffer = NULL; 492 t->mInputBufferProvider = NULL; 493 t->mReformatBufferProvider = NULL; 494 t->downmixerBufferProvider = NULL; 495 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT; 496 t->mFormat = format; 497 t->mMixerInFormat = kUseFloat && kUseNewMixer 498 ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT; 499 t->mMixerChannelMask = audio_channel_mask_from_representation_and_bits( 500 AUDIO_CHANNEL_REPRESENTATION_POSITION, AUDIO_CHANNEL_OUT_STEREO); 501 t->mMixerChannelCount = audio_channel_count_from_out_mask(t->mMixerChannelMask); 502 // Check the downmixing (or upmixing) requirements. 503 status_t status = initTrackDownmix(t, n); 504 if (status != OK) { 505 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask); 506 return -1; 507 } 508 // initTrackDownmix() may change the input format requirement. 509 // If you desire floating point input to the mixer, it may change 510 // to integer because the downmixer requires integer to process. 511 ALOGVV("mMixerFormat:%#x mMixerInFormat:%#x\n", t->mMixerFormat, t->mMixerInFormat); 512 prepareTrackForReformat(t, n); 513 mTrackNames |= 1 << n; 514 return TRACK0 + n; 515 } 516 ALOGE("AudioMixer::getTrackName out of available tracks"); 517 return -1; 518 } 519 520 void AudioMixer::invalidateState(uint32_t mask) 521 { 522 if (mask != 0) { 523 mState.needsChanged |= mask; 524 mState.hook = process__validate; 525 } 526 } 527 528 // Called when channel masks have changed for a track name 529 // TODO: Fix Downmixbufferprofider not to (possibly) change mixer input format, 530 // which will simplify this logic. 531 bool AudioMixer::setChannelMasks(int name, 532 audio_channel_mask_t trackChannelMask, audio_channel_mask_t mixerChannelMask) { 533 track_t &track = mState.tracks[name]; 534 535 if (trackChannelMask == track.channelMask 536 && mixerChannelMask == track.mMixerChannelMask) { 537 return false; // no need to change 538 } 539 // always recompute for both channel masks even if only one has changed. 540 const uint32_t trackChannelCount = audio_channel_count_from_out_mask(trackChannelMask); 541 const uint32_t mixerChannelCount = audio_channel_count_from_out_mask(mixerChannelMask); 542 const bool mixerChannelCountChanged = track.mMixerChannelCount != mixerChannelCount; 543 544 ALOG_ASSERT((trackChannelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) 545 && trackChannelCount 546 && mixerChannelCount); 547 track.channelMask = trackChannelMask; 548 track.channelCount = trackChannelCount; 549 track.mMixerChannelMask = mixerChannelMask; 550 track.mMixerChannelCount = mixerChannelCount; 551 552 // channel masks have changed, does this track need a downmixer? 553 // update to try using our desired format (if we aren't already using it) 554 const audio_format_t prevMixerInFormat = track.mMixerInFormat; 555 track.mMixerInFormat = kUseFloat && kUseNewMixer 556 ? AUDIO_FORMAT_PCM_FLOAT : AUDIO_FORMAT_PCM_16_BIT; 557 const status_t status = initTrackDownmix(&mState.tracks[name], name); 558 ALOGE_IF(status != OK, 559 "initTrackDownmix error %d, track channel mask %#x, mixer channel mask %#x", 560 status, track.channelMask, track.mMixerChannelMask); 561 562 const bool mixerInFormatChanged = prevMixerInFormat != track.mMixerInFormat; 563 if (mixerInFormatChanged) { 564 prepareTrackForReformat(&track, name); // because of downmixer, track format may change! 565 } 566 567 if (track.resampler && (mixerInFormatChanged || mixerChannelCountChanged)) { 568 // resampler input format or channels may have changed. 569 const uint32_t resetToSampleRate = track.sampleRate; 570 delete track.resampler; 571 track.resampler = NULL; 572 track.sampleRate = mSampleRate; // without resampler, track rate is device sample rate. 573 // recreate the resampler with updated format, channels, saved sampleRate. 574 track.setResampler(resetToSampleRate /*trackSampleRate*/, mSampleRate /*devSampleRate*/); 575 } 576 return true; 577 } 578 579 status_t AudioMixer::initTrackDownmix(track_t* pTrack, int trackName) 580 { 581 // Only remix (upmix or downmix) if the track and mixer/device channel masks 582 // are not the same and not handled internally, as mono -> stereo currently is. 583 if (pTrack->channelMask != pTrack->mMixerChannelMask 584 && !(pTrack->channelMask == AUDIO_CHANNEL_OUT_MONO 585 && pTrack->mMixerChannelMask == AUDIO_CHANNEL_OUT_STEREO)) { 586 return prepareTrackForDownmix(pTrack, trackName); 587 } 588 // no remix necessary 589 unprepareTrackForDownmix(pTrack, trackName); 590 return NO_ERROR; 591 } 592 593 void AudioMixer::unprepareTrackForDownmix(track_t* pTrack, int trackName __unused) { 594 ALOGV("AudioMixer::unprepareTrackForDownmix(%d)", trackName); 595 596 if (pTrack->downmixerBufferProvider != NULL) { 597 // this track had previously been configured with a downmixer, delete it 598 ALOGV(" deleting old downmixer"); 599 delete pTrack->downmixerBufferProvider; 600 pTrack->downmixerBufferProvider = NULL; 601 reconfigureBufferProviders(pTrack); 602 } else { 603 ALOGV(" nothing to do, no downmixer to delete"); 604 } 605 } 606 607 status_t AudioMixer::prepareTrackForDownmix(track_t* pTrack, int trackName) 608 { 609 ALOGV("AudioMixer::prepareTrackForDownmix(%d) with mask 0x%x", trackName, pTrack->channelMask); 610 611 // discard the previous downmixer if there was one 612 unprepareTrackForDownmix(pTrack, trackName); 613 if (DownmixerBufferProvider::isMultichannelCapable()) { 614 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(pTrack->channelMask, 615 pTrack->mMixerChannelMask, 616 AUDIO_FORMAT_PCM_16_BIT /* TODO: use pTrack->mMixerInFormat, now only PCM 16 */, 617 pTrack->sampleRate, pTrack->sessionId, kCopyBufferFrameCount); 618 619 if (pDbp->isValid()) { // if constructor completed properly 620 pTrack->mMixerInFormat = AUDIO_FORMAT_PCM_16_BIT; // PCM 16 bit required for downmix 621 pTrack->downmixerBufferProvider = pDbp; 622 reconfigureBufferProviders(pTrack); 623 return NO_ERROR; 624 } 625 delete pDbp; 626 } 627 628 // Effect downmixer does not accept the channel conversion. Let's use our remixer. 629 RemixBufferProvider* pRbp = new RemixBufferProvider(pTrack->channelMask, 630 pTrack->mMixerChannelMask, pTrack->mMixerInFormat, kCopyBufferFrameCount); 631 // Remix always finds a conversion whereas Downmixer effect above may fail. 632 pTrack->downmixerBufferProvider = pRbp; 633 reconfigureBufferProviders(pTrack); 634 return NO_ERROR; 635 } 636 637 void AudioMixer::unprepareTrackForReformat(track_t* pTrack, int trackName __unused) { 638 ALOGV("AudioMixer::unprepareTrackForReformat(%d)", trackName); 639 if (pTrack->mReformatBufferProvider != NULL) { 640 delete pTrack->mReformatBufferProvider; 641 pTrack->mReformatBufferProvider = NULL; 642 reconfigureBufferProviders(pTrack); 643 } 644 } 645 646 status_t AudioMixer::prepareTrackForReformat(track_t* pTrack, int trackName) 647 { 648 ALOGV("AudioMixer::prepareTrackForReformat(%d) with format %#x", trackName, pTrack->mFormat); 649 // discard the previous reformatter if there was one 650 unprepareTrackForReformat(pTrack, trackName); 651 // only configure reformatter if needed 652 if (pTrack->mFormat != pTrack->mMixerInFormat) { 653 pTrack->mReformatBufferProvider = new ReformatBufferProvider( 654 audio_channel_count_from_out_mask(pTrack->channelMask), 655 pTrack->mFormat, pTrack->mMixerInFormat, 656 kCopyBufferFrameCount); 657 reconfigureBufferProviders(pTrack); 658 } 659 return NO_ERROR; 660 } 661 662 void AudioMixer::reconfigureBufferProviders(track_t* pTrack) 663 { 664 pTrack->bufferProvider = pTrack->mInputBufferProvider; 665 if (pTrack->mReformatBufferProvider) { 666 pTrack->mReformatBufferProvider->setBufferProvider(pTrack->bufferProvider); 667 pTrack->bufferProvider = pTrack->mReformatBufferProvider; 668 } 669 if (pTrack->downmixerBufferProvider) { 670 pTrack->downmixerBufferProvider->setBufferProvider(pTrack->bufferProvider); 671 pTrack->bufferProvider = pTrack->downmixerBufferProvider; 672 } 673 } 674 675 void AudioMixer::deleteTrackName(int name) 676 { 677 ALOGV("AudioMixer::deleteTrackName(%d)", name); 678 name -= TRACK0; 679 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 680 ALOGV("deleteTrackName(%d)", name); 681 track_t& track(mState.tracks[ name ]); 682 if (track.enabled) { 683 track.enabled = false; 684 invalidateState(1<<name); 685 } 686 // delete the resampler 687 delete track.resampler; 688 track.resampler = NULL; 689 // delete the downmixer 690 unprepareTrackForDownmix(&mState.tracks[name], name); 691 // delete the reformatter 692 unprepareTrackForReformat(&mState.tracks[name], name); 693 694 mTrackNames &= ~(1<<name); 695 } 696 697 void AudioMixer::enable(int name) 698 { 699 name -= TRACK0; 700 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 701 track_t& track = mState.tracks[name]; 702 703 if (!track.enabled) { 704 track.enabled = true; 705 ALOGV("enable(%d)", name); 706 invalidateState(1 << name); 707 } 708 } 709 710 void AudioMixer::disable(int name) 711 { 712 name -= TRACK0; 713 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 714 track_t& track = mState.tracks[name]; 715 716 if (track.enabled) { 717 track.enabled = false; 718 ALOGV("disable(%d)", name); 719 invalidateState(1 << name); 720 } 721 } 722 723 /* Sets the volume ramp variables for the AudioMixer. 724 * 725 * The volume ramp variables are used to transition from the previous 726 * volume to the set volume. ramp controls the duration of the transition. 727 * Its value is typically one state framecount period, but may also be 0, 728 * meaning "immediate." 729 * 730 * FIXME: 1) Volume ramp is enabled only if there is a nonzero integer increment 731 * even if there is a nonzero floating point increment (in that case, the volume 732 * change is immediate). This restriction should be changed when the legacy mixer 733 * is removed (see #2). 734 * FIXME: 2) Integer volume variables are used for Legacy mixing and should be removed 735 * when no longer needed. 736 * 737 * @param newVolume set volume target in floating point [0.0, 1.0]. 738 * @param ramp number of frames to increment over. if ramp is 0, the volume 739 * should be set immediately. Currently ramp should not exceed 65535 (frames). 740 * @param pIntSetVolume pointer to the U4.12 integer target volume, set on return. 741 * @param pIntPrevVolume pointer to the U4.28 integer previous volume, set on return. 742 * @param pIntVolumeInc pointer to the U4.28 increment per output audio frame, set on return. 743 * @param pSetVolume pointer to the float target volume, set on return. 744 * @param pPrevVolume pointer to the float previous volume, set on return. 745 * @param pVolumeInc pointer to the float increment per output audio frame, set on return. 746 * @return true if the volume has changed, false if volume is same. 747 */ 748 static inline bool setVolumeRampVariables(float newVolume, int32_t ramp, 749 int16_t *pIntSetVolume, int32_t *pIntPrevVolume, int32_t *pIntVolumeInc, 750 float *pSetVolume, float *pPrevVolume, float *pVolumeInc) { 751 if (newVolume == *pSetVolume) { 752 return false; 753 } 754 /* set the floating point volume variables */ 755 if (ramp != 0) { 756 *pVolumeInc = (newVolume - *pSetVolume) / ramp; 757 *pPrevVolume = *pSetVolume; 758 } else { 759 *pVolumeInc = 0; 760 *pPrevVolume = newVolume; 761 } 762 *pSetVolume = newVolume; 763 764 /* set the legacy integer volume variables */ 765 int32_t intVolume = newVolume * AudioMixer::UNITY_GAIN_INT; 766 if (intVolume > AudioMixer::UNITY_GAIN_INT) { 767 intVolume = AudioMixer::UNITY_GAIN_INT; 768 } else if (intVolume < 0) { 769 ALOGE("negative volume %.7g", newVolume); 770 intVolume = 0; // should never happen, but for safety check. 771 } 772 if (intVolume == *pIntSetVolume) { 773 *pIntVolumeInc = 0; 774 /* TODO: integer/float workaround: ignore floating volume ramp */ 775 *pVolumeInc = 0; 776 *pPrevVolume = newVolume; 777 return true; 778 } 779 if (ramp != 0) { 780 *pIntVolumeInc = ((intVolume - *pIntSetVolume) << 16) / ramp; 781 *pIntPrevVolume = (*pIntVolumeInc == 0 ? intVolume : *pIntSetVolume) << 16; 782 } else { 783 *pIntVolumeInc = 0; 784 *pIntPrevVolume = intVolume << 16; 785 } 786 *pIntSetVolume = intVolume; 787 return true; 788 } 789 790 void AudioMixer::setParameter(int name, int target, int param, void *value) 791 { 792 name -= TRACK0; 793 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 794 track_t& track = mState.tracks[name]; 795 796 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value)); 797 int32_t *valueBuf = reinterpret_cast<int32_t*>(value); 798 799 switch (target) { 800 801 case TRACK: 802 switch (param) { 803 case CHANNEL_MASK: { 804 const audio_channel_mask_t trackChannelMask = 805 static_cast<audio_channel_mask_t>(valueInt); 806 if (setChannelMasks(name, trackChannelMask, track.mMixerChannelMask)) { 807 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", trackChannelMask); 808 invalidateState(1 << name); 809 } 810 } break; 811 case MAIN_BUFFER: 812 if (track.mainBuffer != valueBuf) { 813 track.mainBuffer = valueBuf; 814 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); 815 invalidateState(1 << name); 816 } 817 break; 818 case AUX_BUFFER: 819 if (track.auxBuffer != valueBuf) { 820 track.auxBuffer = valueBuf; 821 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf); 822 invalidateState(1 << name); 823 } 824 break; 825 case FORMAT: { 826 audio_format_t format = static_cast<audio_format_t>(valueInt); 827 if (track.mFormat != format) { 828 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format); 829 track.mFormat = format; 830 ALOGV("setParameter(TRACK, FORMAT, %#x)", format); 831 prepareTrackForReformat(&track, name); 832 invalidateState(1 << name); 833 } 834 } break; 835 // FIXME do we want to support setting the downmix type from AudioFlinger? 836 // for a specific track? or per mixer? 837 /* case DOWNMIX_TYPE: 838 break */ 839 case MIXER_FORMAT: { 840 audio_format_t format = static_cast<audio_format_t>(valueInt); 841 if (track.mMixerFormat != format) { 842 track.mMixerFormat = format; 843 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format); 844 } 845 } break; 846 case MIXER_CHANNEL_MASK: { 847 const audio_channel_mask_t mixerChannelMask = 848 static_cast<audio_channel_mask_t>(valueInt); 849 if (setChannelMasks(name, track.channelMask, mixerChannelMask)) { 850 ALOGV("setParameter(TRACK, MIXER_CHANNEL_MASK, %#x)", mixerChannelMask); 851 invalidateState(1 << name); 852 } 853 } break; 854 default: 855 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param); 856 } 857 break; 858 859 case RESAMPLE: 860 switch (param) { 861 case SAMPLE_RATE: 862 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt); 863 if (track.setResampler(uint32_t(valueInt), mSampleRate)) { 864 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)", 865 uint32_t(valueInt)); 866 invalidateState(1 << name); 867 } 868 break; 869 case RESET: 870 track.resetResampler(); 871 invalidateState(1 << name); 872 break; 873 case REMOVE: 874 delete track.resampler; 875 track.resampler = NULL; 876 track.sampleRate = mSampleRate; 877 invalidateState(1 << name); 878 break; 879 default: 880 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param); 881 } 882 break; 883 884 case RAMP_VOLUME: 885 case VOLUME: 886 switch (param) { 887 case AUXLEVEL: 888 if (setVolumeRampVariables(*reinterpret_cast<float*>(value), 889 target == RAMP_VOLUME ? mState.frameCount : 0, 890 &track.auxLevel, &track.prevAuxLevel, &track.auxInc, 891 &track.mAuxLevel, &track.mPrevAuxLevel, &track.mAuxInc)) { 892 ALOGV("setParameter(%s, AUXLEVEL: %04x)", 893 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel); 894 invalidateState(1 << name); 895 } 896 break; 897 default: 898 if ((unsigned)param >= VOLUME0 && (unsigned)param < VOLUME0 + MAX_NUM_VOLUMES) { 899 if (setVolumeRampVariables(*reinterpret_cast<float*>(value), 900 target == RAMP_VOLUME ? mState.frameCount : 0, 901 &track.volume[param - VOLUME0], &track.prevVolume[param - VOLUME0], 902 &track.volumeInc[param - VOLUME0], 903 &track.mVolume[param - VOLUME0], &track.mPrevVolume[param - VOLUME0], 904 &track.mVolumeInc[param - VOLUME0])) { 905 ALOGV("setParameter(%s, VOLUME%d: %04x)", 906 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0, 907 track.volume[param - VOLUME0]); 908 invalidateState(1 << name); 909 } 910 } else { 911 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param); 912 } 913 } 914 break; 915 916 default: 917 LOG_ALWAYS_FATAL("setParameter: bad target %d", target); 918 } 919 } 920 921 bool AudioMixer::track_t::setResampler(uint32_t trackSampleRate, uint32_t devSampleRate) 922 { 923 if (trackSampleRate != devSampleRate || resampler != NULL) { 924 if (sampleRate != trackSampleRate) { 925 sampleRate = trackSampleRate; 926 if (resampler == NULL) { 927 ALOGV("Creating resampler from track %d Hz to device %d Hz", 928 trackSampleRate, devSampleRate); 929 AudioResampler::src_quality quality; 930 // force lowest quality level resampler if use case isn't music or video 931 // FIXME this is flawed for dynamic sample rates, as we choose the resampler 932 // quality level based on the initial ratio, but that could change later. 933 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios. 934 if (!((trackSampleRate == 44100 && devSampleRate == 48000) || 935 (trackSampleRate == 48000 && devSampleRate == 44100))) { 936 quality = AudioResampler::DYN_LOW_QUALITY; 937 } else { 938 quality = AudioResampler::DEFAULT_QUALITY; 939 } 940 941 // TODO: Remove MONO_HACK. Resampler sees #channels after the downmixer 942 // but if none exists, it is the channel count (1 for mono). 943 const int resamplerChannelCount = downmixerBufferProvider != NULL 944 ? mMixerChannelCount : channelCount; 945 ALOGVV("Creating resampler:" 946 " format(%#x) channels(%d) devSampleRate(%u) quality(%d)\n", 947 mMixerInFormat, resamplerChannelCount, devSampleRate, quality); 948 resampler = AudioResampler::create( 949 mMixerInFormat, 950 resamplerChannelCount, 951 devSampleRate, quality); 952 resampler->setLocalTimeFreq(sLocalTimeFreq); 953 } 954 return true; 955 } 956 } 957 return false; 958 } 959 960 /* Checks to see if the volume ramp has completed and clears the increment 961 * variables appropriately. 962 * 963 * FIXME: There is code to handle int/float ramp variable switchover should it not 964 * complete within a mixer buffer processing call, but it is preferred to avoid switchover 965 * due to precision issues. The switchover code is included for legacy code purposes 966 * and can be removed once the integer volume is removed. 967 * 968 * It is not sufficient to clear only the volumeInc integer variable because 969 * if one channel requires ramping, all channels are ramped. 970 * 971 * There is a bit of duplicated code here, but it keeps backward compatibility. 972 */ 973 inline void AudioMixer::track_t::adjustVolumeRamp(bool aux, bool useFloat) 974 { 975 if (useFloat) { 976 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) { 977 if (mVolumeInc[i] != 0 && fabs(mVolume[i] - mPrevVolume[i]) <= fabs(mVolumeInc[i])) { 978 volumeInc[i] = 0; 979 prevVolume[i] = volume[i] << 16; 980 mVolumeInc[i] = 0.; 981 mPrevVolume[i] = mVolume[i]; 982 } else { 983 //ALOGV("ramp: %f %f %f", mVolume[i], mPrevVolume[i], mVolumeInc[i]); 984 prevVolume[i] = u4_28_from_float(mPrevVolume[i]); 985 } 986 } 987 } else { 988 for (uint32_t i = 0; i < MAX_NUM_VOLUMES; i++) { 989 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) || 990 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) { 991 volumeInc[i] = 0; 992 prevVolume[i] = volume[i] << 16; 993 mVolumeInc[i] = 0.; 994 mPrevVolume[i] = mVolume[i]; 995 } else { 996 //ALOGV("ramp: %d %d %d", volume[i] << 16, prevVolume[i], volumeInc[i]); 997 mPrevVolume[i] = float_from_u4_28(prevVolume[i]); 998 } 999 } 1000 } 1001 /* TODO: aux is always integer regardless of output buffer type */ 1002 if (aux) { 1003 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) || 1004 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) { 1005 auxInc = 0; 1006 prevAuxLevel = auxLevel << 16; 1007 mAuxInc = 0.; 1008 mPrevAuxLevel = mAuxLevel; 1009 } else { 1010 //ALOGV("aux ramp: %d %d %d", auxLevel << 16, prevAuxLevel, auxInc); 1011 } 1012 } 1013 } 1014 1015 size_t AudioMixer::getUnreleasedFrames(int name) const 1016 { 1017 name -= TRACK0; 1018 if (uint32_t(name) < MAX_NUM_TRACKS) { 1019 return mState.tracks[name].getUnreleasedFrames(); 1020 } 1021 return 0; 1022 } 1023 1024 void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) 1025 { 1026 name -= TRACK0; 1027 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 1028 1029 if (mState.tracks[name].mInputBufferProvider == bufferProvider) { 1030 return; // don't reset any buffer providers if identical. 1031 } 1032 if (mState.tracks[name].mReformatBufferProvider != NULL) { 1033 mState.tracks[name].mReformatBufferProvider->reset(); 1034 } else if (mState.tracks[name].downmixerBufferProvider != NULL) { 1035 } 1036 1037 mState.tracks[name].mInputBufferProvider = bufferProvider; 1038 reconfigureBufferProviders(&mState.tracks[name]); 1039 } 1040 1041 1042 void AudioMixer::process(int64_t pts) 1043 { 1044 mState.hook(&mState, pts); 1045 } 1046 1047 1048 void AudioMixer::process__validate(state_t* state, int64_t pts) 1049 { 1050 ALOGW_IF(!state->needsChanged, 1051 "in process__validate() but nothing's invalid"); 1052 1053 uint32_t changed = state->needsChanged; 1054 state->needsChanged = 0; // clear the validation flag 1055 1056 // recompute which tracks are enabled / disabled 1057 uint32_t enabled = 0; 1058 uint32_t disabled = 0; 1059 while (changed) { 1060 const int i = 31 - __builtin_clz(changed); 1061 const uint32_t mask = 1<<i; 1062 changed &= ~mask; 1063 track_t& t = state->tracks[i]; 1064 (t.enabled ? enabled : disabled) |= mask; 1065 } 1066 state->enabledTracks &= ~disabled; 1067 state->enabledTracks |= enabled; 1068 1069 // compute everything we need... 1070 int countActiveTracks = 0; 1071 // TODO: fix all16BitsStereNoResample logic to 1072 // either properly handle muted tracks (it should ignore them) 1073 // or remove altogether as an obsolete optimization. 1074 bool all16BitsStereoNoResample = true; 1075 bool resampling = false; 1076 bool volumeRamp = false; 1077 uint32_t en = state->enabledTracks; 1078 while (en) { 1079 const int i = 31 - __builtin_clz(en); 1080 en &= ~(1<<i); 1081 1082 countActiveTracks++; 1083 track_t& t = state->tracks[i]; 1084 uint32_t n = 0; 1085 // FIXME can overflow (mask is only 3 bits) 1086 n |= NEEDS_CHANNEL_1 + t.channelCount - 1; 1087 if (t.doesResample()) { 1088 n |= NEEDS_RESAMPLE; 1089 } 1090 if (t.auxLevel != 0 && t.auxBuffer != NULL) { 1091 n |= NEEDS_AUX; 1092 } 1093 1094 if (t.volumeInc[0]|t.volumeInc[1]) { 1095 volumeRamp = true; 1096 } else if (!t.doesResample() && t.volumeRL == 0) { 1097 n |= NEEDS_MUTE; 1098 } 1099 t.needs = n; 1100 1101 if (n & NEEDS_MUTE) { 1102 t.hook = track__nop; 1103 } else { 1104 if (n & NEEDS_AUX) { 1105 all16BitsStereoNoResample = false; 1106 } 1107 if (n & NEEDS_RESAMPLE) { 1108 all16BitsStereoNoResample = false; 1109 resampling = true; 1110 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, t.mMixerChannelCount, 1111 t.mMixerInFormat, t.mMixerFormat); 1112 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 1113 "Track %d needs downmix + resample", i); 1114 } else { 1115 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){ 1116 t.hook = getTrackHook( 1117 t.mMixerChannelCount == 2 // TODO: MONO_HACK. 1118 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE, 1119 t.mMixerChannelCount, 1120 t.mMixerInFormat, t.mMixerFormat); 1121 all16BitsStereoNoResample = false; 1122 } 1123 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){ 1124 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, t.mMixerChannelCount, 1125 t.mMixerInFormat, t.mMixerFormat); 1126 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 1127 "Track %d needs downmix", i); 1128 } 1129 } 1130 } 1131 } 1132 1133 // select the processing hooks 1134 state->hook = process__nop; 1135 if (countActiveTracks > 0) { 1136 if (resampling) { 1137 if (!state->outputTemp) { 1138 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 1139 } 1140 if (!state->resampleTemp) { 1141 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 1142 } 1143 state->hook = process__genericResampling; 1144 } else { 1145 if (state->outputTemp) { 1146 delete [] state->outputTemp; 1147 state->outputTemp = NULL; 1148 } 1149 if (state->resampleTemp) { 1150 delete [] state->resampleTemp; 1151 state->resampleTemp = NULL; 1152 } 1153 state->hook = process__genericNoResampling; 1154 if (all16BitsStereoNoResample && !volumeRamp) { 1155 if (countActiveTracks == 1) { 1156 const int i = 31 - __builtin_clz(state->enabledTracks); 1157 track_t& t = state->tracks[i]; 1158 if ((t.needs & NEEDS_MUTE) == 0) { 1159 // The check prevents a muted track from acquiring a process hook. 1160 // 1161 // This is dangerous if the track is MONO as that requires 1162 // special case handling due to implicit channel duplication. 1163 // Stereo or Multichannel should actually be fine here. 1164 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, 1165 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat); 1166 } 1167 } 1168 } 1169 } 1170 } 1171 1172 ALOGV("mixer configuration change: %d activeTracks (%08x) " 1173 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d", 1174 countActiveTracks, state->enabledTracks, 1175 all16BitsStereoNoResample, resampling, volumeRamp); 1176 1177 state->hook(state, pts); 1178 1179 // Now that the volume ramp has been done, set optimal state and 1180 // track hooks for subsequent mixer process 1181 if (countActiveTracks > 0) { 1182 bool allMuted = true; 1183 uint32_t en = state->enabledTracks; 1184 while (en) { 1185 const int i = 31 - __builtin_clz(en); 1186 en &= ~(1<<i); 1187 track_t& t = state->tracks[i]; 1188 if (!t.doesResample() && t.volumeRL == 0) { 1189 t.needs |= NEEDS_MUTE; 1190 t.hook = track__nop; 1191 } else { 1192 allMuted = false; 1193 } 1194 } 1195 if (allMuted) { 1196 state->hook = process__nop; 1197 } else if (all16BitsStereoNoResample) { 1198 if (countActiveTracks == 1) { 1199 const int i = 31 - __builtin_clz(state->enabledTracks); 1200 track_t& t = state->tracks[i]; 1201 // Muted single tracks handled by allMuted above. 1202 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, 1203 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat); 1204 } 1205 } 1206 } 1207 } 1208 1209 1210 void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, 1211 int32_t* temp, int32_t* aux) 1212 { 1213 ALOGVV("track__genericResample\n"); 1214 t->resampler->setSampleRate(t->sampleRate); 1215 1216 // ramp gain - resample to temp buffer and scale/mix in 2nd step 1217 if (aux != NULL) { 1218 // always resample with unity gain when sending to auxiliary buffer to be able 1219 // to apply send level after resampling 1220 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 1221 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(int32_t)); 1222 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 1223 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1224 volumeRampStereo(t, out, outFrameCount, temp, aux); 1225 } else { 1226 volumeStereo(t, out, outFrameCount, temp, aux); 1227 } 1228 } else { 1229 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1230 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 1231 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 1232 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 1233 volumeRampStereo(t, out, outFrameCount, temp, aux); 1234 } 1235 1236 // constant gain 1237 else { 1238 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]); 1239 t->resampler->resample(out, outFrameCount, t->bufferProvider); 1240 } 1241 } 1242 } 1243 1244 void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused, 1245 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused) 1246 { 1247 } 1248 1249 void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 1250 int32_t* aux) 1251 { 1252 int32_t vl = t->prevVolume[0]; 1253 int32_t vr = t->prevVolume[1]; 1254 const int32_t vlInc = t->volumeInc[0]; 1255 const int32_t vrInc = t->volumeInc[1]; 1256 1257 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1258 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1259 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1260 1261 // ramp volume 1262 if (CC_UNLIKELY(aux != NULL)) { 1263 int32_t va = t->prevAuxLevel; 1264 const int32_t vaInc = t->auxInc; 1265 int32_t l; 1266 int32_t r; 1267 1268 do { 1269 l = (*temp++ >> 12); 1270 r = (*temp++ >> 12); 1271 *out++ += (vl >> 16) * l; 1272 *out++ += (vr >> 16) * r; 1273 *aux++ += (va >> 17) * (l + r); 1274 vl += vlInc; 1275 vr += vrInc; 1276 va += vaInc; 1277 } while (--frameCount); 1278 t->prevAuxLevel = va; 1279 } else { 1280 do { 1281 *out++ += (vl >> 16) * (*temp++ >> 12); 1282 *out++ += (vr >> 16) * (*temp++ >> 12); 1283 vl += vlInc; 1284 vr += vrInc; 1285 } while (--frameCount); 1286 } 1287 t->prevVolume[0] = vl; 1288 t->prevVolume[1] = vr; 1289 t->adjustVolumeRamp(aux != NULL); 1290 } 1291 1292 void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 1293 int32_t* aux) 1294 { 1295 const int16_t vl = t->volume[0]; 1296 const int16_t vr = t->volume[1]; 1297 1298 if (CC_UNLIKELY(aux != NULL)) { 1299 const int16_t va = t->auxLevel; 1300 do { 1301 int16_t l = (int16_t)(*temp++ >> 12); 1302 int16_t r = (int16_t)(*temp++ >> 12); 1303 out[0] = mulAdd(l, vl, out[0]); 1304 int16_t a = (int16_t)(((int32_t)l + r) >> 1); 1305 out[1] = mulAdd(r, vr, out[1]); 1306 out += 2; 1307 aux[0] = mulAdd(a, va, aux[0]); 1308 aux++; 1309 } while (--frameCount); 1310 } else { 1311 do { 1312 int16_t l = (int16_t)(*temp++ >> 12); 1313 int16_t r = (int16_t)(*temp++ >> 12); 1314 out[0] = mulAdd(l, vl, out[0]); 1315 out[1] = mulAdd(r, vr, out[1]); 1316 out += 2; 1317 } while (--frameCount); 1318 } 1319 } 1320 1321 void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, 1322 int32_t* temp __unused, int32_t* aux) 1323 { 1324 ALOGVV("track__16BitsStereo\n"); 1325 const int16_t *in = static_cast<const int16_t *>(t->in); 1326 1327 if (CC_UNLIKELY(aux != NULL)) { 1328 int32_t l; 1329 int32_t r; 1330 // ramp gain 1331 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1332 int32_t vl = t->prevVolume[0]; 1333 int32_t vr = t->prevVolume[1]; 1334 int32_t va = t->prevAuxLevel; 1335 const int32_t vlInc = t->volumeInc[0]; 1336 const int32_t vrInc = t->volumeInc[1]; 1337 const int32_t vaInc = t->auxInc; 1338 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1339 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1340 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1341 1342 do { 1343 l = (int32_t)*in++; 1344 r = (int32_t)*in++; 1345 *out++ += (vl >> 16) * l; 1346 *out++ += (vr >> 16) * r; 1347 *aux++ += (va >> 17) * (l + r); 1348 vl += vlInc; 1349 vr += vrInc; 1350 va += vaInc; 1351 } while (--frameCount); 1352 1353 t->prevVolume[0] = vl; 1354 t->prevVolume[1] = vr; 1355 t->prevAuxLevel = va; 1356 t->adjustVolumeRamp(true); 1357 } 1358 1359 // constant gain 1360 else { 1361 const uint32_t vrl = t->volumeRL; 1362 const int16_t va = (int16_t)t->auxLevel; 1363 do { 1364 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1365 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1); 1366 in += 2; 1367 out[0] = mulAddRL(1, rl, vrl, out[0]); 1368 out[1] = mulAddRL(0, rl, vrl, out[1]); 1369 out += 2; 1370 aux[0] = mulAdd(a, va, aux[0]); 1371 aux++; 1372 } while (--frameCount); 1373 } 1374 } else { 1375 // ramp gain 1376 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1377 int32_t vl = t->prevVolume[0]; 1378 int32_t vr = t->prevVolume[1]; 1379 const int32_t vlInc = t->volumeInc[0]; 1380 const int32_t vrInc = t->volumeInc[1]; 1381 1382 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1383 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1384 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1385 1386 do { 1387 *out++ += (vl >> 16) * (int32_t) *in++; 1388 *out++ += (vr >> 16) * (int32_t) *in++; 1389 vl += vlInc; 1390 vr += vrInc; 1391 } while (--frameCount); 1392 1393 t->prevVolume[0] = vl; 1394 t->prevVolume[1] = vr; 1395 t->adjustVolumeRamp(false); 1396 } 1397 1398 // constant gain 1399 else { 1400 const uint32_t vrl = t->volumeRL; 1401 do { 1402 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1403 in += 2; 1404 out[0] = mulAddRL(1, rl, vrl, out[0]); 1405 out[1] = mulAddRL(0, rl, vrl, out[1]); 1406 out += 2; 1407 } while (--frameCount); 1408 } 1409 } 1410 t->in = in; 1411 } 1412 1413 void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, 1414 int32_t* temp __unused, int32_t* aux) 1415 { 1416 ALOGVV("track__16BitsMono\n"); 1417 const int16_t *in = static_cast<int16_t const *>(t->in); 1418 1419 if (CC_UNLIKELY(aux != NULL)) { 1420 // ramp gain 1421 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1422 int32_t vl = t->prevVolume[0]; 1423 int32_t vr = t->prevVolume[1]; 1424 int32_t va = t->prevAuxLevel; 1425 const int32_t vlInc = t->volumeInc[0]; 1426 const int32_t vrInc = t->volumeInc[1]; 1427 const int32_t vaInc = t->auxInc; 1428 1429 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1430 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1431 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1432 1433 do { 1434 int32_t l = *in++; 1435 *out++ += (vl >> 16) * l; 1436 *out++ += (vr >> 16) * l; 1437 *aux++ += (va >> 16) * l; 1438 vl += vlInc; 1439 vr += vrInc; 1440 va += vaInc; 1441 } while (--frameCount); 1442 1443 t->prevVolume[0] = vl; 1444 t->prevVolume[1] = vr; 1445 t->prevAuxLevel = va; 1446 t->adjustVolumeRamp(true); 1447 } 1448 // constant gain 1449 else { 1450 const int16_t vl = t->volume[0]; 1451 const int16_t vr = t->volume[1]; 1452 const int16_t va = (int16_t)t->auxLevel; 1453 do { 1454 int16_t l = *in++; 1455 out[0] = mulAdd(l, vl, out[0]); 1456 out[1] = mulAdd(l, vr, out[1]); 1457 out += 2; 1458 aux[0] = mulAdd(l, va, aux[0]); 1459 aux++; 1460 } while (--frameCount); 1461 } 1462 } else { 1463 // ramp gain 1464 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1465 int32_t vl = t->prevVolume[0]; 1466 int32_t vr = t->prevVolume[1]; 1467 const int32_t vlInc = t->volumeInc[0]; 1468 const int32_t vrInc = t->volumeInc[1]; 1469 1470 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1471 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1472 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1473 1474 do { 1475 int32_t l = *in++; 1476 *out++ += (vl >> 16) * l; 1477 *out++ += (vr >> 16) * l; 1478 vl += vlInc; 1479 vr += vrInc; 1480 } while (--frameCount); 1481 1482 t->prevVolume[0] = vl; 1483 t->prevVolume[1] = vr; 1484 t->adjustVolumeRamp(false); 1485 } 1486 // constant gain 1487 else { 1488 const int16_t vl = t->volume[0]; 1489 const int16_t vr = t->volume[1]; 1490 do { 1491 int16_t l = *in++; 1492 out[0] = mulAdd(l, vl, out[0]); 1493 out[1] = mulAdd(l, vr, out[1]); 1494 out += 2; 1495 } while (--frameCount); 1496 } 1497 } 1498 t->in = in; 1499 } 1500 1501 // no-op case 1502 void AudioMixer::process__nop(state_t* state, int64_t pts) 1503 { 1504 ALOGVV("process__nop\n"); 1505 uint32_t e0 = state->enabledTracks; 1506 while (e0) { 1507 // process by group of tracks with same output buffer to 1508 // avoid multiple memset() on same buffer 1509 uint32_t e1 = e0, e2 = e0; 1510 int i = 31 - __builtin_clz(e1); 1511 { 1512 track_t& t1 = state->tracks[i]; 1513 e2 &= ~(1<<i); 1514 while (e2) { 1515 i = 31 - __builtin_clz(e2); 1516 e2 &= ~(1<<i); 1517 track_t& t2 = state->tracks[i]; 1518 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1519 e1 &= ~(1<<i); 1520 } 1521 } 1522 e0 &= ~(e1); 1523 1524 memset(t1.mainBuffer, 0, state->frameCount * t1.mMixerChannelCount 1525 * audio_bytes_per_sample(t1.mMixerFormat)); 1526 } 1527 1528 while (e1) { 1529 i = 31 - __builtin_clz(e1); 1530 e1 &= ~(1<<i); 1531 { 1532 track_t& t3 = state->tracks[i]; 1533 size_t outFrames = state->frameCount; 1534 while (outFrames) { 1535 t3.buffer.frameCount = outFrames; 1536 int64_t outputPTS = calculateOutputPTS( 1537 t3, pts, state->frameCount - outFrames); 1538 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS); 1539 if (t3.buffer.raw == NULL) break; 1540 outFrames -= t3.buffer.frameCount; 1541 t3.bufferProvider->releaseBuffer(&t3.buffer); 1542 } 1543 } 1544 } 1545 } 1546 } 1547 1548 // generic code without resampling 1549 void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts) 1550 { 1551 ALOGVV("process__genericNoResampling\n"); 1552 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32))); 1553 1554 // acquire each track's buffer 1555 uint32_t enabledTracks = state->enabledTracks; 1556 uint32_t e0 = enabledTracks; 1557 while (e0) { 1558 const int i = 31 - __builtin_clz(e0); 1559 e0 &= ~(1<<i); 1560 track_t& t = state->tracks[i]; 1561 t.buffer.frameCount = state->frameCount; 1562 t.bufferProvider->getNextBuffer(&t.buffer, pts); 1563 t.frameCount = t.buffer.frameCount; 1564 t.in = t.buffer.raw; 1565 } 1566 1567 e0 = enabledTracks; 1568 while (e0) { 1569 // process by group of tracks with same output buffer to 1570 // optimize cache use 1571 uint32_t e1 = e0, e2 = e0; 1572 int j = 31 - __builtin_clz(e1); 1573 track_t& t1 = state->tracks[j]; 1574 e2 &= ~(1<<j); 1575 while (e2) { 1576 j = 31 - __builtin_clz(e2); 1577 e2 &= ~(1<<j); 1578 track_t& t2 = state->tracks[j]; 1579 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1580 e1 &= ~(1<<j); 1581 } 1582 } 1583 e0 &= ~(e1); 1584 // this assumes output 16 bits stereo, no resampling 1585 int32_t *out = t1.mainBuffer; 1586 size_t numFrames = 0; 1587 do { 1588 memset(outTemp, 0, sizeof(outTemp)); 1589 e2 = e1; 1590 while (e2) { 1591 const int i = 31 - __builtin_clz(e2); 1592 e2 &= ~(1<<i); 1593 track_t& t = state->tracks[i]; 1594 size_t outFrames = BLOCKSIZE; 1595 int32_t *aux = NULL; 1596 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1597 aux = t.auxBuffer + numFrames; 1598 } 1599 while (outFrames) { 1600 // t.in == NULL can happen if the track was flushed just after having 1601 // been enabled for mixing. 1602 if (t.in == NULL) { 1603 enabledTracks &= ~(1<<i); 1604 e1 &= ~(1<<i); 1605 break; 1606 } 1607 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount; 1608 if (inFrames > 0) { 1609 t.hook(&t, outTemp + (BLOCKSIZE - outFrames) * t.mMixerChannelCount, 1610 inFrames, state->resampleTemp, aux); 1611 t.frameCount -= inFrames; 1612 outFrames -= inFrames; 1613 if (CC_UNLIKELY(aux != NULL)) { 1614 aux += inFrames; 1615 } 1616 } 1617 if (t.frameCount == 0 && outFrames) { 1618 t.bufferProvider->releaseBuffer(&t.buffer); 1619 t.buffer.frameCount = (state->frameCount - numFrames) - 1620 (BLOCKSIZE - outFrames); 1621 int64_t outputPTS = calculateOutputPTS( 1622 t, pts, numFrames + (BLOCKSIZE - outFrames)); 1623 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1624 t.in = t.buffer.raw; 1625 if (t.in == NULL) { 1626 enabledTracks &= ~(1<<i); 1627 e1 &= ~(1<<i); 1628 break; 1629 } 1630 t.frameCount = t.buffer.frameCount; 1631 } 1632 } 1633 } 1634 1635 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat, 1636 BLOCKSIZE * t1.mMixerChannelCount); 1637 // TODO: fix ugly casting due to choice of out pointer type 1638 out = reinterpret_cast<int32_t*>((uint8_t*)out 1639 + BLOCKSIZE * t1.mMixerChannelCount 1640 * audio_bytes_per_sample(t1.mMixerFormat)); 1641 numFrames += BLOCKSIZE; 1642 } while (numFrames < state->frameCount); 1643 } 1644 1645 // release each track's buffer 1646 e0 = enabledTracks; 1647 while (e0) { 1648 const int i = 31 - __builtin_clz(e0); 1649 e0 &= ~(1<<i); 1650 track_t& t = state->tracks[i]; 1651 t.bufferProvider->releaseBuffer(&t.buffer); 1652 } 1653 } 1654 1655 1656 // generic code with resampling 1657 void AudioMixer::process__genericResampling(state_t* state, int64_t pts) 1658 { 1659 ALOGVV("process__genericResampling\n"); 1660 // this const just means that local variable outTemp doesn't change 1661 int32_t* const outTemp = state->outputTemp; 1662 size_t numFrames = state->frameCount; 1663 1664 uint32_t e0 = state->enabledTracks; 1665 while (e0) { 1666 // process by group of tracks with same output buffer 1667 // to optimize cache use 1668 uint32_t e1 = e0, e2 = e0; 1669 int j = 31 - __builtin_clz(e1); 1670 track_t& t1 = state->tracks[j]; 1671 e2 &= ~(1<<j); 1672 while (e2) { 1673 j = 31 - __builtin_clz(e2); 1674 e2 &= ~(1<<j); 1675 track_t& t2 = state->tracks[j]; 1676 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1677 e1 &= ~(1<<j); 1678 } 1679 } 1680 e0 &= ~(e1); 1681 int32_t *out = t1.mainBuffer; 1682 memset(outTemp, 0, sizeof(*outTemp) * t1.mMixerChannelCount * state->frameCount); 1683 while (e1) { 1684 const int i = 31 - __builtin_clz(e1); 1685 e1 &= ~(1<<i); 1686 track_t& t = state->tracks[i]; 1687 int32_t *aux = NULL; 1688 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1689 aux = t.auxBuffer; 1690 } 1691 1692 // this is a little goofy, on the resampling case we don't 1693 // acquire/release the buffers because it's done by 1694 // the resampler. 1695 if (t.needs & NEEDS_RESAMPLE) { 1696 t.resampler->setPTS(pts); 1697 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux); 1698 } else { 1699 1700 size_t outFrames = 0; 1701 1702 while (outFrames < numFrames) { 1703 t.buffer.frameCount = numFrames - outFrames; 1704 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames); 1705 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1706 t.in = t.buffer.raw; 1707 // t.in == NULL can happen if the track was flushed just after having 1708 // been enabled for mixing. 1709 if (t.in == NULL) break; 1710 1711 if (CC_UNLIKELY(aux != NULL)) { 1712 aux += outFrames; 1713 } 1714 t.hook(&t, outTemp + outFrames * t.mMixerChannelCount, t.buffer.frameCount, 1715 state->resampleTemp, aux); 1716 outFrames += t.buffer.frameCount; 1717 t.bufferProvider->releaseBuffer(&t.buffer); 1718 } 1719 } 1720 } 1721 convertMixerFormat(out, t1.mMixerFormat, 1722 outTemp, t1.mMixerInFormat, numFrames * t1.mMixerChannelCount); 1723 } 1724 } 1725 1726 // one track, 16 bits stereo without resampling is the most common case 1727 void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state, 1728 int64_t pts) 1729 { 1730 ALOGVV("process__OneTrack16BitsStereoNoResampling\n"); 1731 // This method is only called when state->enabledTracks has exactly 1732 // one bit set. The asserts below would verify this, but are commented out 1733 // since the whole point of this method is to optimize performance. 1734 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled"); 1735 const int i = 31 - __builtin_clz(state->enabledTracks); 1736 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 1737 const track_t& t = state->tracks[i]; 1738 1739 AudioBufferProvider::Buffer& b(t.buffer); 1740 1741 int32_t* out = t.mainBuffer; 1742 float *fout = reinterpret_cast<float*>(out); 1743 size_t numFrames = state->frameCount; 1744 1745 const int16_t vl = t.volume[0]; 1746 const int16_t vr = t.volume[1]; 1747 const uint32_t vrl = t.volumeRL; 1748 while (numFrames) { 1749 b.frameCount = numFrames; 1750 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer); 1751 t.bufferProvider->getNextBuffer(&b, outputPTS); 1752 const int16_t *in = b.i16; 1753 1754 // in == NULL can happen if the track was flushed just after having 1755 // been enabled for mixing. 1756 if (in == NULL || (((uintptr_t)in) & 3)) { 1757 memset(out, 0, numFrames 1758 * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat)); 1759 ALOGE_IF((((uintptr_t)in) & 3), 1760 "process__OneTrack16BitsStereoNoResampling: misaligned buffer" 1761 " %p track %d, channels %d, needs %08x, volume %08x vfl %f vfr %f", 1762 in, i, t.channelCount, t.needs, vrl, t.mVolume[0], t.mVolume[1]); 1763 return; 1764 } 1765 size_t outFrames = b.frameCount; 1766 1767 switch (t.mMixerFormat) { 1768 case AUDIO_FORMAT_PCM_FLOAT: 1769 do { 1770 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1771 in += 2; 1772 int32_t l = mulRL(1, rl, vrl); 1773 int32_t r = mulRL(0, rl, vrl); 1774 *fout++ = float_from_q4_27(l); 1775 *fout++ = float_from_q4_27(r); 1776 // Note: In case of later int16_t sink output, 1777 // conversion and clamping is done by memcpy_to_i16_from_float(). 1778 } while (--outFrames); 1779 break; 1780 case AUDIO_FORMAT_PCM_16_BIT: 1781 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) { 1782 // volume is boosted, so we might need to clamp even though 1783 // we process only one track. 1784 do { 1785 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1786 in += 2; 1787 int32_t l = mulRL(1, rl, vrl) >> 12; 1788 int32_t r = mulRL(0, rl, vrl) >> 12; 1789 // clamping... 1790 l = clamp16(l); 1791 r = clamp16(r); 1792 *out++ = (r<<16) | (l & 0xFFFF); 1793 } while (--outFrames); 1794 } else { 1795 do { 1796 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1797 in += 2; 1798 int32_t l = mulRL(1, rl, vrl) >> 12; 1799 int32_t r = mulRL(0, rl, vrl) >> 12; 1800 *out++ = (r<<16) | (l & 0xFFFF); 1801 } while (--outFrames); 1802 } 1803 break; 1804 default: 1805 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat); 1806 } 1807 numFrames -= b.frameCount; 1808 t.bufferProvider->releaseBuffer(&b); 1809 } 1810 } 1811 1812 int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS, 1813 int outputFrameIndex) 1814 { 1815 if (AudioBufferProvider::kInvalidPTS == basePTS) { 1816 return AudioBufferProvider::kInvalidPTS; 1817 } 1818 1819 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate); 1820 } 1821 1822 /*static*/ uint64_t AudioMixer::sLocalTimeFreq; 1823 /*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; 1824 1825 /*static*/ void AudioMixer::sInitRoutine() 1826 { 1827 LocalClock lc; 1828 sLocalTimeFreq = lc.getLocalFreq(); // for the resampler 1829 1830 DownmixerBufferProvider::init(); // for the downmixer 1831 } 1832 1833 /* TODO: consider whether this level of optimization is necessary. 1834 * Perhaps just stick with a single for loop. 1835 */ 1836 1837 // Needs to derive a compile time constant (constexpr). Could be targeted to go 1838 // to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication. 1839 #define MIXTYPE_MONOVOL(mixtype) (mixtype == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \ 1840 mixtype == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : mixtype) 1841 1842 /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1843 * TO: int32_t (Q4.27) or float 1844 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1845 * TA: int32_t (Q4.27) 1846 */ 1847 template <int MIXTYPE, 1848 typename TO, typename TI, typename TV, typename TA, typename TAV> 1849 static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount, 1850 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc) 1851 { 1852 switch (channels) { 1853 case 1: 1854 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc); 1855 break; 1856 case 2: 1857 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc); 1858 break; 1859 case 3: 1860 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, 1861 frameCount, in, aux, vol, volinc, vola, volainc); 1862 break; 1863 case 4: 1864 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, 1865 frameCount, in, aux, vol, volinc, vola, volainc); 1866 break; 1867 case 5: 1868 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, 1869 frameCount, in, aux, vol, volinc, vola, volainc); 1870 break; 1871 case 6: 1872 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, 1873 frameCount, in, aux, vol, volinc, vola, volainc); 1874 break; 1875 case 7: 1876 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, 1877 frameCount, in, aux, vol, volinc, vola, volainc); 1878 break; 1879 case 8: 1880 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, 1881 frameCount, in, aux, vol, volinc, vola, volainc); 1882 break; 1883 } 1884 } 1885 1886 /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1887 * TO: int32_t (Q4.27) or float 1888 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1889 * TA: int32_t (Q4.27) 1890 */ 1891 template <int MIXTYPE, 1892 typename TO, typename TI, typename TV, typename TA, typename TAV> 1893 static void volumeMulti(uint32_t channels, TO* out, size_t frameCount, 1894 const TI* in, TA* aux, const TV *vol, TAV vola) 1895 { 1896 switch (channels) { 1897 case 1: 1898 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola); 1899 break; 1900 case 2: 1901 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola); 1902 break; 1903 case 3: 1904 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola); 1905 break; 1906 case 4: 1907 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola); 1908 break; 1909 case 5: 1910 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola); 1911 break; 1912 case 6: 1913 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola); 1914 break; 1915 case 7: 1916 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola); 1917 break; 1918 case 8: 1919 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola); 1920 break; 1921 } 1922 } 1923 1924 /* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1925 * USEFLOATVOL (set to true if float volume is used) 1926 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards) 1927 * TO: int32_t (Q4.27) or float 1928 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1929 * TA: int32_t (Q4.27) 1930 */ 1931 template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL, 1932 typename TO, typename TI, typename TA> 1933 void AudioMixer::volumeMix(TO *out, size_t outFrames, 1934 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t) 1935 { 1936 if (USEFLOATVOL) { 1937 if (ramp) { 1938 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1939 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc); 1940 if (ADJUSTVOL) { 1941 t->adjustVolumeRamp(aux != NULL, true); 1942 } 1943 } else { 1944 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1945 t->mVolume, t->auxLevel); 1946 } 1947 } else { 1948 if (ramp) { 1949 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1950 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc); 1951 if (ADJUSTVOL) { 1952 t->adjustVolumeRamp(aux != NULL); 1953 } 1954 } else { 1955 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1956 t->volume, t->auxLevel); 1957 } 1958 } 1959 } 1960 1961 /* This process hook is called when there is a single track without 1962 * aux buffer, volume ramp, or resampling. 1963 * TODO: Update the hook selection: this can properly handle aux and ramp. 1964 * 1965 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1966 * TO: int32_t (Q4.27) or float 1967 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1968 * TA: int32_t (Q4.27) 1969 */ 1970 template <int MIXTYPE, typename TO, typename TI, typename TA> 1971 void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts) 1972 { 1973 ALOGVV("process_NoResampleOneTrack\n"); 1974 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz. 1975 const int i = 31 - __builtin_clz(state->enabledTracks); 1976 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 1977 track_t *t = &state->tracks[i]; 1978 const uint32_t channels = t->mMixerChannelCount; 1979 TO* out = reinterpret_cast<TO*>(t->mainBuffer); 1980 TA* aux = reinterpret_cast<TA*>(t->auxBuffer); 1981 const bool ramp = t->needsRamp(); 1982 1983 for (size_t numFrames = state->frameCount; numFrames; ) { 1984 AudioBufferProvider::Buffer& b(t->buffer); 1985 // get input buffer 1986 b.frameCount = numFrames; 1987 const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames); 1988 t->bufferProvider->getNextBuffer(&b, outputPTS); 1989 const TI *in = reinterpret_cast<TI*>(b.raw); 1990 1991 // in == NULL can happen if the track was flushed just after having 1992 // been enabled for mixing. 1993 if (in == NULL || (((uintptr_t)in) & 3)) { 1994 memset(out, 0, numFrames 1995 * channels * audio_bytes_per_sample(t->mMixerFormat)); 1996 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: " 1997 "buffer %p track %p, channels %d, needs %#x", 1998 in, t, t->channelCount, t->needs); 1999 return; 2000 } 2001 2002 const size_t outFrames = b.frameCount; 2003 volumeMix<MIXTYPE, is_same<TI, float>::value, false> ( 2004 out, outFrames, in, aux, ramp, t); 2005 2006 out += outFrames * channels; 2007 if (aux != NULL) { 2008 aux += channels; 2009 } 2010 numFrames -= b.frameCount; 2011 2012 // release buffer 2013 t->bufferProvider->releaseBuffer(&b); 2014 } 2015 if (ramp) { 2016 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value); 2017 } 2018 } 2019 2020 /* This track hook is called to do resampling then mixing, 2021 * pulling from the track's upstream AudioBufferProvider. 2022 * 2023 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 2024 * TO: int32_t (Q4.27) or float 2025 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 2026 * TA: int32_t (Q4.27) 2027 */ 2028 template <int MIXTYPE, typename TO, typename TI, typename TA> 2029 void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux) 2030 { 2031 ALOGVV("track__Resample\n"); 2032 t->resampler->setSampleRate(t->sampleRate); 2033 const bool ramp = t->needsRamp(); 2034 if (ramp || aux != NULL) { 2035 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step. 2036 // if aux != NULL: resample with unity gain to temp buffer then apply send level. 2037 2038 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 2039 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(TO)); 2040 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider); 2041 2042 volumeMix<MIXTYPE, is_same<TI, float>::value, true>( 2043 out, outFrameCount, temp, aux, ramp, t); 2044 2045 } else { // constant volume gain 2046 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]); 2047 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider); 2048 } 2049 } 2050 2051 /* This track hook is called to mix a track, when no resampling is required. 2052 * The input buffer should be present in t->in. 2053 * 2054 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 2055 * TO: int32_t (Q4.27) or float 2056 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 2057 * TA: int32_t (Q4.27) 2058 */ 2059 template <int MIXTYPE, typename TO, typename TI, typename TA> 2060 void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount, 2061 TO* temp __unused, TA* aux) 2062 { 2063 ALOGVV("track__NoResample\n"); 2064 const TI *in = static_cast<const TI *>(t->in); 2065 2066 volumeMix<MIXTYPE, is_same<TI, float>::value, true>( 2067 out, frameCount, in, aux, t->needsRamp(), t); 2068 2069 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels. 2070 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels. 2071 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * t->mMixerChannelCount; 2072 t->in = in; 2073 } 2074 2075 /* The Mixer engine generates either int32_t (Q4_27) or float data. 2076 * We use this function to convert the engine buffers 2077 * to the desired mixer output format, either int16_t (Q.15) or float. 2078 */ 2079 void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat, 2080 void *in, audio_format_t mixerInFormat, size_t sampleCount) 2081 { 2082 switch (mixerInFormat) { 2083 case AUDIO_FORMAT_PCM_FLOAT: 2084 switch (mixerOutFormat) { 2085 case AUDIO_FORMAT_PCM_FLOAT: 2086 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out 2087 break; 2088 case AUDIO_FORMAT_PCM_16_BIT: 2089 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount); 2090 break; 2091 default: 2092 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2093 break; 2094 } 2095 break; 2096 case AUDIO_FORMAT_PCM_16_BIT: 2097 switch (mixerOutFormat) { 2098 case AUDIO_FORMAT_PCM_FLOAT: 2099 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount); 2100 break; 2101 case AUDIO_FORMAT_PCM_16_BIT: 2102 // two int16_t are produced per iteration 2103 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1); 2104 break; 2105 default: 2106 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2107 break; 2108 } 2109 break; 2110 default: 2111 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2112 break; 2113 } 2114 } 2115 2116 /* Returns the proper track hook to use for mixing the track into the output buffer. 2117 */ 2118 AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, uint32_t channelCount, 2119 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused) 2120 { 2121 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { 2122 switch (trackType) { 2123 case TRACKTYPE_NOP: 2124 return track__nop; 2125 case TRACKTYPE_RESAMPLE: 2126 return track__genericResample; 2127 case TRACKTYPE_NORESAMPLEMONO: 2128 return track__16BitsMono; 2129 case TRACKTYPE_NORESAMPLE: 2130 return track__16BitsStereo; 2131 default: 2132 LOG_ALWAYS_FATAL("bad trackType: %d", trackType); 2133 break; 2134 } 2135 } 2136 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS); 2137 switch (trackType) { 2138 case TRACKTYPE_NOP: 2139 return track__nop; 2140 case TRACKTYPE_RESAMPLE: 2141 switch (mixerInFormat) { 2142 case AUDIO_FORMAT_PCM_FLOAT: 2143 return (AudioMixer::hook_t) 2144 track__Resample<MIXTYPE_MULTI, float /*TO*/, float /*TI*/, int32_t /*TA*/>; 2145 case AUDIO_FORMAT_PCM_16_BIT: 2146 return (AudioMixer::hook_t)\ 2147 track__Resample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>; 2148 default: 2149 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2150 break; 2151 } 2152 break; 2153 case TRACKTYPE_NORESAMPLEMONO: 2154 switch (mixerInFormat) { 2155 case AUDIO_FORMAT_PCM_FLOAT: 2156 return (AudioMixer::hook_t) 2157 track__NoResample<MIXTYPE_MONOEXPAND, float, float, int32_t>; 2158 case AUDIO_FORMAT_PCM_16_BIT: 2159 return (AudioMixer::hook_t) 2160 track__NoResample<MIXTYPE_MONOEXPAND, int32_t, int16_t, int32_t>; 2161 default: 2162 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2163 break; 2164 } 2165 break; 2166 case TRACKTYPE_NORESAMPLE: 2167 switch (mixerInFormat) { 2168 case AUDIO_FORMAT_PCM_FLOAT: 2169 return (AudioMixer::hook_t) 2170 track__NoResample<MIXTYPE_MULTI, float, float, int32_t>; 2171 case AUDIO_FORMAT_PCM_16_BIT: 2172 return (AudioMixer::hook_t) 2173 track__NoResample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>; 2174 default: 2175 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2176 break; 2177 } 2178 break; 2179 default: 2180 LOG_ALWAYS_FATAL("bad trackType: %d", trackType); 2181 break; 2182 } 2183 return NULL; 2184 } 2185 2186 /* Returns the proper process hook for mixing tracks. Currently works only for 2187 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling. 2188 * 2189 * TODO: Due to the special mixing considerations of duplicating to 2190 * a stereo output track, the input track cannot be MONO. This should be 2191 * prevented by the caller. 2192 */ 2193 AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, uint32_t channelCount, 2194 audio_format_t mixerInFormat, audio_format_t mixerOutFormat) 2195 { 2196 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK 2197 LOG_ALWAYS_FATAL("bad processType: %d", processType); 2198 return NULL; 2199 } 2200 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { 2201 return process__OneTrack16BitsStereoNoResampling; 2202 } 2203 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS); 2204 switch (mixerInFormat) { 2205 case AUDIO_FORMAT_PCM_FLOAT: 2206 switch (mixerOutFormat) { 2207 case AUDIO_FORMAT_PCM_FLOAT: 2208 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2209 float /*TO*/, float /*TI*/, int32_t /*TA*/>; 2210 case AUDIO_FORMAT_PCM_16_BIT: 2211 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2212 int16_t, float, int32_t>; 2213 default: 2214 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2215 break; 2216 } 2217 break; 2218 case AUDIO_FORMAT_PCM_16_BIT: 2219 switch (mixerOutFormat) { 2220 case AUDIO_FORMAT_PCM_FLOAT: 2221 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2222 float, int16_t, int32_t>; 2223 case AUDIO_FORMAT_PCM_16_BIT: 2224 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2225 int16_t, int16_t, int32_t>; 2226 default: 2227 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2228 break; 2229 } 2230 break; 2231 default: 2232 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2233 break; 2234 } 2235 return NULL; 2236 } 2237 2238 // ---------------------------------------------------------------------------- 2239 }; // namespace android 2240