AudioMixer.cpp revision 17a736c3e1d062d7fc916329eb32aef8935614af
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 <stdint.h> 22#include <string.h> 23#include <stdlib.h> 24#include <sys/types.h> 25 26#include <utils/Errors.h> 27#include <utils/Log.h> 28 29#include <cutils/bitops.h> 30#include <cutils/compiler.h> 31#include <utils/Debug.h> 32 33#include <system/audio.h> 34 35#include <audio_utils/primitives.h> 36#include <common_time/local_clock.h> 37#include <common_time/cc_helper.h> 38 39#include "AudioMixer.h" 40 41namespace android { 42 43// ---------------------------------------------------------------------------- 44 45AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate) 46 : mTrackNames(0), mSampleRate(sampleRate) 47{ 48 // AudioMixer is not yet capable of multi-channel beyond stereo 49 COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS); 50 51 LocalClock lc; 52 53 mState.enabledTracks= 0; 54 mState.needsChanged = 0; 55 mState.frameCount = frameCount; 56 mState.hook = process__nop; 57 mState.outputTemp = NULL; 58 mState.resampleTemp = NULL; 59 // mState.reserved 60 61 // FIXME Most of the following initialization is probably redundant since 62 // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0 63 // and mTrackNames is initially 0. However, leave it here until that's verified. 64 track_t* t = mState.tracks; 65 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 66 t->needs = 0; 67 t->volume[0] = UNITY_GAIN; 68 t->volume[1] = UNITY_GAIN; 69 // no initialization needed 70 // t->prevVolume[0] 71 // t->prevVolume[1] 72 t->volumeInc[0] = 0; 73 t->volumeInc[1] = 0; 74 t->auxLevel = 0; 75 t->auxInc = 0; 76 // no initialization needed 77 // t->prevAuxLevel 78 // t->frameCount 79 t->channelCount = 2; 80 t->enabled = false; 81 t->format = 16; 82 t->channelMask = AUDIO_CHANNEL_OUT_STEREO; 83 t->bufferProvider = NULL; 84 t->buffer.raw = NULL; 85 // t->buffer.frameCount 86 t->hook = NULL; 87 t->in = NULL; 88 t->resampler = NULL; 89 t->sampleRate = mSampleRate; 90 t->mainBuffer = NULL; 91 t->auxBuffer = NULL; 92 t->localTimeFreq = lc.getLocalFreq(); 93 t++; 94 } 95} 96 97AudioMixer::~AudioMixer() 98{ 99 track_t* t = mState.tracks; 100 for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) { 101 delete t->resampler; 102 t++; 103 } 104 delete [] mState.outputTemp; 105 delete [] mState.resampleTemp; 106} 107 108int AudioMixer::getTrackName() 109{ 110 uint32_t names = ~mTrackNames; 111 if (names != 0) { 112 int n = __builtin_ctz(names); 113 ALOGV("add track (%d)", n); 114 mTrackNames |= 1 << n; 115 return TRACK0 + n; 116 } 117 return -1; 118} 119 120void AudioMixer::invalidateState(uint32_t mask) 121{ 122 if (mask) { 123 mState.needsChanged |= mask; 124 mState.hook = process__validate; 125 } 126 } 127 128void AudioMixer::deleteTrackName(int name) 129{ 130 name -= TRACK0; 131 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 132 ALOGV("deleteTrackName(%d)", name); 133 track_t& track(mState.tracks[ name ]); 134 if (track.enabled) { 135 track.enabled = false; 136 invalidateState(1<<name); 137 } 138 if (track.resampler != NULL) { 139 // delete the resampler 140 delete track.resampler; 141 track.resampler = NULL; 142 track.sampleRate = mSampleRate; 143 invalidateState(1<<name); 144 } 145 track.volumeInc[0] = 0; 146 track.volumeInc[1] = 0; 147 mTrackNames &= ~(1<<name); 148} 149 150void AudioMixer::enable(int name) 151{ 152 name -= TRACK0; 153 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 154 track_t& track = mState.tracks[name]; 155 156 if (!track.enabled) { 157 track.enabled = true; 158 ALOGV("enable(%d)", name); 159 invalidateState(1 << name); 160 } 161} 162 163void AudioMixer::disable(int name) 164{ 165 name -= TRACK0; 166 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 167 track_t& track = mState.tracks[name]; 168 169 if (track.enabled) { 170 track.enabled = false; 171 ALOGV("disable(%d)", name); 172 invalidateState(1 << name); 173 } 174} 175 176void AudioMixer::setParameter(int name, int target, int param, void *value) 177{ 178 name -= TRACK0; 179 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 180 track_t& track = mState.tracks[name]; 181 182 int valueInt = (int)value; 183 int32_t *valueBuf = (int32_t *)value; 184 185 switch (target) { 186 187 case TRACK: 188 switch (param) { 189 case CHANNEL_MASK: { 190 uint32_t mask = (uint32_t)value; 191 if (track.channelMask != mask) { 192 uint32_t channelCount = popcount(mask); 193 ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS) && (channelCount), 194 "bad channel count %u", channelCount); 195 track.channelMask = mask; 196 track.channelCount = channelCount; 197 ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask); 198 invalidateState(1 << name); 199 } 200 } break; 201 case MAIN_BUFFER: 202 if (track.mainBuffer != valueBuf) { 203 track.mainBuffer = valueBuf; 204 ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf); 205 invalidateState(1 << name); 206 } 207 break; 208 case AUX_BUFFER: 209 if (track.auxBuffer != valueBuf) { 210 track.auxBuffer = valueBuf; 211 ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf); 212 invalidateState(1 << name); 213 } 214 break; 215 default: 216 LOG_FATAL("bad param"); 217 } 218 break; 219 220 case RESAMPLE: 221 switch (param) { 222 case SAMPLE_RATE: 223 ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt); 224 if (track.setResampler(uint32_t(valueInt), mSampleRate)) { 225 ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)", 226 uint32_t(valueInt)); 227 invalidateState(1 << name); 228 } 229 break; 230 case RESET: 231 track.resetResampler(); 232 invalidateState(1 << name); 233 break; 234 default: 235 LOG_FATAL("bad param"); 236 } 237 break; 238 239 case RAMP_VOLUME: 240 case VOLUME: 241 switch (param) { 242 case VOLUME0: 243 case VOLUME1: 244 if (track.volume[param-VOLUME0] != valueInt) { 245 ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt); 246 track.prevVolume[param-VOLUME0] = track.volume[param-VOLUME0] << 16; 247 track.volume[param-VOLUME0] = valueInt; 248 if (target == VOLUME) { 249 track.prevVolume[param-VOLUME0] = valueInt << 16; 250 track.volumeInc[param-VOLUME0] = 0; 251 } else { 252 int32_t d = (valueInt<<16) - track.prevVolume[param-VOLUME0]; 253 int32_t volInc = d / int32_t(mState.frameCount); 254 track.volumeInc[param-VOLUME0] = volInc; 255 if (volInc == 0) { 256 track.prevVolume[param-VOLUME0] = valueInt << 16; 257 } 258 } 259 invalidateState(1 << name); 260 } 261 break; 262 case AUXLEVEL: 263 //ALOG_ASSERT(0 <= valueInt && valueInt <= MAX_GAIN_INT, "bad aux level %d", valueInt); 264 if (track.auxLevel != valueInt) { 265 ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt); 266 track.prevAuxLevel = track.auxLevel << 16; 267 track.auxLevel = valueInt; 268 if (target == VOLUME) { 269 track.prevAuxLevel = valueInt << 16; 270 track.auxInc = 0; 271 } else { 272 int32_t d = (valueInt<<16) - track.prevAuxLevel; 273 int32_t volInc = d / int32_t(mState.frameCount); 274 track.auxInc = volInc; 275 if (volInc == 0) { 276 track.prevAuxLevel = valueInt << 16; 277 } 278 } 279 invalidateState(1 << name); 280 } 281 break; 282 default: 283 LOG_FATAL("bad param"); 284 } 285 break; 286 287 default: 288 LOG_FATAL("bad target"); 289 } 290} 291 292bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate) 293{ 294 if (value!=devSampleRate || resampler) { 295 if (sampleRate != value) { 296 sampleRate = value; 297 if (resampler == NULL) { 298 resampler = AudioResampler::create( 299 format, channelCount, devSampleRate); 300 resampler->setLocalTimeFreq(localTimeFreq); 301 } 302 return true; 303 } 304 } 305 return false; 306} 307 308inline 309void AudioMixer::track_t::adjustVolumeRamp(bool aux) 310{ 311 for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) { 312 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) || 313 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) { 314 volumeInc[i] = 0; 315 prevVolume[i] = volume[i]<<16; 316 } 317 } 318 if (aux) { 319 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) || 320 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) { 321 auxInc = 0; 322 prevAuxLevel = auxLevel<<16; 323 } 324 } 325} 326 327size_t AudioMixer::getUnreleasedFrames(int name) const 328{ 329 name -= TRACK0; 330 if (uint32_t(name) < MAX_NUM_TRACKS) { 331 return mState.tracks[name].getUnreleasedFrames(); 332 } 333 return 0; 334} 335 336void AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider) 337{ 338 name -= TRACK0; 339 ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name); 340 mState.tracks[name].bufferProvider = bufferProvider; 341} 342 343 344 345void AudioMixer::process(int64_t pts) 346{ 347 mState.hook(&mState, pts); 348} 349 350 351void AudioMixer::process__validate(state_t* state, int64_t pts) 352{ 353 ALOGW_IF(!state->needsChanged, 354 "in process__validate() but nothing's invalid"); 355 356 uint32_t changed = state->needsChanged; 357 state->needsChanged = 0; // clear the validation flag 358 359 // recompute which tracks are enabled / disabled 360 uint32_t enabled = 0; 361 uint32_t disabled = 0; 362 while (changed) { 363 const int i = 31 - __builtin_clz(changed); 364 const uint32_t mask = 1<<i; 365 changed &= ~mask; 366 track_t& t = state->tracks[i]; 367 (t.enabled ? enabled : disabled) |= mask; 368 } 369 state->enabledTracks &= ~disabled; 370 state->enabledTracks |= enabled; 371 372 // compute everything we need... 373 int countActiveTracks = 0; 374 bool all16BitsStereoNoResample = true; 375 bool resampling = false; 376 bool volumeRamp = false; 377 uint32_t en = state->enabledTracks; 378 while (en) { 379 const int i = 31 - __builtin_clz(en); 380 en &= ~(1<<i); 381 382 countActiveTracks++; 383 track_t& t = state->tracks[i]; 384 uint32_t n = 0; 385 n |= NEEDS_CHANNEL_1 + t.channelCount - 1; 386 n |= NEEDS_FORMAT_16; 387 n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED; 388 if (t.auxLevel != 0 && t.auxBuffer != NULL) { 389 n |= NEEDS_AUX_ENABLED; 390 } 391 392 if (t.volumeInc[0]|t.volumeInc[1]) { 393 volumeRamp = true; 394 } else if (!t.doesResample() && t.volumeRL == 0) { 395 n |= NEEDS_MUTE_ENABLED; 396 } 397 t.needs = n; 398 399 if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) { 400 t.hook = track__nop; 401 } else { 402 if ((n & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) { 403 all16BitsStereoNoResample = false; 404 } 405 if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) { 406 all16BitsStereoNoResample = false; 407 resampling = true; 408 t.hook = track__genericResample; 409 } else { 410 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){ 411 t.hook = track__16BitsMono; 412 all16BitsStereoNoResample = false; 413 } 414 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_2){ 415 t.hook = track__16BitsStereo; 416 } 417 } 418 } 419 } 420 421 // select the processing hooks 422 state->hook = process__nop; 423 if (countActiveTracks) { 424 if (resampling) { 425 if (!state->outputTemp) { 426 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 427 } 428 if (!state->resampleTemp) { 429 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount]; 430 } 431 state->hook = process__genericResampling; 432 } else { 433 if (state->outputTemp) { 434 delete [] state->outputTemp; 435 state->outputTemp = NULL; 436 } 437 if (state->resampleTemp) { 438 delete [] state->resampleTemp; 439 state->resampleTemp = NULL; 440 } 441 state->hook = process__genericNoResampling; 442 if (all16BitsStereoNoResample && !volumeRamp) { 443 if (countActiveTracks == 1) { 444 state->hook = process__OneTrack16BitsStereoNoResampling; 445 } 446 } 447 } 448 } 449 450 ALOGV("mixer configuration change: %d activeTracks (%08x) " 451 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d", 452 countActiveTracks, state->enabledTracks, 453 all16BitsStereoNoResample, resampling, volumeRamp); 454 455 state->hook(state, pts); 456 457 // Now that the volume ramp has been done, set optimal state and 458 // track hooks for subsequent mixer process 459 if (countActiveTracks) { 460 bool allMuted = true; 461 uint32_t en = state->enabledTracks; 462 while (en) { 463 const int i = 31 - __builtin_clz(en); 464 en &= ~(1<<i); 465 track_t& t = state->tracks[i]; 466 if (!t.doesResample() && t.volumeRL == 0) 467 { 468 t.needs |= NEEDS_MUTE_ENABLED; 469 t.hook = track__nop; 470 } else { 471 allMuted = false; 472 } 473 } 474 if (allMuted) { 475 state->hook = process__nop; 476 } else if (all16BitsStereoNoResample) { 477 if (countActiveTracks == 1) { 478 state->hook = process__OneTrack16BitsStereoNoResampling; 479 } 480 } 481 } 482} 483 484 485void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux) 486{ 487 t->resampler->setSampleRate(t->sampleRate); 488 489 // ramp gain - resample to temp buffer and scale/mix in 2nd step 490 if (aux != NULL) { 491 // always resample with unity gain when sending to auxiliary buffer to be able 492 // to apply send level after resampling 493 // TODO: modify each resampler to support aux channel? 494 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN); 495 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 496 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 497 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 498 volumeRampStereo(t, out, outFrameCount, temp, aux); 499 } else { 500 volumeStereo(t, out, outFrameCount, temp, aux); 501 } 502 } else { 503 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 504 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN); 505 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t)); 506 t->resampler->resample(temp, outFrameCount, t->bufferProvider); 507 volumeRampStereo(t, out, outFrameCount, temp, aux); 508 } 509 510 // constant gain 511 else { 512 t->resampler->setVolume(t->volume[0], t->volume[1]); 513 t->resampler->resample(out, outFrameCount, t->bufferProvider); 514 } 515 } 516} 517 518void AudioMixer::track__nop(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux) 519{ 520} 521 522void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux) 523{ 524 int32_t vl = t->prevVolume[0]; 525 int32_t vr = t->prevVolume[1]; 526 const int32_t vlInc = t->volumeInc[0]; 527 const int32_t vrInc = t->volumeInc[1]; 528 529 //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 530 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 531 // (vl + vlInc*frameCount)/65536.0f, frameCount); 532 533 // ramp volume 534 if (CC_UNLIKELY(aux != NULL)) { 535 int32_t va = t->prevAuxLevel; 536 const int32_t vaInc = t->auxInc; 537 int32_t l; 538 int32_t r; 539 540 do { 541 l = (*temp++ >> 12); 542 r = (*temp++ >> 12); 543 *out++ += (vl >> 16) * l; 544 *out++ += (vr >> 16) * r; 545 *aux++ += (va >> 17) * (l + r); 546 vl += vlInc; 547 vr += vrInc; 548 va += vaInc; 549 } while (--frameCount); 550 t->prevAuxLevel = va; 551 } else { 552 do { 553 *out++ += (vl >> 16) * (*temp++ >> 12); 554 *out++ += (vr >> 16) * (*temp++ >> 12); 555 vl += vlInc; 556 vr += vrInc; 557 } while (--frameCount); 558 } 559 t->prevVolume[0] = vl; 560 t->prevVolume[1] = vr; 561 t->adjustVolumeRamp(aux != NULL); 562} 563 564void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux) 565{ 566 const int16_t vl = t->volume[0]; 567 const int16_t vr = t->volume[1]; 568 569 if (CC_UNLIKELY(aux != NULL)) { 570 const int16_t va = t->auxLevel; 571 do { 572 int16_t l = (int16_t)(*temp++ >> 12); 573 int16_t r = (int16_t)(*temp++ >> 12); 574 out[0] = mulAdd(l, vl, out[0]); 575 int16_t a = (int16_t)(((int32_t)l + r) >> 1); 576 out[1] = mulAdd(r, vr, out[1]); 577 out += 2; 578 aux[0] = mulAdd(a, va, aux[0]); 579 aux++; 580 } while (--frameCount); 581 } else { 582 do { 583 int16_t l = (int16_t)(*temp++ >> 12); 584 int16_t r = (int16_t)(*temp++ >> 12); 585 out[0] = mulAdd(l, vl, out[0]); 586 out[1] = mulAdd(r, vr, out[1]); 587 out += 2; 588 } while (--frameCount); 589 } 590} 591 592void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux) 593{ 594 const int16_t *in = static_cast<const int16_t *>(t->in); 595 596 if (CC_UNLIKELY(aux != NULL)) { 597 int32_t l; 598 int32_t r; 599 // ramp gain 600 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 601 int32_t vl = t->prevVolume[0]; 602 int32_t vr = t->prevVolume[1]; 603 int32_t va = t->prevAuxLevel; 604 const int32_t vlInc = t->volumeInc[0]; 605 const int32_t vrInc = t->volumeInc[1]; 606 const int32_t vaInc = t->auxInc; 607 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 608 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 609 // (vl + vlInc*frameCount)/65536.0f, frameCount); 610 611 do { 612 l = (int32_t)*in++; 613 r = (int32_t)*in++; 614 *out++ += (vl >> 16) * l; 615 *out++ += (vr >> 16) * r; 616 *aux++ += (va >> 17) * (l + r); 617 vl += vlInc; 618 vr += vrInc; 619 va += vaInc; 620 } while (--frameCount); 621 622 t->prevVolume[0] = vl; 623 t->prevVolume[1] = vr; 624 t->prevAuxLevel = va; 625 t->adjustVolumeRamp(true); 626 } 627 628 // constant gain 629 else { 630 const uint32_t vrl = t->volumeRL; 631 const int16_t va = (int16_t)t->auxLevel; 632 do { 633 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 634 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1); 635 in += 2; 636 out[0] = mulAddRL(1, rl, vrl, out[0]); 637 out[1] = mulAddRL(0, rl, vrl, out[1]); 638 out += 2; 639 aux[0] = mulAdd(a, va, aux[0]); 640 aux++; 641 } while (--frameCount); 642 } 643 } else { 644 // ramp gain 645 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 646 int32_t vl = t->prevVolume[0]; 647 int32_t vr = t->prevVolume[1]; 648 const int32_t vlInc = t->volumeInc[0]; 649 const int32_t vrInc = t->volumeInc[1]; 650 651 // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 652 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 653 // (vl + vlInc*frameCount)/65536.0f, frameCount); 654 655 do { 656 *out++ += (vl >> 16) * (int32_t) *in++; 657 *out++ += (vr >> 16) * (int32_t) *in++; 658 vl += vlInc; 659 vr += vrInc; 660 } while (--frameCount); 661 662 t->prevVolume[0] = vl; 663 t->prevVolume[1] = vr; 664 t->adjustVolumeRamp(false); 665 } 666 667 // constant gain 668 else { 669 const uint32_t vrl = t->volumeRL; 670 do { 671 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 672 in += 2; 673 out[0] = mulAddRL(1, rl, vrl, out[0]); 674 out[1] = mulAddRL(0, rl, vrl, out[1]); 675 out += 2; 676 } while (--frameCount); 677 } 678 } 679 t->in = in; 680} 681 682void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux) 683{ 684 const int16_t *in = static_cast<int16_t const *>(t->in); 685 686 if (CC_UNLIKELY(aux != NULL)) { 687 // ramp gain 688 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) { 689 int32_t vl = t->prevVolume[0]; 690 int32_t vr = t->prevVolume[1]; 691 int32_t va = t->prevAuxLevel; 692 const int32_t vlInc = t->volumeInc[0]; 693 const int32_t vrInc = t->volumeInc[1]; 694 const int32_t vaInc = t->auxInc; 695 696 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 697 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 698 // (vl + vlInc*frameCount)/65536.0f, frameCount); 699 700 do { 701 int32_t l = *in++; 702 *out++ += (vl >> 16) * l; 703 *out++ += (vr >> 16) * l; 704 *aux++ += (va >> 16) * l; 705 vl += vlInc; 706 vr += vrInc; 707 va += vaInc; 708 } while (--frameCount); 709 710 t->prevVolume[0] = vl; 711 t->prevVolume[1] = vr; 712 t->prevAuxLevel = va; 713 t->adjustVolumeRamp(true); 714 } 715 // constant gain 716 else { 717 const int16_t vl = t->volume[0]; 718 const int16_t vr = t->volume[1]; 719 const int16_t va = (int16_t)t->auxLevel; 720 do { 721 int16_t l = *in++; 722 out[0] = mulAdd(l, vl, out[0]); 723 out[1] = mulAdd(l, vr, out[1]); 724 out += 2; 725 aux[0] = mulAdd(l, va, aux[0]); 726 aux++; 727 } while (--frameCount); 728 } 729 } else { 730 // ramp gain 731 if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) { 732 int32_t vl = t->prevVolume[0]; 733 int32_t vr = t->prevVolume[1]; 734 const int32_t vlInc = t->volumeInc[0]; 735 const int32_t vrInc = t->volumeInc[1]; 736 737 // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d", 738 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0], 739 // (vl + vlInc*frameCount)/65536.0f, frameCount); 740 741 do { 742 int32_t l = *in++; 743 *out++ += (vl >> 16) * l; 744 *out++ += (vr >> 16) * l; 745 vl += vlInc; 746 vr += vrInc; 747 } while (--frameCount); 748 749 t->prevVolume[0] = vl; 750 t->prevVolume[1] = vr; 751 t->adjustVolumeRamp(false); 752 } 753 // constant gain 754 else { 755 const int16_t vl = t->volume[0]; 756 const int16_t vr = t->volume[1]; 757 do { 758 int16_t l = *in++; 759 out[0] = mulAdd(l, vl, out[0]); 760 out[1] = mulAdd(l, vr, out[1]); 761 out += 2; 762 } while (--frameCount); 763 } 764 } 765 t->in = in; 766} 767 768// no-op case 769void AudioMixer::process__nop(state_t* state, int64_t pts) 770{ 771 uint32_t e0 = state->enabledTracks; 772 size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS; 773 while (e0) { 774 // process by group of tracks with same output buffer to 775 // avoid multiple memset() on same buffer 776 uint32_t e1 = e0, e2 = e0; 777 int i = 31 - __builtin_clz(e1); 778 track_t& t1 = state->tracks[i]; 779 e2 &= ~(1<<i); 780 while (e2) { 781 i = 31 - __builtin_clz(e2); 782 e2 &= ~(1<<i); 783 track_t& t2 = state->tracks[i]; 784 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 785 e1 &= ~(1<<i); 786 } 787 } 788 e0 &= ~(e1); 789 790 memset(t1.mainBuffer, 0, bufSize); 791 792 while (e1) { 793 i = 31 - __builtin_clz(e1); 794 e1 &= ~(1<<i); 795 t1 = state->tracks[i]; 796 size_t outFrames = state->frameCount; 797 while (outFrames) { 798 t1.buffer.frameCount = outFrames; 799 int64_t outputPTS = calculateOutputPTS( 800 t1, pts, state->frameCount - outFrames); 801 t1.bufferProvider->getNextBuffer(&t1.buffer, outputPTS); 802 if (t1.buffer.raw == NULL) break; 803 outFrames -= t1.buffer.frameCount; 804 t1.bufferProvider->releaseBuffer(&t1.buffer); 805 } 806 } 807 } 808} 809 810// generic code without resampling 811void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts) 812{ 813 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32))); 814 815 // acquire each track's buffer 816 uint32_t enabledTracks = state->enabledTracks; 817 uint32_t e0 = enabledTracks; 818 while (e0) { 819 const int i = 31 - __builtin_clz(e0); 820 e0 &= ~(1<<i); 821 track_t& t = state->tracks[i]; 822 t.buffer.frameCount = state->frameCount; 823 t.bufferProvider->getNextBuffer(&t.buffer, pts); 824 t.frameCount = t.buffer.frameCount; 825 t.in = t.buffer.raw; 826 // t.in == NULL can happen if the track was flushed just after having 827 // been enabled for mixing. 828 if (t.in == NULL) 829 enabledTracks &= ~(1<<i); 830 } 831 832 e0 = enabledTracks; 833 while (e0) { 834 // process by group of tracks with same output buffer to 835 // optimize cache use 836 uint32_t e1 = e0, e2 = e0; 837 int j = 31 - __builtin_clz(e1); 838 track_t& t1 = state->tracks[j]; 839 e2 &= ~(1<<j); 840 while (e2) { 841 j = 31 - __builtin_clz(e2); 842 e2 &= ~(1<<j); 843 track_t& t2 = state->tracks[j]; 844 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 845 e1 &= ~(1<<j); 846 } 847 } 848 e0 &= ~(e1); 849 // this assumes output 16 bits stereo, no resampling 850 int32_t *out = t1.mainBuffer; 851 size_t numFrames = 0; 852 do { 853 memset(outTemp, 0, sizeof(outTemp)); 854 e2 = e1; 855 while (e2) { 856 const int i = 31 - __builtin_clz(e2); 857 e2 &= ~(1<<i); 858 track_t& t = state->tracks[i]; 859 size_t outFrames = BLOCKSIZE; 860 int32_t *aux = NULL; 861 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) { 862 aux = t.auxBuffer + numFrames; 863 } 864 while (outFrames) { 865 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount; 866 if (inFrames) { 867 t.hook(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, state->resampleTemp, aux); 868 t.frameCount -= inFrames; 869 outFrames -= inFrames; 870 if (CC_UNLIKELY(aux != NULL)) { 871 aux += inFrames; 872 } 873 } 874 if (t.frameCount == 0 && outFrames) { 875 t.bufferProvider->releaseBuffer(&t.buffer); 876 t.buffer.frameCount = (state->frameCount - numFrames) - (BLOCKSIZE - outFrames); 877 int64_t outputPTS = calculateOutputPTS( 878 t, pts, numFrames + (BLOCKSIZE - outFrames)); 879 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 880 t.in = t.buffer.raw; 881 if (t.in == NULL) { 882 enabledTracks &= ~(1<<i); 883 e1 &= ~(1<<i); 884 break; 885 } 886 t.frameCount = t.buffer.frameCount; 887 } 888 } 889 } 890 ditherAndClamp(out, outTemp, BLOCKSIZE); 891 out += BLOCKSIZE; 892 numFrames += BLOCKSIZE; 893 } while (numFrames < state->frameCount); 894 } 895 896 // release each track's buffer 897 e0 = enabledTracks; 898 while (e0) { 899 const int i = 31 - __builtin_clz(e0); 900 e0 &= ~(1<<i); 901 track_t& t = state->tracks[i]; 902 t.bufferProvider->releaseBuffer(&t.buffer); 903 } 904} 905 906 907// generic code with resampling 908void AudioMixer::process__genericResampling(state_t* state, int64_t pts) 909{ 910 // this const just means that local variable outTemp doesn't change 911 int32_t* const outTemp = state->outputTemp; 912 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount; 913 914 size_t numFrames = state->frameCount; 915 916 uint32_t e0 = state->enabledTracks; 917 while (e0) { 918 // process by group of tracks with same output buffer 919 // to optimize cache use 920 uint32_t e1 = e0, e2 = e0; 921 int j = 31 - __builtin_clz(e1); 922 track_t& t1 = state->tracks[j]; 923 e2 &= ~(1<<j); 924 while (e2) { 925 j = 31 - __builtin_clz(e2); 926 e2 &= ~(1<<j); 927 track_t& t2 = state->tracks[j]; 928 if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) { 929 e1 &= ~(1<<j); 930 } 931 } 932 e0 &= ~(e1); 933 int32_t *out = t1.mainBuffer; 934 memset(outTemp, 0, size); 935 while (e1) { 936 const int i = 31 - __builtin_clz(e1); 937 e1 &= ~(1<<i); 938 track_t& t = state->tracks[i]; 939 int32_t *aux = NULL; 940 if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) { 941 aux = t.auxBuffer; 942 } 943 944 // this is a little goofy, on the resampling case we don't 945 // acquire/release the buffers because it's done by 946 // the resampler. 947 if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) { 948 t.resampler->setPTS(pts); 949 t.hook(&t, outTemp, numFrames, state->resampleTemp, aux); 950 } else { 951 952 size_t outFrames = 0; 953 954 while (outFrames < numFrames) { 955 t.buffer.frameCount = numFrames - outFrames; 956 int64_t outputPTS = calculateOutputPTS(t, pts, outFrames); 957 t.bufferProvider->getNextBuffer(&t.buffer, outputPTS); 958 t.in = t.buffer.raw; 959 // t.in == NULL can happen if the track was flushed just after having 960 // been enabled for mixing. 961 if (t.in == NULL) break; 962 963 if (CC_UNLIKELY(aux != NULL)) { 964 aux += outFrames; 965 } 966 t.hook(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, state->resampleTemp, aux); 967 outFrames += t.buffer.frameCount; 968 t.bufferProvider->releaseBuffer(&t.buffer); 969 } 970 } 971 } 972 ditherAndClamp(out, outTemp, numFrames); 973 } 974} 975 976// one track, 16 bits stereo without resampling is the most common case 977void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state, 978 int64_t pts) 979{ 980 // This method is only called when state->enabledTracks has exactly 981 // one bit set. The asserts below would verify this, but are commented out 982 // since the whole point of this method is to optimize performance. 983 //ALOG_ASSERT(0 != state->enabledTracks, "no tracks enabled"); 984 const int i = 31 - __builtin_clz(state->enabledTracks); 985 //ALOG_ASSERT((1 << i) == state->enabledTracks, "more than 1 track enabled"); 986 const track_t& t = state->tracks[i]; 987 988 AudioBufferProvider::Buffer& b(t.buffer); 989 990 int32_t* out = t.mainBuffer; 991 size_t numFrames = state->frameCount; 992 993 const int16_t vl = t.volume[0]; 994 const int16_t vr = t.volume[1]; 995 const uint32_t vrl = t.volumeRL; 996 while (numFrames) { 997 b.frameCount = numFrames; 998 int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer); 999 t.bufferProvider->getNextBuffer(&b, outputPTS); 1000 const int16_t *in = b.i16; 1001 1002 // in == NULL can happen if the track was flushed just after having 1003 // been enabled for mixing. 1004 if (in == NULL || ((unsigned long)in & 3)) { 1005 memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t)); 1006 ALOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: buffer %p track %d, channels %d, needs %08x", 1007 in, i, t.channelCount, t.needs); 1008 return; 1009 } 1010 size_t outFrames = b.frameCount; 1011 1012 if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) { 1013 // volume is boosted, so we might need to clamp even though 1014 // we process only one track. 1015 do { 1016 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1017 in += 2; 1018 int32_t l = mulRL(1, rl, vrl) >> 12; 1019 int32_t r = mulRL(0, rl, vrl) >> 12; 1020 // clamping... 1021 l = clamp16(l); 1022 r = clamp16(r); 1023 *out++ = (r<<16) | (l & 0xFFFF); 1024 } while (--outFrames); 1025 } else { 1026 do { 1027 uint32_t rl = *reinterpret_cast<const uint32_t *>(in); 1028 in += 2; 1029 int32_t l = mulRL(1, rl, vrl) >> 12; 1030 int32_t r = mulRL(0, rl, vrl) >> 12; 1031 *out++ = (r<<16) | (l & 0xFFFF); 1032 } while (--outFrames); 1033 } 1034 numFrames -= b.frameCount; 1035 t.bufferProvider->releaseBuffer(&b); 1036 } 1037} 1038 1039#if 0 1040// 2 tracks is also a common case 1041// NEVER used in current implementation of process__validate() 1042// only use if the 2 tracks have the same output buffer 1043void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state, 1044 int64_t pts) 1045{ 1046 int i; 1047 uint32_t en = state->enabledTracks; 1048 1049 i = 31 - __builtin_clz(en); 1050 const track_t& t0 = state->tracks[i]; 1051 AudioBufferProvider::Buffer& b0(t0.buffer); 1052 1053 en &= ~(1<<i); 1054 i = 31 - __builtin_clz(en); 1055 const track_t& t1 = state->tracks[i]; 1056 AudioBufferProvider::Buffer& b1(t1.buffer); 1057 1058 const int16_t *in0; 1059 const int16_t vl0 = t0.volume[0]; 1060 const int16_t vr0 = t0.volume[1]; 1061 size_t frameCount0 = 0; 1062 1063 const int16_t *in1; 1064 const int16_t vl1 = t1.volume[0]; 1065 const int16_t vr1 = t1.volume[1]; 1066 size_t frameCount1 = 0; 1067 1068 //FIXME: only works if two tracks use same buffer 1069 int32_t* out = t0.mainBuffer; 1070 size_t numFrames = state->frameCount; 1071 const int16_t *buff = NULL; 1072 1073 1074 while (numFrames) { 1075 1076 if (frameCount0 == 0) { 1077 b0.frameCount = numFrames; 1078 int64_t outputPTS = calculateOutputPTS(t0, pts, 1079 out - t0.mainBuffer); 1080 t0.bufferProvider->getNextBuffer(&b0, outputPTS); 1081 if (b0.i16 == NULL) { 1082 if (buff == NULL) { 1083 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount]; 1084 } 1085 in0 = buff; 1086 b0.frameCount = numFrames; 1087 } else { 1088 in0 = b0.i16; 1089 } 1090 frameCount0 = b0.frameCount; 1091 } 1092 if (frameCount1 == 0) { 1093 b1.frameCount = numFrames; 1094 int64_t outputPTS = calculateOutputPTS(t1, pts, 1095 out - t0.mainBuffer); 1096 t1.bufferProvider->getNextBuffer(&b1, outputPTS); 1097 if (b1.i16 == NULL) { 1098 if (buff == NULL) { 1099 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount]; 1100 } 1101 in1 = buff; 1102 b1.frameCount = numFrames; 1103 } else { 1104 in1 = b1.i16; 1105 } 1106 frameCount1 = b1.frameCount; 1107 } 1108 1109 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1; 1110 1111 numFrames -= outFrames; 1112 frameCount0 -= outFrames; 1113 frameCount1 -= outFrames; 1114 1115 do { 1116 int32_t l0 = *in0++; 1117 int32_t r0 = *in0++; 1118 l0 = mul(l0, vl0); 1119 r0 = mul(r0, vr0); 1120 int32_t l = *in1++; 1121 int32_t r = *in1++; 1122 l = mulAdd(l, vl1, l0) >> 12; 1123 r = mulAdd(r, vr1, r0) >> 12; 1124 // clamping... 1125 l = clamp16(l); 1126 r = clamp16(r); 1127 *out++ = (r<<16) | (l & 0xFFFF); 1128 } while (--outFrames); 1129 1130 if (frameCount0 == 0) { 1131 t0.bufferProvider->releaseBuffer(&b0); 1132 } 1133 if (frameCount1 == 0) { 1134 t1.bufferProvider->releaseBuffer(&b1); 1135 } 1136 } 1137 1138 delete [] buff; 1139} 1140#endif 1141 1142int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS, 1143 int outputFrameIndex) 1144{ 1145 if (AudioBufferProvider::kInvalidPTS == basePTS) 1146 return AudioBufferProvider::kInvalidPTS; 1147 1148 return basePTS + ((outputFrameIndex * t.localTimeFreq) / t.sampleRate); 1149} 1150 1151// ---------------------------------------------------------------------------- 1152}; // namespace android 1153