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