AudioMixer.cpp revision 3b81acab52b7140c1b8b20be2d67be3e221637e7
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 <assert.h>
22#include <stdint.h>
23#include <string.h>
24#include <stdlib.h>
25#include <sys/types.h>
26
27#include <utils/Errors.h>
28#include <utils/Log.h>
29
30#include <cutils/bitops.h>
31#include <cutils/compiler.h>
32
33#include <system/audio.h>
34
35#include <audio_utils/primitives.h>
36#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)
46    :   mTrackNames(0), mSampleRate(sampleRate)
47{
48    // AudioMixer is not yet capable of multi-channel beyond stereo
49    assert(2 == MAX_NUM_CHANNELS);
50
51    LocalClock lc;
52
53    mState.enabledTracks= 0;
54    mState.needsChanged = 0;
55    mState.frameCount   = frameCount;
56    mState.hook         = process__nop;
57    mState.outputTemp   = NULL;
58    mState.resampleTemp = NULL;
59    // mState.reserved
60    track_t* t = mState.tracks;
61    for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
62        t->needs = 0;
63        t->volume[0] = UNITY_GAIN;
64        t->volume[1] = UNITY_GAIN;
65        // no initialization needed
66        // t->prevVolume[0]
67        // t->prevVolume[1]
68        t->volumeInc[0] = 0;
69        t->volumeInc[1] = 0;
70        t->auxLevel = 0;
71        t->auxInc = 0;
72        // no initialization needed
73        // t->prevAuxLevel
74        // t->frameCount
75        t->channelCount = 2;
76        t->enabled = false;
77        t->format = 16;
78        t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
79        t->bufferProvider = NULL;
80        t->buffer.raw = NULL;
81        // t->buffer.frameCount
82        t->hook = NULL;
83        t->in = NULL;
84        t->resampler = NULL;
85        t->sampleRate = mSampleRate;
86        t->mainBuffer = NULL;
87        t->auxBuffer = NULL;
88        t->localTimeFreq = lc.getLocalFreq();
89        t++;
90    }
91}
92
93AudioMixer::~AudioMixer()
94{
95    track_t* t = mState.tracks;
96    for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
97        delete t->resampler;
98        t++;
99    }
100    delete [] mState.outputTemp;
101    delete [] mState.resampleTemp;
102}
103
104int AudioMixer::getTrackName()
105{
106    uint32_t names = ~mTrackNames;
107    if (names != 0) {
108        int n = __builtin_ctz(names);
109        ALOGV("add track (%d)", n);
110        mTrackNames |= 1 << n;
111        return TRACK0 + n;
112    }
113    return -1;
114}
115
116void AudioMixer::invalidateState(uint32_t mask)
117{
118    if (mask) {
119        mState.needsChanged |= mask;
120        mState.hook = process__validate;
121    }
122 }
123
124void AudioMixer::deleteTrackName(int name)
125{
126    name -= TRACK0;
127    assert(uint32_t(name) < MAX_NUM_TRACKS);
128    ALOGV("deleteTrackName(%d)", name);
129    track_t& track(mState.tracks[ name ]);
130    if (track.enabled) {
131        track.enabled = false;
132        invalidateState(1<<name);
133    }
134    if (track.resampler != NULL) {
135        // delete  the resampler
136        delete track.resampler;
137        track.resampler = NULL;
138        track.sampleRate = mSampleRate;
139        invalidateState(1<<name);
140    }
141    track.volumeInc[0] = 0;
142    track.volumeInc[1] = 0;
143    mTrackNames &= ~(1<<name);
144}
145
146void AudioMixer::enable(int name)
147{
148    name -= TRACK0;
149    assert(uint32_t(name) < MAX_NUM_TRACKS);
150    track_t& track = mState.tracks[name];
151
152    if (!track.enabled) {
153        track.enabled = true;
154        ALOGV("enable(%d)", name);
155        invalidateState(1 << name);
156    }
157}
158
159void AudioMixer::disable(int name)
160{
161    name -= TRACK0;
162    assert(uint32_t(name) < MAX_NUM_TRACKS);
163    track_t& track = mState.tracks[name];
164
165    if (track.enabled) {
166        track.enabled = false;
167        ALOGV("disable(%d)", name);
168        invalidateState(1 << name);
169    }
170}
171
172void AudioMixer::setParameter(int name, int target, int param, void *value)
173{
174    name -= TRACK0;
175    assert(uint32_t(name) < MAX_NUM_TRACKS);
176    track_t& track = mState.tracks[name];
177
178    int valueInt = (int)value;
179    int32_t *valueBuf = (int32_t *)value;
180
181    switch (target) {
182
183    case TRACK:
184        switch (param) {
185        case CHANNEL_MASK: {
186            uint32_t mask = (uint32_t)value;
187            if (track.channelMask != mask) {
188                uint8_t channelCount = popcount(mask);
189                assert((channelCount <= MAX_NUM_CHANNELS) && (channelCount));
190                track.channelMask = mask;
191                track.channelCount = channelCount;
192                ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
193                invalidateState(1 << name);
194            }
195            } break;
196        case MAIN_BUFFER:
197            if (track.mainBuffer != valueBuf) {
198                track.mainBuffer = valueBuf;
199                ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
200                invalidateState(1 << name);
201            }
202            break;
203        case AUX_BUFFER:
204            if (track.auxBuffer != valueBuf) {
205                track.auxBuffer = valueBuf;
206                ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
207                invalidateState(1 << name);
208            }
209            break;
210        default:
211            // bad param
212            assert(false);
213        }
214        break;
215
216    case RESAMPLE:
217        switch (param) {
218        case SAMPLE_RATE:
219            assert(valueInt > 0);
220            if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
221                ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
222                        uint32_t(valueInt));
223                invalidateState(1 << name);
224            }
225            break;
226        case RESET:
227            track.resetResampler();
228            invalidateState(1 << name);
229            break;
230        default:
231            // bad param
232            assert(false);
233        }
234        break;
235
236    case RAMP_VOLUME:
237    case VOLUME:
238        switch (param) {
239        case VOLUME0:
240        case VOLUME1:
241            if (track.volume[param-VOLUME0] != valueInt) {
242                ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
243                track.prevVolume[param-VOLUME0] = track.volume[param-VOLUME0] << 16;
244                track.volume[param-VOLUME0] = valueInt;
245                if (target == VOLUME) {
246                    track.prevVolume[param-VOLUME0] = valueInt << 16;
247                    track.volumeInc[param-VOLUME0] = 0;
248                } else {
249                    int32_t d = (valueInt<<16) - track.prevVolume[param-VOLUME0];
250                    int32_t volInc = d / int32_t(mState.frameCount);
251                    track.volumeInc[param-VOLUME0] = volInc;
252                    if (volInc == 0) {
253                        track.prevVolume[param-VOLUME0] = valueInt << 16;
254                    }
255                }
256                invalidateState(1 << name);
257            }
258            break;
259        case AUXLEVEL:
260            //assert(0 <= valueInt && valueInt <= MAX_GAIN_INT);
261            if (track.auxLevel != valueInt) {
262                ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
263                track.prevAuxLevel = track.auxLevel << 16;
264                track.auxLevel = valueInt;
265                if (target == VOLUME) {
266                    track.prevAuxLevel = valueInt << 16;
267                    track.auxInc = 0;
268                } else {
269                    int32_t d = (valueInt<<16) - track.prevAuxLevel;
270                    int32_t volInc = d / int32_t(mState.frameCount);
271                    track.auxInc = volInc;
272                    if (volInc == 0) {
273                        track.prevAuxLevel = valueInt << 16;
274                    }
275                }
276                invalidateState(1 << name);
277            }
278            break;
279        default:
280            // bad param
281            assert(false);
282        }
283        break;
284
285    default:
286        // bad target
287        assert(false);
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* buffer)
336{
337    name -= TRACK0;
338    assert(uint32_t(name) < MAX_NUM_TRACKS);
339    mState.tracks[name].bufferProvider = buffer;
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    //assert(0 != state->enabledTracks);
983    const int i = 31 - __builtin_clz(state->enabledTracks);
984    //assert((1 << i) == state->enabledTracks);
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