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