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