AudioMixer.cpp revision 4ff14bae91075eb274eb1c2975982358946e7e63
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            if (track.auxLevel != valueInt) {
261                ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
262                track.prevAuxLevel = track.auxLevel << 16;
263                track.auxLevel = valueInt;
264                if (target == VOLUME) {
265                    track.prevAuxLevel = valueInt << 16;
266                    track.auxInc = 0;
267                } else {
268                    int32_t d = (valueInt<<16) - track.prevAuxLevel;
269                    int32_t volInc = d / int32_t(mState.frameCount);
270                    track.auxInc = volInc;
271                    if (volInc == 0) {
272                        track.prevAuxLevel = valueInt << 16;
273                    }
274                }
275                invalidateState(1 << name);
276            }
277            break;
278        default:
279            // bad param
280            assert(false);
281        }
282        break;
283
284    default:
285        // bad target
286        assert(false);
287    }
288}
289
290bool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
291{
292    if (value!=devSampleRate || resampler) {
293        if (sampleRate != value) {
294            sampleRate = value;
295            if (resampler == NULL) {
296                resampler = AudioResampler::create(
297                        format, channelCount, devSampleRate);
298                resampler->setLocalTimeFreq(localTimeFreq);
299            }
300            return true;
301        }
302    }
303    return false;
304}
305
306inline
307void AudioMixer::track_t::adjustVolumeRamp(bool aux)
308{
309    for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) {
310        if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
311            ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
312            volumeInc[i] = 0;
313            prevVolume[i] = volume[i]<<16;
314        }
315    }
316    if (aux) {
317        if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
318            ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
319            auxInc = 0;
320            prevAuxLevel = auxLevel<<16;
321        }
322    }
323}
324
325size_t AudioMixer::getUnreleasedFrames(int name) const
326{
327    name -= TRACK0;
328    if (uint32_t(name) < MAX_NUM_TRACKS) {
329        return mState.tracks[name].getUnreleasedFrames();
330    }
331    return 0;
332}
333
334void AudioMixer::setBufferProvider(int name, AudioBufferProvider* buffer)
335{
336    name -= TRACK0;
337    assert(uint32_t(name) < MAX_NUM_TRACKS);
338    mState.tracks[name].bufferProvider = buffer;
339}
340
341
342
343void AudioMixer::process(int64_t pts)
344{
345    mState.hook(&mState, pts);
346}
347
348
349void AudioMixer::process__validate(state_t* state, int64_t pts)
350{
351    ALOGW_IF(!state->needsChanged,
352        "in process__validate() but nothing's invalid");
353
354    uint32_t changed = state->needsChanged;
355    state->needsChanged = 0; // clear the validation flag
356
357    // recompute which tracks are enabled / disabled
358    uint32_t enabled = 0;
359    uint32_t disabled = 0;
360    while (changed) {
361        const int i = 31 - __builtin_clz(changed);
362        const uint32_t mask = 1<<i;
363        changed &= ~mask;
364        track_t& t = state->tracks[i];
365        (t.enabled ? enabled : disabled) |= mask;
366    }
367    state->enabledTracks &= ~disabled;
368    state->enabledTracks |=  enabled;
369
370    // compute everything we need...
371    int countActiveTracks = 0;
372    bool all16BitsStereoNoResample = true;
373    bool resampling = false;
374    bool volumeRamp = false;
375    uint32_t en = state->enabledTracks;
376    while (en) {
377        const int i = 31 - __builtin_clz(en);
378        en &= ~(1<<i);
379
380        countActiveTracks++;
381        track_t& t = state->tracks[i];
382        uint32_t n = 0;
383        n |= NEEDS_CHANNEL_1 + t.channelCount - 1;
384        n |= NEEDS_FORMAT_16;
385        n |= t.doesResample() ? NEEDS_RESAMPLE_ENABLED : NEEDS_RESAMPLE_DISABLED;
386        if (t.auxLevel != 0 && t.auxBuffer != NULL) {
387            n |= NEEDS_AUX_ENABLED;
388        }
389
390        if (t.volumeInc[0]|t.volumeInc[1]) {
391            volumeRamp = true;
392        } else if (!t.doesResample() && t.volumeRL == 0) {
393            n |= NEEDS_MUTE_ENABLED;
394        }
395        t.needs = n;
396
397        if ((n & NEEDS_MUTE__MASK) == NEEDS_MUTE_ENABLED) {
398            t.hook = track__nop;
399        } else {
400            if ((n & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED) {
401                all16BitsStereoNoResample = false;
402            }
403            if ((n & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
404                all16BitsStereoNoResample = false;
405                resampling = true;
406                t.hook = track__genericResample;
407            } else {
408                if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_1){
409                    t.hook = track__16BitsMono;
410                    all16BitsStereoNoResample = false;
411                }
412                if ((n & NEEDS_CHANNEL_COUNT__MASK) == NEEDS_CHANNEL_2){
413                    t.hook = track__16BitsStereo;
414                }
415            }
416        }
417    }
418
419    // select the processing hooks
420    state->hook = process__nop;
421    if (countActiveTracks) {
422        if (resampling) {
423            if (!state->outputTemp) {
424                state->outputTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
425            }
426            if (!state->resampleTemp) {
427                state->resampleTemp = new int32_t[MAX_NUM_CHANNELS * state->frameCount];
428            }
429            state->hook = process__genericResampling;
430        } else {
431            if (state->outputTemp) {
432                delete [] state->outputTemp;
433                state->outputTemp = NULL;
434            }
435            if (state->resampleTemp) {
436                delete [] state->resampleTemp;
437                state->resampleTemp = NULL;
438            }
439            state->hook = process__genericNoResampling;
440            if (all16BitsStereoNoResample && !volumeRamp) {
441                if (countActiveTracks == 1) {
442                    state->hook = process__OneTrack16BitsStereoNoResampling;
443                }
444            }
445        }
446    }
447
448    ALOGV("mixer configuration change: %d activeTracks (%08x) "
449        "all16BitsStereoNoResample=%d, resampling=%d, volumeRamp=%d",
450        countActiveTracks, state->enabledTracks,
451        all16BitsStereoNoResample, resampling, volumeRamp);
452
453   state->hook(state, pts);
454
455    // Now that the volume ramp has been done, set optimal state and
456    // track hooks for subsequent mixer process
457    if (countActiveTracks) {
458        bool allMuted = true;
459        uint32_t en = state->enabledTracks;
460        while (en) {
461            const int i = 31 - __builtin_clz(en);
462            en &= ~(1<<i);
463            track_t& t = state->tracks[i];
464            if (!t.doesResample() && t.volumeRL == 0)
465            {
466                t.needs |= NEEDS_MUTE_ENABLED;
467                t.hook = track__nop;
468            } else {
469                allMuted = false;
470            }
471        }
472        if (allMuted) {
473            state->hook = process__nop;
474        } else if (all16BitsStereoNoResample) {
475            if (countActiveTracks == 1) {
476                state->hook = process__OneTrack16BitsStereoNoResampling;
477            }
478        }
479    }
480}
481
482
483void AudioMixer::track__genericResample(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
484{
485    t->resampler->setSampleRate(t->sampleRate);
486
487    // ramp gain - resample to temp buffer and scale/mix in 2nd step
488    if (aux != NULL) {
489        // always resample with unity gain when sending to auxiliary buffer to be able
490        // to apply send level after resampling
491        // TODO: modify each resampler to support aux channel?
492        t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
493        memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
494        t->resampler->resample(temp, outFrameCount, t->bufferProvider);
495        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
496            volumeRampStereo(t, out, outFrameCount, temp, aux);
497        } else {
498            volumeStereo(t, out, outFrameCount, temp, aux);
499        }
500    } else {
501        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
502            t->resampler->setVolume(UNITY_GAIN, UNITY_GAIN);
503            memset(temp, 0, outFrameCount * MAX_NUM_CHANNELS * sizeof(int32_t));
504            t->resampler->resample(temp, outFrameCount, t->bufferProvider);
505            volumeRampStereo(t, out, outFrameCount, temp, aux);
506        }
507
508        // constant gain
509        else {
510            t->resampler->setVolume(t->volume[0], t->volume[1]);
511            t->resampler->resample(out, outFrameCount, t->bufferProvider);
512        }
513    }
514}
515
516void AudioMixer::track__nop(track_t* t, int32_t* out, size_t outFrameCount, int32_t* temp, int32_t* aux)
517{
518}
519
520void AudioMixer::volumeRampStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
521{
522    int32_t vl = t->prevVolume[0];
523    int32_t vr = t->prevVolume[1];
524    const int32_t vlInc = t->volumeInc[0];
525    const int32_t vrInc = t->volumeInc[1];
526
527    //ALOGD("[0] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
528    //        t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
529    //       (vl + vlInc*frameCount)/65536.0f, frameCount);
530
531    // ramp volume
532    if (CC_UNLIKELY(aux != NULL)) {
533        int32_t va = t->prevAuxLevel;
534        const int32_t vaInc = t->auxInc;
535        int32_t l;
536        int32_t r;
537
538        do {
539            l = (*temp++ >> 12);
540            r = (*temp++ >> 12);
541            *out++ += (vl >> 16) * l;
542            *out++ += (vr >> 16) * r;
543            *aux++ += (va >> 17) * (l + r);
544            vl += vlInc;
545            vr += vrInc;
546            va += vaInc;
547        } while (--frameCount);
548        t->prevAuxLevel = va;
549    } else {
550        do {
551            *out++ += (vl >> 16) * (*temp++ >> 12);
552            *out++ += (vr >> 16) * (*temp++ >> 12);
553            vl += vlInc;
554            vr += vrInc;
555        } while (--frameCount);
556    }
557    t->prevVolume[0] = vl;
558    t->prevVolume[1] = vr;
559    t->adjustVolumeRamp((aux != NULL));
560}
561
562void AudioMixer::volumeStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
563{
564    const int16_t vl = t->volume[0];
565    const int16_t vr = t->volume[1];
566
567    if (CC_UNLIKELY(aux != NULL)) {
568        const int16_t va = (int16_t)t->auxLevel;
569        do {
570            int16_t l = (int16_t)(*temp++ >> 12);
571            int16_t r = (int16_t)(*temp++ >> 12);
572            out[0] = mulAdd(l, vl, out[0]);
573            int16_t a = (int16_t)(((int32_t)l + r) >> 1);
574            out[1] = mulAdd(r, vr, out[1]);
575            out += 2;
576            aux[0] = mulAdd(a, va, aux[0]);
577            aux++;
578        } while (--frameCount);
579    } else {
580        do {
581            int16_t l = (int16_t)(*temp++ >> 12);
582            int16_t r = (int16_t)(*temp++ >> 12);
583            out[0] = mulAdd(l, vl, out[0]);
584            out[1] = mulAdd(r, vr, out[1]);
585            out += 2;
586        } while (--frameCount);
587    }
588}
589
590void AudioMixer::track__16BitsStereo(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
591{
592    const int16_t *in = static_cast<const int16_t *>(t->in);
593
594    if (CC_UNLIKELY(aux != NULL)) {
595        int32_t l;
596        int32_t r;
597        // ramp gain
598        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
599            int32_t vl = t->prevVolume[0];
600            int32_t vr = t->prevVolume[1];
601            int32_t va = t->prevAuxLevel;
602            const int32_t vlInc = t->volumeInc[0];
603            const int32_t vrInc = t->volumeInc[1];
604            const int32_t vaInc = t->auxInc;
605            // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
606            //        t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
607            //        (vl + vlInc*frameCount)/65536.0f, frameCount);
608
609            do {
610                l = (int32_t)*in++;
611                r = (int32_t)*in++;
612                *out++ += (vl >> 16) * l;
613                *out++ += (vr >> 16) * r;
614                *aux++ += (va >> 17) * (l + r);
615                vl += vlInc;
616                vr += vrInc;
617                va += vaInc;
618            } while (--frameCount);
619
620            t->prevVolume[0] = vl;
621            t->prevVolume[1] = vr;
622            t->prevAuxLevel = va;
623            t->adjustVolumeRamp(true);
624        }
625
626        // constant gain
627        else {
628            const uint32_t vrl = t->volumeRL;
629            const int16_t va = (int16_t)t->auxLevel;
630            do {
631                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
632                int16_t a = (int16_t)(((int32_t)in[0] + in[1]) >> 1);
633                in += 2;
634                out[0] = mulAddRL(1, rl, vrl, out[0]);
635                out[1] = mulAddRL(0, rl, vrl, out[1]);
636                out += 2;
637                aux[0] = mulAdd(a, va, aux[0]);
638                aux++;
639            } while (--frameCount);
640        }
641    } else {
642        // ramp gain
643        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
644            int32_t vl = t->prevVolume[0];
645            int32_t vr = t->prevVolume[1];
646            const int32_t vlInc = t->volumeInc[0];
647            const int32_t vrInc = t->volumeInc[1];
648
649            // ALOGD("[1] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
650            //        t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
651            //        (vl + vlInc*frameCount)/65536.0f, frameCount);
652
653            do {
654                *out++ += (vl >> 16) * (int32_t) *in++;
655                *out++ += (vr >> 16) * (int32_t) *in++;
656                vl += vlInc;
657                vr += vrInc;
658            } while (--frameCount);
659
660            t->prevVolume[0] = vl;
661            t->prevVolume[1] = vr;
662            t->adjustVolumeRamp(false);
663        }
664
665        // constant gain
666        else {
667            const uint32_t vrl = t->volumeRL;
668            do {
669                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
670                in += 2;
671                out[0] = mulAddRL(1, rl, vrl, out[0]);
672                out[1] = mulAddRL(0, rl, vrl, out[1]);
673                out += 2;
674            } while (--frameCount);
675        }
676    }
677    t->in = in;
678}
679
680void AudioMixer::track__16BitsMono(track_t* t, int32_t* out, size_t frameCount, int32_t* temp, int32_t* aux)
681{
682    const int16_t *in = static_cast<int16_t const *>(t->in);
683
684    if (CC_UNLIKELY(aux != NULL)) {
685        // ramp gain
686        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1]|t->auxInc)) {
687            int32_t vl = t->prevVolume[0];
688            int32_t vr = t->prevVolume[1];
689            int32_t va = t->prevAuxLevel;
690            const int32_t vlInc = t->volumeInc[0];
691            const int32_t vrInc = t->volumeInc[1];
692            const int32_t vaInc = t->auxInc;
693
694            // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
695            //         t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
696            //         (vl + vlInc*frameCount)/65536.0f, frameCount);
697
698            do {
699                int32_t l = *in++;
700                *out++ += (vl >> 16) * l;
701                *out++ += (vr >> 16) * l;
702                *aux++ += (va >> 16) * l;
703                vl += vlInc;
704                vr += vrInc;
705                va += vaInc;
706            } while (--frameCount);
707
708            t->prevVolume[0] = vl;
709            t->prevVolume[1] = vr;
710            t->prevAuxLevel = va;
711            t->adjustVolumeRamp(true);
712        }
713        // constant gain
714        else {
715            const int16_t vl = t->volume[0];
716            const int16_t vr = t->volume[1];
717            const int16_t va = (int16_t)t->auxLevel;
718            do {
719                int16_t l = *in++;
720                out[0] = mulAdd(l, vl, out[0]);
721                out[1] = mulAdd(l, vr, out[1]);
722                out += 2;
723                aux[0] = mulAdd(l, va, aux[0]);
724                aux++;
725            } while (--frameCount);
726        }
727    } else {
728        // ramp gain
729        if (CC_UNLIKELY(t->volumeInc[0]|t->volumeInc[1])) {
730            int32_t vl = t->prevVolume[0];
731            int32_t vr = t->prevVolume[1];
732            const int32_t vlInc = t->volumeInc[0];
733            const int32_t vrInc = t->volumeInc[1];
734
735            // ALOGD("[2] %p: inc=%f, v0=%f, v1=%d, final=%f, count=%d",
736            //         t, vlInc/65536.0f, vl/65536.0f, t->volume[0],
737            //         (vl + vlInc*frameCount)/65536.0f, frameCount);
738
739            do {
740                int32_t l = *in++;
741                *out++ += (vl >> 16) * l;
742                *out++ += (vr >> 16) * l;
743                vl += vlInc;
744                vr += vrInc;
745            } while (--frameCount);
746
747            t->prevVolume[0] = vl;
748            t->prevVolume[1] = vr;
749            t->adjustVolumeRamp(false);
750        }
751        // constant gain
752        else {
753            const int16_t vl = t->volume[0];
754            const int16_t vr = t->volume[1];
755            do {
756                int16_t l = *in++;
757                out[0] = mulAdd(l, vl, out[0]);
758                out[1] = mulAdd(l, vr, out[1]);
759                out += 2;
760            } while (--frameCount);
761        }
762    }
763    t->in = in;
764}
765
766// no-op case
767void AudioMixer::process__nop(state_t* state, int64_t pts)
768{
769    uint32_t e0 = state->enabledTracks;
770    size_t bufSize = state->frameCount * sizeof(int16_t) * MAX_NUM_CHANNELS;
771    while (e0) {
772        // process by group of tracks with same output buffer to
773        // avoid multiple memset() on same buffer
774        uint32_t e1 = e0, e2 = e0;
775        int i = 31 - __builtin_clz(e1);
776        track_t& t1 = state->tracks[i];
777        e2 &= ~(1<<i);
778        while (e2) {
779            i = 31 - __builtin_clz(e2);
780            e2 &= ~(1<<i);
781            track_t& t2 = state->tracks[i];
782            if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
783                e1 &= ~(1<<i);
784            }
785        }
786        e0 &= ~(e1);
787
788        memset(t1.mainBuffer, 0, bufSize);
789
790        while (e1) {
791            i = 31 - __builtin_clz(e1);
792            e1 &= ~(1<<i);
793            t1 = state->tracks[i];
794            size_t outFrames = state->frameCount;
795            while (outFrames) {
796                t1.buffer.frameCount = outFrames;
797                int64_t outputPTS = calculateOutputPTS(
798                    t1, pts, state->frameCount - outFrames);
799                t1.bufferProvider->getNextBuffer(&t1.buffer, outputPTS);
800                if (t1.buffer.raw == NULL) break;
801                outFrames -= t1.buffer.frameCount;
802                t1.bufferProvider->releaseBuffer(&t1.buffer);
803            }
804        }
805    }
806}
807
808// generic code without resampling
809void AudioMixer::process__genericNoResampling(state_t* state, int64_t pts)
810{
811    int32_t outTemp[BLOCKSIZE * MAX_NUM_CHANNELS] __attribute__((aligned(32)));
812
813    // acquire each track's buffer
814    uint32_t enabledTracks = state->enabledTracks;
815    uint32_t e0 = enabledTracks;
816    while (e0) {
817        const int i = 31 - __builtin_clz(e0);
818        e0 &= ~(1<<i);
819        track_t& t = state->tracks[i];
820        t.buffer.frameCount = state->frameCount;
821        t.bufferProvider->getNextBuffer(&t.buffer, pts);
822        t.frameCount = t.buffer.frameCount;
823        t.in = t.buffer.raw;
824        // t.in == NULL can happen if the track was flushed just after having
825        // been enabled for mixing.
826        if (t.in == NULL)
827            enabledTracks &= ~(1<<i);
828    }
829
830    e0 = enabledTracks;
831    while (e0) {
832        // process by group of tracks with same output buffer to
833        // optimize cache use
834        uint32_t e1 = e0, e2 = e0;
835        int j = 31 - __builtin_clz(e1);
836        track_t& t1 = state->tracks[j];
837        e2 &= ~(1<<j);
838        while (e2) {
839            j = 31 - __builtin_clz(e2);
840            e2 &= ~(1<<j);
841            track_t& t2 = state->tracks[j];
842            if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
843                e1 &= ~(1<<j);
844            }
845        }
846        e0 &= ~(e1);
847        // this assumes output 16 bits stereo, no resampling
848        int32_t *out = t1.mainBuffer;
849        size_t numFrames = 0;
850        do {
851            memset(outTemp, 0, sizeof(outTemp));
852            e2 = e1;
853            while (e2) {
854                const int i = 31 - __builtin_clz(e2);
855                e2 &= ~(1<<i);
856                track_t& t = state->tracks[i];
857                size_t outFrames = BLOCKSIZE;
858                int32_t *aux = NULL;
859                if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
860                    aux = t.auxBuffer + numFrames;
861                }
862                while (outFrames) {
863                    size_t inFrames = (t.frameCount > outFrames)?outFrames:t.frameCount;
864                    if (inFrames) {
865                        (t.hook)(&t, outTemp + (BLOCKSIZE-outFrames)*MAX_NUM_CHANNELS, inFrames, state->resampleTemp, aux);
866                        t.frameCount -= inFrames;
867                        outFrames -= inFrames;
868                        if (CC_UNLIKELY(aux != NULL)) {
869                            aux += inFrames;
870                        }
871                    }
872                    if (t.frameCount == 0 && outFrames) {
873                        t.bufferProvider->releaseBuffer(&t.buffer);
874                        t.buffer.frameCount = (state->frameCount - numFrames) - (BLOCKSIZE - outFrames);
875                        int64_t outputPTS = calculateOutputPTS(
876                            t, pts, numFrames + (BLOCKSIZE - outFrames));
877                        t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
878                        t.in = t.buffer.raw;
879                        if (t.in == NULL) {
880                            enabledTracks &= ~(1<<i);
881                            e1 &= ~(1<<i);
882                            break;
883                        }
884                        t.frameCount = t.buffer.frameCount;
885                    }
886                }
887            }
888            ditherAndClamp(out, outTemp, BLOCKSIZE);
889            out += BLOCKSIZE;
890            numFrames += BLOCKSIZE;
891        } while (numFrames < state->frameCount);
892    }
893
894    // release each track's buffer
895    e0 = enabledTracks;
896    while (e0) {
897        const int i = 31 - __builtin_clz(e0);
898        e0 &= ~(1<<i);
899        track_t& t = state->tracks[i];
900        t.bufferProvider->releaseBuffer(&t.buffer);
901    }
902}
903
904
905// generic code with resampling
906void AudioMixer::process__genericResampling(state_t* state, int64_t pts)
907{
908    // this const just means that local variable outTemp doesn't change
909    int32_t* const outTemp = state->outputTemp;
910    const size_t size = sizeof(int32_t) * MAX_NUM_CHANNELS * state->frameCount;
911
912    size_t numFrames = state->frameCount;
913
914    uint32_t e0 = state->enabledTracks;
915    while (e0) {
916        // process by group of tracks with same output buffer
917        // to optimize cache use
918        uint32_t e1 = e0, e2 = e0;
919        int j = 31 - __builtin_clz(e1);
920        track_t& t1 = state->tracks[j];
921        e2 &= ~(1<<j);
922        while (e2) {
923            j = 31 - __builtin_clz(e2);
924            e2 &= ~(1<<j);
925            track_t& t2 = state->tracks[j];
926            if (CC_UNLIKELY(t2.mainBuffer != t1.mainBuffer)) {
927                e1 &= ~(1<<j);
928            }
929        }
930        e0 &= ~(e1);
931        int32_t *out = t1.mainBuffer;
932        memset(outTemp, 0, size);
933        while (e1) {
934            const int i = 31 - __builtin_clz(e1);
935            e1 &= ~(1<<i);
936            track_t& t = state->tracks[i];
937            int32_t *aux = NULL;
938            if (CC_UNLIKELY((t.needs & NEEDS_AUX__MASK) == NEEDS_AUX_ENABLED)) {
939                aux = t.auxBuffer;
940            }
941
942            // this is a little goofy, on the resampling case we don't
943            // acquire/release the buffers because it's done by
944            // the resampler.
945            if ((t.needs & NEEDS_RESAMPLE__MASK) == NEEDS_RESAMPLE_ENABLED) {
946                t.resampler->setPTS(pts);
947                (t.hook)(&t, outTemp, numFrames, state->resampleTemp, aux);
948            } else {
949
950                size_t outFrames = 0;
951
952                while (outFrames < numFrames) {
953                    t.buffer.frameCount = numFrames - outFrames;
954                    int64_t outputPTS = calculateOutputPTS(t, pts, outFrames);
955                    t.bufferProvider->getNextBuffer(&t.buffer, outputPTS);
956                    t.in = t.buffer.raw;
957                    // t.in == NULL can happen if the track was flushed just after having
958                    // been enabled for mixing.
959                    if (t.in == NULL) break;
960
961                    if (CC_UNLIKELY(aux != NULL)) {
962                        aux += outFrames;
963                    }
964                    (t.hook)(&t, outTemp + outFrames*MAX_NUM_CHANNELS, t.buffer.frameCount, state->resampleTemp, aux);
965                    outFrames += t.buffer.frameCount;
966                    t.bufferProvider->releaseBuffer(&t.buffer);
967                }
968            }
969        }
970        ditherAndClamp(out, outTemp, numFrames);
971    }
972}
973
974// one track, 16 bits stereo without resampling is the most common case
975void AudioMixer::process__OneTrack16BitsStereoNoResampling(state_t* state,
976                                                           int64_t pts)
977{
978    // This method is only called when state->enabledTracks has exactly
979    // one bit set.  The asserts below would verify this, but are commented out
980    // since the whole point of this method is to optimize performance.
981    //assert(0 != state->enabledTracks);
982    const int i = 31 - __builtin_clz(state->enabledTracks);
983    //assert((1 << i) == state->enabledTracks);
984    const track_t& t = state->tracks[i];
985
986    AudioBufferProvider::Buffer& b(t.buffer);
987
988    int32_t* out = t.mainBuffer;
989    size_t numFrames = state->frameCount;
990
991    const int16_t vl = t.volume[0];
992    const int16_t vr = t.volume[1];
993    const uint32_t vrl = t.volumeRL;
994    while (numFrames) {
995        b.frameCount = numFrames;
996        int64_t outputPTS = calculateOutputPTS(t, pts, out - t.mainBuffer);
997        t.bufferProvider->getNextBuffer(&b, outputPTS);
998        const int16_t *in = b.i16;
999
1000        // in == NULL can happen if the track was flushed just after having
1001        // been enabled for mixing.
1002        if (in == NULL || ((unsigned long)in & 3)) {
1003            memset(out, 0, numFrames*MAX_NUM_CHANNELS*sizeof(int16_t));
1004            ALOGE_IF(((unsigned long)in & 3), "process stereo track: input buffer alignment pb: buffer %p track %d, channels %d, needs %08x",
1005                    in, i, t.channelCount, t.needs);
1006            return;
1007        }
1008        size_t outFrames = b.frameCount;
1009
1010        if (CC_UNLIKELY(uint32_t(vl) > UNITY_GAIN || uint32_t(vr) > UNITY_GAIN)) {
1011            // volume is boosted, so we might need to clamp even though
1012            // we process only one track.
1013            do {
1014                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1015                in += 2;
1016                int32_t l = mulRL(1, rl, vrl) >> 12;
1017                int32_t r = mulRL(0, rl, vrl) >> 12;
1018                // clamping...
1019                l = clamp16(l);
1020                r = clamp16(r);
1021                *out++ = (r<<16) | (l & 0xFFFF);
1022            } while (--outFrames);
1023        } else {
1024            do {
1025                uint32_t rl = *reinterpret_cast<const uint32_t *>(in);
1026                in += 2;
1027                int32_t l = mulRL(1, rl, vrl) >> 12;
1028                int32_t r = mulRL(0, rl, vrl) >> 12;
1029                *out++ = (r<<16) | (l & 0xFFFF);
1030            } while (--outFrames);
1031        }
1032        numFrames -= b.frameCount;
1033        t.bufferProvider->releaseBuffer(&b);
1034    }
1035}
1036
1037#if 0
1038// 2 tracks is also a common case
1039// NEVER used in current implementation of process__validate()
1040// only use if the 2 tracks have the same output buffer
1041void AudioMixer::process__TwoTracks16BitsStereoNoResampling(state_t* state,
1042                                                            int64_t pts)
1043{
1044    int i;
1045    uint32_t en = state->enabledTracks;
1046
1047    i = 31 - __builtin_clz(en);
1048    const track_t& t0 = state->tracks[i];
1049    AudioBufferProvider::Buffer& b0(t0.buffer);
1050
1051    en &= ~(1<<i);
1052    i = 31 - __builtin_clz(en);
1053    const track_t& t1 = state->tracks[i];
1054    AudioBufferProvider::Buffer& b1(t1.buffer);
1055
1056    const int16_t *in0;
1057    const int16_t vl0 = t0.volume[0];
1058    const int16_t vr0 = t0.volume[1];
1059    size_t frameCount0 = 0;
1060
1061    const int16_t *in1;
1062    const int16_t vl1 = t1.volume[0];
1063    const int16_t vr1 = t1.volume[1];
1064    size_t frameCount1 = 0;
1065
1066    //FIXME: only works if two tracks use same buffer
1067    int32_t* out = t0.mainBuffer;
1068    size_t numFrames = state->frameCount;
1069    const int16_t *buff = NULL;
1070
1071
1072    while (numFrames) {
1073
1074        if (frameCount0 == 0) {
1075            b0.frameCount = numFrames;
1076            int64_t outputPTS = calculateOutputPTS(t0, pts,
1077                                                   out - t0.mainBuffer);
1078            t0.bufferProvider->getNextBuffer(&b0, outputPTS);
1079            if (b0.i16 == NULL) {
1080                if (buff == NULL) {
1081                    buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1082                }
1083                in0 = buff;
1084                b0.frameCount = numFrames;
1085            } else {
1086                in0 = b0.i16;
1087            }
1088            frameCount0 = b0.frameCount;
1089        }
1090        if (frameCount1 == 0) {
1091            b1.frameCount = numFrames;
1092            int64_t outputPTS = calculateOutputPTS(t1, pts,
1093                                                   out - t0.mainBuffer);
1094            t1.bufferProvider->getNextBuffer(&b1, outputPTS);
1095            if (b1.i16 == NULL) {
1096                if (buff == NULL) {
1097                    buff = new int16_t[MAX_NUM_CHANNELS * state->frameCount];
1098                }
1099                in1 = buff;
1100                b1.frameCount = numFrames;
1101            } else {
1102                in1 = b1.i16;
1103            }
1104            frameCount1 = b1.frameCount;
1105        }
1106
1107        size_t outFrames = frameCount0 < frameCount1?frameCount0:frameCount1;
1108
1109        numFrames -= outFrames;
1110        frameCount0 -= outFrames;
1111        frameCount1 -= outFrames;
1112
1113        do {
1114            int32_t l0 = *in0++;
1115            int32_t r0 = *in0++;
1116            l0 = mul(l0, vl0);
1117            r0 = mul(r0, vr0);
1118            int32_t l = *in1++;
1119            int32_t r = *in1++;
1120            l = mulAdd(l, vl1, l0) >> 12;
1121            r = mulAdd(r, vr1, r0) >> 12;
1122            // clamping...
1123            l = clamp16(l);
1124            r = clamp16(r);
1125            *out++ = (r<<16) | (l & 0xFFFF);
1126        } while (--outFrames);
1127
1128        if (frameCount0 == 0) {
1129            t0.bufferProvider->releaseBuffer(&b0);
1130        }
1131        if (frameCount1 == 0) {
1132            t1.bufferProvider->releaseBuffer(&b1);
1133        }
1134    }
1135
1136    delete [] buff;
1137}
1138#endif
1139
1140int64_t AudioMixer::calculateOutputPTS(const track_t& t, int64_t basePTS,
1141                                       int outputFrameIndex)
1142{
1143    if (AudioBufferProvider::kInvalidPTS == basePTS)
1144        return AudioBufferProvider::kInvalidPTS;
1145
1146    return basePTS + ((outputFrameIndex * t.localTimeFreq) / t.sampleRate);
1147}
1148
1149// ----------------------------------------------------------------------------
1150}; // namespace android
1151