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