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