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