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