199e53b86eebb605b70dd7591b89bf61a9414ed0eGlenn Kasten/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Copyright 2007, The Android Open Source Project
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Licensed under the Apache License, Version 2.0 (the "License");
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** you may not use this file except in compliance with the License.
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** You may obtain a copy of the License at
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**     http://www.apache.org/licenses/LICENSE-2.0
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian**
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** Unless required by applicable law or agreed to in writing, software
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** distributed under the License is distributed on an "AS IS" BASIS,
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** See the License for the specific language governing permissions and
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian** limitations under the License.
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian*/
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define LOG_TAG "AudioMixer"
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//#define LOG_NDEBUG 0
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h>
2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <string.h>
2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdlib.h>
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/types.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Errors.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
290d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi#include <cutils/bitops.h>
30f6b1678f8f508b447155a81b44e214475ab634a8Glenn Kasten#include <cutils/compiler.h>
315798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten#include <utils/Debug.h>
320d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi
330d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi#include <system/audio.h>
340d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi
353b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten#include <audio_utils/primitives.h>
364ff14bae91075eb274eb1c2975982358946e7e63John Grossman#include <common_time/local_clock.h>
374ff14bae91075eb274eb1c2975982358946e7e63John Grossman#include <common_time/cc_helper.h>
383b21c50ef95fe4e7ac3426ca14b365749e66ff08Glenn Kasten
397d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi#include <media/EffectsFactoryApi.h>
407d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
4165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioMixer.h"
4265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
467d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel TriviAudioMixer::DownmixerBufferProvider::DownmixerBufferProvider() : AudioBufferProvider(),
477d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        mTrackBufferProvider(NULL), mDownmixHandle(NULL)
487d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi{
497d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi}
507d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
517d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel TriviAudioMixer::DownmixerBufferProvider::~DownmixerBufferProvider()
527d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi{
537d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    ALOGV("AudioMixer deleting DownmixerBufferProvider (%p)", this);
547d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    EffectRelease(mDownmixHandle);
557d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi}
567d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
577d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivistatus_t AudioMixer::DownmixerBufferProvider::getNextBuffer(AudioBufferProvider::Buffer *pBuffer,
587d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        int64_t pts) {
597d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    //ALOGV("DownmixerBufferProvider::getNextBuffer()");
607d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    if (this->mTrackBufferProvider != NULL) {
617d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        status_t res = mTrackBufferProvider->getNextBuffer(pBuffer, pts);
627d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        if (res == OK) {
637d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            mDownmixConfig.inputCfg.buffer.frameCount = pBuffer->frameCount;
647d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            mDownmixConfig.inputCfg.buffer.raw = pBuffer->raw;
657d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            mDownmixConfig.outputCfg.buffer.frameCount = pBuffer->frameCount;
667d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            mDownmixConfig.outputCfg.buffer.raw = mDownmixConfig.inputCfg.buffer.raw;
677d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            // in-place so overwrite the buffer contents, has been set in prepareTrackForDownmix()
687d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            //mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
697d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
707d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            res = (*mDownmixHandle)->process(mDownmixHandle,
717d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                    &mDownmixConfig.inputCfg.buffer, &mDownmixConfig.outputCfg.buffer);
729bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi            //ALOGV("getNextBuffer is downmixing");
737d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        }
747d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        return res;
757d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    } else {
767d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        ALOGE("DownmixerBufferProvider::getNextBuffer() error: NULL track buffer provider");
777d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        return NO_INIT;
787d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }
797d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi}
807d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
817d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivivoid AudioMixer::DownmixerBufferProvider::releaseBuffer(AudioBufferProvider::Buffer *pBuffer) {
829bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    //ALOGV("DownmixerBufferProvider::releaseBuffer()");
837d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    if (this->mTrackBufferProvider != NULL) {
847d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        mTrackBufferProvider->releaseBuffer(pBuffer);
857d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    } else {
867d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        ALOGE("DownmixerBufferProvider::releaseBuffer() error: NULL track buffer provider");
877d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }
887d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi}
897d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
907d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
917d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi// ----------------------------------------------------------------------------
927d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivibool AudioMixer::isMultichannelCapable = false;
937d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
947d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivieffect_descriptor_t AudioMixer::dwnmFxDesc;
9565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
963c0a0e8541846427db0587c2fffb90f60ee680b0Paul Lind// Ensure mConfiguredNames bitmask is initialized properly on all architectures.
973c0a0e8541846427db0587c2fffb90f60ee680b0Paul Lind// The value of 1 << x is undefined in C when x >= 32.
983c0a0e8541846427db0587c2fffb90f60ee680b0Paul Lind
995c94b6c7689a335e26a86e8a0d04b56dc627738eGlenn KastenAudioMixer::AudioMixer(size_t frameCount, uint32_t sampleRate, uint32_t maxNumTracks)
1003c0a0e8541846427db0587c2fffb90f60ee680b0Paul Lind    :   mTrackNames(0), mConfiguredNames((maxNumTracks >= 32 ? 0 : 1 << maxNumTracks) - 1),
1013c0a0e8541846427db0587c2fffb90f60ee680b0Paul Lind        mSampleRate(sampleRate)
10265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
103788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten    // AudioMixer is not yet capable of multi-channel beyond stereo
1045798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten    COMPILE_TIME_ASSERT_FUNCTION_SCOPE(2 == MAX_NUM_CHANNELS);
105acb86cccbd9d245439a04cef0bcefa589addaa4cJean-Michel Trivi
1065c94b6c7689a335e26a86e8a0d04b56dc627738eGlenn Kasten    ALOG_ASSERT(maxNumTracks <= MAX_NUM_TRACKS, "maxNumTracks %u > MAX_NUM_TRACKS %u",
1075c94b6c7689a335e26a86e8a0d04b56dc627738eGlenn Kasten            maxNumTracks, MAX_NUM_TRACKS);
1085c94b6c7689a335e26a86e8a0d04b56dc627738eGlenn Kasten
1094ff14bae91075eb274eb1c2975982358946e7e63John Grossman    LocalClock lc;
1104ff14bae91075eb274eb1c2975982358946e7e63John Grossman
11165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mState.enabledTracks= 0;
11265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mState.needsChanged = 0;
11365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mState.frameCount   = frameCount;
11484afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten    mState.hook         = process__nop;
115e0feee3da22beeffbd9357540e265f13b2119cbbGlenn Kasten    mState.outputTemp   = NULL;
116e0feee3da22beeffbd9357540e265f13b2119cbbGlenn Kasten    mState.resampleTemp = NULL;
11784afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten    // mState.reserved
11817a736c3e1d062d7fc916329eb32aef8935614afGlenn Kasten
11917a736c3e1d062d7fc916329eb32aef8935614afGlenn Kasten    // FIXME Most of the following initialization is probably redundant since
12017a736c3e1d062d7fc916329eb32aef8935614afGlenn Kasten    // tracks[i] should only be referenced if (mTrackNames & (1 << i)) != 0
12117a736c3e1d062d7fc916329eb32aef8935614afGlenn Kasten    // and mTrackNames is initially 0.  However, leave it here until that's verified.
12265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    track_t* t = mState.tracks;
123bf71f1e7948406492376c6cbd5e6a30c8cb670e4Glenn Kasten    for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
124deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        // FIXME redundant per track
125deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        t->localTimeFreq = lc.getLocalFreq();
126a5e821439996de6005b2fa36b3bdd31f003ce23fEric Laurent        t->resampler = NULL;
1279bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        t->downmixerBufferProvider = NULL;
128deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        t++;
129deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    }
1307d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
1317d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    // find multichannel downmix effect if we have to play multichannel content
1327d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    uint32_t numEffects = 0;
1337d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    int ret = EffectQueryNumberEffects(&numEffects);
1347d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    if (ret != 0) {
1357d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        ALOGE("AudioMixer() error %d querying number of effects", ret);
1367d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        return;
1377d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }
1387d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
1397d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
1407d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    for (uint32_t i = 0 ; i < numEffects ; i++) {
1417d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        if (EffectQueryEffect(i, &dwnmFxDesc) == 0) {
1427d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            ALOGV("effect %d is called %s", i, dwnmFxDesc.name);
1437d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            if (memcmp(&dwnmFxDesc.type, EFFECT_UIID_DOWNMIX, sizeof(effect_uuid_t)) == 0) {
1447d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                ALOGI("found effect \"%s\" from %s",
1457d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                        dwnmFxDesc.name, dwnmFxDesc.implementor);
1467d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                isMultichannelCapable = true;
1477d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                break;
1487d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            }
1497d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        }
1507d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }
1517d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    ALOGE_IF(!isMultichannelCapable, "unable to find downmix effect");
152deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten}
153deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten
154deeb1282621f3177ad667360b40eef8e4fedb298Glenn KastenAudioMixer::~AudioMixer()
155deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten{
156deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    track_t* t = mState.tracks;
157deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    for (unsigned i=0 ; i < MAX_NUM_TRACKS ; i++) {
158deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        delete t->resampler;
1599bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        delete t->downmixerBufferProvider;
160deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        t++;
161deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    }
162deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    delete [] mState.outputTemp;
163deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    delete [] mState.resampleTemp;
164deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten}
165deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten
166fe3156ec6fd9fa57dde913fd8567530d095a6550Jean-Michel Triviint AudioMixer::getTrackName(audio_channel_mask_t channelMask, int sessionId)
167deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten{
168deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    uint32_t names = (~mTrackNames) & mConfiguredNames;
169deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten    if (names != 0) {
170deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        int n = __builtin_ctz(names);
171deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        ALOGV("add track (%d)", n);
172deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        mTrackNames |= 1 << n;
173deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        // assume default parameters for the track, except where noted below
174deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        track_t* t = &mState.tracks[n];
17565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->needs = 0;
17665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->volume[0] = UNITY_GAIN;
17765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->volume[1] = UNITY_GAIN;
1780cfd8231e4c489392809bf44c174315df2690145Glenn Kasten        // no initialization needed
1790cfd8231e4c489392809bf44c174315df2690145Glenn Kasten        // t->prevVolume[0]
1800cfd8231e4c489392809bf44c174315df2690145Glenn Kasten        // t->prevVolume[1]
18165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->volumeInc[0] = 0;
18265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->volumeInc[1] = 0;
18365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->auxLevel = 0;
18465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->auxInc = 0;
1850cfd8231e4c489392809bf44c174315df2690145Glenn Kasten        // no initialization needed
1860cfd8231e4c489392809bf44c174315df2690145Glenn Kasten        // t->prevAuxLevel
1870cfd8231e4c489392809bf44c174315df2690145Glenn Kasten        // t->frameCount
18865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->channelCount = 2;
1894c340c6521b634f159d2d6bc4e9359226fd8edf8Glenn Kasten        t->enabled = false;
19065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->format = 16;
1910d255b2d9061ba31f13ada3fc0f7e51916407176Jean-Michel Trivi        t->channelMask = AUDIO_CHANNEL_OUT_STEREO;
192fe3156ec6fd9fa57dde913fd8567530d095a6550Jean-Michel Trivi        t->sessionId = sessionId;
193deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        // setBufferProvider(name, AudioBufferProvider *) is required before enable(name)
194e0feee3da22beeffbd9357540e265f13b2119cbbGlenn Kasten        t->bufferProvider = NULL;
1957d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        t->downmixerBufferProvider = NULL;
19684afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        t->buffer.raw = NULL;
197deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        // no initialization needed
19884afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        // t->buffer.frameCount
199e0feee3da22beeffbd9357540e265f13b2119cbbGlenn Kasten        t->hook = NULL;
20084afa3b51ac48f84ed62489529ce78cba7fca00eGlenn Kasten        t->in = NULL;
201e0feee3da22beeffbd9357540e265f13b2119cbbGlenn Kasten        t->resampler = NULL;
20265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->sampleRate = mSampleRate;
203deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        // setParameter(name, TRACK, MAIN_BUFFER, mixBuffer) is required before enable(name)
20465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->mainBuffer = NULL;
20565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        t->auxBuffer = NULL;
206deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        // see t->localTimeFreq in constructor above
2079bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi
2089bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        status_t status = initTrackDownmix(&mState.tracks[n], n, channelMask);
2099bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        if (status == OK) {
2109bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi            return TRACK0 + n;
2119bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        }
2129bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        ALOGE("AudioMixer::getTrackName(0x%x) failed, error preparing track for downmix",
2139bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi                channelMask);
21465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
21565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return -1;
216c5ac4cb3a5124860ccfc7e4ff66251c55a5595caGlenn Kasten}
21765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
218c5ac4cb3a5124860ccfc7e4ff66251c55a5595caGlenn Kastenvoid AudioMixer::invalidateState(uint32_t mask)
219c5ac4cb3a5124860ccfc7e4ff66251c55a5595caGlenn Kasten{
22065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (mask) {
22165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mState.needsChanged |= mask;
22265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mState.hook = process__validate;
22365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
22465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian }
22565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2269bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivistatus_t AudioMixer::initTrackDownmix(track_t* pTrack, int trackNum, audio_channel_mask_t mask)
2277d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi{
2289bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    uint32_t channelCount = popcount(mask);
2299bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount);
2309bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    status_t status = OK;
2319bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    if (channelCount > MAX_NUM_CHANNELS) {
2329bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        pTrack->channelMask = mask;
2339bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        pTrack->channelCount = channelCount;
2349bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        ALOGV("initTrackDownmix(track=%d, mask=0x%x) calls prepareTrackForDownmix()",
2359bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi                trackNum, mask);
2369bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        status = prepareTrackForDownmix(pTrack, trackNum);
2379bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    } else {
2389bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        unprepareTrackForDownmix(pTrack, trackNum);
2399bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    }
2409bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    return status;
2419bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi}
2429bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi
2439bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivivoid AudioMixer::unprepareTrackForDownmix(track_t* pTrack, int trackName) {
2449bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    ALOGV("AudioMixer::unprepareTrackForDownmix(%d)", trackName);
2457d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
2467d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    if (pTrack->downmixerBufferProvider != NULL) {
2479bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        // this track had previously been configured with a downmixer, delete it
2489bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        ALOGV(" deleting old downmixer");
2497d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        pTrack->bufferProvider = pTrack->downmixerBufferProvider->mTrackBufferProvider;
2507d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        delete pTrack->downmixerBufferProvider;
2519bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        pTrack->downmixerBufferProvider = NULL;
2529bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    } else {
2539bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi        ALOGV(" nothing to do, no downmixer to delete");
2547d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }
2559bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi}
2569bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi
2579bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivistatus_t AudioMixer::prepareTrackForDownmix(track_t* pTrack, int trackName)
2589bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi{
2599bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    ALOGV("AudioMixer::prepareTrackForDownmix(%d) with mask 0x%x", trackName, pTrack->channelMask);
2609bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi
2619bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    // discard the previous downmixer if there was one
2629bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    unprepareTrackForDownmix(pTrack, trackName);
2637d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
2647d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    DownmixerBufferProvider* pDbp = new DownmixerBufferProvider();
2657d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    int32_t status;
2667d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
2677d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    if (!isMultichannelCapable) {
2687d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        ALOGE("prepareTrackForDownmix(%d) fails: mixer doesn't support multichannel content",
2697d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                trackName);
2707d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        goto noDownmixForActiveTrack;
2717d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }
2727d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
2737d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    if (EffectCreate(&dwnmFxDesc.uuid,
274fe3156ec6fd9fa57dde913fd8567530d095a6550Jean-Michel Trivi            pTrack->sessionId /*sessionId*/, -2 /*ioId not relevant here, using random value*/,
2757d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            &pDbp->mDownmixHandle/*pHandle*/) != 0) {
2767d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        ALOGE("prepareTrackForDownmix(%d) fails: error creating downmixer effect", trackName);
2777d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        goto noDownmixForActiveTrack;
2787d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }
2797d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
2807d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    // channel input configuration will be overridden per-track
2817d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.inputCfg.channels = pTrack->channelMask;
2827d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.outputCfg.channels = AUDIO_CHANNEL_OUT_STEREO;
2837d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
2847d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
2857d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.inputCfg.samplingRate = pTrack->sampleRate;
2867d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.outputCfg.samplingRate = pTrack->sampleRate;
2877d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
2887d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
2897d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    // input and output buffer provider, and frame count will not be used as the downmix effect
2907d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    // process() function is called directly (see DownmixerBufferProvider::getNextBuffer())
2917d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.inputCfg.mask = EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS |
2927d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            EFFECT_CONFIG_FORMAT | EFFECT_CONFIG_ACC_MODE;
2937d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mDownmixConfig.outputCfg.mask = pDbp->mDownmixConfig.inputCfg.mask;
2947d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
2957d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    {// scope for local variables that are not used in goto label "noDownmixForActiveTrack"
2967d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        int cmdStatus;
2977d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        uint32_t replySize = sizeof(int);
2987d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
2997d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        // Configure and enable downmixer
3007d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle,
3017d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                EFFECT_CMD_SET_CONFIG /*cmdCode*/, sizeof(effect_config_t) /*cmdSize*/,
3027d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                &pDbp->mDownmixConfig /*pCmdData*/,
3037d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                &replySize /*replySize*/, &cmdStatus /*pReplyData*/);
3047d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        if ((status != 0) || (cmdStatus != 0)) {
3057d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            ALOGE("error %d while configuring downmixer for track %d", status, trackName);
3067d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            goto noDownmixForActiveTrack;
3077d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        }
3087d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        replySize = sizeof(int);
3097d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle,
3107d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                EFFECT_CMD_ENABLE /*cmdCode*/, 0 /*cmdSize*/, NULL /*pCmdData*/,
3117d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                &replySize /*replySize*/, &cmdStatus /*pReplyData*/);
3127d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        if ((status != 0) || (cmdStatus != 0)) {
3137d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            ALOGE("error %d while enabling downmixer for track %d", status, trackName);
3147d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            goto noDownmixForActiveTrack;
3157d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        }
3167d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
3177d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        // Set downmix type
3187d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        // parameter size rounded for padding on 32bit boundary
3197d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        const int psizePadded = ((sizeof(downmix_params_t) - 1)/sizeof(int) + 1) * sizeof(int);
3207d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        const int downmixParamSize =
3217d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                sizeof(effect_param_t) + psizePadded + sizeof(downmix_type_t);
3227d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        effect_param_t * const param = (effect_param_t *) malloc(downmixParamSize);
3237d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        param->psize = sizeof(downmix_params_t);
3247d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        const downmix_params_t downmixParam = DOWNMIX_PARAM_TYPE;
3257d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        memcpy(param->data, &downmixParam, param->psize);
3267d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        const downmix_type_t downmixType = DOWNMIX_TYPE_FOLD;
3277d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        param->vsize = sizeof(downmix_type_t);
3287d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        memcpy(param->data + psizePadded, &downmixType, param->vsize);
3297d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
3307d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        status = (*pDbp->mDownmixHandle)->command(pDbp->mDownmixHandle,
3317d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                EFFECT_CMD_SET_PARAM /* cmdCode */, downmixParamSize/* cmdSize */,
3327d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                param /*pCmndData*/, &replySize /*replySize*/, &cmdStatus /*pReplyData*/);
3337d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
3347d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        free(param);
3357d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
3367d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        if ((status != 0) || (cmdStatus != 0)) {
3377d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            ALOGE("error %d while setting downmix type for track %d", status, trackName);
3387d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            goto noDownmixForActiveTrack;
3397d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        } else {
3407d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            ALOGV("downmix type set to %d for track %d", (int) downmixType, trackName);
3417d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        }
3427d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    }// end of scope for local variables that are not used in goto label "noDownmixForActiveTrack"
3437d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
3447d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    // initialization successful:
3457d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    // - keep track of the real buffer provider in case it was set before
3467d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pDbp->mTrackBufferProvider = pTrack->bufferProvider;
3477d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    // - we'll use the downmix effect integrated inside this
3487d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    //    track's buffer provider, and we'll use it as the track's buffer provider
3497d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pTrack->downmixerBufferProvider = pDbp;
3507d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pTrack->bufferProvider = pDbp;
3517d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
3527d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    return NO_ERROR;
3537d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
3547d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel TrivinoDownmixForActiveTrack:
3557d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    delete pDbp;
3567d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    pTrack->downmixerBufferProvider = NULL;
3577d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi    return NO_INIT;
3587d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi}
3597d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
360c5ac4cb3a5124860ccfc7e4ff66251c55a5595caGlenn Kastenvoid AudioMixer::deleteTrackName(int name)
361c5ac4cb3a5124860ccfc7e4ff66251c55a5595caGlenn Kasten{
3629bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    ALOGV("AudioMixer::deleteTrackName(%d)", name);
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    name -= TRACK0;
3645798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
365237a624f674800d2300806b115eee8c9bb7db033Glenn Kasten    ALOGV("deleteTrackName(%d)", name);
366237a624f674800d2300806b115eee8c9bb7db033Glenn Kasten    track_t& track(mState.tracks[ name ]);
3674c340c6521b634f159d2d6bc4e9359226fd8edf8Glenn Kasten    if (track.enabled) {
3684c340c6521b634f159d2d6bc4e9359226fd8edf8Glenn Kasten        track.enabled = false;
369237a624f674800d2300806b115eee8c9bb7db033Glenn Kasten        invalidateState(1<<name);
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
3714e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten    // delete the resampler
3724e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten    delete track.resampler;
3734e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten    track.resampler = NULL;
3749bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    // delete the downmixer
3759bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi    unprepareTrackForDownmix(&mState.tracks[name], name);
3769bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi
377237a624f674800d2300806b115eee8c9bb7db033Glenn Kasten    mTrackNames &= ~(1<<name);
378c5ac4cb3a5124860ccfc7e4ff66251c55a5595caGlenn Kasten}
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3809c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kastenvoid AudioMixer::enable(int name)
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
3829c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten    name -= TRACK0;
3835798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
3849c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten    track_t& track = mState.tracks[name];
3859c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten
3864c340c6521b634f159d2d6bc4e9359226fd8edf8Glenn Kasten    if (!track.enabled) {
3874c340c6521b634f159d2d6bc4e9359226fd8edf8Glenn Kasten        track.enabled = true;
3889c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        ALOGV("enable(%d)", name);
3899c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        invalidateState(1 << name);
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3939c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kastenvoid AudioMixer::disable(int name)
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
3959c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten    name -= TRACK0;
3965798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
3979c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten    track_t& track = mState.tracks[name];
3989c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten
3994c340c6521b634f159d2d6bc4e9359226fd8edf8Glenn Kasten    if (track.enabled) {
4004c340c6521b634f159d2d6bc4e9359226fd8edf8Glenn Kasten        track.enabled = false;
4019c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        ALOGV("disable(%d)", name);
4029c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        invalidateState(1 << name);
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
4069c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kastenvoid AudioMixer::setParameter(int name, int target, int param, void *value)
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
4089c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten    name -= TRACK0;
4095798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
4109c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten    track_t& track = mState.tracks[name];
41165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int valueInt = (int)value;
41365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    int32_t *valueBuf = (int32_t *)value;
41465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    switch (target) {
416788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    case TRACK:
4189c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        switch (param) {
419788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten        case CHANNEL_MASK: {
420254af180475346b6186b49c297f340c9c4817511Glenn Kasten            audio_channel_mask_t mask = (audio_channel_mask_t) value;
4219c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            if (track.channelMask != mask) {
4225798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten                uint32_t channelCount = popcount(mask);
4237d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi                ALOG_ASSERT((channelCount <= MAX_NUM_CHANNELS_TO_DOWNMIX) && channelCount);
4249c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                track.channelMask = mask;
4259c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                track.channelCount = channelCount;
4269bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi                // the mask has changed, does this track need a downmixer?
4279bd23229fdec1657398abc682ccccfce1c95f8aaJean-Michel Trivi                initTrackDownmix(&mState.tracks[name], name, mask);
428788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten                ALOGV("setParameter(TRACK, CHANNEL_MASK, %x)", mask);
4299c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                invalidateState(1 << name);
43065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
431788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten            } break;
432788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten        case MAIN_BUFFER:
4339c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            if (track.mainBuffer != valueBuf) {
4349c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                track.mainBuffer = valueBuf;
4353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("setParameter(TRACK, MAIN_BUFFER, %p)", valueBuf);
4369c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                invalidateState(1 << name);
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
438788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten            break;
439788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten        case AUX_BUFFER:
4409c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            if (track.auxBuffer != valueBuf) {
4419c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                track.auxBuffer = valueBuf;
4423856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("setParameter(TRACK, AUX_BUFFER, %p)", valueBuf);
4439c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                invalidateState(1 << name);
44465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
445788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten            break;
446deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten        case FORMAT:
447deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten            ALOG_ASSERT(valueInt == AUDIO_FORMAT_PCM_16_BIT);
448deeb1282621f3177ad667360b40eef8e4fedb298Glenn Kasten            break;
4497d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        // FIXME do we want to support setting the downmix type from AudioFlinger?
4507d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        //         for a specific track? or per mixer?
4517d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi        /* case DOWNMIX_TYPE:
4527d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi            break          */
453788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten        default:
4545798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten            LOG_FATAL("bad param");
45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
45665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        break;
457788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten
45865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    case RESAMPLE:
4599c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        switch (param) {
4609c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        case SAMPLE_RATE:
4615798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten            ALOG_ASSERT(valueInt > 0, "bad sample rate %d", valueInt);
462788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten            if (track.setResampler(uint32_t(valueInt), mSampleRate)) {
463788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten                ALOGV("setParameter(RESAMPLE, SAMPLE_RATE, %u)",
464788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten                        uint32_t(valueInt));
4659c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                invalidateState(1 << name);
46665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
4679c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            break;
4689c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        case RESET:
469243f5f91755c01614a8cafe90b0806396e22d553Eric Laurent            track.resetResampler();
4709c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            invalidateState(1 << name);
4719c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            break;
4724e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten        case REMOVE:
4734e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten            delete track.resampler;
4744e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten            track.resampler = NULL;
4754e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten            track.sampleRate = mSampleRate;
4764e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten            invalidateState(1 << name);
4774e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten            break;
478788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten        default:
4795798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten            LOG_FATAL("bad param");
480243f5f91755c01614a8cafe90b0806396e22d553Eric Laurent        }
48165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        break;
482788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten
48365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    case RAMP_VOLUME:
48465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    case VOLUME:
4859c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        switch (param) {
486788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten        case VOLUME0:
4879c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        case VOLUME1:
4889c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            if (track.volume[param-VOLUME0] != valueInt) {
4893856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("setParameter(VOLUME, VOLUME0/1: %04x)", valueInt);
4909c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                track.prevVolume[param-VOLUME0] = track.volume[param-VOLUME0] << 16;
4919c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                track.volume[param-VOLUME0] = valueInt;
49265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                if (target == VOLUME) {
4939c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                    track.prevVolume[param-VOLUME0] = valueInt << 16;
4949c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                    track.volumeInc[param-VOLUME0] = 0;
49565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                } else {
4969c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                    int32_t d = (valueInt<<16) - track.prevVolume[param-VOLUME0];
49765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    int32_t volInc = d / int32_t(mState.frameCount);
4989c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                    track.volumeInc[param-VOLUME0] = volInc;
49965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (volInc == 0) {
5009c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                        track.prevVolume[param-VOLUME0] = valueInt << 16;
50165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
50265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
5039c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                invalidateState(1 << name);
50465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
5059c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            break;
5069c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten        case AUXLEVEL:
5075798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten            //ALOG_ASSERT(0 <= valueInt && valueInt <= MAX_GAIN_INT, "bad aux level %d", valueInt);
50865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (track.auxLevel != valueInt) {
5093856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("setParameter(VOLUME, AUXLEVEL: %04x)", valueInt);
51065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                track.prevAuxLevel = track.auxLevel << 16;
51165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                track.auxLevel = valueInt;
51265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                if (target == VOLUME) {
51365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    track.prevAuxLevel = valueInt << 16;
51465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    track.auxInc = 0;
51565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                } else {
51665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    int32_t d = (valueInt<<16) - track.prevAuxLevel;
51765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    int32_t volInc = d / int32_t(mState.frameCount);
51865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    track.auxInc = volInc;
51965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (volInc == 0) {
52065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        track.prevAuxLevel = valueInt << 16;
52165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
52265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
5239c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten                invalidateState(1 << name);
52465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
5259c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten            break;
526788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten        default:
5275798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten            LOG_FATAL("bad param");
52865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
52965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        break;
530788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten
531788040c5189bbdaf567ce4b29ffd1db08ea1020cGlenn Kasten    default:
5325798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten        LOG_FATAL("bad target");
53365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
53465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
53565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioMixer::track_t::setResampler(uint32_t value, uint32_t devSampleRate)
53765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
5384e2293f29f2e719af1245d365747ea06d074b345Glenn Kasten    if (value != devSampleRate || resampler != NULL) {
53965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (sampleRate != value) {
54065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            sampleRate = value;
541e0feee3da22beeffbd9357540e265f13b2119cbbGlenn Kasten            if (resampler == NULL) {
542ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                ALOGV("creating resampler from track %d Hz to device %d Hz", value, devSampleRate);
543ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                AudioResampler::src_quality quality;
544ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                // force lowest quality level resampler if use case isn't music or video
545ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                // FIXME this is flawed for dynamic sample rates, as we choose the resampler
546ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                // quality level based on the initial ratio, but that could change later.
547ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                // Should have a way to distinguish tracks with static ratios vs. dynamic ratios.
548ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                if (!((value == 44100 && devSampleRate == 48000) ||
549ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                      (value == 48000 && devSampleRate == 44100))) {
550ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                    quality = AudioResampler::LOW_QUALITY;
551ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                } else {
552ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                    quality = AudioResampler::DEFAULT_QUALITY;
553ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                }
55465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                resampler = AudioResampler::create(
555acb86cccbd9d245439a04cef0bcefa589addaa4cJean-Michel Trivi                        format,
556acb86cccbd9d245439a04cef0bcefa589addaa4cJean-Michel Trivi                        // the resampler sees the number of channels after the downmixer, if any
557acb86cccbd9d245439a04cef0bcefa589addaa4cJean-Michel Trivi                        downmixerBufferProvider != NULL ? MAX_NUM_CHANNELS : channelCount,
558ac6020508acedd316391dee42329040bf45f8d90Glenn Kasten                        devSampleRate, quality);
5594ff14bae91075eb274eb1c2975982358946e7e63John Grossman                resampler->setLocalTimeFreq(localTimeFreq);
56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
56165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return true;
56265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return false;
56565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
56765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianinline
56865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioMixer::track_t::adjustVolumeRamp(bool aux)
56965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
570f9a27779634ce3a01e5957f234cd04eba74fa07fGlenn Kasten    for (uint32_t i=0 ; i<MAX_NUM_CHANNELS ; i++) {
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (((volumeInc[i]>0) && (((prevVolume[i]+volumeInc[i])>>16) >= volume[i])) ||
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ((volumeInc[i]<0) && (((prevVolume[i]+volumeInc[i])>>16) <= volume[i]))) {
57365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            volumeInc[i] = 0;
57465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            prevVolume[i] = volume[i]<<16;
57565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
57665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
57765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (aux) {
57865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (((auxInc>0) && (((prevAuxLevel+auxInc)>>16) >= auxLevel)) ||
57965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ((auxInc<0) && (((prevAuxLevel+auxInc)>>16) <= auxLevel))) {
58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            auxInc = 0;
58165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            prevAuxLevel = auxLevel<<16;
58265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
58365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
58465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
58565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
586c59c004a3a6042c0990d71179f88eee2ce781e3cGlenn Kastensize_t AudioMixer::getUnreleasedFrames(int name) const
587071ccd5a9702500f3f7d62ef881300914926184dEric Laurent{
588071ccd5a9702500f3f7d62ef881300914926184dEric Laurent    name -= TRACK0;
589071ccd5a9702500f3f7d62ef881300914926184dEric Laurent    if (uint32_t(name) < MAX_NUM_TRACKS) {
590c59c004a3a6042c0990d71179f88eee2ce781e3cGlenn Kasten        return mState.tracks[name].getUnreleasedFrames();
591071ccd5a9702500f3f7d62ef881300914926184dEric Laurent    }
592071ccd5a9702500f3f7d62ef881300914926184dEric Laurent    return 0;
593071ccd5a9702500f3f7d62ef881300914926184dEric Laurent}
59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
59501c4ebf6b794493898114a502ed36de13137f7e5Glenn Kastenvoid AudioMixer::setBufferProvider(int name, AudioBufferProvider* bufferProvider)
59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
5979c56d4ae6212c21ce5fd71ed534eb195983a07c1Glenn Kasten    name -= TRACK0;
5985798d4ebf236357a4b13246f40e52b90a34d09a4Glenn Kasten    ALOG_ASSERT(uint32_t(name) < MAX_NUM_TRACKS, "bad track name %d", name);
5997d5b26230a179cd7bcc01f6578cd80d8c15a92a5Jean-Michel Trivi
600