⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mixengine.cpp

📁 著名的 helix realplayer 基于手机 symbian 系统的 播放器全套源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* ***** BEGIN LICENSE BLOCK ***** 
 * Version: RCSL 1.0/RPSL 1.0 
 *  
 * Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved. 
 *      
 * The contents of this file, and the files included with this file, are 
 * subject to the current version of the RealNetworks Public Source License 
 * Version 1.0 (the "RPSL") available at 
 * http://www.helixcommunity.org/content/rpsl unless you have licensed 
 * the file under the RealNetworks Community Source License Version 1.0 
 * (the "RCSL") available at http://www.helixcommunity.org/content/rcsl, 
 * in which case the RCSL will apply. You may also obtain the license terms 
 * directly from RealNetworks.  You may not use this file except in 
 * compliance with the RPSL or, if you have a valid RCSL with RealNetworks 
 * applicable to this file, the RCSL.  Please see the applicable RPSL or 
 * RCSL for the rights, obligations and limitations governing use of the 
 * contents of the file.  
 *  
 * This file is part of the Helix DNA Technology. RealNetworks is the 
 * developer of the Original Code and owns the copyrights in the portions 
 * it created. 
 *  
 * This file, and the files included with this file, is distributed and made 
 * available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
 * EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS 
 * FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
 * 
 * Technology Compatibility Kit Test Suite(s) Location: 
 *    http://www.helixcommunity.org/content/tck 
 * 
 * Contributor(s): 
 *  
 * ***** END LICENSE BLOCK ***** */ 


#include "hlxclib/limits.h"
#include "hlxclib/math.h"

#include "hxtypes.h"
#include "hxresult.h"
#include "hxassert.h"

#include "mixengine.h"

#ifdef HELIX_FEATURE_RESAMPLER
#include "RAResampler.h"
#endif
#ifdef HELIX_FEATURE_GAINTOOL
#include "gain.h"
#endif
#ifdef HELIX_FEATURE_CROSSFADE
#include "xfade.h"
#endif
#ifdef HELIX_FEATURE_LIMITER
#include "limiter.h"
#endif

#include "math64.h"

/*
   DSP is done in four stages:

   - input
       (1)
   - downmix
       (2)
   - resample
       (3)
   - mix into output buffer
       (4)

   In the general case, the number of samples/call in pipes (1)-(4) will be different.
   In the code below, nSamples_X denotes the number of samples flowing in pipe (X)

   For example, say the input is 44100 stereo, and the output is 22050 3-channel,
   and for some reason we want to downmix to mono before going through the resampler.

   if nSamples_4 == 3 * 22050 (1 seconds' worth of data), then
   nSamples_3 = nSamples_4 / 3 = 22050
   nSamples_2 = nSamples_3 * 44100/22050 = 44100
   nSamples_1 = nSamples_2 * 2 / 1 = 88200 (1 seconds' worth of data on the input);

   For convenience, we might also count sample frames; that's samples_X/nChannels_X

   All variables are postfixed by a indicator of where they are relevant; for example,
   m_nChannels_2 is the number of channels after downmixing, and m_pBuffer3 is the
   output buffer for the resampler.
 */

HXAudioSvcMixEngine::HXAudioSvcMixEngine()
: m_pResampler(0)
, m_pXFader(0)
, m_pGaintool(0)
, m_pLimiter(0)
, m_pBuffer_1(0)
, m_pBuffer_3(0)
, m_ulBytesPerSample(2)
, m_eCrossFadeDirection(FADE_OUT)
{}

HXAudioSvcMixEngine::~HXAudioSvcMixEngine()
{
    releaseResources() ;
}

void HXAudioSvcMixEngine::releaseResources()
{
    if (m_pBuffer_1) delete[] m_pBuffer_1 ; m_pBuffer_1 = 0 ;
    if (m_pBuffer_3) delete[] m_pBuffer_3 ; m_pBuffer_3 = 0 ;
#ifdef HELIX_FEATURE_RESAMPLER
    if (m_pResampler) delete m_pResampler ; m_pResampler = 0 ;
#endif /* HELIX_FEATURE_RESAMPLER */
#ifdef HELIX_FEATURE_GAINTOOL
    if (m_pGaintool) gainFree(m_pGaintool); m_pGaintool = 0 ;
#endif
#ifdef HELIX_FEATURE_CROSSFADE
    if (m_pXFader) XFader_free(m_pXFader) ; m_pXFader = 0 ;
#endif
#ifdef HELIX_FEATURE_LIMITER
    if (m_pLimiter) LimiterFree(m_pLimiter); m_pLimiter = 0 ;
#endif
}

