blob: 1200f75f13d8363be64f233355c8c06464f107e8 [file] [log] [blame]
Mathias Agopian65ab4712010-07-14 17:59:35 -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//#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
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070029#include <cutils/bitops.h>
30
31#include <system/audio.h>
32
Mathias Agopian65ab4712010-07-14 17:59:35 -070033#include "AudioMixer.h"
34
35namespace android {
36// ----------------------------------------------------------------------------
37
38static inline int16_t clamp16(int32_t sample)
39{
40 if ((sample>>15) ^ (sample>>31))
41 sample = 0x7FFF ^ (sample>>31);
42 return sample;
43}
44
45// ----------------------------------------------------------------------------
46
47AudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate)
48 : mActiveTrack(0), mTrackNames(0), mSampleRate(sampleRate)
49{
50 mState.enabledTracks= 0;
51 mState.needsChanged = 0;
52 mState.frameCount = frameCount;
53 mState.outputTemp = 0;
54 mState.resampleTemp = 0;
55 mState.hook = process__nop;
56 track_t* t = mState.tracks;
57 for (int i=0 ; i<32 ; i++) {
58 t->needs = 0;
59 t->volume[0] = UNITY_GAIN;
60 t->volume[1] = UNITY_GAIN;
61 t->volumeInc[0] = 0;
62 t->volumeInc[1] = 0;
63 t->auxLevel = 0;
64 t->auxInc = 0;
65 t->channelCount = 2;
66 t->enabled = 0;
67 t->format = 16;
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -070068 t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
Mathias Agopian65ab4712010-07-14 17:59:35 -070069 t->buffer.raw = 0;
70 t->bufferProvider = 0;
71 t->hook = 0;
72 t->resampler = 0;
73 t->sampleRate = mSampleRate;
74 t->in = 0;
75 t->mainBuffer = NULL;
76 t->auxBuffer = NULL;
77 t++;
78 }
79}
80
81 AudioMixer::~AudioMixer()
82 {
83 track_t* t = mState.tracks;
84 for (int i=0 ; i<32 ; i++) {
85 delete t->resampler;
86 t++;
87 }
88 delete [] mState.outputTemp;
89 delete [] mState.resampleTemp;
90 }
91
92 int AudioMixer::getTrackName()
93 {
94 uint32_t names = mTrackNames;
95 uint32_t mask = 1;
96 int n = 0;
97 while (names & mask) {
98 mask <<= 1;
99 n++;
100 }
101 if (mask) {
102 LOGV("add track (%d)", n);
103 mTrackNames |= mask;
104 return TRACK0 + n;
105 }
106 return -1;
107 }
108
109 void AudioMixer::invalidateState(uint32_t mask)
110 {
111 if (mask) {
112 mState.needsChanged |= mask;
113 mState.hook = process__validate;
114 }
115 }
116
117 void AudioMixer::deleteTrackName(int name)
118 {
119 name -= TRACK0;
120 if (uint32_t(name) < MAX_NUM_TRACKS) {
121 LOGV("deleteTrackName(%d)", name);
122 track_t& track(mState.tracks[ name ]);
123 if (track.enabled != 0) {
124 track.enabled = 0;
125 invalidateState(1<<name);
126 }
127 if (track.resampler) {
128 // delete the resampler
129 delete track.resampler;
130 track.resampler = 0;
131 track.sampleRate = mSampleRate;
132 invalidateState(1<<name);
133 }
134 track.volumeInc[0] = 0;
135 track.volumeInc[1] = 0;
136 mTrackNames &= ~(1<<name);
137 }
138 }
139
140status_t AudioMixer::enable(int name)
141{
142 switch (name) {
143 case MIXING: {
144 if (mState.tracks[ mActiveTrack ].enabled != 1) {
145 mState.tracks[ mActiveTrack ].enabled = 1;
146 LOGV("enable(%d)", mActiveTrack);
147 invalidateState(1<<mActiveTrack);
148 }
149 } break;
150 default:
151 return NAME_NOT_FOUND;
152 }
153 return NO_ERROR;
154}
155
156status_t AudioMixer::disable(int name)
157{
158 switch (name) {
159 case MIXING: {
160 if (mState.tracks[ mActiveTrack ].enabled != 0) {
161 mState.tracks[ mActiveTrack ].enabled = 0;
162 LOGV("disable(%d)", mActiveTrack);
163 invalidateState(1<<mActiveTrack);
164 }
165 } break;
166 default:
167 return NAME_NOT_FOUND;
168 }
169 return NO_ERROR;
170}
171
172status_t AudioMixer::setActiveTrack(int track)
173{
174 if (uint32_t(track-TRACK0) >= MAX_NUM_TRACKS) {
175 return BAD_VALUE;
176 }
177 mActiveTrack = track - TRACK0;
178 return NO_ERROR;
179}
180
181status_t AudioMixer::setParameter(int target, int name, void *value)
182{
183 int valueInt = (int)value;
184 int32_t *valueBuf = (int32_t *)value;
185
186 switch (target) {
187 case TRACK:
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700188 if (name == CHANNEL_MASK) {
189 uint32_t mask = (uint32_t)value;
190 if (mState.tracks[ mActiveTrack ].channelMask != mask) {
191 uint8_t channelCount = popcount(mask);
192 if ((channelCount <= MAX_NUM_CHANNELS) && (channelCount)) {
193 mState.tracks[ mActiveTrack ].channelMask = mask;
194 mState.tracks[ mActiveTrack ].channelCount = channelCount;
195 LOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
Mathias Agopian65ab4712010-07-14 17:59:35 -0700196 invalidateState(1<<mActiveTrack);
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700197 return NO_ERROR;
Mathias Agopian65ab4712010-07-14 17:59:35 -0700198 }
Jean-Michel Trivi0d255b22011-05-24 15:53:33 -0700199 } else {
Mathias Agopian65ab4712010-07-14 17:59:35 -0700200 return NO_ERROR;
201 }
202 }
203 if (name == MAIN_BUFFER) {
204 if (mState.tracks[ mActiveTrack ].mainBuffer != valueBuf) {
205 mState.tracks[ mActiveTrack ].mainBuffer = valueBuf;
206 LOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
207 invalidateState(1<<mActiveTrack);
208 }
209 return NO_ERROR;
210 }
211 if (name == AUX_BUFFER) {
212 if (mState.tracks[ mActiveTrack ].auxBuffer != valueBuf) {
213 mState.tracks[ mActiveTrack ].auxBuffer = valueBuf;
214 LOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
215 invalidateState(1<<mActiveTrack);
216 }
217 return NO_ERROR;
218 }
219
220 break;
221 case RESAMPLE:
222 if (name == SAMPLE_RATE) {
223 if (valueInt > 0) {
224 track_t& track = mState.tracks[ mActiveTrack ];
225 if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
226 LOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
227 uint32_t(valueInt));
228 invalidateState(1<<mActiveTrack);
229 }
230 return NO_ERROR;
231 }
232 }
Eric Laurent243f5f92011-02-28 16:52:51 -0800233 if (name == RESET) {
234 track_t& track = mState.tracks[ mActiveTrack ];
235 track.resetResampler();
236 invalidateState(1<<mActiveTrack);
237 return NO_ERROR;
238 }
Mathias Agopian65ab4712010-07-14 17:59:35 -0700239 break;
240 case RAMP_VOLUME:
241 case VOLUME:
242 if ((uint32_t(name-VOLUME0) < MAX_NUM_CHANNELS)) {
243 track_t& track = mState.tracks[ mActiveTrack ];
244 if (track.volume[name-VOLUME0] != valueInt) {
245 LOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
246 track.prevVolume[name-VOLUME0] = track.volume[name-VOLUME0] << 16;
247 track.volume[name-VOLUME0] = valueInt;
248 if (target == VOLUME) {
249 track.prevVolume[name-VOLUME0] = valueInt << 16;
250 track.volumeInc[name-VOLUME0] = 0;
251 } else {
252 int32_t d = (valueInt<<16) - track.prevVolume[name-VOLUME0];
253 int32_t volInc = d / int32_t(mState.frameCount);
254 track.volumeInc[name-VOLUME0] = volInc;
255 if (volInc == 0) {
256 track.prevVolume[name-VOLUME0] = valueInt << 16;
257 }
258 }
259 invalidateState(1<<mActiveTrack);
260 }
261 return NO_ERROR;
262 } else if (name == AUXLEVEL) {
263 track_t& track = mState.tracks[ mActiveTrack ];
264 if (track.auxLevel != valueInt) {
265 LOGV("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<<mActiveTrack);
280 }
281 return NO_ERROR;
282 }
283 break;
284 }
285 return BAD_VALUE;
286}
287
288bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
289{
290 if (value!=devSampleRate || resampler) {
291 if (sampleRate != value) {
292 sampleRate = value;
293 if (resampler == 0) {
294 resampler = AudioResampler::create(
295 format, channelCount, devSampleRate);
296 }
297 return true;
298 }
299 }
300 return false;
301}
302
303bool AudioMixer::track_t::doesResample() const
304{
305 return resampler != 0;
306}
307
Eric Laurent243f5f92011-02-28 16:52:51 -0800308void AudioMixer::track_t::resetResampler()
309{
310 if (resampler != 0) {
311 resampler->reset();
312 }
313}
314
Mathias Agopian65ab4712010-07-14 17:59:35 -0700315inline
316void AudioMixer::track_t::adjustVolumeRamp(bool aux)
317{
318 for (int i=0 ; i<2 ; i++) {
319 if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
320 ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
321 volumeInc[i] = 0;
322 prevVolume[i] = volume[i]<<16;
323 }
324 }
325 if (aux) {
326 if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
327 ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
328 auxInc = 0;
329 prevAuxLevel = auxLevel<<16;
330 }
331 }
332}
333
Eric Laurent071ccd52011-12-22 16:08:41 -0800334size_t AudioMixer::track_t::getUnreleasedFrames()
335{
336 if (resampler != NULL) {
337 return resampler->getUnreleasedFrames();
338 }
339 return 0;
340}
341
342size_t AudioMixer::getUnreleasedFrames(int name)
343{
344 name -= TRACK0;
345 if (uint32_t(name) < MAX_NUM_TRACKS) {
346 track_t& track(mState.tracks[name]);
347 return track.getUnreleasedFrames();
348 }
349 return 0;
350}
Mathias Agopian65ab4712010-07-14 17:59:35 -0700351
352status_t AudioMixer::setBufferProvider(AudioBufferProvider* buffer)
353{
354 mState.tracks[ mActiveTrack ].bufferProvider = buffer;
355 return NO_ERROR;
356}
357
358
359
360void AudioMixer::process()
361{
362 mState.hook(&mState);
363}
364
365
366void AudioMixer::process__validate(state_t* state)
367{
368 LOGW_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 = 0;
451 }
452 if (state->resampleTemp) {
453 delete [] state->resampleTemp;
454 state->resampleTemp = 0;
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 LOGV("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
499static inline
500int32_t mulAdd(int16_t in, int16_t v, int32_t a)
501{
502#if defined(__arm__) && !defined(__thumb__)
503 int32_t out;
504 asm( "smlabb %[out], %[in], %[v], %[a] \n"
505 : [out]"=r"(out)
506 : [in]"%r"(in), [v]"r"(v), [a]"r"(a)
507 : );
508 return out;
509#else
510 return a + in * int32_t(v);
511#endif
512}
513
514static inline
515int32_t mul(int16_t in, int16_t v)
516{
517#if defined(__arm__) && !defined(__thumb__)
518 int32_t out;
519 asm( "smulbb %[out], %[in], %[v] \n"
520 : [out]"=r"(out)
521 : [in]"%r"(in), [v]"r"(v)
522 : );
523 return out;
524#else
525 return in * int32_t(v);
526#endif
527}
528
529static inline
530int32_t mulAddRL(int left, uint32_t inRL, uint32_t vRL, int32_t a)
531{
532#if defined(__arm__) && !defined(__thumb__)
533 int32_t out;
534 if (left) {
535 asm( "smlabb %[out], %[inRL], %[vRL], %[a] \n"
536 : [out]"=r"(out)
537 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
538 : );
539 } else {
540 asm( "smlatt %[out], %[inRL], %[vRL], %[a] \n"
541 : [out]"=r"(out)
542 : [inRL]"%r"(inRL), [vRL]"r"(vRL), [a]"r"(a)
543 : );
544 }
545 return out;
546#else
547 if (left) {
548 return a + int16_t(inRL&0xFFFF) * int16_t(vRL&0xFFFF);
549 } else {
550 return a + int16_t(inRL>>16) * int16_t(vRL>>16);
551 }
552#endif
553}
554
555static inline
556int32_t mulRL(int left, uint32_t inRL, uint32_t vRL)
557{
558#if defined(__arm__) && !defined(__thumb__)
559 int32_t out;
560 if (left) {
561 asm( "smulbb %[out], %[inRL], %[vRL] \n"
562 : [out]"=r"(out)
563 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
564 : );
565 } else {
566 asm( "smultt %[out], %[inRL], %[vRL] \n"
567 : [out]"=r"(out)
568 : [inRL]"%r"(inRL), [vRL]"r"(vRL)
569 : );
570 }
571 return out;
572#else
573 if (left) {
574 return int16_t(inRL&0xFFFF) * int16_t(vRL&0xFFFF);
575 } else {
576 return int16_t(inRL>>16) * int16_t(vRL>>16);
577 }
578#endif
579}
580
581
582void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
583{
584 t->resampler->setSampleRate(t->sampleRate);
585
586 // ramp gain - resample to temp buffer and scale/mix in 2nd step
587 if (aux != NULL) {
588 // always resample with unity gain when sending to auxiliary buffer to be able
589 // to apply send level after resampling
590 // TODO: modify each resampler to support aux channel?
591 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
592 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
593 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
594 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc) {
595 volumeRampStereo(t, out, outFrameCount, temp, aux);
596 } else {
597 volumeStereo(t, out, outFrameCount, temp, aux);
598 }
599 } else {
600 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
601 t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
602 memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
603 t->resampler->resample(temp, outFrameCount, t->bufferProvider);
604 volumeRampStereo(t, out, outFrameCount, temp, aux);
605 }
606
607 // constant gain
608 else {
609 t->resampler->setVolume(t->volume[0], t->volume[1]);
610 t->resampler->resample(out, outFrameCount, t->bufferProvider);
611 }
612 }
613}
614
615void AudioMixer::track__nop(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
616{
617}
618
619void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
620{
621 int32_t vl = t->prevVolume[0];
622 int32_t vr = t->prevVolume[1];
623 const int32_t vlInc = t->volumeInc[0];
624 const int32_t vrInc = t->volumeInc[1];
625
626 //LOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
627 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
628 // (vl + vlInc*frameCount)/65536.0f, frameCount);
629
630 // ramp volume
631 if UNLIKELY(aux != NULL) {
632 int32_t va = t->prevAuxLevel;
633 const int32_t vaInc = t->auxInc;
634 int32_t l;
635 int32_t r;
636
637 do {
638 l = (*temp++ >> 12);
639 r = (*temp++ >> 12);
640 *out++ += (vl >> 16) * l;
641 *out++ += (vr >> 16) * r;
642 *aux++ += (va >> 17) * (l + r);
643 vl += vlInc;
644 vr += vrInc;
645 va += vaInc;
646 } while (--frameCount);
647 t->prevAuxLevel = va;
648 } else {
649 do {
650 *out++ += (vl >> 16) * (*temp++ >> 12);
651 *out++ += (vr >> 16) * (*temp++ >> 12);
652 vl += vlInc;
653 vr += vrInc;
654 } while (--frameCount);
655 }
656 t->prevVolume[0] = vl;
657 t->prevVolume[1] = vr;
658 t->adjustVolumeRamp((aux != NULL));
659}
660
661void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
662{
663 const int16_t vl = t->volume[0];
664 const int16_t vr = t->volume[1];
665
666 if UNLIKELY(aux != NULL) {
667 const int16_t va = (int16_t)t->auxLevel;
668 do {
669 int16_t l = (int16_t)(*temp++ >> 12);
670 int16_t r = (int16_t)(*temp++ >> 12);
671 out[0] = mulAdd(l, vl, out[0]);
672 int16_t a = (int16_t)(((int32_t)l + r) >> 1);
673 out[1] = mulAdd(r, vr, out[1]);
674 out += 2;
675 aux[0] = mulAdd(a, va, aux[0]);
676 aux++;
677 } while (--frameCount);
678 } else {
679 do {
680 int16_t l = (int16_t)(*temp++ >> 12);
681 int16_t r = (int16_t)(*temp++ >> 12);
682 out[0] = mulAdd(l, vl, out[0]);
683 out[1] = mulAdd(r, vr, out[1]);
684 out += 2;
685 } while (--frameCount);
686 }
687}
688
689void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
690{
691 int16_t const *in = static_cast<int16_t const *>(t->in);
692
693 if UNLIKELY(aux != NULL) {
694 int32_t l;
695 int32_t r;
696 // ramp gain
697 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc) {
698 int32_t vl = t->prevVolume[0];
699 int32_t vr = t->prevVolume[1];
700 int32_t va = t->prevAuxLevel;
701 const int32_t vlInc = t->volumeInc[0];
702 const int32_t vrInc = t->volumeInc[1];
703 const int32_t vaInc = t->auxInc;
704 // LOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
705 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
706 // (vl + vlInc*frameCount)/65536.0f, frameCount);
707
708 do {
709 l = (int32_t)*in++;
710 r = (int32_t)*in++;
711 *out++ += (vl >> 16) * l;
712 *out++ += (vr >> 16) * r;
713 *aux++ += (va >> 17) * (l + r);
714 vl += vlInc;
715 vr += vrInc;
716 va += vaInc;
717 } while (--frameCount);
718
719 t->prevVolume[0] = vl;
720 t->prevVolume[1] = vr;
721 t->prevAuxLevel = va;
722 t->adjustVolumeRamp(true);
723 }
724
725 // constant gain
726 else {
727 const uint32_t vrl = t->volumeRL;
728 const int16_t va = (int16_t)t->auxLevel;
729 do {
730 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
731 int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
732 in += 2;
733 out[0] = mulAddRL(1, rl, vrl, out[0]);
734 out[1] = mulAddRL(0, rl, vrl, out[1]);
735 out += 2;
736 aux[0] = mulAdd(a, va, aux[0]);
737 aux++;
738 } while (--frameCount);
739 }
740 } else {
741 // ramp gain
742 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
743 int32_t vl = t->prevVolume[0];
744 int32_t vr = t->prevVolume[1];
745 const int32_t vlInc = t->volumeInc[0];
746 const int32_t vrInc = t->volumeInc[1];
747
748 // LOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
749 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
750 // (vl + vlInc*frameCount)/65536.0f, frameCount);
751
752 do {
753 *out++ += (vl >> 16) * (int32_t) *in++;
754 *out++ += (vr >> 16) * (int32_t) *in++;
755 vl += vlInc;
756 vr += vrInc;
757 } while (--frameCount);
758
759 t->prevVolume[0] = vl;
760 t->prevVolume[1] = vr;
761 t->adjustVolumeRamp(false);
762 }
763
764 // constant gain
765 else {
766 const uint32_t vrl = t->volumeRL;
767 do {
768 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
769 in += 2;
770 out[0] = mulAddRL(1, rl, vrl, out[0]);
771 out[1] = mulAddRL(0, rl, vrl, out[1]);
772 out += 2;
773 } while (--frameCount);
774 }
775 }
776 t->in = in;
777}
778
779void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
780{
781 int16_t const *in = static_cast<int16_t const *>(t->in);
782
783 if UNLIKELY(aux != NULL) {
784 // ramp gain
785 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc) {
786 int32_t vl = t->prevVolume[0];
787 int32_t vr = t->prevVolume[1];
788 int32_t va = t->prevAuxLevel;
789 const int32_t vlInc = t->volumeInc[0];
790 const int32_t vrInc = t->volumeInc[1];
791 const int32_t vaInc = t->auxInc;
792
793 // LOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
794 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
795 // (vl + vlInc*frameCount)/65536.0f, frameCount);
796
797 do {
798 int32_t l = *in++;
799 *out++ += (vl >> 16) * l;
800 *out++ += (vr >> 16) * l;
801 *aux++ += (va >> 16) * l;
802 vl += vlInc;
803 vr += vrInc;
804 va += vaInc;
805 } while (--frameCount);
806
807 t->prevVolume[0] = vl;
808 t->prevVolume[1] = vr;
809 t->prevAuxLevel = va;
810 t->adjustVolumeRamp(true);
811 }
812 // constant gain
813 else {
814 const int16_t vl = t->volume[0];
815 const int16_t vr = t->volume[1];
816 const int16_t va = (int16_t)t->auxLevel;
817 do {
818 int16_t l = *in++;
819 out[0] = mulAdd(l, vl, out[0]);
820 out[1] = mulAdd(l, vr, out[1]);
821 out += 2;
822 aux[0] = mulAdd(l, va, aux[0]);
823 aux++;
824 } while (--frameCount);
825 }
826 } else {
827 // ramp gain
828 if UNLIKELY(t->volumeInc[0]|t->volumeInc[1]) {
829 int32_t vl = t->prevVolume[0];
830 int32_t vr = t->prevVolume[1];
831 const int32_t vlInc = t->volumeInc[0];
832 const int32_t vrInc = t->volumeInc[1];
833
834 // LOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
835 // t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
836 // (vl + vlInc*frameCount)/65536.0f, frameCount);
837
838 do {
839 int32_t l = *in++;
840 *out++ += (vl >> 16) * l;
841 *out++ += (vr >> 16) * l;
842 vl += vlInc;
843 vr += vrInc;
844 } while (--frameCount);
845
846 t->prevVolume[0] = vl;
847 t->prevVolume[1] = vr;
848 t->adjustVolumeRamp(false);
849 }
850 // constant gain
851 else {
852 const int16_t vl = t->volume[0];
853 const int16_t vr = t->volume[1];
854 do {
855 int16_t l = *in++;
856 out[0] = mulAdd(l, vl, out[0]);
857 out[1] = mulAdd(l, vr, out[1]);
858 out += 2;
859 } while (--frameCount);
860 }
861 }
862 t->in = in;
863}
864
865void AudioMixer::ditherAndClamp(int32_t* out, int32_t const *sums, size_t c)
866{
867 for (size_t i=0 ; i<c ; i++) {
868 int32_t l = *sums++;
869 int32_t r = *sums++;
870 int32_t nl = l >> 12;
871 int32_t nr = r >> 12;
872 l = clamp16(nl);
873 r = clamp16(nr);
874 *out++ = (r<<16) | (l & 0xFFFF);
875 }
876}
877
878// no-op case
879void AudioMixer::process__nop(state_t* state)
880{
881 uint32_t e0 = state->enabledTracks;
882 size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS;
883 while (e0) {
884 // process by group of tracks with same output buffer to
885 // avoid multiple memset() on same buffer
886 uint32_t e1 = e0, e2 = e0;
887 int i = 31 - __builtin_clz(e1);
888 track_t& t1 = state->tracks[i];
889 e2 &= ~(1<<i);
890 while (e2) {
891 i = 31 - __builtin_clz(e2);
892 e2 &= ~(1<<i);
893 track_t& t2 = state->tracks[i];
894 if UNLIKELY(t2.mainBuffer != t1.mainBuffer) {
895 e1 &= ~(1<<i);
896 }
897 }
898 e0 &= ~(e1);
899
900 memset(t1.mainBuffer, 0, bufSize);
901
902 while (e1) {
903 i = 31 - __builtin_clz(e1);
904 e1 &= ~(1<<i);
905 t1 = state->tracks[i];
906 size_t outFrames = state->frameCount;
907 while (outFrames) {
908 t1.buffer.frameCount = outFrames;
909 t1.bufferProvider->getNextBuffer(&t1.buffer);
910 if (!t1.buffer.raw) break;
911 outFrames -= t1.buffer.frameCount;
912 t1.bufferProvider->releaseBuffer(&t1.buffer);
913 }
914 }
915 }
916}
917
918// generic code without resampling
919void AudioMixer::process__genericNoResampling(state_t* state)
920{
921 int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
922
923 // acquire each track's buffer
924 uint32_t enabledTracks = state->enabledTracks;
925 uint32_t e0 = enabledTracks;
926 while (e0) {
927 const int i = 31 - __builtin_clz(e0);
928 e0 &= ~(1<<i);
929 track_t& t = state->tracks[i];
930 t.buffer.frameCount = state->frameCount;
931 t.bufferProvider->getNextBuffer(&t.buffer);
932 t.frameCount = t.buffer.frameCount;
933 t.in = t.buffer.raw;
934 // t.in == NULL can happen if the track was flushed just after having
935 // been enabled for mixing.
936 if (t.in == NULL)
937 enabledTracks &= ~(1<<i);
938 }
939
940 e0 = enabledTracks;
941 while (e0) {
942 // process by group of tracks with same output buffer to
943 // optimize cache use
944 uint32_t e1 = e0, e2 = e0;
945 int j = 31 - __builtin_clz(e1);
946 track_t& t1 = state->tracks[j];
947 e2 &= ~(1<<j);
948 while (e2) {
949 j = 31 - __builtin_clz(e2);
950 e2 &= ~(1<<j);
951 track_t& t2 = state->tracks[j];
952 if UNLIKELY(t2.mainBuffer != t1.mainBuffer) {
953 e1 &= ~(1<<j);
954 }
955 }
956 e0 &= ~(e1);
957 // this assumes output 16 bits stereo, no resampling
958 int32_t *out = t1.mainBuffer;
959 size_t numFrames = 0;
960 do {
961 memset(outTemp, 0, sizeof(outTemp));
962 e2 = e1;
963 while (e2) {
964 const int i = 31 - __builtin_clz(e2);
965 e2 &= ~(1<<i);
966 track_t& t = state->tracks[i];
967 size_t outFrames = BLOCKSIZE;
968 int32_t *aux = NULL;
969 if UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
970 aux = t.auxBuffer + numFrames;
971 }
972 while (outFrames) {
973 size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
974 if (inFrames) {
975 (t.hook)(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, state->resampleTemp, aux);
976 t.frameCount -= inFrames;
977 outFrames -= inFrames;
978 if UNLIKELY(aux != NULL) {
979 aux += inFrames;
980 }
981 }
982 if (t.frameCount == 0 && outFrames) {
983 t.bufferProvider->releaseBuffer(&t.buffer);
984 t.buffer.frameCount = (state->frameCount - numFrames) - (BLOCKSIZE - outFrames);
985 t.bufferProvider->getNextBuffer(&t.buffer);
986 t.in = t.buffer.raw;
987 if (t.in == NULL) {
988 enabledTracks &= ~(1<<i);
989 e1 &= ~(1<<i);
990 break;
991 }
992 t.frameCount = t.buffer.frameCount;
993 }
994 }
995 }
996 ditherAndClamp(out, outTemp, BLOCKSIZE);
997 out += BLOCKSIZE;
998 numFrames += BLOCKSIZE;
999 } while (numFrames < state->frameCount);
1000 }
1001
1002 // release each track's buffer
1003 e0 = enabledTracks;
1004 while (e0) {
1005 const int i = 31 - __builtin_clz(e0);
1006 e0 &= ~(1<<i);
1007 track_t& t = state->tracks[i];
1008 t.bufferProvider->releaseBuffer(&t.buffer);
1009 }
1010}
1011
1012
1013 // generic code with resampling
1014void AudioMixer::process__genericResampling(state_t* state)
1015{
1016 int32_t* const outTemp = state->outputTemp;
1017 const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
Mathias Agopian65ab4712010-07-14 17:59:35 -07001018
1019 size_t numFrames = state->frameCount;
1020
1021 uint32_t e0 = state->enabledTracks;
1022 while (e0) {
1023 // process by group of tracks with same output buffer
1024 // to optimize cache use
1025 uint32_t e1 = e0, e2 = e0;
1026 int j = 31 - __builtin_clz(e1);
1027 track_t& t1 = state->tracks[j];
1028 e2 &= ~(1<<j);
1029 while (e2) {
1030 j = 31 - __builtin_clz(e2);
1031 e2 &= ~(1<<j);
1032 track_t& t2 = state->tracks[j];
1033 if UNLIKELY(t2.mainBuffer != t1.mainBuffer) {
1034 e1 &= ~(1<<j);
1035 }
1036 }
1037 e0 &= ~(e1);
1038 int32_t *out = t1.mainBuffer;
Yuuhi Yamaguchi2151d7b2011-02-04 15:24:34 +01001039 memset(outTemp, 0, size);
Mathias Agopian65ab4712010-07-14 17:59:35 -07001040 while (e1) {
1041 const int i = 31 - __builtin_clz(e1);
1042 e1 &= ~(1<<i);
1043 track_t& t = state->tracks[i];
1044 int32_t *aux = NULL;
1045 if UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
1046 aux = t.auxBuffer;
1047 }
1048
1049 // this is a little goofy, on the resampling case we don't
1050 // acquire/release the buffers because it's done by
1051 // the resampler.
1052 if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
1053 (t.hook)(&t, outTemp, numFrames, state->resampleTemp, aux);
1054 } else {
1055
1056 size_t outFrames = 0;
1057
1058 while (outFrames < numFrames) {
1059 t.buffer.frameCount = numFrames - outFrames;
1060 t.bufferProvider->getNextBuffer(&t.buffer);
1061 t.in = t.buffer.raw;
1062 // t.in == NULL can happen if the track was flushed just after having
1063 // been enabled for mixing.
1064 if (t.in == NULL) break;
1065
1066 if UNLIKELY(aux != NULL) {
1067 aux += outFrames;
1068 }
1069 (t.hook)(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, state->resampleTemp, aux);
1070 outFrames += t.buffer.frameCount;
1071 t.bufferProvider->releaseBuffer(&t.buffer);
1072 }
1073 }
1074 }
1075 ditherAndClamp(out, outTemp, numFrames);
1076 }
1077}
1078
1079// one track, 16 bits stereo without resampling is the most common case
1080void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state)
1081{
1082 const int i = 31 - __builtin_clz(state->enabledTracks);
1083 const track_t& t = state->tracks[i];
1084
1085 AudioBufferProvider::Buffer& b(t.buffer);
1086
1087 int32_t* out = t.mainBuffer;
1088 size_t numFrames = state->frameCount;
1089
1090 const int16_t vl = t.volume[0];
1091 const int16_t vr = t.volume[1];
1092 const uint32_t vrl = t.volumeRL;
1093 while (numFrames) {
1094 b.frameCount = numFrames;
1095 t.bufferProvider->getNextBuffer(&b);
1096 int16_t const *in = b.i16;
1097
1098 // in == NULL can happen if the track was flushed just after having
1099 // been enabled for mixing.
1100 if (in == NULL || ((unsigned long)in & 3)) {
1101 memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t));
1102 LOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: buffer %p track %d, channels %d, needs %08x",
1103 in, i, t.channelCount, t.needs);
1104 return;
1105 }
1106 size_t outFrames = b.frameCount;
1107
1108 if (UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
1109 // volume is boosted, so we might need to clamp even though
1110 // we process only one track.
1111 do {
1112 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
1113 in += 2;
1114 int32_t l = mulRL(1, rl, vrl) >> 12;
1115 int32_t r = mulRL(0, rl, vrl) >> 12;
1116 // clamping...
1117 l = clamp16(l);
1118 r = clamp16(r);
1119 *out++ = (r<<16) | (l & 0xFFFF);
1120 } while (--outFrames);
1121 } else {
1122 do {
1123 uint32_t rl = *reinterpret_cast<uint32_t const *>(in);
1124 in += 2;
1125 int32_t l = mulRL(1, rl, vrl) >> 12;
1126 int32_t r = mulRL(0, rl, vrl) >> 12;
1127 *out++ = (r<<16) | (l & 0xFFFF);
1128 } while (--outFrames);
1129 }
1130 numFrames -= b.frameCount;
1131 t.bufferProvider->releaseBuffer(&b);
1132 }
1133}
1134
1135// 2 tracks is also a common case
1136// NEVER used in current implementation of process__validate()
1137// only use if the 2 tracks have the same output buffer
1138void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state)
1139{
1140 int i;
1141 uint32_t en = state->enabledTracks;
1142
1143 i = 31 - __builtin_clz(en);
1144 const track_t& t0 = state->tracks[i];
1145 AudioBufferProvider::Buffer& b0(t0.buffer);
1146
1147 en &= ~(1<<i);
1148 i = 31 - __builtin_clz(en);
1149 const track_t& t1 = state->tracks[i];
1150 AudioBufferProvider::Buffer& b1(t1.buffer);
1151
1152 int16_t const *in0;
1153 const int16_t vl0 = t0.volume[0];
1154 const int16_t vr0 = t0.volume[1];
1155 size_t frameCount0 = 0;
1156
1157 int16_t const *in1;
1158 const int16_t vl1 = t1.volume[0];
1159 const int16_t vr1 = t1.volume[1];
1160 size_t frameCount1 = 0;
1161
1162 //FIXME: only works if two tracks use same buffer
1163 int32_t* out = t0.mainBuffer;
1164 size_t numFrames = state->frameCount;
1165 int16_t const *buff = NULL;
1166
1167
1168 while (numFrames) {
1169
1170 if (frameCount0 == 0) {
1171 b0.frameCount = numFrames;
1172 t0.bufferProvider->getNextBuffer(&b0);
1173 if (b0.i16 == NULL) {
1174 if (buff == NULL) {
1175 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1176 }
1177 in0 = buff;
1178 b0.frameCount = numFrames;
1179 } else {
1180 in0 = b0.i16;
1181 }
1182 frameCount0 = b0.frameCount;
1183 }
1184 if (frameCount1 == 0) {
1185 b1.frameCount = numFrames;
1186 t1.bufferProvider->getNextBuffer(&b1);
1187 if (b1.i16 == NULL) {
1188 if (buff == NULL) {
1189 buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1190 }
1191 in1 = buff;
1192 b1.frameCount = numFrames;
1193 } else {
1194 in1 = b1.i16;
1195 }
1196 frameCount1 = b1.frameCount;
1197 }
1198
1199 size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1;
1200
1201 numFrames -= outFrames;
1202 frameCount0 -= outFrames;
1203 frameCount1 -= outFrames;
1204
1205 do {
1206 int32_t l0 = *in0++;
1207 int32_t r0 = *in0++;
1208 l0 = mul(l0, vl0);
1209 r0 = mul(r0, vr0);
1210 int32_t l = *in1++;
1211 int32_t r = *in1++;
1212 l = mulAdd(l, vl1, l0) >> 12;
1213 r = mulAdd(r, vr1, r0) >> 12;
1214 // clamping...
1215 l = clamp16(l);
1216 r = clamp16(r);
1217 *out++ = (r<<16) | (l & 0xFFFF);
1218 } while (--outFrames);
1219
1220 if (frameCount0 == 0) {
1221 t0.bufferProvider->releaseBuffer(&b0);
1222 }
1223 if (frameCount1 == 0) {
1224 t1.bufferProvider->releaseBuffer(&b1);
1225 }
1226 }
1227
1228 if (buff != NULL) {
1229 delete [] buff;
1230 }
1231}
1232
1233// ----------------------------------------------------------------------------
1234}; // namespace android
1235