blob: 9f1b17f082c05d7a9518c6fd51b4560a25f85b6c [file] [log] [blame]
The Android Open Source Project7c1b96a2008-10-21 07:00:00 -07001/* //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
20#include <stdint.h>
21#include <string.h>
22#include <stdlib.h>
23#include <sys/types.h>
24
25#include <utils/Errors.h>
26#include <utils/Log.h>
27
28#include "AudioMixer.h"
29
30namespace android {
31// ----------------------------------------------------------------------------
32
33static inline int16_t clamp16(int32_t sample)
34{
35 if ((sample>>15) ^ (sample>>31))
36 sample = 0x7FFF ^ (sample>>31);
37 return sample;
38}
39
40// ----------------------------------------------------------------------------
41
42AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate)
43 : mActiveTrack(0), mTrackNames(0), mSampleRate(sampleRate)
44{
45 mState.enabledTracks= 0;
46 mState.needsChanged = 0;
47 mState.frameCount = frameCount;
48 mState.outputTemp = 0;
49 mState.resampleTemp = 0;
50 mState.hook = process__nop;
51 track_t* t = mState.tracks;
52 for (int i=0 ; i<32 ; i++) {
53 t->needs = 0;
54 t->volume[0] = UNITY_GAIN;
55 t->volume[1] = UNITY_GAIN;
56 t->volumeInc[0] = 0;
57 t->volumeInc[1] = 0;
58 t->channelCount = 2;
59 t->enabled = 0;
60 t->format = 16;
61 t->buffer.raw = 0;
62 t->bufferProvider = 0;
63 t->hook = 0;
64 t->resampler = 0;
65 t->sampleRate = mSampleRate;
66 t->in = 0;
67 t++;
68 }
69}
70
71 AudioMixer::~AudioMixer()
72 {
73 track_t* t = mState.tracks;
74 for (int i=0 ; i<32 ; i++) {
75 delete t->resampler;
76 t++;
77 }
78 delete [] mState.outputTemp;
79 delete [] mState.resampleTemp;
80 }
81
82 int AudioMixer::getTrackName()
83 {
84 uint32_t names = mTrackNames;
85 uint32_t mask = 1;
86 int n = 0;
87 while (names & mask) {
88 mask <<= 1;
89 n++;
90 }
91 if (mask) {
92 LOGV("add track (%d)", n);
93 mTrackNames |= mask;
94 return TRACK0 + n;
95 }
96 return -1;
97 }
98
99 void AudioMixer::invalidateState(uint32_t mask)
100 {
101 if (mask) {
102 mState.needsChanged |= mask;
103 mState.hook = process__validate;
104 }
105 }
106
107 void AudioMixer::deleteTrackName(int name)
108 {
109 name -= TRACK0;
110 if (uint32_t(name) < MAX_NUM_TRACKS) {
111 LOGV("deleteTrackName(%d)", name);
112 track_t& track(mState.tracks[ name ]);
113 if (track.enabled != 0) {
114 track.enabled = 0;
115 invalidateState(1<<name);
116 }
117 if (track.resampler) {
118 // delete the resampler
119 delete track.resampler;
120 track.resampler = 0;
121 track.sampleRate = mSampleRate;
122 invalidateState(1<<name);
123 }
124 track.volumeInc[0] = 0;
125 track.volumeInc[1] = 0;
126 mTrackNames &= ~(1<<name);
127 }
128 }
129
130status_t AudioMixer::enable(int name)
131{
132 switch (name) {
133 case MIXING: {
134 if (mState.tracks[ mActiveTrack ].enabled != 1) {
135 mState.tracks[ mActiveTrack ].enabled = 1;
136 LOGV("enable(%d)", mActiveTrack);
137 invalidateState(1<<mActiveTrack);
138 }
139 } break;
140 default:
141 return NAME_NOT_FOUND;
142 }
143 return NO_ERROR;
144}
145
146status_t AudioMixer::disable(int name)
147{
148 switch (name) {
149 case MIXING: {
150 if (mState.tracks[ mActiveTrack ].enabled != 0) {
151 mState.tracks[ mActiveTrack ].enabled = 0;
152 LOGV("disable(%d)", mActiveTrack);
153 invalidateState(1<<mActiveTrack);
154 }
155 } break;
156 default:
157 return NAME_NOT_FOUND;
158 }
159 return NO_ERROR;
160}
161
162status_t AudioMixer::setActiveTrack(int track)
163{
164 if (uint32_t(track-TRACK0) >= MAX_NUM_TRACKS) {
165 return BAD_VALUE;
166 }
167 mActiveTrack = track - TRACK0;
168 return NO_ERROR;
169}
170
171status_t AudioMixer::setParameter(int target, int name, int value)
172{
173 switch (target) {
174 case TRACK:
175 if (name == CHANNEL_COUNT) {
176 if ((uint32_t(value) <= MAX_NUM_CHANNELS) && (value)) {
177 if (mState.tracks[ mActiveTrack ].channelCount != value) {
178 mState.tracks[ mActiveTrack ].channelCount = value;
179 LOGV("setParameter(TRACK, CHANNEL_COUNT, %d)", value);
180 invalidateState(1<<mActiveTrack);
181 }
182 return NO_ERROR;
183 }
184 }
185 break;
186 case RESAMPLE:
187 if (name == SAMPLE_RATE) {
188 if (value > 0) {
189 track_t& track = mState.tracks[ mActiveTrack ];
190 if (track.setResampler(uint32_t(value), mSampleRate)) {
191 LOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
192 uint32_t(value));
193 invalidateState(1<<mActiveTrack);
194 }
195 return NO_ERROR;
196 }
197 }
198 break;
199 case RAMP_VOLUME:
200 case VOLUME:
201 if ((uint32_t(name-VOLUME0) < MAX_NUM_CHANNELS)) {
202 track_t& track = mState.tracks[ mActiveTrack ];
203 if (track.volume[name-VOLUME0] != value) {
204 track.prevVolume[name-VOLUME0] = track.volume[name-VOLUME0] << 16;
205 track.volume[name-VOLUME0] = value;
206 if (target == VOLUME) {
207 track.prevVolume[name-VOLUME0] = value << 16;
208 track.volumeInc[name-VOLUME0] = 0;
209 } else {
210 int32_t d = (value<<16) - track.prevVolume[name-VOLUME0];
211 int32_t volInc = d / int32_t(mState.frameCount);
212 track.volumeInc[name-VOLUME0] = volInc;
213 if (volInc == 0) {
214 track.prevVolume[name-VOLUME0] = value << 16;
215 }
216 }
217 invalidateState(1<<mActiveTrack);
218 }
219 return NO_ERROR;
220 }
221 break;
222 }
223 return BAD_VALUE;
224}
225
226bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
227{
228 if (value!=devSampleRate || resampler) {
229 if (sampleRate != value) {
230 sampleRate = value;
231 if (resampler == 0) {
232 resampler = AudioResampler::create(
233 format, channelCount, devSampleRate);
234 }
235 return true;
236 }
237 }
238 return false;
239}
240
241bool AudioMixer::track_t::doesResample() const
242{
243 return resampler != 0;
244}
245
246inline
247void AudioMixer::track_t::adjustVolumeRamp()
248{
249 for (int i=0 ; i<2 ; i++) {
250 if (((volumeInc[i]>0) && ((prevVolume[i]>>16) >= volume[i])) ||
251 ((volumeInc[i]<0) && ((prevVolume[i]>>16) <= volume[i]))) {
252 volumeInc[i] = 0;
253 prevVolume[i] = volume[i]<<16;
254 }
255 }
256}
257
258
259status_t AudioMixer::setBufferProvider(AudioBufferProvider* buffer)
260{
261 mState.tracks[ mActiveTrack ].bufferProvider = buffer;
262 return NO_ERROR;
263}
264
265
266
267void AudioMixer::process(void* output)
268{
269 mState.hook(&mState, output);
270}
271
272
273void AudioMixer::process__validate(state_t* state, void* output)
274{
275 LOGW_IF(!state->needsChanged,
276 "in process__validate() but nothing's invalid");
277
278 uint32_t changed = state->needsChanged;
279 state->needsChanged = 0; // clear the validation flag
280
281 // recompute which tracks are enabled / disabled
282 uint32_t enabled = 0;
283 uint32_t disabled = 0;
284 while (changed) {
285 const int i = 31 - __builtin_clz(changed);
286 const uint32_t mask = 1<<i;
287 changed &= ~mask;
288 track_t& t = state->tracks[i];
289 (t.enabled ? enabled : disabled) |= mask;
290 }
291 state->enabledTracks &= ~disabled;
292 state->enabledTracks |= enabled;
293
294 // compute everything we need...
295 int countActiveTracks = 0;
296 int all16BitsStereoNoResample = 1;
297 int resampling = 0;
298 int volumeRamp = 0;
299 uint32_t en = state->enabledTracks;
300 while (en) {
301 const int i = 31 - __builtin_clz(en);
302 en &= ~(1<<i);
303
304 countActiveTracks++;
305 track_t& t = state->tracks[i];
306 uint32_t n = 0;
307 n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
308 n |= NEEDS_FORMAT_16;
309 n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED;
310
311 if (t.volumeInc[0]|t.volumeInc[1]) {
312 volumeRamp = 1;
313 } else if (!t.doesResample() && t.volumeRL == 0) {
314 n |= NEEDS_MUTE_ENABLED;
315 }
316 t.needs = n;
317
318 if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) {
319 t.hook = track__nop;
320 } else {
321 if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
322 all16BitsStereoNoResample = 0;
323 resampling = 1;
324 t.hook = track__genericResample;
325 } else {
326 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
327 t.hook = track__16BitsMono;
328 all16BitsStereoNoResample = 0;
329 }
330 if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_2){
331 t.hook = track__16BitsStereo;
332 }
333 }
334 }
335 }
336
337 // select the processing hooks
338 state->hook = process__nop;
339 if (countActiveTracks) {
340 if (resampling) {
341 if (!state->outputTemp) {
342 state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
343 }
344 if (!state->resampleTemp) {
345 state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
346 }
347 state->hook = process__genericResampling;
348 } else {
349 if (state->outputTemp) {
350 delete [] state->outputTemp;
351 state->outputTemp = 0;
352 }
353 if (state->resampleTemp) {
354 delete [] state->resampleTemp;
355 state->resampleTemp = 0;
356 }
357 state->hook = process__genericNoResampling;
358 if (all16BitsStereoNoResample && !volumeRamp) {
359 if (countActiveTracks == 1) {
360 state->hook = process__OneTrack16BitsStereoNoResampling;
361 }
362 }
363 }
364 }
365
366 LOGV("mixer configuration change: %d activeTracks (%08x) "
367 "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
368 countActiveTracks, state->enabledTracks,
369 all16BitsStereoNoResample, resampling, volumeRamp);
370
371 state->hook(state, output);
372
373 // Now that the volume ramp has been done, set optimal state and
374 // track hooks for subsequent mixer process
375 if (countActiveTracks) {
376 int allMuted = 1;
377 uint32_t en = state->enabledTracks;
378 while (en) {
379 const int i = 31 - __builtin_clz(en);
380 en &= ~(1<<i);
381 track_t& t = state->tracks[i];
382 if (!t.doesResample() && t.volumeRL == 0)
383 {
384 t.needs |= NEEDS_MUTE_ENABLED;
385 t.hook = track__nop;
386 } else {
387 allMuted = 0;
388 }
389 }
390 if (allMuted) {
391 state->hook = process__nop;
392 } else if (!resampling && all16BitsStereoNoResample) {
393 if (countActiveTracks == 1) {
394 state->hook = process__OneTrack16BitsStereoNoResampling;
395 }
396 }
397 }
398}
399
400static inline
401int32_t mulAdd(int16_t in, int16_t v, int32_t a)
402{
403#if defined(__arm__) && !defined(__thumb__)
404 int32_t out;
405 asm( "smlabb %[out], %[in], %[v], %[a] \n"
406 : [out]"=r"(out)
407 : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
408 : );
409 return out;
410#else
411 return a + in * int32_t(v);
412#endif
413}
414
415static inline
416int32_t mul(int16_t in, int16_t v)
417{
418#if defined(__arm__) && !defined(__thumb__)
419 int32_t out;
420 asm( "smulbb %[out], %[in], %[v] \n"
421 : [out]"=r"(out)
422 : [in]"%r"(in), [v]"r"(v)
423 : );
424 return out;
425#else
426 return in * int32_t(v);
427#endif
428}
429
430static inline
431int32_t mulAddRL(int left, uint32_t inRL, uint32_t vRL, int32_t a)
432{
433#if defined(__arm__) && !defined(__thumb__)
434 int32_t out;
435 if (left) {
436 asm( "smlabb %[out], %[inRL], %[vRL], %[a] \n"
437 : [out]"=r"(out)
438 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
439 : );
440 } else {
441 asm( "smlatt %[out], %[inRL], %[vRL], %[a] \n"
442 : [out]"=r"(out)
443 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
444 : );
445 }
446 return out;
447#else
448 if (left) {
449 return a + int16_t(inRL&0xFFFF) * int16_t(vRL&0xFFFF);
450 } else {
451 return a + int16_t(inRL>>16) * int16_t(vRL>>16);
452 }
453#endif
454}
455
456static inline
457int32_t mulRL(int left, uint32_t inRL, uint32_t vRL)
458{
459#if defined(__arm__) && !defined(__thumb__)
460 int32_t out;
461 if (left) {
462 asm( "smulbb %[out], %[inRL], %[vRL] \n"
463 : [out]"=r"(out)
464 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
465 : );
466 } else {
467 asm( "smultt %[out], %[inRL], %[vRL] \n"
468 : [out]"=r"(out)
469 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
470 : );
471 }
472 return out;
473#else
474 if (left) {
475 return int16_t(inRL&0xFFFF) * int16_t(vRL&0xFFFF);
476 } else {
477 return int16_t(inRL>>16) * int16_t(vRL>>16);
478 }
479#endif
480}
481
482
483void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp)
484{
485 t->resampler->setSampleRate(t->sampleRate);
486
487 // ramp gain - resample to temp buffer and scale/mix in 2nd step
488 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
489 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
490 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
491 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
492 volumeRampStereo(t, out, outFrameCount, temp);
493 }
494
495 // constant gain
496 else {
497 t->resampler->setVolume(t->volume[0], t->volume[1]);
498 t->resampler->resample(out, outFrameCount, t->bufferProvider);
499 }
500}
501
502void AudioMixer::track__nop(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp)
503{
504}
505
506void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp)
507{
508 int32_t vl = t->prevVolume[0];
509 int32_t vr = t->prevVolume[1];
510 const int32_t vlInc = t->volumeInc[0];
511 const int32_t vrInc = t->volumeInc[1];
512
513 //LOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
514 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
515 // (vl + vlInc*frameCount)/65536.0f, frameCount);
516
517 // ramp volume
518 do {
519 *out++ += (vl >> 16) * (*temp++ >> 12);
520 *out++ += (vr >> 16) * (*temp++ >> 12);
521 vl += vlInc;
522 vr += vrInc;
523 } while (--frameCount);
524
525 t->prevVolume[0] = vl;
526 t->prevVolume[1] = vr;
527 t->adjustVolumeRamp();
528}
529
530void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp)
531{
532 int16_t const *in = static_cast<int16_t const *>(t->in);
533
534 // ramp gain
535 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
536 int32_t vl = t->prevVolume[0];
537 int32_t vr = t->prevVolume[1];
538 const int32_t vlInc = t->volumeInc[0];
539 const int32_t vrInc = t->volumeInc[1];
540
541 // LOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
542 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
543 // (vl + vlInc*frameCount)/65536.0f, frameCount);
544
545 do {
546 *out++ += (vl >> 16) * (int32_t) *in++;
547 *out++ += (vr >> 16) * (int32_t) *in++;
548 vl += vlInc;
549 vr += vrInc;
550 } while (--frameCount);
551
552 t->prevVolume[0] = vl;
553 t->prevVolume[1] = vr;
554 t->adjustVolumeRamp();
555 }
556
557 // constant gain
558 else {
559 const uint32_t vrl = t->volumeRL;
560 do {
561 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
562 in += 2;
563 out[0] = mulAddRL(1, rl, vrl, out[0]);
564 out[1] = mulAddRL(0, rl, vrl, out[1]);
565 out += 2;
566 } while (--frameCount);
567 }
568 t->in = in;
569}
570
571void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, int32_t* temp)
572{
573 int16_t const *in = static_cast<int16_t const *>(t->in);
574
575 // ramp gain
576 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
577 int32_t vl = t->prevVolume[0];
578 int32_t vr = t->prevVolume[1];
579 const int32_t vlInc = t->volumeInc[0];
580 const int32_t vrInc = t->volumeInc[1];
581
582 // LOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
583 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
584 // (vl + vlInc*frameCount)/65536.0f, frameCount);
585
586 do {
587 int32_t l = *in++;
588 *out++ += (vl >> 16) * l;
589 *out++ += (vr >> 16) * l;
590 vl += vlInc;
591 vr += vrInc;
592 } while (--frameCount);
593
594 t->prevVolume[0] = vl;
595 t->prevVolume[1] = vr;
596 t->adjustVolumeRamp();
597 }
598 // constant gain
599 else {
600 const int16_t vl = t->volume[0];
601 const int16_t vr = t->volume[1];
602 do {
603 int16_t l = *in++;
604 out[0] = mulAdd(l, vl, out[0]);
605 out[1] = mulAdd(l, vr, out[1]);
606 out += 2;
607 } while (--frameCount);
608 }
609 t->in = in;
610}
611
612inline
613void AudioMixer::ditherAndClamp(int32_t* out, int32_t const *sums, size_t c)
614{
615 for (size_t i=0 ; i<c ; i++) {
616 int32_t l = *sums++;
617 int32_t r = *sums++;
618 int32_t nl = l >> 12;
619 int32_t nr = r >> 12;
620 l = clamp16(nl);
621 r = clamp16(nr);
622 *out++ = (r<<16) | (l & 0xFFFF);
623 }
624}
625
626// no-op case
627void AudioMixer::process__nop(state_t* state, void* output)
628{
629 // this assumes output 16 bits stereo, no resampling
630 memset(output, 0, state->frameCount*4);
631 uint32_t en = state->enabledTracks;
632 while (en) {
633 const int i = 31 - __builtin_clz(en);
634 en &= ~(1<<i);
635 track_t& t = state->tracks[i];
636 t.bufferProvider->getNextBuffer(&t.buffer);
637 if (t.buffer.raw) {
638 t.bufferProvider->releaseBuffer(&t.buffer);
639 }
640 }
641}
642
643// generic code without resampling
644void AudioMixer::process__genericNoResampling(state_t* state, void* output)
645{
646 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
647
648 // acquire each track's buffer
649 uint32_t enabledTracks = state->enabledTracks;
650 uint32_t en = enabledTracks;
651 while (en) {
652 const int i = 31 - __builtin_clz(en);
653 en &= ~(1<<i);
654 track_t& t = state->tracks[i];
655 t.bufferProvider->getNextBuffer(&t.buffer);
656 t.in = t.buffer.raw;
657 // t.in == NULL can happen if the track was flushed just after having
658 // been enabled for mixing.
659 if (t.in == NULL)
660 enabledTracks &= ~(1<<i);
661 }
662
663 // this assumes output 16 bits stereo, no resampling
664 int32_t* out = static_cast<int32_t*>(output);
665 size_t numFrames = state->frameCount;
666 do {
667 memset(outTemp, 0, sizeof(outTemp));
668
669 en = enabledTracks;
670 while (en) {
671 const int i = 31 - __builtin_clz(en);
672 en &= ~(1<<i);
673 track_t& t = state->tracks[i];
674 (t.hook)(&t, outTemp, BLOCKSIZE, state->resampleTemp);
675 }
676
677 ditherAndClamp(out, outTemp, BLOCKSIZE);
678 out += BLOCKSIZE;
679
680 numFrames -= BLOCKSIZE;
681 } while (numFrames);
682
683
684 // release each track's buffer
685 en = enabledTracks;
686 while (en) {
687 const int i = 31 - __builtin_clz(en);
688 en &= ~(1<<i);
689 track_t& t = state->tracks[i];
690 t.bufferProvider->releaseBuffer(&t.buffer);
691 }
692}
693
694// generic code with resampling
695void AudioMixer::process__genericResampling(state_t* state, void* output)
696{
697 int32_t* const outTemp = state->outputTemp;
698 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
699 memset(outTemp, 0, size);
700
701 int32_t* out = static_cast<int32_t*>(output);
702 size_t numFrames = state->frameCount;
703
704 uint32_t en = state->enabledTracks;
705 while (en) {
706 const int i = 31 - __builtin_clz(en);
707 en &= ~(1<<i);
708 track_t& t = state->tracks[i];
709
710 // this is a little goofy, on the resampling case we don't
711 // acquire/release the buffers because it's done by
712 // the resampler.
713 if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
714 (t.hook)(&t, outTemp, numFrames, state->resampleTemp);
715 } else {
716 t.bufferProvider->getNextBuffer(&t.buffer);
717 t.in = t.buffer.raw;
718 // t.in == NULL can happen if the track was flushed just after having
719 // been enabled for mixing.
720 if (t.in) {
721 (t.hook)(&t, outTemp, numFrames, state->resampleTemp);
722 t.bufferProvider->releaseBuffer(&t.buffer);
723 }
724 }
725 }
726
727 ditherAndClamp(out, outTemp, numFrames);
728}
729
730// one track, 16 bits stereo without resampling is the most common case
731void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state, void* output)
732{
733 const int i = 31 - __builtin_clz(state->enabledTracks);
734 const track_t& t = state->tracks[i];
735
736 AudioBufferProvider::Buffer& b(t.buffer);
737 t.bufferProvider->getNextBuffer(&b);
738 int16_t const *in = t.buffer.i16;
739
740 // in == NULL can happen if the track was flushed just after having
741 // been enabled for mixing.
742 if (in == NULL) {
743 memset(output, 0, state->frameCount*MAX_NUM_CHANNELS*sizeof(int16_t));
744 return;
745 }
746
747 int32_t* out = static_cast<int32_t*>(output);
748 size_t numFrames = state->frameCount;
749 const int16_t vl = t.volume[0];
750 const int16_t vr = t.volume[1];
751 const uint32_t vrl = t.volumeRL;
752 if (UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
753 // volume is boosted, so we might need to clamp even though
754 // we process only one track.
755 do {
756 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
757 in += 2;
758 int32_t l = mulRL(1, rl, vrl) >> 12;
759 int32_t r = mulRL(0, rl, vrl) >> 12;
760 // clamping...
761 l = clamp16(l);
762 r = clamp16(r);
763 *out++ = (r<<16) | (l & 0xFFFF);
764 } while (--numFrames);
765 } else {
766 do {
767 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
768 in += 2;
769 int32_t l = mulRL(1, rl, vrl) >> 12;
770 int32_t r = mulRL(0, rl, vrl) >> 12;
771 *out++ = (r<<16) | (l & 0xFFFF);
772 } while (--numFrames);
773 }
774
775 t.bufferProvider->releaseBuffer(&b);
776}
777
778// 2 tracks is also a common case
779void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state, void* output)
780{
781 int i;
782 uint32_t en = state->enabledTracks;
783
784 i = 31 - __builtin_clz(en);
785 const track_t& t0 = state->tracks[i];
786 AudioBufferProvider::Buffer& b0(t0.buffer);
787 t0.bufferProvider->getNextBuffer(&b0);
788
789 en &= ~(1<<i);
790 i = 31 - __builtin_clz(en);
791 const track_t& t1 = state->tracks[i];
792 AudioBufferProvider::Buffer& b1(t1.buffer);
793 t1.bufferProvider->getNextBuffer(&b1);
794
795 int16_t const *in0;
796 const int16_t vl0 = t0.volume[0];
797 const int16_t vr0 = t0.volume[1];
798 int16_t const *in1;
799 const int16_t vl1 = t1.volume[0];
800 const int16_t vr1 = t1.volume[1];
801 size_t numFrames = state->frameCount;
802 int32_t* out = static_cast<int32_t*>(output);
803
804 // t0/1.buffer.i16 == NULL can happen if the track was flushed just after having
805 // been enabled for mixing.
806 if (t0.buffer.i16 != NULL) {
807 in0 = t0.buffer.i16;
808 if (t1.buffer.i16 != NULL) {
809 in1 = t1.buffer.i16;
810 } else {
811 in1 = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
812 memset((void *)in1, 0, state->frameCount*MAX_NUM_CHANNELS*sizeof(int16_t));
813 }
814 } else {
815 in0 = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
816 memset((void *)in0, 0, state->frameCount*MAX_NUM_CHANNELS*sizeof(int16_t));
817 if (t1.buffer.i16 != NULL) {
818 in1 = t1.buffer.i16;
819 } else {
820 in1 = in0;
821 }
822 }
823
824 do {
825 int32_t l0 = *in0++;
826 int32_t r0 = *in0++;
827 l0 = mul(l0, vl0);
828 r0 = mul(r0, vr0);
829 int32_t l = *in1++;
830 int32_t r = *in1++;
831 l = mulAdd(l, vl1, l0) >> 12;
832 r = mulAdd(r, vr1, r0) >> 12;
833 // clamping...
834 l = clamp16(l);
835 r = clamp16(r);
836 *out++ = (r<<16) | (l & 0xFFFF);
837 } while (--numFrames);
838
839
840 if (t0.buffer.i16 != NULL) {
841 t0.bufferProvider->releaseBuffer(&b0);
842 if (t1.buffer.i16 != NULL) {
843 t1.bufferProvider->releaseBuffer(&b1);
844 } else {
845 delete [] in1;
846 }
847 } else {
848 delete [] in0;
849 if (t1.buffer.i16 != NULL) {
850 t1.bufferProvider->releaseBuffer(&b1);
851 }
852 }
853}
854
855// ----------------------------------------------------------------------------
856}; // namespace android
857