HX_RESULT HXAudioSvcMixEngine::SetSampleConverter(CAudioSvcSampleConverter *pCvt)
{
    m_pCvt = pCvt ;
    return pCvt ? HXR_OK : HXR_FAIL ;
}

HX_RESULT HXAudioSvcMixEngine::SetupResamplerAndBuffers(void)
{
    if (m_ulSampleRate_1_2 == m_ulSampleRate_3_4)
    {
        // no resampling.
        m_ulChunkSize_1 = BATCHSIZE ;
        m_ulChunkSize_1 -= m_ulChunkSize_1 % m_nChannels_1 ;

        m_ulChunkSize_3 = m_ulChunkSize_1 / m_nChannels_1 * m_nChannels_2_3 ;
    }
    else
    {
#ifdef HELIX_FEATURE_RESAMPLER
        HX_RESULT res = RAExactResampler::Create(&m_pResampler, m_ulSampleRate_1_2, m_ulSampleRate_3_4, m_nChannels_2_3, NBITS_PER_AUDIOSAMPLE == 32 ? RAExactResampler::_INT32 : RAExactResampler::_INT16) ;
        if (FAILED(res))
            return res ;

        // determine the chunk sizes on resampler input and output. The side with the higher
        // datarate limits the other side
        if (m_nChannels_1 * m_ulSampleRate_1_2 <= m_nChannels_2_3 * m_ulSampleRate_3_4)
        {
            // downstream (right) side limits size
            m_ulChunkSize_3 = BATCHSIZE ;
            m_ulChunkSize_3 -= m_ulChunkSize_3 % m_nChannels_2_3 ;

            m_ulChunkSize_1 = m_pResampler->GetMinInput(m_ulChunkSize_3) ;
            m_ulChunkSize_1 = m_ulChunkSize_1 / m_nChannels_2_3 * m_nChannels_1 ;
        }
        else
        {
            // upstream side limits size
            m_ulChunkSize_1 = BATCHSIZE ;
            m_ulChunkSize_1 -= m_ulChunkSize_1 % m_nChannels_1 ;

            m_ulChunkSize_3 = m_pResampler->GetMaxOutput(m_ulChunkSize_1 / m_nChannels_1 * m_nChannels_2_3) ;

            while ((unsigned)m_pResampler->GetMinInput(m_ulChunkSize_3) / m_nChannels_2_3 * m_nChannels_1 > m_ulChunkSize_1)
            {
                m_ulChunkSize_3 -= m_nChannels_2_3 ;
            }
        }
        m_ulBufferSize_3 = m_ulChunkSize_3 + m_pResampler->GetMaxOutput(m_nChannels_2_3) ;
#else
        return HXR_NOTIMPL ; // resampler not implemented
#endif
    }

    // delay allocation of sample buffers until they are really needed.

    return HXR_OK ;
}

HX_RESULT HXAudioSvcMixEngine::Init(INT32 sampleRateIn, INT32 sampleRateOut, INT32 nChannelsIn, INT32 nChannelsOut)
{
    HX_RESULT res = HXR_OK;

    // if we have any old resources, release them
    releaseResources() ;

    m_ulSampleRate_1_2 = sampleRateIn ;
    m_ulSampleRate_3_4 = sampleRateOut ;
    m_nChannels_1 = nChannelsIn ;
    m_nChannels_4 = nChannelsOut ;

    res = SetupUpDownmix() ;
    if (FAILED(res))
        return res ;

    res = SetupResamplerAndBuffers() ;
    if (FAILED(res))
        return res ;

#ifdef HELIX_FEATURE_GAINTOOL
    m_pGaintool = gainInit(m_ulSampleRate_1_2, m_nChannels_2_3, 0) ;
    gainSetTimeConstant(100, m_pGaintool) ;
    gainSetImmediate(0.0, m_pGaintool) ;
#endif

#ifdef HELIX_FEATURE_CROSSFADE
    m_pXFader = XFader_init(m_ulSampleRate_1_2, m_nChannels_2_3, XFader_sin2tab) ;
#endif
    ResetTimeLineInMillis(0) ;

    return HXR_OK ;
}

