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