AudioMixer.cpp revision 5241b8cacee86e17223695190959f4752910680f
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. 74static 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. 79static const bool kUseFloat = true; 80 81// Set to default copy buffer size in frames for input processing. 82static const size_t kCopyBufferFrameCount = 256; 83 84namespace android { 85 86// ---------------------------------------------------------------------------- 87 88template <typename T> 89T min(const T& a, const T& b) 90{ 91 return a < b ? a : b; 92} 93 94AudioMixer::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 113AudioMixer::CopyBufferProvider::~CopyBufferProvider() 114{ 115 ALOGV("~CopyBufferProvider(%p)", this); 116 if (mBuffer.frameCount != 0) { 117 mTrackBufferProvider->releaseBuffer(&mBuffer); 118 } 119 free(mLocalBufferData); 120} 121 122status_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 160void 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 178void AudioMixer::CopyBufferProvider::reset() 179{ 180 if (mBuffer.frameCount != 0) { 181 mTrackBufferProvider->releaseBuffer(&mBuffer); 182 } 183 mConsumed = 0; 184} 185 186AudioMixer::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 278AudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider() 279{ 280 ALOGV("~DownmixerBufferProvider (%p)", this); 281 EffectRelease(mDownmixHandle); 282 mDownmixHandle = NULL; 283} 284 285void 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 327AudioMixer::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 351void 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 357AudioMixer::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 371void 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 381AudioMixer::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 415AudioMixer::~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 428void AudioMixer::setLog(NBLog::Writer *log) 429{ 430 mState.mLog = log; 431} 432 433int 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 520void 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. 531bool 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 579status_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 593void 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 607status_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 637void 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 646status_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 662void 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 675void 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 697void 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 710void 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 */ 748static 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 790void 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 921bool 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 */ 973inline 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 1015size_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 1024void 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 1042void AudioMixer::process(int64_t pts) 1043{ 1044 mState.hook(&mState, pts); 1045} 1046 1047 1048void 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 bool all16BitsStereoNoResample = true; 1072 bool resampling = false; 1073 bool volumeRamp = false; 1074 uint32_t en = state->enabledTracks; 1075 while (en) { 1076 const int i = 31 - __builtin_clz(en); 1077 en &= ~(1<<i); 1078 1079 countActiveTracks++; 1080 track_t& t = state->tracks[i]; 1081 uint32_t n = 0; 1082 // FIXME can overflow (mask is only 3 bits) 1083 n |= NEEDS_CHANNEL_1 + t.channelCount - 1; 1084 if (t.doesResample()) { 1085 n |= NEEDS_RESAMPLE; 1086 } 1087 if (t.auxLevel != 0 && t.auxBuffer != NULL) { 1088 n |= NEEDS_AUX; 1089 } 1090 1091 if (t.volumeInc[0]|t.volumeInc[1]) { 1092 volumeRamp = true; 1093 } else if (!t.doesResample() && t.volumeRL == 0) { 1094 n |= NEEDS_MUTE; 1095 } 1096 t.needs = n; 1097 1098 if (n & NEEDS_MUTE) { 1099 t.hook = track__nop; 1100 } else { 1101 if (n & NEEDS_AUX) { 1102 all16BitsStereoNoResample = false; 1103 } 1104 if (n & NEEDS_RESAMPLE) { 1105 all16BitsStereoNoResample = false; 1106 resampling = true; 1107 t.hook = getTrackHook(TRACKTYPE_RESAMPLE, t.mMixerChannelCount, 1108 t.mMixerInFormat, t.mMixerFormat); 1109 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 1110 "Track %d needs downmix + resample", i); 1111 } else { 1112 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){ 1113 t.hook = getTrackHook( 1114 t.mMixerChannelCount == 2 // TODO: MONO_HACK. 1115 ? TRACKTYPE_NORESAMPLEMONO : TRACKTYPE_NORESAMPLE, 1116 t.mMixerChannelCount, 1117 t.mMixerInFormat, t.mMixerFormat); 1118 all16BitsStereoNoResample = false; 1119 } 1120 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){ 1121 t.hook = getTrackHook(TRACKTYPE_NORESAMPLE, t.mMixerChannelCount, 1122 t.mMixerInFormat, t.mMixerFormat); 1123 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 1124 "Track %d needs downmix", i); 1125 } 1126 } 1127 } 1128 } 1129 1130 // select the processing hooks 1131 state->hook = process__nop; 1132 if (countActiveTracks > 0) { 1133 if (resampling) { 1134 if (!state->outputTemp) { 1135 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 1136 } 1137 if (!state->resampleTemp) { 1138 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 1139 } 1140 state->hook = process__genericResampling; 1141 } else { 1142 if (state->outputTemp) { 1143 delete [] state->outputTemp; 1144 state->outputTemp = NULL; 1145 } 1146 if (state->resampleTemp) { 1147 delete [] state->resampleTemp; 1148 state->resampleTemp = NULL; 1149 } 1150 state->hook = process__genericNoResampling; 1151 if (all16BitsStereoNoResample && !volumeRamp) { 1152 if (countActiveTracks == 1) { 1153 const int i = 31 - __builtin_clz(state->enabledTracks); 1154 track_t& t = state->tracks[i]; 1155 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, 1156 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat); 1157 } 1158 } 1159 } 1160 } 1161 1162 ALOGV("mixer configuration change: %d activeTracks (%08x) " 1163 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d", 1164 countActiveTracks, state->enabledTracks, 1165 all16BitsStereoNoResample, resampling, volumeRamp); 1166 1167 state->hook(state, pts); 1168 1169 // Now that the volume ramp has been done, set optimal state and 1170 // track hooks for subsequent mixer process 1171 if (countActiveTracks > 0) { 1172 bool allMuted = true; 1173 uint32_t en = state->enabledTracks; 1174 while (en) { 1175 const int i = 31 - __builtin_clz(en); 1176 en &= ~(1<<i); 1177 track_t& t = state->tracks[i]; 1178 if (!t.doesResample() && t.volumeRL == 0) { 1179 t.needs |= NEEDS_MUTE; 1180 t.hook = track__nop; 1181 } else { 1182 allMuted = false; 1183 } 1184 } 1185 if (allMuted) { 1186 state->hook = process__nop; 1187 } else if (all16BitsStereoNoResample) { 1188 if (countActiveTracks == 1) { 1189 const int i = 31 - __builtin_clz(state->enabledTracks); 1190 track_t& t = state->tracks[i]; 1191 state->hook = getProcessHook(PROCESSTYPE_NORESAMPLEONETRACK, 1192 t.mMixerChannelCount, t.mMixerInFormat, t.mMixerFormat); 1193 } 1194 } 1195 } 1196} 1197 1198 1199void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, 1200 int32_t* temp, int32_t* aux) 1201{ 1202 ALOGVV("track__genericResample\n"); 1203 t->resampler->setSampleRate(t->sampleRate); 1204 1205 // ramp gain - resample to temp buffer and scale/mix in 2nd step 1206 if (aux != NULL) { 1207 // always resample with unity gain when sending to auxiliary buffer to be able 1208 // to apply send level after resampling 1209 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 1210 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(int32_t)); 1211 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 1212 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1213 volumeRampStereo(t, out, outFrameCount, temp, aux); 1214 } else { 1215 volumeStereo(t, out, outFrameCount, temp, aux); 1216 } 1217 } else { 1218 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1219 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 1220 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 1221 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 1222 volumeRampStereo(t, out, outFrameCount, temp, aux); 1223 } 1224 1225 // constant gain 1226 else { 1227 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]); 1228 t->resampler->resample(out, outFrameCount, t->bufferProvider); 1229 } 1230 } 1231} 1232 1233void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused, 1234 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused) 1235{ 1236} 1237 1238void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 1239 int32_t* aux) 1240{ 1241 int32_t vl = t->prevVolume[0]; 1242 int32_t vr = t->prevVolume[1]; 1243 const int32_t vlInc = t->volumeInc[0]; 1244 const int32_t vrInc = t->volumeInc[1]; 1245 1246 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1247 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1248 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1249 1250 // ramp volume 1251 if (CC_UNLIKELY(aux != NULL)) { 1252 int32_t va = t->prevAuxLevel; 1253 const int32_t vaInc = t->auxInc; 1254 int32_t l; 1255 int32_t r; 1256 1257 do { 1258 l = (*temp++ >> 12); 1259 r = (*temp++ >> 12); 1260 *out++ += (vl >> 16) * l; 1261 *out++ += (vr >> 16) * r; 1262 *aux++ += (va >> 17) * (l + r); 1263 vl += vlInc; 1264 vr += vrInc; 1265 va += vaInc; 1266 } while (--frameCount); 1267 t->prevAuxLevel = va; 1268 } else { 1269 do { 1270 *out++ += (vl >> 16) * (*temp++ >> 12); 1271 *out++ += (vr >> 16) * (*temp++ >> 12); 1272 vl += vlInc; 1273 vr += vrInc; 1274 } while (--frameCount); 1275 } 1276 t->prevVolume[0] = vl; 1277 t->prevVolume[1] = vr; 1278 t->adjustVolumeRamp(aux != NULL); 1279} 1280 1281void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 1282 int32_t* aux) 1283{ 1284 const int16_t vl = t->volume[0]; 1285 const int16_t vr = t->volume[1]; 1286 1287 if (CC_UNLIKELY(aux != NULL)) { 1288 const int16_t va = t->auxLevel; 1289 do { 1290 int16_t l = (int16_t)(*temp++ >> 12); 1291 int16_t r = (int16_t)(*temp++ >> 12); 1292 out[0] = mulAdd(l, vl, out[0]); 1293 int16_t a = (int16_t)(((int32_t)l + r) >> 1); 1294 out[1] = mulAdd(r, vr, out[1]); 1295 out += 2; 1296 aux[0] = mulAdd(a, va, aux[0]); 1297 aux++; 1298 } while (--frameCount); 1299 } else { 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 out[1] = mulAdd(r, vr, out[1]); 1305 out += 2; 1306 } while (--frameCount); 1307 } 1308} 1309 1310void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, 1311 int32_t* temp __unused, int32_t* aux) 1312{ 1313 ALOGVV("track__16BitsStereo\n"); 1314 const int16_t *in = static_cast<const int16_t *>(t->in); 1315 1316 if (CC_UNLIKELY(aux != NULL)) { 1317 int32_t l; 1318 int32_t r; 1319 // ramp gain 1320 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1321 int32_t vl = t->prevVolume[0]; 1322 int32_t vr = t->prevVolume[1]; 1323 int32_t va = t->prevAuxLevel; 1324 const int32_t vlInc = t->volumeInc[0]; 1325 const int32_t vrInc = t->volumeInc[1]; 1326 const int32_t vaInc = t->auxInc; 1327 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1328 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1329 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1330 1331 do { 1332 l = (int32_t)*in++; 1333 r = (int32_t)*in++; 1334 *out++ += (vl >> 16) * l; 1335 *out++ += (vr >> 16) * r; 1336 *aux++ += (va >> 17) * (l + r); 1337 vl += vlInc; 1338 vr += vrInc; 1339 va += vaInc; 1340 } while (--frameCount); 1341 1342 t->prevVolume[0] = vl; 1343 t->prevVolume[1] = vr; 1344 t->prevAuxLevel = va; 1345 t->adjustVolumeRamp(true); 1346 } 1347 1348 // constant gain 1349 else { 1350 const uint32_t vrl = t->volumeRL; 1351 const int16_t va = (int16_t)t->auxLevel; 1352 do { 1353 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1354 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1); 1355 in += 2; 1356 out[0] = mulAddRL(1, rl, vrl, out[0]); 1357 out[1] = mulAddRL(0, rl, vrl, out[1]); 1358 out += 2; 1359 aux[0] = mulAdd(a, va, aux[0]); 1360 aux++; 1361 } while (--frameCount); 1362 } 1363 } else { 1364 // ramp gain 1365 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1366 int32_t vl = t->prevVolume[0]; 1367 int32_t vr = t->prevVolume[1]; 1368 const int32_t vlInc = t->volumeInc[0]; 1369 const int32_t vrInc = t->volumeInc[1]; 1370 1371 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1372 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1373 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1374 1375 do { 1376 *out++ += (vl >> 16) * (int32_t) *in++; 1377 *out++ += (vr >> 16) * (int32_t) *in++; 1378 vl += vlInc; 1379 vr += vrInc; 1380 } while (--frameCount); 1381 1382 t->prevVolume[0] = vl; 1383 t->prevVolume[1] = vr; 1384 t->adjustVolumeRamp(false); 1385 } 1386 1387 // constant gain 1388 else { 1389 const uint32_t vrl = t->volumeRL; 1390 do { 1391 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1392 in += 2; 1393 out[0] = mulAddRL(1, rl, vrl, out[0]); 1394 out[1] = mulAddRL(0, rl, vrl, out[1]); 1395 out += 2; 1396 } while (--frameCount); 1397 } 1398 } 1399 t->in = in; 1400} 1401 1402void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, 1403 int32_t* temp __unused, int32_t* aux) 1404{ 1405 ALOGVV("track__16BitsMono\n"); 1406 const int16_t *in = static_cast<int16_t const *>(t->in); 1407 1408 if (CC_UNLIKELY(aux != NULL)) { 1409 // ramp gain 1410 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1411 int32_t vl = t->prevVolume[0]; 1412 int32_t vr = t->prevVolume[1]; 1413 int32_t va = t->prevAuxLevel; 1414 const int32_t vlInc = t->volumeInc[0]; 1415 const int32_t vrInc = t->volumeInc[1]; 1416 const int32_t vaInc = t->auxInc; 1417 1418 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1419 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1420 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1421 1422 do { 1423 int32_t l = *in++; 1424 *out++ += (vl >> 16) * l; 1425 *out++ += (vr >> 16) * l; 1426 *aux++ += (va >> 16) * l; 1427 vl += vlInc; 1428 vr += vrInc; 1429 va += vaInc; 1430 } while (--frameCount); 1431 1432 t->prevVolume[0] = vl; 1433 t->prevVolume[1] = vr; 1434 t->prevAuxLevel = va; 1435 t->adjustVolumeRamp(true); 1436 } 1437 // constant gain 1438 else { 1439 const int16_t vl = t->volume[0]; 1440 const int16_t vr = t->volume[1]; 1441 const int16_t va = (int16_t)t->auxLevel; 1442 do { 1443 int16_t l = *in++; 1444 out[0] = mulAdd(l, vl, out[0]); 1445 out[1] = mulAdd(l, vr, out[1]); 1446 out += 2; 1447 aux[0] = mulAdd(l, va, aux[0]); 1448 aux++; 1449 } while (--frameCount); 1450 } 1451 } else { 1452 // ramp gain 1453 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1454 int32_t vl = t->prevVolume[0]; 1455 int32_t vr = t->prevVolume[1]; 1456 const int32_t vlInc = t->volumeInc[0]; 1457 const int32_t vrInc = t->volumeInc[1]; 1458 1459 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1460 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1461 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1462 1463 do { 1464 int32_t l = *in++; 1465 *out++ += (vl >> 16) * l; 1466 *out++ += (vr >> 16) * l; 1467 vl += vlInc; 1468 vr += vrInc; 1469 } while (--frameCount); 1470 1471 t->prevVolume[0] = vl; 1472 t->prevVolume[1] = vr; 1473 t->adjustVolumeRamp(false); 1474 } 1475 // constant gain 1476 else { 1477 const int16_t vl = t->volume[0]; 1478 const int16_t vr = t->volume[1]; 1479 do { 1480 int16_t l = *in++; 1481 out[0] = mulAdd(l, vl, out[0]); 1482 out[1] = mulAdd(l, vr, out[1]); 1483 out += 2; 1484 } while (--frameCount); 1485 } 1486 } 1487 t->in = in; 1488} 1489 1490// no-op case 1491void AudioMixer::process__nop(state_t* state, int64_t pts) 1492{ 1493 ALOGVV("process__nop\n"); 1494 uint32_t e0 = state->enabledTracks; 1495 while (e0) { 1496 // process by group of tracks with same output buffer to 1497 // avoid multiple memset() on same buffer 1498 uint32_t e1 = e0, e2 = e0; 1499 int i = 31 - __builtin_clz(e1); 1500 { 1501 track_t& t1 = state->tracks[i]; 1502 e2 &= ~(1<<i); 1503 while (e2) { 1504 i = 31 - __builtin_clz(e2); 1505 e2 &= ~(1<<i); 1506 track_t& t2 = state->tracks[i]; 1507 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1508 e1 &= ~(1<<i); 1509 } 1510 } 1511 e0 &= ~(e1); 1512 1513 memset(t1.mainBuffer, 0, state->frameCount * t1.mMixerChannelCount 1514 * audio_bytes_per_sample(t1.mMixerFormat)); 1515 } 1516 1517 while (e1) { 1518 i = 31 - __builtin_clz(e1); 1519 e1 &= ~(1<<i); 1520 { 1521 track_t& t3 = state->tracks[i]; 1522 size_t outFrames = state->frameCount; 1523 while (outFrames) { 1524 t3.buffer.frameCount = outFrames; 1525 int64_t outputPTS = calculateOutputPTS( 1526 t3, pts, state->frameCount - outFrames); 1527 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS); 1528 if (t3.buffer.raw == NULL) break; 1529 outFrames -= t3.buffer.frameCount; 1530 t3.bufferProvider->releaseBuffer(&t3.buffer); 1531 } 1532 } 1533 } 1534 } 1535} 1536 1537// generic code without resampling 1538void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts) 1539{ 1540 ALOGVV("process__genericNoResampling\n"); 1541 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32))); 1542 1543 // acquire each track's buffer 1544 uint32_t enabledTracks = state->enabledTracks; 1545 uint32_t e0 = enabledTracks; 1546 while (e0) { 1547 const int i = 31 - __builtin_clz(e0); 1548 e0 &= ~(1<<i); 1549 track_t& t = state->tracks[i]; 1550 t.buffer.frameCount = state->frameCount; 1551 t.bufferProvider->getNextBuffer(&t.buffer, pts); 1552 t.frameCount = t.buffer.frameCount; 1553 t.in = t.buffer.raw; 1554 } 1555 1556 e0 = enabledTracks; 1557 while (e0) { 1558 // process by group of tracks with same output buffer to 1559 // optimize cache use 1560 uint32_t e1 = e0, e2 = e0; 1561 int j = 31 - __builtin_clz(e1); 1562 track_t& t1 = state->tracks[j]; 1563 e2 &= ~(1<<j); 1564 while (e2) { 1565 j = 31 - __builtin_clz(e2); 1566 e2 &= ~(1<<j); 1567 track_t& t2 = state->tracks[j]; 1568 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1569 e1 &= ~(1<<j); 1570 } 1571 } 1572 e0 &= ~(e1); 1573 // this assumes output 16 bits stereo, no resampling 1574 int32_t *out = t1.mainBuffer; 1575 size_t numFrames = 0; 1576 do { 1577 memset(outTemp, 0, sizeof(outTemp)); 1578 e2 = e1; 1579 while (e2) { 1580 const int i = 31 - __builtin_clz(e2); 1581 e2 &= ~(1<<i); 1582 track_t& t = state->tracks[i]; 1583 size_t outFrames = BLOCKSIZE; 1584 int32_t *aux = NULL; 1585 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1586 aux = t.auxBuffer + numFrames; 1587 } 1588 while (outFrames) { 1589 // t.in == NULL can happen if the track was flushed just after having 1590 // been enabled for mixing. 1591 if (t.in == NULL) { 1592 enabledTracks &= ~(1<<i); 1593 e1 &= ~(1<<i); 1594 break; 1595 } 1596 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount; 1597 if (inFrames > 0) { 1598 t.hook(&t, outTemp + (BLOCKSIZE - outFrames) * t.mMixerChannelCount, 1599 inFrames, state->resampleTemp, aux); 1600 t.frameCount -= inFrames; 1601 outFrames -= inFrames; 1602 if (CC_UNLIKELY(aux != NULL)) { 1603 aux += inFrames; 1604 } 1605 } 1606 if (t.frameCount == 0 && outFrames) { 1607 t.bufferProvider->releaseBuffer(&t.buffer); 1608 t.buffer.frameCount = (state->frameCount - numFrames) - 1609 (BLOCKSIZE - outFrames); 1610 int64_t outputPTS = calculateOutputPTS( 1611 t, pts, numFrames + (BLOCKSIZE - outFrames)); 1612 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1613 t.in = t.buffer.raw; 1614 if (t.in == NULL) { 1615 enabledTracks &= ~(1<<i); 1616 e1 &= ~(1<<i); 1617 break; 1618 } 1619 t.frameCount = t.buffer.frameCount; 1620 } 1621 } 1622 } 1623 1624 convertMixerFormat(out, t1.mMixerFormat, outTemp, t1.mMixerInFormat, 1625 BLOCKSIZE * t1.mMixerChannelCount); 1626 // TODO: fix ugly casting due to choice of out pointer type 1627 out = reinterpret_cast<int32_t*>((uint8_t*)out 1628 + BLOCKSIZE * t1.mMixerChannelCount 1629 * audio_bytes_per_sample(t1.mMixerFormat)); 1630 numFrames += BLOCKSIZE; 1631 } while (numFrames < state->frameCount); 1632 } 1633 1634 // release each track's buffer 1635 e0 = enabledTracks; 1636 while (e0) { 1637 const int i = 31 - __builtin_clz(e0); 1638 e0 &= ~(1<<i); 1639 track_t& t = state->tracks[i]; 1640 t.bufferProvider->releaseBuffer(&t.buffer); 1641 } 1642} 1643 1644 1645// generic code with resampling 1646void AudioMixer::process__genericResampling(state_t* state, int64_t pts) 1647{ 1648 ALOGVV("process__genericResampling\n"); 1649 // this const just means that local variable outTemp doesn't change 1650 int32_t* const outTemp = state->outputTemp; 1651 size_t numFrames = state->frameCount; 1652 1653 uint32_t e0 = state->enabledTracks; 1654 while (e0) { 1655 // process by group of tracks with same output buffer 1656 // to optimize cache use 1657 uint32_t e1 = e0, e2 = e0; 1658 int j = 31 - __builtin_clz(e1); 1659 track_t& t1 = state->tracks[j]; 1660 e2 &= ~(1<<j); 1661 while (e2) { 1662 j = 31 - __builtin_clz(e2); 1663 e2 &= ~(1<<j); 1664 track_t& t2 = state->tracks[j]; 1665 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1666 e1 &= ~(1<<j); 1667 } 1668 } 1669 e0 &= ~(e1); 1670 int32_t *out = t1.mainBuffer; 1671 memset(outTemp, 0, sizeof(*outTemp) * t1.mMixerChannelCount * state->frameCount); 1672 while (e1) { 1673 const int i = 31 - __builtin_clz(e1); 1674 e1 &= ~(1<<i); 1675 track_t& t = state->tracks[i]; 1676 int32_t *aux = NULL; 1677 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1678 aux = t.auxBuffer; 1679 } 1680 1681 // this is a little goofy, on the resampling case we don't 1682 // acquire/release the buffers because it's done by 1683 // the resampler. 1684 if (t.needs & NEEDS_RESAMPLE) { 1685 t.resampler->setPTS(pts); 1686 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux); 1687 } else { 1688 1689 size_t outFrames = 0; 1690 1691 while (outFrames < numFrames) { 1692 t.buffer.frameCount = numFrames - outFrames; 1693 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames); 1694 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1695 t.in = t.buffer.raw; 1696 // t.in == NULL can happen if the track was flushed just after having 1697 // been enabled for mixing. 1698 if (t.in == NULL) break; 1699 1700 if (CC_UNLIKELY(aux != NULL)) { 1701 aux += outFrames; 1702 } 1703 t.hook(&t, outTemp + outFrames * t.mMixerChannelCount, t.buffer.frameCount, 1704 state->resampleTemp, aux); 1705 outFrames += t.buffer.frameCount; 1706 t.bufferProvider->releaseBuffer(&t.buffer); 1707 } 1708 } 1709 } 1710 convertMixerFormat(out, t1.mMixerFormat, 1711 outTemp, t1.mMixerInFormat, numFrames * t1.mMixerChannelCount); 1712 } 1713} 1714 1715// one track, 16 bits stereo without resampling is the most common case 1716void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state, 1717 int64_t pts) 1718{ 1719 ALOGVV("process__OneTrack16BitsStereoNoResampling\n"); 1720 // This method is only called when state->enabledTracks has exactly 1721 // one bit set. The asserts below would verify this, but are commented out 1722 // since the whole point of this method is to optimize performance. 1723 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled"); 1724 const int i = 31 - __builtin_clz(state->enabledTracks); 1725 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 1726 const track_t& t = state->tracks[i]; 1727 1728 AudioBufferProvider::Buffer& b(t.buffer); 1729 1730 int32_t* out = t.mainBuffer; 1731 float *fout = reinterpret_cast<float*>(out); 1732 size_t numFrames = state->frameCount; 1733 1734 const int16_t vl = t.volume[0]; 1735 const int16_t vr = t.volume[1]; 1736 const uint32_t vrl = t.volumeRL; 1737 while (numFrames) { 1738 b.frameCount = numFrames; 1739 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer); 1740 t.bufferProvider->getNextBuffer(&b, outputPTS); 1741 const int16_t *in = b.i16; 1742 1743 // in == NULL can happen if the track was flushed just after having 1744 // been enabled for mixing. 1745 if (in == NULL || (((uintptr_t)in) & 3)) { 1746 memset(out, 0, numFrames 1747 * t.mMixerChannelCount * audio_bytes_per_sample(t.mMixerFormat)); 1748 ALOGE_IF((((uintptr_t)in) & 3), "process stereo track: input buffer alignment pb: " 1749 "buffer %p track %d, channels %d, needs %08x", 1750 in, i, t.channelCount, t.needs); 1751 return; 1752 } 1753 size_t outFrames = b.frameCount; 1754 1755 switch (t.mMixerFormat) { 1756 case AUDIO_FORMAT_PCM_FLOAT: 1757 do { 1758 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1759 in += 2; 1760 int32_t l = mulRL(1, rl, vrl); 1761 int32_t r = mulRL(0, rl, vrl); 1762 *fout++ = float_from_q4_27(l); 1763 *fout++ = float_from_q4_27(r); 1764 // Note: In case of later int16_t sink output, 1765 // conversion and clamping is done by memcpy_to_i16_from_float(). 1766 } while (--outFrames); 1767 break; 1768 case AUDIO_FORMAT_PCM_16_BIT: 1769 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) { 1770 // volume is boosted, so we might need to clamp even though 1771 // we process only one track. 1772 do { 1773 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1774 in += 2; 1775 int32_t l = mulRL(1, rl, vrl) >> 12; 1776 int32_t r = mulRL(0, rl, vrl) >> 12; 1777 // clamping... 1778 l = clamp16(l); 1779 r = clamp16(r); 1780 *out++ = (r<<16) | (l & 0xFFFF); 1781 } while (--outFrames); 1782 } else { 1783 do { 1784 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1785 in += 2; 1786 int32_t l = mulRL(1, rl, vrl) >> 12; 1787 int32_t r = mulRL(0, rl, vrl) >> 12; 1788 *out++ = (r<<16) | (l & 0xFFFF); 1789 } while (--outFrames); 1790 } 1791 break; 1792 default: 1793 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat); 1794 } 1795 numFrames -= b.frameCount; 1796 t.bufferProvider->releaseBuffer(&b); 1797 } 1798} 1799 1800int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS, 1801 int outputFrameIndex) 1802{ 1803 if (AudioBufferProvider::kInvalidPTS == basePTS) { 1804 return AudioBufferProvider::kInvalidPTS; 1805 } 1806 1807 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate); 1808} 1809 1810/*static*/ uint64_t AudioMixer::sLocalTimeFreq; 1811/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; 1812 1813/*static*/ void AudioMixer::sInitRoutine() 1814{ 1815 LocalClock lc; 1816 sLocalTimeFreq = lc.getLocalFreq(); // for the resampler 1817 1818 DownmixerBufferProvider::init(); // for the downmixer 1819} 1820 1821/* TODO: consider whether this level of optimization is necessary. 1822 * Perhaps just stick with a single for loop. 1823 */ 1824 1825// Needs to derive a compile time constant (constexpr). Could be targeted to go 1826// to a MONOVOL mixtype based on MAX_NUM_VOLUMES, but that's an unnecessary complication. 1827#define MIXTYPE_MONOVOL(mixtype) (mixtype == MIXTYPE_MULTI ? MIXTYPE_MULTI_MONOVOL : \ 1828 mixtype == MIXTYPE_MULTI_SAVEONLY ? MIXTYPE_MULTI_SAVEONLY_MONOVOL : mixtype) 1829 1830/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1831 * TO: int32_t (Q4.27) or float 1832 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1833 * TA: int32_t (Q4.27) 1834 */ 1835template <int MIXTYPE, 1836 typename TO, typename TI, typename TV, typename TA, typename TAV> 1837static void volumeRampMulti(uint32_t channels, TO* out, size_t frameCount, 1838 const TI* in, TA* aux, TV *vol, const TV *volinc, TAV *vola, TAV volainc) 1839{ 1840 switch (channels) { 1841 case 1: 1842 volumeRampMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, volinc, vola, volainc); 1843 break; 1844 case 2: 1845 volumeRampMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, volinc, vola, volainc); 1846 break; 1847 case 3: 1848 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, 1849 frameCount, in, aux, vol, volinc, vola, volainc); 1850 break; 1851 case 4: 1852 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, 1853 frameCount, in, aux, vol, volinc, vola, volainc); 1854 break; 1855 case 5: 1856 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, 1857 frameCount, in, aux, vol, volinc, vola, volainc); 1858 break; 1859 case 6: 1860 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, 1861 frameCount, in, aux, vol, volinc, vola, volainc); 1862 break; 1863 case 7: 1864 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, 1865 frameCount, in, aux, vol, volinc, vola, volainc); 1866 break; 1867 case 8: 1868 volumeRampMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, 1869 frameCount, in, aux, vol, volinc, vola, volainc); 1870 break; 1871 } 1872} 1873 1874/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1875 * TO: int32_t (Q4.27) or float 1876 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1877 * TA: int32_t (Q4.27) 1878 */ 1879template <int MIXTYPE, 1880 typename TO, typename TI, typename TV, typename TA, typename TAV> 1881static void volumeMulti(uint32_t channels, TO* out, size_t frameCount, 1882 const TI* in, TA* aux, const TV *vol, TAV vola) 1883{ 1884 switch (channels) { 1885 case 1: 1886 volumeMulti<MIXTYPE, 1>(out, frameCount, in, aux, vol, vola); 1887 break; 1888 case 2: 1889 volumeMulti<MIXTYPE, 2>(out, frameCount, in, aux, vol, vola); 1890 break; 1891 case 3: 1892 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 3>(out, frameCount, in, aux, vol, vola); 1893 break; 1894 case 4: 1895 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 4>(out, frameCount, in, aux, vol, vola); 1896 break; 1897 case 5: 1898 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 5>(out, frameCount, in, aux, vol, vola); 1899 break; 1900 case 6: 1901 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 6>(out, frameCount, in, aux, vol, vola); 1902 break; 1903 case 7: 1904 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 7>(out, frameCount, in, aux, vol, vola); 1905 break; 1906 case 8: 1907 volumeMulti<MIXTYPE_MONOVOL(MIXTYPE), 8>(out, frameCount, in, aux, vol, vola); 1908 break; 1909 } 1910} 1911 1912/* MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1913 * USEFLOATVOL (set to true if float volume is used) 1914 * ADJUSTVOL (set to true if volume ramp parameters needs adjustment afterwards) 1915 * TO: int32_t (Q4.27) or float 1916 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1917 * TA: int32_t (Q4.27) 1918 */ 1919template <int MIXTYPE, bool USEFLOATVOL, bool ADJUSTVOL, 1920 typename TO, typename TI, typename TA> 1921void AudioMixer::volumeMix(TO *out, size_t outFrames, 1922 const TI *in, TA *aux, bool ramp, AudioMixer::track_t *t) 1923{ 1924 if (USEFLOATVOL) { 1925 if (ramp) { 1926 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1927 t->mPrevVolume, t->mVolumeInc, &t->prevAuxLevel, t->auxInc); 1928 if (ADJUSTVOL) { 1929 t->adjustVolumeRamp(aux != NULL, true); 1930 } 1931 } else { 1932 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1933 t->mVolume, t->auxLevel); 1934 } 1935 } else { 1936 if (ramp) { 1937 volumeRampMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1938 t->prevVolume, t->volumeInc, &t->prevAuxLevel, t->auxInc); 1939 if (ADJUSTVOL) { 1940 t->adjustVolumeRamp(aux != NULL); 1941 } 1942 } else { 1943 volumeMulti<MIXTYPE>(t->mMixerChannelCount, out, outFrames, in, aux, 1944 t->volume, t->auxLevel); 1945 } 1946 } 1947} 1948 1949/* This process hook is called when there is a single track without 1950 * aux buffer, volume ramp, or resampling. 1951 * TODO: Update the hook selection: this can properly handle aux and ramp. 1952 * 1953 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 1954 * TO: int32_t (Q4.27) or float 1955 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 1956 * TA: int32_t (Q4.27) 1957 */ 1958template <int MIXTYPE, typename TO, typename TI, typename TA> 1959void AudioMixer::process_NoResampleOneTrack(state_t* state, int64_t pts) 1960{ 1961 ALOGVV("process_NoResampleOneTrack\n"); 1962 // CLZ is faster than CTZ on ARM, though really not sure if true after 31 - clz. 1963 const int i = 31 - __builtin_clz(state->enabledTracks); 1964 ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 1965 track_t *t = &state->tracks[i]; 1966 const uint32_t channels = t->mMixerChannelCount; 1967 TO* out = reinterpret_cast<TO*>(t->mainBuffer); 1968 TA* aux = reinterpret_cast<TA*>(t->auxBuffer); 1969 const bool ramp = t->needsRamp(); 1970 1971 for (size_t numFrames = state->frameCount; numFrames; ) { 1972 AudioBufferProvider::Buffer& b(t->buffer); 1973 // get input buffer 1974 b.frameCount = numFrames; 1975 const int64_t outputPTS = calculateOutputPTS(*t, pts, state->frameCount - numFrames); 1976 t->bufferProvider->getNextBuffer(&b, outputPTS); 1977 const TI *in = reinterpret_cast<TI*>(b.raw); 1978 1979 // in == NULL can happen if the track was flushed just after having 1980 // been enabled for mixing. 1981 if (in == NULL || (((uintptr_t)in) & 3)) { 1982 memset(out, 0, numFrames 1983 * channels * audio_bytes_per_sample(t->mMixerFormat)); 1984 ALOGE_IF((((uintptr_t)in) & 3), "process_NoResampleOneTrack: bus error: " 1985 "buffer %p track %p, channels %d, needs %#x", 1986 in, t, t->channelCount, t->needs); 1987 return; 1988 } 1989 1990 const size_t outFrames = b.frameCount; 1991 volumeMix<MIXTYPE, is_same<TI, float>::value, false> ( 1992 out, outFrames, in, aux, ramp, t); 1993 1994 out += outFrames * channels; 1995 if (aux != NULL) { 1996 aux += channels; 1997 } 1998 numFrames -= b.frameCount; 1999 2000 // release buffer 2001 t->bufferProvider->releaseBuffer(&b); 2002 } 2003 if (ramp) { 2004 t->adjustVolumeRamp(aux != NULL, is_same<TI, float>::value); 2005 } 2006} 2007 2008/* This track hook is called to do resampling then mixing, 2009 * pulling from the track's upstream AudioBufferProvider. 2010 * 2011 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 2012 * TO: int32_t (Q4.27) or float 2013 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 2014 * TA: int32_t (Q4.27) 2015 */ 2016template <int MIXTYPE, typename TO, typename TI, typename TA> 2017void AudioMixer::track__Resample(track_t* t, TO* out, size_t outFrameCount, TO* temp, TA* aux) 2018{ 2019 ALOGVV("track__Resample\n"); 2020 t->resampler->setSampleRate(t->sampleRate); 2021 const bool ramp = t->needsRamp(); 2022 if (ramp || aux != NULL) { 2023 // if ramp: resample with unity gain to temp buffer and scale/mix in 2nd step. 2024 // if aux != NULL: resample with unity gain to temp buffer then apply send level. 2025 2026 t->resampler->setVolume(UNITY_GAIN_FLOAT, UNITY_GAIN_FLOAT); 2027 memset(temp, 0, outFrameCount * t->mMixerChannelCount * sizeof(TO)); 2028 t->resampler->resample((int32_t*)temp, outFrameCount, t->bufferProvider); 2029 2030 volumeMix<MIXTYPE, is_same<TI, float>::value, true>( 2031 out, outFrameCount, temp, aux, ramp, t); 2032 2033 } else { // constant volume gain 2034 t->resampler->setVolume(t->mVolume[0], t->mVolume[1]); 2035 t->resampler->resample((int32_t*)out, outFrameCount, t->bufferProvider); 2036 } 2037} 2038 2039/* This track hook is called to mix a track, when no resampling is required. 2040 * The input buffer should be present in t->in. 2041 * 2042 * MIXTYPE (see AudioMixerOps.h MIXTYPE_* enumeration) 2043 * TO: int32_t (Q4.27) or float 2044 * TI: int32_t (Q4.27) or int16_t (Q0.15) or float 2045 * TA: int32_t (Q4.27) 2046 */ 2047template <int MIXTYPE, typename TO, typename TI, typename TA> 2048void AudioMixer::track__NoResample(track_t* t, TO* out, size_t frameCount, 2049 TO* temp __unused, TA* aux) 2050{ 2051 ALOGVV("track__NoResample\n"); 2052 const TI *in = static_cast<const TI *>(t->in); 2053 2054 volumeMix<MIXTYPE, is_same<TI, float>::value, true>( 2055 out, frameCount, in, aux, t->needsRamp(), t); 2056 2057 // MIXTYPE_MONOEXPAND reads a single input channel and expands to NCHAN output channels. 2058 // MIXTYPE_MULTI reads NCHAN input channels and places to NCHAN output channels. 2059 in += (MIXTYPE == MIXTYPE_MONOEXPAND) ? frameCount : frameCount * t->mMixerChannelCount; 2060 t->in = in; 2061} 2062 2063/* The Mixer engine generates either int32_t (Q4_27) or float data. 2064 * We use this function to convert the engine buffers 2065 * to the desired mixer output format, either int16_t (Q.15) or float. 2066 */ 2067void AudioMixer::convertMixerFormat(void *out, audio_format_t mixerOutFormat, 2068 void *in, audio_format_t mixerInFormat, size_t sampleCount) 2069{ 2070 switch (mixerInFormat) { 2071 case AUDIO_FORMAT_PCM_FLOAT: 2072 switch (mixerOutFormat) { 2073 case AUDIO_FORMAT_PCM_FLOAT: 2074 memcpy(out, in, sampleCount * sizeof(float)); // MEMCPY. TODO optimize out 2075 break; 2076 case AUDIO_FORMAT_PCM_16_BIT: 2077 memcpy_to_i16_from_float((int16_t*)out, (float*)in, sampleCount); 2078 break; 2079 default: 2080 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2081 break; 2082 } 2083 break; 2084 case AUDIO_FORMAT_PCM_16_BIT: 2085 switch (mixerOutFormat) { 2086 case AUDIO_FORMAT_PCM_FLOAT: 2087 memcpy_to_float_from_q4_27((float*)out, (int32_t*)in, sampleCount); 2088 break; 2089 case AUDIO_FORMAT_PCM_16_BIT: 2090 // two int16_t are produced per iteration 2091 ditherAndClamp((int32_t*)out, (int32_t*)in, sampleCount >> 1); 2092 break; 2093 default: 2094 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2095 break; 2096 } 2097 break; 2098 default: 2099 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2100 break; 2101 } 2102} 2103 2104/* Returns the proper track hook to use for mixing the track into the output buffer. 2105 */ 2106AudioMixer::hook_t AudioMixer::getTrackHook(int trackType, uint32_t channelCount, 2107 audio_format_t mixerInFormat, audio_format_t mixerOutFormat __unused) 2108{ 2109 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { 2110 switch (trackType) { 2111 case TRACKTYPE_NOP: 2112 return track__nop; 2113 case TRACKTYPE_RESAMPLE: 2114 return track__genericResample; 2115 case TRACKTYPE_NORESAMPLEMONO: 2116 return track__16BitsMono; 2117 case TRACKTYPE_NORESAMPLE: 2118 return track__16BitsStereo; 2119 default: 2120 LOG_ALWAYS_FATAL("bad trackType: %d", trackType); 2121 break; 2122 } 2123 } 2124 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS); 2125 switch (trackType) { 2126 case TRACKTYPE_NOP: 2127 return track__nop; 2128 case TRACKTYPE_RESAMPLE: 2129 switch (mixerInFormat) { 2130 case AUDIO_FORMAT_PCM_FLOAT: 2131 return (AudioMixer::hook_t) 2132 track__Resample<MIXTYPE_MULTI, float /*TO*/, float /*TI*/, int32_t /*TA*/>; 2133 case AUDIO_FORMAT_PCM_16_BIT: 2134 return (AudioMixer::hook_t)\ 2135 track__Resample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>; 2136 default: 2137 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2138 break; 2139 } 2140 break; 2141 case TRACKTYPE_NORESAMPLEMONO: 2142 switch (mixerInFormat) { 2143 case AUDIO_FORMAT_PCM_FLOAT: 2144 return (AudioMixer::hook_t) 2145 track__NoResample<MIXTYPE_MONOEXPAND, float, float, int32_t>; 2146 case AUDIO_FORMAT_PCM_16_BIT: 2147 return (AudioMixer::hook_t) 2148 track__NoResample<MIXTYPE_MONOEXPAND, int32_t, int16_t, int32_t>; 2149 default: 2150 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2151 break; 2152 } 2153 break; 2154 case TRACKTYPE_NORESAMPLE: 2155 switch (mixerInFormat) { 2156 case AUDIO_FORMAT_PCM_FLOAT: 2157 return (AudioMixer::hook_t) 2158 track__NoResample<MIXTYPE_MULTI, float, float, int32_t>; 2159 case AUDIO_FORMAT_PCM_16_BIT: 2160 return (AudioMixer::hook_t) 2161 track__NoResample<MIXTYPE_MULTI, int32_t, int16_t, int32_t>; 2162 default: 2163 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2164 break; 2165 } 2166 break; 2167 default: 2168 LOG_ALWAYS_FATAL("bad trackType: %d", trackType); 2169 break; 2170 } 2171 return NULL; 2172} 2173 2174/* Returns the proper process hook for mixing tracks. Currently works only for 2175 * PROCESSTYPE_NORESAMPLEONETRACK, a mix involving one track, no resampling. 2176 */ 2177AudioMixer::process_hook_t AudioMixer::getProcessHook(int processType, uint32_t channelCount, 2178 audio_format_t mixerInFormat, audio_format_t mixerOutFormat) 2179{ 2180 if (processType != PROCESSTYPE_NORESAMPLEONETRACK) { // Only NORESAMPLEONETRACK 2181 LOG_ALWAYS_FATAL("bad processType: %d", processType); 2182 return NULL; 2183 } 2184 if (!kUseNewMixer && channelCount == FCC_2 && mixerInFormat == AUDIO_FORMAT_PCM_16_BIT) { 2185 return process__OneTrack16BitsStereoNoResampling; 2186 } 2187 LOG_ALWAYS_FATAL_IF(channelCount > MAX_NUM_CHANNELS); 2188 switch (mixerInFormat) { 2189 case AUDIO_FORMAT_PCM_FLOAT: 2190 switch (mixerOutFormat) { 2191 case AUDIO_FORMAT_PCM_FLOAT: 2192 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2193 float /*TO*/, float /*TI*/, int32_t /*TA*/>; 2194 case AUDIO_FORMAT_PCM_16_BIT: 2195 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2196 int16_t, float, int32_t>; 2197 default: 2198 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2199 break; 2200 } 2201 break; 2202 case AUDIO_FORMAT_PCM_16_BIT: 2203 switch (mixerOutFormat) { 2204 case AUDIO_FORMAT_PCM_FLOAT: 2205 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2206 float, int16_t, int32_t>; 2207 case AUDIO_FORMAT_PCM_16_BIT: 2208 return process_NoResampleOneTrack<MIXTYPE_MULTI_SAVEONLY, 2209 int16_t, int16_t, int32_t>; 2210 default: 2211 LOG_ALWAYS_FATAL("bad mixerOutFormat: %#x", mixerOutFormat); 2212 break; 2213 } 2214 break; 2215 default: 2216 LOG_ALWAYS_FATAL("bad mixerInFormat: %#x", mixerInFormat); 2217 break; 2218 } 2219 return NULL; 2220} 2221 2222// ---------------------------------------------------------------------------- 2223}; // namespace android 2224