/*----------------------------------------------------------------------------
 *
 * File:
 * eas_mixer.c
 *
 * Contents and purpose:
 * This file contains the critical components of the mix engine that
 * must be optimized for best performance.
 *
 * Copyright Sonic Network Inc. 2005

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *----------------------------------------------------------------------------
 * Revision Control:
 *   $Revision: 706 $
 *   $Date: 2007-05-31 17:22:51 -0700 (Thu, 31 May 2007) $
 *----------------------------------------------------------------------------
*/

//3 dls: This module is in the midst of being converted from a synth
//3 specific module to a general purpose mix engine

/*------------------------------------
 * includes
 *------------------------------------
*/
#include "eas_data.h"
#include "eas_host.h"
#include "eas_math.h"
#include "eas_mixer.h"
#include "eas_config.h"
#include "eas_report.h"

#ifdef _MAXIMIZER_ENABLED
EAS_I32 MaximizerProcess (EAS_VOID_PTR pInstData, EAS_I32 *pSrc, EAS_I32 *pDst, EAS_I32 numSamples);
#endif

/*------------------------------------
 * defines
 *------------------------------------
*/

/* need to boost stereo by ~3dB to compensate for the panner */
#define STEREO_3DB_GAIN_BOOST       512

/*----------------------------------------------------------------------------
 * EAS_MixEngineInit()
 *----------------------------------------------------------------------------
 * Purpose:
 * Prepares the mix engine for work, allocates buffers, locates effects modules, etc.
 *
 * Inputs:
 * pEASData         - instance data
 * pInstData        - pointer to variable to receive instance data handle
 *
 * Outputs:
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_MixEngineInit (S_EAS_DATA *pEASData)
{

    /* check Configuration Module for mix buffer allocation */
    if (pEASData->staticMemoryModel)
        pEASData->pMixBuffer = EAS_CMEnumData(EAS_CM_MIX_BUFFER);
    else
        pEASData->pMixBuffer = EAS_HWMalloc(pEASData->hwInstData, BUFFER_SIZE_IN_MONO_SAMPLES * NUM_OUTPUT_CHANNELS * sizeof(EAS_I32));
    if (pEASData->pMixBuffer == NULL)
    {
        { /* dpp: EAS_ReportEx(_EAS_SEVERITY_FATAL, "Failed to allocate mix buffer memory\n"); */ }
        return EAS_ERROR_MALLOC_FAILED;
    }
    EAS_HWMemSet((void *)(pEASData->pMixBuffer), 0, BUFFER_SIZE_IN_MONO_SAMPLES * NUM_OUTPUT_CHANNELS * sizeof(EAS_I32));

    return EAS_SUCCESS;
}

/*----------------------------------------------------------------------------
 * EAS_MixEnginePrep()
 *----------------------------------------------------------------------------
 * Purpose:
 * Performs prep before synthesize a buffer of audio, such as clearing
 * audio buffers, etc.
 *
 * Inputs:
 * psEASData - pointer to overall EAS data structure
 *
 * Outputs:
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
void EAS_MixEnginePrep (S_EAS_DATA *pEASData, EAS_I32 numSamples)
{

    /* clear the mix buffer */
#if (NUM_OUTPUT_CHANNELS == 2)
    EAS_HWMemSet(pEASData->pMixBuffer, 0, numSamples * (EAS_I32) sizeof(long) * 2);
#else
    EAS_HWMemSet(pEASData->pMixBuffer, 0, (EAS_I32) numSamples * (EAS_I32) sizeof(long));
#endif

    /* need to clear other side-chain effect buffers (chorus & reverb) */
}