HX_RESULT HXAudioSvcMixEngine::ResetTimeLineInMillis(INT64 millis)
{
    m_nOutputSamplesLeft_3 = 0 ;
    m_ulResamplerPhase = 0;

    // set the cross fade state to fade in, and the time so that we are post the fade in.
    m_llFadeStart = INT_MIN ; // or something really small
    m_eCrossFadeDirection = FADE_OUT ;
    m_bPastXFade = FALSE ;

    // sample frames, output side
    m_llTimestamp_1 = m_llTimestamp_3 = millis * m_ulSampleRate_3_4 / 1000 ; // llBufTimeInSamples / m_nChannels_4 ;
    // correct for resampler delay
#ifdef HELIX_FEATURE_RESAMPLER
    if (m_pResampler) m_llTimestamp_1 -= m_pResampler->GetDelay() ;
#endif
    // convert to input side, samples
    m_llTimestamp_1 = m_llTimestamp_1 * m_ulSampleRate_1_2 / m_ulSampleRate_3_4 * m_nChannels_1 ;
    // convert to samples
    m_llTimestamp_3 *= m_nChannels_2_3 ;

    return HXR_OK ;
}

void HXAudioSvcMixEngine::GetMixRange(UINT32 nBytesToMix, INT64& llStart, INT64& llEnd) const
{
    llStart = m_llTimestamp_1 ;
    // number of samples at resampler output
    INT32 n = nBytesToMix / (m_ulBytesPerSample * m_nChannels_4) * m_nChannels_2_3 ;
#ifdef HELIX_FEATURE_RESAMPLER
    if (m_pResampler) n = m_pResampler->GetMinInput(n - m_nOutputSamplesLeft_3) ;
#endif
    // number of samples at input
    n = n / m_nChannels_2_3 * m_nChannels_1 ;
    llEnd   = llStart + n ;
}

INT64 HXAudioSvcMixEngine::GetNextMixTimeMillis(void) const
{
    return INT64(1000) * m_llTimestamp_1 / (m_ulSampleRate_1_2 * m_nChannels_1) ;
}

HX_RESULT HXAudioSvcMixEngine::SetOutputBytesPerSample(UINT32 bps)
{
    switch (bps)
    {
    case 2: case 4:
        m_ulBytesPerSample = bps ; return HXR_OK ;
    default:
        return HXR_FAIL ;
    }
}

#ifdef HELIX_FEATURE_GAINTOOL
// set the volume. This is in tenth of a dB. 0 == unity gain, 6dB = twice as loud, -6 = half as loud
HX_RESULT HXAudioSvcMixEngine::SetVolume(INT32 tenthOfDB, BOOL bImmediate)
{
    // currently, no amplification is allowed
    if (tenthOfDB > 0)
        return HXR_FAIL ;

    if (bImmediate)
	gainSetImmediate(0.1f * tenthOfDB, m_pGaintool) ;
    else
	gainSetSmooth(0.1f * tenthOfDB, m_pGaintool) ;

    return HXR_OK ;
}

INT32 HXAudioSvcMixEngine::HXVolume2TenthOfDB(INT32 vol)
{
    // if HX_MAX_VOLUME changes from 100, need to re-generate the table below.
    // here is the formula:
    //  if (vol > 0) return (INT32)(100.0 * log10((float)vol / HX_MAX_VOLUME )) ;
    //  else return -2000 ;

#define HX_MAX_VOLUME 100
    static const unsigned char vol2TenthOfDb[HX_MAX_VOLUME+1] = {
        255,
        200, 170, 152, 140, 130, 122, 115, 110, 105, 100, 96, 92,
        89, 85, 82, 80, 77, 74, 72, 70, 68, 66, 64, 62,
        60, 59, 57, 55, 54, 52, 51, 49, 48, 47, 46, 44,
        43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32,
        31, 30, 29, 28, 28, 27, 26, 25, 24, 24, 23, 22,
        21, 21, 20, 19, 19, 18, 17, 17, 16, 15, 15, 14,
        14, 13, 12, 12, 11, 11, 10, 10, 9, 9, 8, 8,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -