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