/*----------------------------------------------------------------------------
 * EAS_MixEnginePost
 *----------------------------------------------------------------------------
 * Purpose:
 * This routine does the post-processing after all voices have been
 * synthesized. It calls any sweeteners and does the final mixdown to
 * the output buffer.
 *
 * Inputs:
 *
 * Outputs:
 *
 * Notes:
 *----------------------------------------------------------------------------
*/
void EAS_MixEnginePost (S_EAS_DATA *pEASData, EAS_I32 numSamples)
{
    EAS_U16 gain;

//3 dls: Need to restore the mix engine metrics

    /* calculate the gain multiplier */
#ifdef _MAXIMIZER_ENABLED
    if (pEASData->effectsModules[EAS_MODULE_MAXIMIZER].effect)
    {
        EAS_I32 temp;
        temp = MaximizerProcess(pEASData->effectsModules[EAS_MODULE_MAXIMIZER].effectData, pEASData->pMixBuffer, pEASData->pMixBuffer, numSamples);
        temp = (temp * pEASData->masterGain) >> 15;
        if (temp > 32767)
            gain = 32767;
        else
            gain = (EAS_U16) temp;
    }
    else
        gain = (EAS_U16) pEASData->masterGain;
#else
    gain = (EAS_U16) pEASData->masterGain;
#endif

    /* Not using all the gain bits for now
     * Reduce the input to the compressor by 6dB to prevent saturation
     */
#ifdef _COMPRESSOR_ENABLED
    if (pEASData->effectsModules[EAS_MODULE_COMPRESSOR].effectData)
        gain = gain >> 5;
    else
        gain = gain >> 4;
#else
    gain = gain >> 4;
#endif

    /* convert 32-bit mix buffer to 16-bit output format */
#if (NUM_OUTPUT_CHANNELS == 2)
    SynthMasterGain(pEASData->pMixBuffer, pEASData->pOutputAudioBuffer, gain, (EAS_U16) ((EAS_U16) numSamples * 2));
#else
    SynthMasterGain(pEASData->pMixBuffer, pEASData->pOutputAudioBuffer, gain, (EAS_U16) numSamples);
#endif

#ifdef _ENHANCER_ENABLED
    /* enhancer effect */
    if (pEASData->effectsModules[EAS_MODULE_ENHANCER].effectData)
        (*pEASData->effectsModules[EAS_MODULE_ENHANCER].effect->pfProcess)
            (pEASData->effectsModules[EAS_MODULE_ENHANCER].effectData,
            pEASData->pOutputAudioBuffer,
            pEASData->pOutputAudioBuffer,
            numSamples);
#endif

#ifdef _GRAPHIC_EQ_ENABLED
    /* graphic EQ effect */
    if (pEASData->effectsModules[EAS_MODULE_GRAPHIC_EQ].effectData)
        (*pEASData->effectsModules[EAS_MODULE_GRAPHIC_EQ].effect->pfProcess)
            (pEASData->effectsModules[EAS_MODULE_GRAPHIC_EQ].effectData,
            pEASData->pOutputAudioBuffer,
            pEASData->pOutputAudioBuffer,
            numSamples);
#endif

#ifdef _COMPRESSOR_ENABLED
    /* compressor effect */
    if (pEASData->effectsModules[EAS_MODULE_COMPRESSOR].effectData)
        (*pEASData->effectsModules[EAS_MODULE_COMPRESSOR].effect->pfProcess)
            (pEASData->effectsModules[EAS_MODULE_COMPRESSOR].effectData,
            pEASData->pOutputAudioBuffer,
            pEASData->pOutputAudioBuffer,
            numSamples);
#endif

#ifdef _WOW_ENABLED
    /* WOW requires a 32-bit buffer, borrow the mix buffer and
     * pass it as the destination buffer
     */
    /*lint -e{740} temporarily passing a parameter through an existing I/F */
    if (pEASData->effectsModules[EAS_MODULE_WOW].effectData)
        (*pEASData->effectsModules[EAS_MODULE_WOW].effect->pfProcess)
            (pEASData->effectsModules[EAS_MODULE_WOW].effectData,
            pEASData->pOutputAudioBuffer,
            (EAS_PCM*) pEASData->pMixBuffer,
            numSamples);
#endif

#ifdef _TONECONTROLEQ_ENABLED
    /* ToneControlEQ effect */
    if (pEASData->effectsModules[EAS_MODULE_TONECONTROLEQ].effectData)
        (*pEASData->effectsModules[EAS_MODULE_TONECONTROLEQ].effect->pfProcess)
            (pEASData->effectsModules[EAS_MODULE_TONECONTROLEQ].effectData,
            pEASData->pOutputAudioBuffer,
            pEASData->pOutputAudioBuffer,
            numSamples);
#endif

#ifdef _REVERB_ENABLED
    /* Reverb effect */
    if (pEASData->effectsModules[EAS_MODULE_REVERB].effectData)
        (*pEASData->effectsModules[EAS_MODULE_REVERB].effect->pfProcess)
            (pEASData->effectsModules[EAS_MODULE_REVERB].effectData,
            pEASData->pOutputAudioBuffer,
            pEASData->pOutputAudioBuffer,
            numSamples);
#endif

#ifdef _CHORUS_ENABLED
    /* Chorus effect */
    if (pEASData->effectsModules[EAS_MODULE_CHORUS].effectData)
        (*pEASData->effectsModules[EAS_MODULE_CHORUS].effect->pfProcess)
            (pEASData->effectsModules[EAS_MODULE_CHORUS].effectData,
            pEASData->pOutputAudioBuffer,
            pEASData->pOutputAudioBuffer,
            numSamples);
#endif

}

