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