AudioMixer.cpp revision 6be494077f8d7970f3a88129c5d139c5a0c88f6d
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 <sys/types.h> 26 27#include <utils/Errors.h> 28#include <utils/Log.h> 29 30#include <cutils/bitops.h> 31#include <cutils/compiler.h> 32#include <utils/Debug.h> 33 34#include <system/audio.h> 35 36#include <audio_utils/primitives.h> 37#include <audio_utils/format.h> 38#include <common_time/local_clock.h> 39#include <common_time/cc_helper.h> 40 41#include <media/EffectsFactoryApi.h> 42 43#include "AudioMixer.h" 44 45namespace android { 46 47// ---------------------------------------------------------------------------- 48AudioMixer::DownmixerBufferProvider::DownmixerBufferProvider() : AudioBufferProvider(), 49 mTrackBufferProvider(NULL), mDownmixHandle(NULL) 50{ 51} 52 53AudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider() 54{ 55 ALOGV("AudioMixer deleting DownmixerBufferProvider (%p)", this); 56 EffectRelease(mDownmixHandle); 57} 58 59status_t AudioMixer::DownmixerBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer, 60 int64_t pts) { 61 //ALOGV("DownmixerBufferProvider::getNextBuffer()"); 62 if (mTrackBufferProvider != NULL) { 63 status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts); 64 if (res == OK) { 65 mDownmixConfig.inputCfg.buffer.frameCount = pBuffer->frameCount; 66 mDownmixConfig.inputCfg.buffer.raw = pBuffer->raw; 67 mDownmixConfig.outputCfg.buffer.frameCount = pBuffer->frameCount; 68 mDownmixConfig.outputCfg.buffer.raw = mDownmixConfig.inputCfg.buffer.raw; 69 // in-place so overwrite the buffer contents, has been set in prepareTrackForDownmix() 70 //mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; 71 72 res = (*mDownmixHandle)->process(mDownmixHandle, 73 &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer); 74 //ALOGV("getNextBuffer is downmixing"); 75 } 76 return res; 77 } else { 78 ALOGE("DownmixerBufferProvider::getNextBuffer() error: NULL track buffer provider"); 79 return NO_INIT; 80 } 81} 82 83void AudioMixer::DownmixerBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) { 84 //ALOGV("DownmixerBufferProvider::releaseBuffer()"); 85 if (mTrackBufferProvider != NULL) { 86 mTrackBufferProvider->releaseBuffer(pBuffer); 87 } else { 88 ALOGE("DownmixerBufferProvider::releaseBuffer() error: NULL track buffer provider"); 89 } 90} 91 92template <typename T> 93T min(const T& a, const T& b) 94{ 95 return a < b ? a : b; 96} 97 98AudioMixer::ReformatBufferProvider::ReformatBufferProvider(int32_t channels, 99 audio_format_t inputFormat, audio_format_t outputFormat) : 100 mTrackBufferProvider(NULL), 101 mChannels(channels), 102 mInputFormat(inputFormat), 103 mOutputFormat(outputFormat), 104 mInputFrameSize(channels * audio_bytes_per_sample(inputFormat)), 105 mOutputFrameSize(channels * audio_bytes_per_sample(outputFormat)), 106 mOutputData(NULL), 107 mOutputCount(0), 108 mConsumed(0) 109{ 110 ALOGV("ReformatBufferProvider(%p)(%d, %#x, %#x)", this, channels, inputFormat, outputFormat); 111 if (requiresInternalBuffers()) { 112 mOutputCount = 256; 113 (void)posix_memalign(&mOutputData, 32, mOutputCount * mOutputFrameSize); 114 } 115 mBuffer.frameCount = 0; 116} 117 118AudioMixer::ReformatBufferProvider::~ReformatBufferProvider() 119{ 120 ALOGV("~ReformatBufferProvider(%p)", this); 121 if (mBuffer.frameCount != 0) { 122 mTrackBufferProvider->releaseBuffer(&mBuffer); 123 } 124 free(mOutputData); 125} 126 127status_t AudioMixer::ReformatBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer, 128 int64_t pts) { 129 //ALOGV("ReformatBufferProvider(%p)::getNextBuffer(%p (%zu), %lld)", 130 // this, pBuffer, pBuffer->frameCount, pts); 131 if (!requiresInternalBuffers()) { 132 status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts); 133 if (res == OK) { 134 memcpy_by_audio_format(pBuffer->raw, mOutputFormat, pBuffer->raw, mInputFormat, 135 pBuffer->frameCount * mChannels); 136 } 137 return res; 138 } 139 if (mBuffer.frameCount == 0) { 140 mBuffer.frameCount = pBuffer->frameCount; 141 status_t res = mTrackBufferProvider->getNextBuffer(&mBuffer, pts); 142 // TODO: Track down a bug in the upstream provider 143 // LOG_ALWAYS_FATAL_IF(res == OK && mBuffer.frameCount == 0, 144 // "ReformatBufferProvider::getNextBuffer():" 145 // " Invalid zero framecount returned from getNextBuffer()"); 146 if (res != OK || mBuffer.frameCount == 0) { 147 pBuffer->raw = NULL; 148 pBuffer->frameCount = 0; 149 return res; 150 } 151 } 152 ALOG_ASSERT(mConsumed < mBuffer.frameCount); 153 size_t count = min(mOutputCount, mBuffer.frameCount - mConsumed); 154 count = min(count, pBuffer->frameCount); 155 pBuffer->raw = mOutputData; 156 pBuffer->frameCount = count; 157 //ALOGV("reformatting %d frames from %#x to %#x, %d chan", 158 // pBuffer->frameCount, mInputFormat, mOutputFormat, mChannels); 159 memcpy_by_audio_format(pBuffer->raw, mOutputFormat, 160 (uint8_t*)mBuffer.raw + mConsumed * mInputFrameSize, mInputFormat, 161 pBuffer->frameCount * mChannels); 162 return OK; 163} 164 165void AudioMixer::ReformatBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) { 166 //ALOGV("ReformatBufferProvider(%p)::releaseBuffer(%p(%zu))", 167 // this, pBuffer, pBuffer->frameCount); 168 if (!requiresInternalBuffers()) { 169 mTrackBufferProvider->releaseBuffer(pBuffer); 170 return; 171 } 172 // LOG_ALWAYS_FATAL_IF(pBuffer->frameCount == 0, "Invalid framecount"); 173 mConsumed += pBuffer->frameCount; // TODO: update for efficiency to reuse existing content 174 if (mConsumed != 0 && mConsumed >= mBuffer.frameCount) { 175 mConsumed = 0; 176 mTrackBufferProvider->releaseBuffer(&mBuffer); 177 // ALOG_ASSERT(mBuffer.frameCount == 0); 178 } 179 pBuffer->raw = NULL; 180 pBuffer->frameCount = 0; 181} 182 183void AudioMixer::ReformatBufferProvider::reset() { 184 if (mBuffer.frameCount != 0) { 185 mTrackBufferProvider->releaseBuffer(&mBuffer); 186 } 187 mConsumed = 0; 188} 189 190// ---------------------------------------------------------------------------- 191bool AudioMixer::sIsMultichannelCapable = false; 192 193effect_descriptor_t AudioMixer::sDwnmFxDesc; 194 195// Ensure mConfiguredNames bitmask is initialized properly on all architectures. 196// The value of 1 << x is undefined in C when x >= 32. 197 198AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks) 199 : mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1), 200 mSampleRate(sampleRate) 201{ 202 // AudioMixer is not yet capable of multi-channel beyond stereo 203 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS); 204 205 ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u", 206 maxNumTracks, MAX_NUM_TRACKS); 207 208 // AudioMixer is not yet capable of more than 32 active track inputs 209 ALOG_ASSERT(32 >= MAX_NUM_TRACKS, "bad MAX_NUM_TRACKS %d", MAX_NUM_TRACKS); 210 211 // AudioMixer is not yet capable of multi-channel output beyond stereo 212 ALOG_ASSERT(2 == MAX_NUM_CHANNELS, "bad MAX_NUM_CHANNELS %d", MAX_NUM_CHANNELS); 213 214 pthread_once(&sOnceControl, &sInitRoutine); 215 216 mState.enabledTracks= 0; 217 mState.needsChanged = 0; 218 mState.frameCount = frameCount; 219 mState.hook = process__nop; 220 mState.outputTemp = NULL; 221 mState.resampleTemp = NULL; 222 mState.mLog = &mDummyLog; 223 // mState.reserved 224 225 // FIXME Most of the following initialization is probably redundant since 226 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0 227 // and mTrackNames is initially 0. However, leave it here until that's verified. 228 track_t* t = mState.tracks; 229 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 230 t->resampler = NULL; 231 t->downmixerBufferProvider = NULL; 232 t++; 233 } 234 235} 236 237AudioMixer::~AudioMixer() 238{ 239 track_t* t = mState.tracks; 240 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 241 delete t->resampler; 242 delete t->downmixerBufferProvider; 243 t++; 244 } 245 delete [] mState.outputTemp; 246 delete [] mState.resampleTemp; 247} 248 249void AudioMixer::setLog(NBLog::Writer *log) 250{ 251 mState.mLog = log; 252} 253 254int AudioMixer::getTrackName(audio_channel_mask_t channelMask, 255 audio_format_t format, int sessionId) 256{ 257 if (!isValidPcmTrackFormat(format)) { 258 ALOGE("AudioMixer::getTrackName invalid format (%#x)", format); 259 return -1; 260 } 261 uint32_t names = (~mTrackNames) & mConfiguredNames; 262 if (names != 0) { 263 int n = __builtin_ctz(names); 264 ALOGV("add track (%d)", n); 265 // assume default parameters for the track, except where noted below 266 track_t* t = &mState.tracks[n]; 267 t->needs = 0; 268 t->volume[0] = UNITY_GAIN_INT; 269 t->volume[1] = UNITY_GAIN_INT; 270 // no initialization needed 271 // t->prevVolume[0] 272 // t->prevVolume[1] 273 t->volumeInc[0] = 0; 274 t->volumeInc[1] = 0; 275 t->auxLevel = 0; 276 t->auxInc = 0; 277 // no initialization needed 278 // t->prevAuxLevel 279 // t->frameCount 280 t->channelCount = audio_channel_count_from_out_mask(channelMask); 281 t->enabled = false; 282 ALOGV_IF(channelMask != AUDIO_CHANNEL_OUT_STEREO, 283 "Non-stereo channel mask: %d\n", channelMask); 284 t->channelMask = channelMask; 285 t->sessionId = sessionId; 286 // setBufferProvider(name, AudioBufferProvider *) is required before enable(name) 287 t->bufferProvider = NULL; 288 t->buffer.raw = NULL; 289 // no initialization needed 290 // t->buffer.frameCount 291 t->hook = NULL; 292 t->in = NULL; 293 t->resampler = NULL; 294 t->sampleRate = mSampleRate; 295 // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name) 296 t->mainBuffer = NULL; 297 t->auxBuffer = NULL; 298 t->mInputBufferProvider = NULL; 299 t->mReformatBufferProvider = NULL; 300 t->downmixerBufferProvider = NULL; 301 t->mMixerFormat = AUDIO_FORMAT_PCM_16_BIT; 302 t->mFormat = format; 303 t->mMixerInFormat = AUDIO_FORMAT_PCM_16_BIT; 304 if (t->mFormat != t->mMixerInFormat) { 305 prepareTrackForReformat(t, n); 306 } 307 status_t status = initTrackDownmix(&mState.tracks[n], n, channelMask); 308 if (status != OK) { 309 ALOGE("AudioMixer::getTrackName invalid channelMask (%#x)", channelMask); 310 return -1; 311 } 312 mTrackNames |= 1 << n; 313 return TRACK0 + n; 314 } 315 ALOGE("AudioMixer::getTrackName out of available tracks"); 316 return -1; 317} 318 319void AudioMixer::invalidateState(uint32_t mask) 320{ 321 if (mask != 0) { 322 mState.needsChanged |= mask; 323 mState.hook = process__validate; 324 } 325 } 326 327status_t AudioMixer::initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask) 328{ 329 uint32_t channelCount = audio_channel_count_from_out_mask(mask); 330 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount); 331 status_t status = OK; 332 if (channelCount > MAX_NUM_CHANNELS) { 333 pTrack->channelMask = mask; 334 pTrack->channelCount = channelCount; 335 ALOGV("initTrackDownmix(track=%d, mask=0x%x) calls prepareTrackForDownmix()", 336 trackNum, mask); 337 status = prepareTrackForDownmix(pTrack, trackNum); 338 } else { 339 unprepareTrackForDownmix(pTrack, trackNum); 340 } 341 return status; 342} 343 344void AudioMixer::unprepareTrackForDownmix(track_t* pTrack, int trackName __unused) { 345 ALOGV("AudioMixer::unprepareTrackForDownmix(%d)", trackName); 346 347 if (pTrack->downmixerBufferProvider != NULL) { 348 // this track had previously been configured with a downmixer, delete it 349 ALOGV(" deleting old downmixer"); 350 delete pTrack->downmixerBufferProvider; 351 pTrack->downmixerBufferProvider = NULL; 352 reconfigureBufferProviders(pTrack); 353 } else { 354 ALOGV(" nothing to do, no downmixer to delete"); 355 } 356} 357 358status_t AudioMixer::prepareTrackForDownmix(track_t* pTrack, int trackName) 359{ 360 ALOGV("AudioMixer::prepareTrackForDownmix(%d) with mask 0x%x", trackName, pTrack->channelMask); 361 362 // discard the previous downmixer if there was one 363 unprepareTrackForDownmix(pTrack, trackName); 364 365 DownmixerBufferProvider* pDbp = new DownmixerBufferProvider(); 366 int32_t status; 367 368 if (!sIsMultichannelCapable) { 369 ALOGE("prepareTrackForDownmix(%d) fails: mixer doesn't support multichannel content", 370 trackName); 371 goto noDownmixForActiveTrack; 372 } 373 374 if (EffectCreate(&sDwnmFxDesc.uuid, 375 pTrack->sessionId /*sessionId*/, -2 /*ioId not relevant here, using random value*/, 376 &pDbp->mDownmixHandle/*pHandle*/) != 0) { 377 ALOGE("prepareTrackForDownmix(%d) fails: error creating downmixer effect", trackName); 378 goto noDownmixForActiveTrack; 379 } 380 381 // channel input configuration will be overridden per-track 382 pDbp->mDownmixConfig.inputCfg.channels = pTrack->channelMask; 383 pDbp->mDownmixConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO; 384 pDbp->mDownmixConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 385 pDbp->mDownmixConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; 386 pDbp->mDownmixConfig.inputCfg.samplingRate = pTrack->sampleRate; 387 pDbp->mDownmixConfig.outputCfg.samplingRate = pTrack->sampleRate; 388 pDbp->mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ; 389 pDbp->mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE; 390 // input and output buffer provider, and frame count will not be used as the downmix effect 391 // process() function is called directly (see DownmixerBufferProvider::getNextBuffer()) 392 pDbp->mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | 393 EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE; 394 pDbp->mDownmixConfig.outputCfg.mask = pDbp->mDownmixConfig.inputCfg.mask; 395 396 {// scope for local variables that are not used in goto label "noDownmixForActiveTrack" 397 int cmdStatus; 398 uint32_t replySize = sizeof(int); 399 400 // Configure and enable downmixer 401 status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle, 402 EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/, 403 &pDbp->mDownmixConfig /*pCmdData*/, 404 &replySize /*replySize*/, &cmdStatus /*pReplyData*/); 405 if ((status != 0) || (cmdStatus != 0)) { 406 ALOGE("error %d while configuring downmixer for track %d", status, trackName); 407 goto noDownmixForActiveTrack; 408 } 409 replySize = sizeof(int); 410 status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle, 411 EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/, 412 &replySize /*replySize*/, &cmdStatus /*pReplyData*/); 413 if ((status != 0) || (cmdStatus != 0)) { 414 ALOGE("error %d while enabling downmixer for track %d", status, trackName); 415 goto noDownmixForActiveTrack; 416 } 417 418 // Set downmix type 419 // parameter size rounded for padding on 32bit boundary 420 const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int); 421 const int downmixParamSize = 422 sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t); 423 effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize); 424 param->psize = sizeof(downmix_params_t); 425 const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE; 426 memcpy(param->data, &downmixParam, param->psize); 427 const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD; 428 param->vsize = sizeof(downmix_type_t); 429 memcpy(param->data + psizePadded, &downmixType, param->vsize); 430 431 status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle, 432 EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize/* cmdSize */, 433 param /*pCmndData*/, &replySize /*replySize*/, &cmdStatus /*pReplyData*/); 434 435 free(param); 436 437 if ((status != 0) || (cmdStatus != 0)) { 438 ALOGE("error %d while setting downmix type for track %d", status, trackName); 439 goto noDownmixForActiveTrack; 440 } else { 441 ALOGV("downmix type set to %d for track %d", (int) downmixType, trackName); 442 } 443 }// end of scope for local variables that are not used in goto label "noDownmixForActiveTrack" 444 445 // initialization successful: 446 pTrack->downmixerBufferProvider = pDbp; 447 reconfigureBufferProviders(pTrack); 448 return NO_ERROR; 449 450noDownmixForActiveTrack: 451 delete pDbp; 452 pTrack->downmixerBufferProvider = NULL; 453 reconfigureBufferProviders(pTrack); 454 return NO_INIT; 455} 456 457void AudioMixer::unprepareTrackForReformat(track_t* pTrack, int trackName __unused) { 458 ALOGV("AudioMixer::unprepareTrackForReformat(%d)", trackName); 459 if (pTrack->mReformatBufferProvider != NULL) { 460 delete pTrack->mReformatBufferProvider; 461 pTrack->mReformatBufferProvider = NULL; 462 reconfigureBufferProviders(pTrack); 463 } 464} 465 466status_t AudioMixer::prepareTrackForReformat(track_t* pTrack, int trackName) 467{ 468 ALOGV("AudioMixer::prepareTrackForReformat(%d) with format %#x", trackName, pTrack->mFormat); 469 // discard the previous reformatter if there was one 470 unprepareTrackForReformat(pTrack, trackName); 471 pTrack->mReformatBufferProvider = new ReformatBufferProvider( 472 audio_channel_count_from_out_mask(pTrack->channelMask), 473 pTrack->mFormat, pTrack->mMixerInFormat); 474 reconfigureBufferProviders(pTrack); 475 return NO_ERROR; 476} 477 478void AudioMixer::reconfigureBufferProviders(track_t* pTrack) 479{ 480 pTrack->bufferProvider = pTrack->mInputBufferProvider; 481 if (pTrack->mReformatBufferProvider) { 482 pTrack->mReformatBufferProvider->mTrackBufferProvider = pTrack->bufferProvider; 483 pTrack->bufferProvider = pTrack->mReformatBufferProvider; 484 } 485 if (pTrack->downmixerBufferProvider) { 486 pTrack->downmixerBufferProvider->mTrackBufferProvider = pTrack->bufferProvider; 487 pTrack->bufferProvider = pTrack->downmixerBufferProvider; 488 } 489} 490 491void AudioMixer::deleteTrackName(int name) 492{ 493 ALOGV("AudioMixer::deleteTrackName(%d)", name); 494 name -= TRACK0; 495 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 496 ALOGV("deleteTrackName(%d)", name); 497 track_t& track(mState.tracks[ name ]); 498 if (track.enabled) { 499 track.enabled = false; 500 invalidateState(1<<name); 501 } 502 // delete the resampler 503 delete track.resampler; 504 track.resampler = NULL; 505 // delete the downmixer 506 unprepareTrackForDownmix(&mState.tracks[name], name); 507 // delete the reformatter 508 unprepareTrackForReformat(&mState.tracks[name], name); 509 510 mTrackNames &= ~(1<<name); 511} 512 513void AudioMixer::enable(int name) 514{ 515 name -= TRACK0; 516 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 517 track_t& track = mState.tracks[name]; 518 519 if (!track.enabled) { 520 track.enabled = true; 521 ALOGV("enable(%d)", name); 522 invalidateState(1 << name); 523 } 524} 525 526void AudioMixer::disable(int name) 527{ 528 name -= TRACK0; 529 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 530 track_t& track = mState.tracks[name]; 531 532 if (track.enabled) { 533 track.enabled = false; 534 ALOGV("disable(%d)", name); 535 invalidateState(1 << name); 536 } 537} 538 539/* Sets the volume ramp variables for the AudioMixer. 540 * 541 * The volume ramp variables are used to transition between the previous 542 * volume to the target volume. The duration of the transition is 543 * set by ramp, which is either 0 for immediate, or typically one state 544 * framecount period. 545 * 546 * @param newFloatValue new volume target in float [0.0, 1.0]. 547 * @param ramp number of frames to increment over. ramp is 0 if the volume 548 * should be set immediately. 549 * @param volume reference to the U4.12 target volume, set on return. 550 * @param prevVolume reference to the U4.27 previous volume, set on return. 551 * @param volumeInc reference to the increment per output audio frame, set on return. 552 * @return true if the volume has changed, false if volume is same. 553 */ 554static inline bool setVolumeRampVariables(float newFloatValue, int32_t ramp, 555 int16_t &volume, int32_t &prevVolume, int32_t &volumeInc) { 556 int32_t newValue = newFloatValue * AudioMixer::UNITY_GAIN_INT; 557 if (newValue > AudioMixer::UNITY_GAIN_INT) { 558 newValue = AudioMixer::UNITY_GAIN_INT; 559 } else if (newValue < 0) { 560 ALOGE("negative volume %.7g", newFloatValue); 561 newValue = 0; // should never happen, but for safety check. 562 } 563 if (newValue == volume) { 564 return false; 565 } 566 if (ramp != 0) { 567 volumeInc = ((newValue - volume) << 16) / ramp; 568 prevVolume = (volumeInc == 0 ? newValue : volume) << 16; 569 } else { 570 volumeInc = 0; 571 prevVolume = newValue << 16; 572 } 573 volume = newValue; 574 return true; 575} 576 577void AudioMixer::setParameter(int name, int target, int param, void *value) 578{ 579 name -= TRACK0; 580 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 581 track_t& track = mState.tracks[name]; 582 583 int valueInt = static_cast<int>(reinterpret_cast<uintptr_t>(value)); 584 int32_t *valueBuf = reinterpret_cast<int32_t*>(value); 585 586 switch (target) { 587 588 case TRACK: 589 switch (param) { 590 case CHANNEL_MASK: { 591 audio_channel_mask_t mask = 592 static_cast<audio_channel_mask_t>(reinterpret_cast<uintptr_t>(value)); 593 if (track.channelMask != mask) { 594 uint32_t channelCount = audio_channel_count_from_out_mask(mask); 595 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount); 596 track.channelMask = mask; 597 track.channelCount = channelCount; 598 // the mask has changed, does this track need a downmixer? 599 initTrackDownmix(&mState.tracks[name], name, mask); 600 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask); 601 invalidateState(1 << name); 602 } 603 } break; 604 case MAIN_BUFFER: 605 if (track.mainBuffer != valueBuf) { 606 track.mainBuffer = valueBuf; 607 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); 608 invalidateState(1 << name); 609 } 610 break; 611 case AUX_BUFFER: 612 if (track.auxBuffer != valueBuf) { 613 track.auxBuffer = valueBuf; 614 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf); 615 invalidateState(1 << name); 616 } 617 break; 618 case FORMAT: { 619 audio_format_t format = static_cast<audio_format_t>(valueInt); 620 if (track.mFormat != format) { 621 ALOG_ASSERT(audio_is_linear_pcm(format), "Invalid format %#x", format); 622 track.mFormat = format; 623 ALOGV("setParameter(TRACK, FORMAT, %#x)", format); 624 //if (track.mFormat != track.mMixerInFormat) 625 { 626 ALOGD("Reformatting!"); 627 prepareTrackForReformat(&track, name); 628 } 629 invalidateState(1 << name); 630 } 631 } break; 632 // FIXME do we want to support setting the downmix type from AudioFlinger? 633 // for a specific track? or per mixer? 634 /* case DOWNMIX_TYPE: 635 break */ 636 case MIXER_FORMAT: { 637 audio_format_t format = static_cast<audio_format_t>(valueInt); 638 if (track.mMixerFormat != format) { 639 track.mMixerFormat = format; 640 ALOGV("setParameter(TRACK, MIXER_FORMAT, %#x)", format); 641 } 642 } break; 643 default: 644 LOG_ALWAYS_FATAL("setParameter track: bad param %d", param); 645 } 646 break; 647 648 case RESAMPLE: 649 switch (param) { 650 case SAMPLE_RATE: 651 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt); 652 if (track.setResampler(uint32_t(valueInt), mSampleRate)) { 653 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)", 654 uint32_t(valueInt)); 655 invalidateState(1 << name); 656 } 657 break; 658 case RESET: 659 track.resetResampler(); 660 invalidateState(1 << name); 661 break; 662 case REMOVE: 663 delete track.resampler; 664 track.resampler = NULL; 665 track.sampleRate = mSampleRate; 666 invalidateState(1 << name); 667 break; 668 default: 669 LOG_ALWAYS_FATAL("setParameter resample: bad param %d", param); 670 } 671 break; 672 673 case RAMP_VOLUME: 674 case VOLUME: 675 switch (param) { 676 case VOLUME0: 677 case VOLUME1: 678 if (setVolumeRampVariables(*reinterpret_cast<float*>(value), 679 target == RAMP_VOLUME ? mState.frameCount : 0, 680 track.volume[param - VOLUME0], track.prevVolume[param - VOLUME0], 681 track.volumeInc[param - VOLUME0])) { 682 ALOGV("setParameter(%s, VOLUME%d: %04x)", 683 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", param - VOLUME0, 684 track.volume[param - VOLUME0]); 685 invalidateState(1 << name); 686 } 687 break; 688 case AUXLEVEL: 689 //ALOG_ASSERT(0 <= valueInt && valueInt <= MAX_GAIN_INT, "bad aux level %d", valueInt); 690 if (setVolumeRampVariables(*reinterpret_cast<float*>(value), 691 target == RAMP_VOLUME ? mState.frameCount : 0, 692 track.auxLevel, track.prevAuxLevel, track.auxInc)) { 693 ALOGV("setParameter(%s, AUXLEVEL: %04x)", 694 target == VOLUME ? "VOLUME" : "RAMP_VOLUME", track.auxLevel); 695 invalidateState(1 << name); 696 } 697 break; 698 default: 699 LOG_ALWAYS_FATAL("setParameter volume: bad param %d", param); 700 } 701 break; 702 703 default: 704 LOG_ALWAYS_FATAL("setParameter: bad target %d", target); 705 } 706} 707 708bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate) 709{ 710 if (value != devSampleRate || resampler != NULL) { 711 if (sampleRate != value) { 712 sampleRate = value; 713 if (resampler == NULL) { 714 ALOGV("creating resampler from track %d Hz to device %d Hz", value, devSampleRate); 715 AudioResampler::src_quality quality; 716 // force lowest quality level resampler if use case isn't music or video 717 // FIXME this is flawed for dynamic sample rates, as we choose the resampler 718 // quality level based on the initial ratio, but that could change later. 719 // Should have a way to distinguish tracks with static ratios vs. dynamic ratios. 720 if (!((value == 44100 && devSampleRate == 48000) || 721 (value == 48000 && devSampleRate == 44100))) { 722 quality = AudioResampler::DYN_LOW_QUALITY; 723 } else { 724 quality = AudioResampler::DEFAULT_QUALITY; 725 } 726 const int bits = mMixerInFormat == AUDIO_FORMAT_PCM_16_BIT ? 16 : /* FLOAT */ 32; 727 resampler = AudioResampler::create( 728 bits, 729 // the resampler sees the number of channels after the downmixer, if any 730 (int) (downmixerBufferProvider != NULL ? MAX_NUM_CHANNELS : channelCount), 731 devSampleRate, quality); 732 resampler->setLocalTimeFreq(sLocalTimeFreq); 733 } 734 return true; 735 } 736 } 737 return false; 738} 739 740inline 741void AudioMixer::track_t::adjustVolumeRamp(bool aux) 742{ 743 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) { 744 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) || 745 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) { 746 volumeInc[i] = 0; 747 prevVolume[i] = volume[i]<<16; 748 } 749 } 750 if (aux) { 751 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) || 752 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) { 753 auxInc = 0; 754 prevAuxLevel = auxLevel<<16; 755 } 756 } 757} 758 759size_t AudioMixer::getUnreleasedFrames(int name) const 760{ 761 name -= TRACK0; 762 if (uint32_t(name) < MAX_NUM_TRACKS) { 763 return mState.tracks[name].getUnreleasedFrames(); 764 } 765 return 0; 766} 767 768void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) 769{ 770 name -= TRACK0; 771 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 772 773 if (mState.tracks[name].mInputBufferProvider == bufferProvider) { 774 return; // don't reset any buffer providers if identical. 775 } 776 if (mState.tracks[name].mReformatBufferProvider != NULL) { 777 mState.tracks[name].mReformatBufferProvider->reset(); 778 } else if (mState.tracks[name].downmixerBufferProvider != NULL) { 779 } 780 781 mState.tracks[name].mInputBufferProvider = bufferProvider; 782 reconfigureBufferProviders(&mState.tracks[name]); 783} 784 785 786void AudioMixer::process(int64_t pts) 787{ 788 mState.hook(&mState, pts); 789} 790 791 792void AudioMixer::process__validate(state_t* state, int64_t pts) 793{ 794 ALOGW_IF(!state->needsChanged, 795 "in process__validate() but nothing's invalid"); 796 797 uint32_t changed = state->needsChanged; 798 state->needsChanged = 0; // clear the validation flag 799 800 // recompute which tracks are enabled / disabled 801 uint32_t enabled = 0; 802 uint32_t disabled = 0; 803 while (changed) { 804 const int i = 31 - __builtin_clz(changed); 805 const uint32_t mask = 1<<i; 806 changed &= ~mask; 807 track_t& t = state->tracks[i]; 808 (t.enabled ? enabled : disabled) |= mask; 809 } 810 state->enabledTracks &= ~disabled; 811 state->enabledTracks |= enabled; 812 813 // compute everything we need... 814 int countActiveTracks = 0; 815 bool all16BitsStereoNoResample = true; 816 bool resampling = false; 817 bool volumeRamp = false; 818 uint32_t en = state->enabledTracks; 819 while (en) { 820 const int i = 31 - __builtin_clz(en); 821 en &= ~(1<<i); 822 823 countActiveTracks++; 824 track_t& t = state->tracks[i]; 825 uint32_t n = 0; 826 // FIXME can overflow (mask is only 3 bits) 827 n |= NEEDS_CHANNEL_1 + t.channelCount - 1; 828 if (t.doesResample()) { 829 n |= NEEDS_RESAMPLE; 830 } 831 if (t.auxLevel != 0 && t.auxBuffer != NULL) { 832 n |= NEEDS_AUX; 833 } 834 835 if (t.volumeInc[0]|t.volumeInc[1]) { 836 volumeRamp = true; 837 } else if (!t.doesResample() && t.volumeRL == 0) { 838 n |= NEEDS_MUTE; 839 } 840 t.needs = n; 841 842 if (n & NEEDS_MUTE) { 843 t.hook = track__nop; 844 } else { 845 if (n & NEEDS_AUX) { 846 all16BitsStereoNoResample = false; 847 } 848 if (n & NEEDS_RESAMPLE) { 849 all16BitsStereoNoResample = false; 850 resampling = true; 851 t.hook = track__genericResample; 852 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 853 "Track %d needs downmix + resample", i); 854 } else { 855 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){ 856 t.hook = track__16BitsMono; 857 all16BitsStereoNoResample = false; 858 } 859 if ((n & NEEDS_CHANNEL_COUNT__MASK) >= NEEDS_CHANNEL_2){ 860 t.hook = track__16BitsStereo; 861 ALOGV_IF((n & NEEDS_CHANNEL_COUNT__MASK) > NEEDS_CHANNEL_2, 862 "Track %d needs downmix", i); 863 } 864 } 865 } 866 } 867 868 // select the processing hooks 869 state->hook = process__nop; 870 if (countActiveTracks > 0) { 871 if (resampling) { 872 if (!state->outputTemp) { 873 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 874 } 875 if (!state->resampleTemp) { 876 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 877 } 878 state->hook = process__genericResampling; 879 } else { 880 if (state->outputTemp) { 881 delete [] state->outputTemp; 882 state->outputTemp = NULL; 883 } 884 if (state->resampleTemp) { 885 delete [] state->resampleTemp; 886 state->resampleTemp = NULL; 887 } 888 state->hook = process__genericNoResampling; 889 if (all16BitsStereoNoResample && !volumeRamp) { 890 if (countActiveTracks == 1) { 891 state->hook = process__OneTrack16BitsStereoNoResampling; 892 } 893 } 894 } 895 } 896 897 ALOGV("mixer configuration change: %d activeTracks (%08x) " 898 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d", 899 countActiveTracks, state->enabledTracks, 900 all16BitsStereoNoResample, resampling, volumeRamp); 901 902 state->hook(state, pts); 903 904 // Now that the volume ramp has been done, set optimal state and 905 // track hooks for subsequent mixer process 906 if (countActiveTracks > 0) { 907 bool allMuted = true; 908 uint32_t en = state->enabledTracks; 909 while (en) { 910 const int i = 31 - __builtin_clz(en); 911 en &= ~(1<<i); 912 track_t& t = state->tracks[i]; 913 if (!t.doesResample() && t.volumeRL == 0) { 914 t.needs |= NEEDS_MUTE; 915 t.hook = track__nop; 916 } else { 917 allMuted = false; 918 } 919 } 920 if (allMuted) { 921 state->hook = process__nop; 922 } else if (all16BitsStereoNoResample) { 923 if (countActiveTracks == 1) { 924 state->hook = process__OneTrack16BitsStereoNoResampling; 925 } 926 } 927 } 928} 929 930 931void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, 932 int32_t* temp, int32_t* aux) 933{ 934 t->resampler->setSampleRate(t->sampleRate); 935 936 // ramp gain - resample to temp buffer and scale/mix in 2nd step 937 if (aux != NULL) { 938 // always resample with unity gain when sending to auxiliary buffer to be able 939 // to apply send level after resampling 940 // TODO: modify each resampler to support aux channel? 941 t->resampler->setVolume(UNITY_GAIN_INT, UNITY_GAIN_INT); 942 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 943 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 944 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 945 volumeRampStereo(t, out, outFrameCount, temp, aux); 946 } else { 947 volumeStereo(t, out, outFrameCount, temp, aux); 948 } 949 } else { 950 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 951 t->resampler->setVolume(UNITY_GAIN_INT, UNITY_GAIN_INT); 952 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 953 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 954 volumeRampStereo(t, out, outFrameCount, temp, aux); 955 } 956 957 // constant gain 958 else { 959 t->resampler->setVolume(t->volume[0], t->volume[1]); 960 t->resampler->resample(out, outFrameCount, t->bufferProvider); 961 } 962 } 963} 964 965void AudioMixer::track__nop(track_t* t __unused, int32_t* out __unused, 966 size_t outFrameCount __unused, int32_t* temp __unused, int32_t* aux __unused) 967{ 968} 969 970void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 971 int32_t* aux) 972{ 973 int32_t vl = t->prevVolume[0]; 974 int32_t vr = t->prevVolume[1]; 975 const int32_t vlInc = t->volumeInc[0]; 976 const int32_t vrInc = t->volumeInc[1]; 977 978 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 979 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 980 // (vl + vlInc*frameCount)/65536.0f, frameCount); 981 982 // ramp volume 983 if (CC_UNLIKELY(aux != NULL)) { 984 int32_t va = t->prevAuxLevel; 985 const int32_t vaInc = t->auxInc; 986 int32_t l; 987 int32_t r; 988 989 do { 990 l = (*temp++ >> 12); 991 r = (*temp++ >> 12); 992 *out++ += (vl >> 16) * l; 993 *out++ += (vr >> 16) * r; 994 *aux++ += (va >> 17) * (l + r); 995 vl += vlInc; 996 vr += vrInc; 997 va += vaInc; 998 } while (--frameCount); 999 t->prevAuxLevel = va; 1000 } else { 1001 do { 1002 *out++ += (vl >> 16) * (*temp++ >> 12); 1003 *out++ += (vr >> 16) * (*temp++ >> 12); 1004 vl += vlInc; 1005 vr += vrInc; 1006 } while (--frameCount); 1007 } 1008 t->prevVolume[0] = vl; 1009 t->prevVolume[1] = vr; 1010 t->adjustVolumeRamp(aux != NULL); 1011} 1012 1013void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, 1014 int32_t* aux) 1015{ 1016 const int16_t vl = t->volume[0]; 1017 const int16_t vr = t->volume[1]; 1018 1019 if (CC_UNLIKELY(aux != NULL)) { 1020 const int16_t va = t->auxLevel; 1021 do { 1022 int16_t l = (int16_t)(*temp++ >> 12); 1023 int16_t r = (int16_t)(*temp++ >> 12); 1024 out[0] = mulAdd(l, vl, out[0]); 1025 int16_t a = (int16_t)(((int32_t)l + r) >> 1); 1026 out[1] = mulAdd(r, vr, out[1]); 1027 out += 2; 1028 aux[0] = mulAdd(a, va, aux[0]); 1029 aux++; 1030 } while (--frameCount); 1031 } else { 1032 do { 1033 int16_t l = (int16_t)(*temp++ >> 12); 1034 int16_t r = (int16_t)(*temp++ >> 12); 1035 out[0] = mulAdd(l, vl, out[0]); 1036 out[1] = mulAdd(r, vr, out[1]); 1037 out += 2; 1038 } while (--frameCount); 1039 } 1040} 1041 1042void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, 1043 int32_t* temp __unused, int32_t* aux) 1044{ 1045 const int16_t *in = static_cast<const int16_t *>(t->in); 1046 1047 if (CC_UNLIKELY(aux != NULL)) { 1048 int32_t l; 1049 int32_t r; 1050 // ramp gain 1051 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1052 int32_t vl = t->prevVolume[0]; 1053 int32_t vr = t->prevVolume[1]; 1054 int32_t va = t->prevAuxLevel; 1055 const int32_t vlInc = t->volumeInc[0]; 1056 const int32_t vrInc = t->volumeInc[1]; 1057 const int32_t vaInc = t->auxInc; 1058 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1059 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1060 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1061 1062 do { 1063 l = (int32_t)*in++; 1064 r = (int32_t)*in++; 1065 *out++ += (vl >> 16) * l; 1066 *out++ += (vr >> 16) * r; 1067 *aux++ += (va >> 17) * (l + r); 1068 vl += vlInc; 1069 vr += vrInc; 1070 va += vaInc; 1071 } while (--frameCount); 1072 1073 t->prevVolume[0] = vl; 1074 t->prevVolume[1] = vr; 1075 t->prevAuxLevel = va; 1076 t->adjustVolumeRamp(true); 1077 } 1078 1079 // constant gain 1080 else { 1081 const uint32_t vrl = t->volumeRL; 1082 const int16_t va = (int16_t)t->auxLevel; 1083 do { 1084 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1085 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1); 1086 in += 2; 1087 out[0] = mulAddRL(1, rl, vrl, out[0]); 1088 out[1] = mulAddRL(0, rl, vrl, out[1]); 1089 out += 2; 1090 aux[0] = mulAdd(a, va, aux[0]); 1091 aux++; 1092 } while (--frameCount); 1093 } 1094 } else { 1095 // ramp gain 1096 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1097 int32_t vl = t->prevVolume[0]; 1098 int32_t vr = t->prevVolume[1]; 1099 const int32_t vlInc = t->volumeInc[0]; 1100 const int32_t vrInc = t->volumeInc[1]; 1101 1102 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1103 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1104 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1105 1106 do { 1107 *out++ += (vl >> 16) * (int32_t) *in++; 1108 *out++ += (vr >> 16) * (int32_t) *in++; 1109 vl += vlInc; 1110 vr += vrInc; 1111 } while (--frameCount); 1112 1113 t->prevVolume[0] = vl; 1114 t->prevVolume[1] = vr; 1115 t->adjustVolumeRamp(false); 1116 } 1117 1118 // constant gain 1119 else { 1120 const uint32_t vrl = t->volumeRL; 1121 do { 1122 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1123 in += 2; 1124 out[0] = mulAddRL(1, rl, vrl, out[0]); 1125 out[1] = mulAddRL(0, rl, vrl, out[1]); 1126 out += 2; 1127 } while (--frameCount); 1128 } 1129 } 1130 t->in = in; 1131} 1132 1133void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, 1134 int32_t* temp __unused, int32_t* aux) 1135{ 1136 const int16_t *in = static_cast<int16_t const *>(t->in); 1137 1138 if (CC_UNLIKELY(aux != NULL)) { 1139 // ramp gain 1140 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 1141 int32_t vl = t->prevVolume[0]; 1142 int32_t vr = t->prevVolume[1]; 1143 int32_t va = t->prevAuxLevel; 1144 const int32_t vlInc = t->volumeInc[0]; 1145 const int32_t vrInc = t->volumeInc[1]; 1146 const int32_t vaInc = t->auxInc; 1147 1148 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1149 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1150 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1151 1152 do { 1153 int32_t l = *in++; 1154 *out++ += (vl >> 16) * l; 1155 *out++ += (vr >> 16) * l; 1156 *aux++ += (va >> 16) * l; 1157 vl += vlInc; 1158 vr += vrInc; 1159 va += vaInc; 1160 } while (--frameCount); 1161 1162 t->prevVolume[0] = vl; 1163 t->prevVolume[1] = vr; 1164 t->prevAuxLevel = va; 1165 t->adjustVolumeRamp(true); 1166 } 1167 // constant gain 1168 else { 1169 const int16_t vl = t->volume[0]; 1170 const int16_t vr = t->volume[1]; 1171 const int16_t va = (int16_t)t->auxLevel; 1172 do { 1173 int16_t l = *in++; 1174 out[0] = mulAdd(l, vl, out[0]); 1175 out[1] = mulAdd(l, vr, out[1]); 1176 out += 2; 1177 aux[0] = mulAdd(l, va, aux[0]); 1178 aux++; 1179 } while (--frameCount); 1180 } 1181 } else { 1182 // ramp gain 1183 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 1184 int32_t vl = t->prevVolume[0]; 1185 int32_t vr = t->prevVolume[1]; 1186 const int32_t vlInc = t->volumeInc[0]; 1187 const int32_t vrInc = t->volumeInc[1]; 1188 1189 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 1190 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 1191 // (vl + vlInc*frameCount)/65536.0f, frameCount); 1192 1193 do { 1194 int32_t l = *in++; 1195 *out++ += (vl >> 16) * l; 1196 *out++ += (vr >> 16) * l; 1197 vl += vlInc; 1198 vr += vrInc; 1199 } while (--frameCount); 1200 1201 t->prevVolume[0] = vl; 1202 t->prevVolume[1] = vr; 1203 t->adjustVolumeRamp(false); 1204 } 1205 // constant gain 1206 else { 1207 const int16_t vl = t->volume[0]; 1208 const int16_t vr = t->volume[1]; 1209 do { 1210 int16_t l = *in++; 1211 out[0] = mulAdd(l, vl, out[0]); 1212 out[1] = mulAdd(l, vr, out[1]); 1213 out += 2; 1214 } while (--frameCount); 1215 } 1216 } 1217 t->in = in; 1218} 1219 1220// no-op case 1221void AudioMixer::process__nop(state_t* state, int64_t pts) 1222{ 1223 uint32_t e0 = state->enabledTracks; 1224 size_t sampleCount = state->frameCount * MAX_NUM_CHANNELS; 1225 while (e0) { 1226 // process by group of tracks with same output buffer to 1227 // avoid multiple memset() on same buffer 1228 uint32_t e1 = e0, e2 = e0; 1229 int i = 31 - __builtin_clz(e1); 1230 { 1231 track_t& t1 = state->tracks[i]; 1232 e2 &= ~(1<<i); 1233 while (e2) { 1234 i = 31 - __builtin_clz(e2); 1235 e2 &= ~(1<<i); 1236 track_t& t2 = state->tracks[i]; 1237 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1238 e1 &= ~(1<<i); 1239 } 1240 } 1241 e0 &= ~(e1); 1242 1243 memset(t1.mainBuffer, 0, sampleCount 1244 * audio_bytes_per_sample(t1.mMixerFormat)); 1245 } 1246 1247 while (e1) { 1248 i = 31 - __builtin_clz(e1); 1249 e1 &= ~(1<<i); 1250 { 1251 track_t& t3 = state->tracks[i]; 1252 size_t outFrames = state->frameCount; 1253 while (outFrames) { 1254 t3.buffer.frameCount = outFrames; 1255 int64_t outputPTS = calculateOutputPTS( 1256 t3, pts, state->frameCount - outFrames); 1257 t3.bufferProvider->getNextBuffer(&t3.buffer, outputPTS); 1258 if (t3.buffer.raw == NULL) break; 1259 outFrames -= t3.buffer.frameCount; 1260 t3.bufferProvider->releaseBuffer(&t3.buffer); 1261 } 1262 } 1263 } 1264 } 1265} 1266 1267// generic code without resampling 1268void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts) 1269{ 1270 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32))); 1271 1272 // acquire each track's buffer 1273 uint32_t enabledTracks = state->enabledTracks; 1274 uint32_t e0 = enabledTracks; 1275 while (e0) { 1276 const int i = 31 - __builtin_clz(e0); 1277 e0 &= ~(1<<i); 1278 track_t& t = state->tracks[i]; 1279 t.buffer.frameCount = state->frameCount; 1280 t.bufferProvider->getNextBuffer(&t.buffer, pts); 1281 t.frameCount = t.buffer.frameCount; 1282 t.in = t.buffer.raw; 1283 } 1284 1285 e0 = enabledTracks; 1286 while (e0) { 1287 // process by group of tracks with same output buffer to 1288 // optimize cache use 1289 uint32_t e1 = e0, e2 = e0; 1290 int j = 31 - __builtin_clz(e1); 1291 track_t& t1 = state->tracks[j]; 1292 e2 &= ~(1<<j); 1293 while (e2) { 1294 j = 31 - __builtin_clz(e2); 1295 e2 &= ~(1<<j); 1296 track_t& t2 = state->tracks[j]; 1297 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1298 e1 &= ~(1<<j); 1299 } 1300 } 1301 e0 &= ~(e1); 1302 // this assumes output 16 bits stereo, no resampling 1303 int32_t *out = t1.mainBuffer; 1304 size_t numFrames = 0; 1305 do { 1306 memset(outTemp, 0, sizeof(outTemp)); 1307 e2 = e1; 1308 while (e2) { 1309 const int i = 31 - __builtin_clz(e2); 1310 e2 &= ~(1<<i); 1311 track_t& t = state->tracks[i]; 1312 size_t outFrames = BLOCKSIZE; 1313 int32_t *aux = NULL; 1314 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1315 aux = t.auxBuffer + numFrames; 1316 } 1317 while (outFrames) { 1318 // t.in == NULL can happen if the track was flushed just after having 1319 // been enabled for mixing. 1320 if (t.in == NULL) { 1321 enabledTracks &= ~(1<<i); 1322 e1 &= ~(1<<i); 1323 break; 1324 } 1325 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount; 1326 if (inFrames > 0) { 1327 t.hook(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, 1328 state->resampleTemp, aux); 1329 t.frameCount -= inFrames; 1330 outFrames -= inFrames; 1331 if (CC_UNLIKELY(aux != NULL)) { 1332 aux += inFrames; 1333 } 1334 } 1335 if (t.frameCount == 0 && outFrames) { 1336 t.bufferProvider->releaseBuffer(&t.buffer); 1337 t.buffer.frameCount = (state->frameCount - numFrames) - 1338 (BLOCKSIZE - outFrames); 1339 int64_t outputPTS = calculateOutputPTS( 1340 t, pts, numFrames + (BLOCKSIZE - outFrames)); 1341 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1342 t.in = t.buffer.raw; 1343 if (t.in == NULL) { 1344 enabledTracks &= ~(1<<i); 1345 e1 &= ~(1<<i); 1346 break; 1347 } 1348 t.frameCount = t.buffer.frameCount; 1349 } 1350 } 1351 } 1352 switch (t1.mMixerFormat) { 1353 case AUDIO_FORMAT_PCM_FLOAT: 1354 memcpy_to_float_from_q4_27(reinterpret_cast<float *>(out), outTemp, BLOCKSIZE * 2); 1355 out += BLOCKSIZE * 2; // output is 2 floats/frame. 1356 break; 1357 case AUDIO_FORMAT_PCM_16_BIT: 1358 ditherAndClamp(out, outTemp, BLOCKSIZE); 1359 out += BLOCKSIZE; // output is 1 int32_t (2 int16_t samples)/frame 1360 break; 1361 default: 1362 LOG_ALWAYS_FATAL("bad mixer format: %d", t1.mMixerFormat); 1363 } 1364 numFrames += BLOCKSIZE; 1365 } while (numFrames < state->frameCount); 1366 } 1367 1368 // release each track's buffer 1369 e0 = enabledTracks; 1370 while (e0) { 1371 const int i = 31 - __builtin_clz(e0); 1372 e0 &= ~(1<<i); 1373 track_t& t = state->tracks[i]; 1374 t.bufferProvider->releaseBuffer(&t.buffer); 1375 } 1376} 1377 1378 1379// generic code with resampling 1380void AudioMixer::process__genericResampling(state_t* state, int64_t pts) 1381{ 1382 // this const just means that local variable outTemp doesn't change 1383 int32_t* const outTemp = state->outputTemp; 1384 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount; 1385 1386 size_t numFrames = state->frameCount; 1387 1388 uint32_t e0 = state->enabledTracks; 1389 while (e0) { 1390 // process by group of tracks with same output buffer 1391 // to optimize cache use 1392 uint32_t e1 = e0, e2 = e0; 1393 int j = 31 - __builtin_clz(e1); 1394 track_t& t1 = state->tracks[j]; 1395 e2 &= ~(1<<j); 1396 while (e2) { 1397 j = 31 - __builtin_clz(e2); 1398 e2 &= ~(1<<j); 1399 track_t& t2 = state->tracks[j]; 1400 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 1401 e1 &= ~(1<<j); 1402 } 1403 } 1404 e0 &= ~(e1); 1405 int32_t *out = t1.mainBuffer; 1406 memset(outTemp, 0, size); 1407 while (e1) { 1408 const int i = 31 - __builtin_clz(e1); 1409 e1 &= ~(1<<i); 1410 track_t& t = state->tracks[i]; 1411 int32_t *aux = NULL; 1412 if (CC_UNLIKELY(t.needs & NEEDS_AUX)) { 1413 aux = t.auxBuffer; 1414 } 1415 1416 // this is a little goofy, on the resampling case we don't 1417 // acquire/release the buffers because it's done by 1418 // the resampler. 1419 if (t.needs & NEEDS_RESAMPLE) { 1420 t.resampler->setPTS(pts); 1421 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux); 1422 } else { 1423 1424 size_t outFrames = 0; 1425 1426 while (outFrames < numFrames) { 1427 t.buffer.frameCount = numFrames - outFrames; 1428 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames); 1429 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 1430 t.in = t.buffer.raw; 1431 // t.in == NULL can happen if the track was flushed just after having 1432 // been enabled for mixing. 1433 if (t.in == NULL) break; 1434 1435 if (CC_UNLIKELY(aux != NULL)) { 1436 aux += outFrames; 1437 } 1438 t.hook(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, 1439 state->resampleTemp, aux); 1440 outFrames += t.buffer.frameCount; 1441 t.bufferProvider->releaseBuffer(&t.buffer); 1442 } 1443 } 1444 } 1445 switch (t1.mMixerFormat) { 1446 case AUDIO_FORMAT_PCM_FLOAT: 1447 memcpy_to_float_from_q4_27(reinterpret_cast<float*>(out), outTemp, numFrames*2); 1448 break; 1449 case AUDIO_FORMAT_PCM_16_BIT: 1450 ditherAndClamp(out, outTemp, numFrames); 1451 break; 1452 default: 1453 LOG_ALWAYS_FATAL("bad mixer format: %d", t1.mMixerFormat); 1454 } 1455 } 1456} 1457 1458// one track, 16 bits stereo without resampling is the most common case 1459void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state, 1460 int64_t pts) 1461{ 1462 // This method is only called when state->enabledTracks has exactly 1463 // one bit set. The asserts below would verify this, but are commented out 1464 // since the whole point of this method is to optimize performance. 1465 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled"); 1466 const int i = 31 - __builtin_clz(state->enabledTracks); 1467 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 1468 const track_t& t = state->tracks[i]; 1469 1470 AudioBufferProvider::Buffer& b(t.buffer); 1471 1472 int32_t* out = t.mainBuffer; 1473 float *fout = reinterpret_cast<float*>(out); 1474 size_t numFrames = state->frameCount; 1475 1476 const int16_t vl = t.volume[0]; 1477 const int16_t vr = t.volume[1]; 1478 const uint32_t vrl = t.volumeRL; 1479 while (numFrames) { 1480 b.frameCount = numFrames; 1481 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer); 1482 t.bufferProvider->getNextBuffer(&b, outputPTS); 1483 const int16_t *in = b.i16; 1484 1485 // in == NULL can happen if the track was flushed just after having 1486 // been enabled for mixing. 1487 if (in == NULL || (((uintptr_t)in) & 3)) { 1488 memset(out, 0, numFrames 1489 * MAX_NUM_CHANNELS * audio_bytes_per_sample(t.mMixerFormat)); 1490 ALOGE_IF((((uintptr_t)in) & 3), "process stereo track: input buffer alignment pb: " 1491 "buffer %p track %d, channels %d, needs %08x", 1492 in, i, t.channelCount, t.needs); 1493 return; 1494 } 1495 size_t outFrames = b.frameCount; 1496 1497 switch (t.mMixerFormat) { 1498 case AUDIO_FORMAT_PCM_FLOAT: 1499 do { 1500 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1501 in += 2; 1502 int32_t l = mulRL(1, rl, vrl); 1503 int32_t r = mulRL(0, rl, vrl); 1504 *fout++ = float_from_q4_27(l); 1505 *fout++ = float_from_q4_27(r); 1506 // Note: In case of later int16_t sink output, 1507 // conversion and clamping is done by memcpy_to_i16_from_float(). 1508 } while (--outFrames); 1509 break; 1510 case AUDIO_FORMAT_PCM_16_BIT: 1511 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN_INT || uint32_t(vr) > UNITY_GAIN_INT)) { 1512 // volume is boosted, so we might need to clamp even though 1513 // we process only one track. 1514 do { 1515 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1516 in += 2; 1517 int32_t l = mulRL(1, rl, vrl) >> 12; 1518 int32_t r = mulRL(0, rl, vrl) >> 12; 1519 // clamping... 1520 l = clamp16(l); 1521 r = clamp16(r); 1522 *out++ = (r<<16) | (l & 0xFFFF); 1523 } while (--outFrames); 1524 } else { 1525 do { 1526 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1527 in += 2; 1528 int32_t l = mulRL(1, rl, vrl) >> 12; 1529 int32_t r = mulRL(0, rl, vrl) >> 12; 1530 *out++ = (r<<16) | (l & 0xFFFF); 1531 } while (--outFrames); 1532 } 1533 break; 1534 default: 1535 LOG_ALWAYS_FATAL("bad mixer format: %d", t.mMixerFormat); 1536 } 1537 numFrames -= b.frameCount; 1538 t.bufferProvider->releaseBuffer(&b); 1539 } 1540} 1541 1542#if 0 1543// 2 tracks is also a common case 1544// NEVER used in current implementation of process__validate() 1545// only use if the 2 tracks have the same output buffer 1546void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state, 1547 int64_t pts) 1548{ 1549 int i; 1550 uint32_t en = state->enabledTracks; 1551 1552 i = 31 - __builtin_clz(en); 1553 const track_t& t0 = state->tracks[i]; 1554 AudioBufferProvider::Buffer& b0(t0.buffer); 1555 1556 en &= ~(1<<i); 1557 i = 31 - __builtin_clz(en); 1558 const track_t& t1 = state->tracks[i]; 1559 AudioBufferProvider::Buffer& b1(t1.buffer); 1560 1561 const int16_t *in0; 1562 const int16_t vl0 = t0.volume[0]; 1563 const int16_t vr0 = t0.volume[1]; 1564 size_t frameCount0 = 0; 1565 1566 const int16_t *in1; 1567 const int16_t vl1 = t1.volume[0]; 1568 const int16_t vr1 = t1.volume[1]; 1569 size_t frameCount1 = 0; 1570 1571 //FIXME: only works if two tracks use same buffer 1572 int32_t* out = t0.mainBuffer; 1573 size_t numFrames = state->frameCount; 1574 const int16_t *buff = NULL; 1575 1576 1577 while (numFrames) { 1578 1579 if (frameCount0 == 0) { 1580 b0.frameCount = numFrames; 1581 int64_t outputPTS = calculateOutputPTS(t0, pts, 1582 out - t0.mainBuffer); 1583 t0.bufferProvider->getNextBuffer(&b0, outputPTS); 1584 if (b0.i16 == NULL) { 1585 if (buff == NULL) { 1586 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount]; 1587 } 1588 in0 = buff; 1589 b0.frameCount = numFrames; 1590 } else { 1591 in0 = b0.i16; 1592 } 1593 frameCount0 = b0.frameCount; 1594 } 1595 if (frameCount1 == 0) { 1596 b1.frameCount = numFrames; 1597 int64_t outputPTS = calculateOutputPTS(t1, pts, 1598 out - t0.mainBuffer); 1599 t1.bufferProvider->getNextBuffer(&b1, outputPTS); 1600 if (b1.i16 == NULL) { 1601 if (buff == NULL) { 1602 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount]; 1603 } 1604 in1 = buff; 1605 b1.frameCount = numFrames; 1606 } else { 1607 in1 = b1.i16; 1608 } 1609 frameCount1 = b1.frameCount; 1610 } 1611 1612 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1; 1613 1614 numFrames -= outFrames; 1615 frameCount0 -= outFrames; 1616 frameCount1 -= outFrames; 1617 1618 do { 1619 int32_t l0 = *in0++; 1620 int32_t r0 = *in0++; 1621 l0 = mul(l0, vl0); 1622 r0 = mul(r0, vr0); 1623 int32_t l = *in1++; 1624 int32_t r = *in1++; 1625 l = mulAdd(l, vl1, l0) >> 12; 1626 r = mulAdd(r, vr1, r0) >> 12; 1627 // clamping... 1628 l = clamp16(l); 1629 r = clamp16(r); 1630 *out++ = (r<<16) | (l & 0xFFFF); 1631 } while (--outFrames); 1632 1633 if (frameCount0 == 0) { 1634 t0.bufferProvider->releaseBuffer(&b0); 1635 } 1636 if (frameCount1 == 0) { 1637 t1.bufferProvider->releaseBuffer(&b1); 1638 } 1639 } 1640 1641 delete [] buff; 1642} 1643#endif 1644 1645int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS, 1646 int outputFrameIndex) 1647{ 1648 if (AudioBufferProvider::kInvalidPTS == basePTS) { 1649 return AudioBufferProvider::kInvalidPTS; 1650 } 1651 1652 return basePTS + ((outputFrameIndex * sLocalTimeFreq) / t.sampleRate); 1653} 1654 1655/*static*/ uint64_t AudioMixer::sLocalTimeFreq; 1656/*static*/ pthread_once_t AudioMixer::sOnceControl = PTHREAD_ONCE_INIT; 1657 1658/*static*/ void AudioMixer::sInitRoutine() 1659{ 1660 LocalClock lc; 1661 sLocalTimeFreq = lc.getLocalFreq(); 1662 1663 // find multichannel downmix effect if we have to play multichannel content 1664 uint32_t numEffects = 0; 1665 int ret = EffectQueryNumberEffects(&numEffects); 1666 if (ret != 0) { 1667 ALOGE("AudioMixer() error %d querying number of effects", ret); 1668 return; 1669 } 1670 ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects); 1671 1672 for (uint32_t i = 0 ; i < numEffects ; i++) { 1673 if (EffectQueryEffect(i, &sDwnmFxDesc) == 0) { 1674 ALOGV("effect %d is called %s", i, sDwnmFxDesc.name); 1675 if (memcmp(&sDwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) { 1676 ALOGI("found effect \"%s\" from %s", 1677 sDwnmFxDesc.name, sDwnmFxDesc.implementor); 1678 sIsMultichannelCapable = true; 1679 break; 1680 } 1681 } 1682 } 1683 ALOGW_IF(!sIsMultichannelCapable, "unable to find downmix effect"); 1684} 1685 1686// ---------------------------------------------------------------------------- 1687}; // namespace android 1688