#ifndef NATIVE_EAS_KERNEL
/*----------------------------------------------------------------------------
 * SynthMasterGain
 *----------------------------------------------------------------------------
 * Purpose:
 * Mixes down audio from 32-bit to 16-bit target buffer
 *
 * Inputs:
 *
 * Outputs:
 *
 *----------------------------------------------------------------------------
*/
void SynthMasterGain (long *pInputBuffer, EAS_PCM *pOutputBuffer, EAS_U16 nGain, EAS_U16 numSamples) {

    /* loop through the buffer */
    while (numSamples--) {
        long s;

        /* read a sample from the input buffer and add some guard bits */
        s = *pInputBuffer++;

        /* add some guard bits */
        /*lint -e{704} <avoid divide for performance>*/
        s = s >> 7;

        /* apply master gain */
        s *= (long) nGain;

        /* shift to lower 16-bits */
        /*lint -e{704} <avoid divide for performance>*/
        s = s >> 9;

        /* saturate */
        s = SATURATE(s);

        *pOutputBuffer++ = (EAS_PCM)s;
    }
}
#endif

/*----------------------------------------------------------------------------
 * EAS_MixEngineShutdown()
 *----------------------------------------------------------------------------
 * Purpose:
 * Shuts down effects modules and deallocates memory
 *
 * Inputs:
 * pEASData         - instance data
 * pInstData        - instance data handle
 *
 * Outputs:
 *
 * Side Effects:
 *
 *----------------------------------------------------------------------------
*/
EAS_RESULT EAS_MixEngineShutdown (S_EAS_DATA *pEASData)
{

    /* check Configuration Module for static memory allocation */
    if (!pEASData->staticMemoryModel && (pEASData->pMixBuffer != NULL))
        EAS_HWFree(pEASData->hwInstData, pEASData->pMixBuffer);

    return EAS_SUCCESS;
}

#ifdef UNIFIED_MIXER
#ifndef NATIVE_MIX_STREAM
/*----------------------------------------------------------------------------
 * EAS_MixStream
 *----------------------------------------------------------------------------
 * Mix a 16-bit stream into a 32-bit buffer
 *
 * pInputBuffer 16-bit input buffer
 * pMixBuffer   32-bit mix buffer
 * numSamples   number of samples to mix
 * gainLeft     initial gain left or mono
 * gainRight    initial gain right
 * gainLeft     left gain increment per sample
 * gainRight    right gain increment per sample
 * flags        bit 0 = stereo source
 *              bit 1 = stereo output
 *----------------------------------------------------------------------------
*/
void EAS_MixStream (EAS_PCM *pInputBuffer, EAS_I32 *pMixBuffer, EAS_I32 numSamples, EAS_I32 gainLeft, EAS_I32 gainRight, EAS_I32 gainIncLeft, EAS_I32 gainIncRight, EAS_I32 flags)
{
    EAS_I32 temp;
    EAS_INT src, dest;

    /* NOTE: There are a lot of optimizations that can be done
     * in the native implementations based on register
     * availability, etc. For example, it may make sense to
     * break this down into 8 separate routines:
     *
     * 1. Mono source to mono output
     * 2. Mono source to stereo output
     * 3. Stereo source to mono output
     * 4. Stereo source to stereo output
     * 5. Mono source to mono output - no gain change
     * 6. Mono source to stereo output - no gain change
     * 7. Stereo source to mono output - no gain change
     * 8. Stereo source to stereo output - no gain change
     *
     * Other possibilities include loop unrolling, skipping
     * a gain calculation every 2 or 4 samples, etc.
     */

    /* no gain change, use fast loops */
    if ((gainIncLeft == 0) && (gainIncRight == 0))
    {
        switch (flags & (MIX_FLAGS_STEREO_SOURCE | MIX_FLAGS_STEREO_OUTPUT))
        {
            /* mono to mono */
            case 0:
                gainLeft >>= 15;
                for (src = dest = 0; src < numSamples; src++, dest++)
                {

                    pMixBuffer[dest] += (pInputBuffer[src] * gainLeft) >> NUM_MIXER_GUARD_BITS;
                }
                break;

            /* mono to stereo */
            case MIX_FLAGS_STEREO_OUTPUT:
                gainLeft >>= 15;
                gainRight >>= 15;
                for (src = dest = 0; src < numSamples; src++, dest+=2)
                {
                    pMixBuffer[dest] += (pInputBuffer[src] * gainLeft) >> NUM_MIXER_GUARD_BITS;
                    pMixBuffer[dest+1] += (pInputBuffer[src] * gainRight) >> NUM_MIXER_GUARD_BITS;
                }
                break;

            /* stereo to mono */
            case MIX_FLAGS_STEREO_SOURCE:
                gainLeft >>= 15;
                gainRight >>= 15;
                for (src = dest = 0; src < numSamples; src+=2, dest++)
                {
                    temp = (pInputBuffer[src] * gainLeft) >> NUM_MIXER_GUARD_BITS;
                    temp += ((pInputBuffer[src+1] * gainRight) >> NUM_MIXER_GUARD_BITS);
                    pMixBuffer[dest] += temp;
                }
                break;

            /* stereo to stereo */
            case MIX_FLAGS_STEREO_SOURCE | MIX_FLAGS_STEREO_OUTPUT:
                gainLeft >>= 15;
                gainRight >>= 15;
                for (src = dest = 0; src < numSamples; src+=2, dest+=2)
                {
                    pMixBuffer[dest] += (pInputBuffer[src] * gainLeft) >> NUM_MIXER_GUARD_BITS;
                    pMixBuffer[dest+1] += (pInputBuffer[src+1] * gainRight) >> NUM_MIXER_GUARD_BITS;
                }
                break;
        }
    }

    /* gain change - do gain increment */
    else
    {
        switch (flags & (MIX_FLAGS_STEREO_SOURCE | MIX_FLAGS_STEREO_OUTPUT))
        {
            /* mono to mono */
            case 0:
                for (src = dest = 0; src < numSamples; src++, dest++)
                {
                    gainLeft += gainIncLeft;
                    pMixBuffer[dest] += (pInputBuffer[src] * (gainLeft >> 15)) >> NUM_MIXER_GUARD_BITS;
                }
                break;

            /* mono to stereo */
            case MIX_FLAGS_STEREO_OUTPUT:
                for (src = dest = 0; src < numSamples; src++, dest+=2)
                {
                    gainLeft += gainIncLeft;
                    gainRight += gainIncRight;
                    pMixBuffer[dest] += (pInputBuffer[src] * (gainLeft >> 15)) >> NUM_MIXER_GUARD_BITS;
                    pMixBuffer[dest+1] += (pInputBuffer[src] * (gainRight >> 15)) >> NUM_MIXER_GUARD_BITS;
                }
                break;

            /* stereo to mono */
            case MIX_FLAGS_STEREO_SOURCE:
                for (src = dest = 0; src < numSamples; src+=2, dest++)
                {
                    gainLeft += gainIncLeft;
                    gainRight += gainIncRight;
                    temp = (pInputBuffer[src] * (gainLeft >> 15)) >> NUM_MIXER_GUARD_BITS;
                    temp += ((pInputBuffer[src+1] * (gainRight >> 15)) >> NUM_MIXER_GUARD_BITS);
                    pMixBuffer[dest] += temp;
                }
                break;

            /* stereo to stereo */
            case MIX_FLAGS_STEREO_SOURCE | MIX_FLAGS_STEREO_OUTPUT:
                for (src = dest = 0; src < numSamples; src+=2, dest+=2)
                {
                    gainLeft += gainIncLeft;
                    gainRight += gainIncRight;
                    pMixBuffer[dest] += (pInputBuffer[src] * (gainLeft >> 15)) >> NUM_MIXER_GUARD_BITS;
                    pMixBuffer[dest+1] += (pInputBuffer[src+1] * (gainRight >> 15)) >> NUM_MIXER_GUARD_BITS;
                }
                break;
        }
    }
}
#endif
#endif

