?? mixengine.cpp
字號:
/* ***** BEGIN LICENSE BLOCK *****
* Source last modified: $Id: mixengine.cpp,v 1.9.8.2 2004/07/09 02:08:08 hubbe Exp $
*
* Portions Copyright (c) 1995-2004 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 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the current version of the RealNetworks Community
* Source License (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.
*
* Alternatively, the contents of this file may be used under the
* terms of the GNU General Public License Version 2 or later (the
* "GPL") in which case the provisions of the GPL are applicable
* instead of those above. If you wish to allow use of your version of
* this file only under the terms of the GPL, and not to allow others
* to use your version of this file under the terms of either the RPSL
* or RCSL, indicate your decision by deleting the provisions above
* and replace them with the notice and other provisions required by
* the GPL. If you do not delete the provisions above, a recipient may
* use your version of this file under the terms of any one of the
* RPSL, the RCSL or the GPL.
*
* 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,
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -