📄 raresampler.cpp
字号:
/* ***** 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/string.h" // memcpy
#include "RAResampler.h"
#include "allresamplers.h"
#include "cpuident.h"
static void cvtShortShortSimple(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
memcpy(d,s,n*sizeof(short)) ; /* Flawfinder: ignore */
}
static void cvtFloatFloatSimple(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
memcpy(d,s,n*sizeof(float)) ; /* Flawfinder: ignore */
}
static void cvtIntFloatSimple(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
int i ;
const int* src = (const int*)s ;
float* dst = (float*)d ;
for (i = 0 ; i < n ; i++)
*dst++ = (1.0f/65536.0f)*(float)*src++ ;
}
static void cvtIntShortSimple(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
int i ;
const int* src = (const int*)s ;
short* dst = (short*)d ;
for (i = 0 ; i < n ; i++)
{
int t = *src++ ;
if (t < 0x7fff8000L) t += 0x8000L ; // rounding
*dst++ = (short)(t >> 16);
}
}
static void cvtShortFloatSimple(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
int i ;
const short* src = (const short*)s ;
float* dst = (float*)d ;
for (i = 0 ; i < n ; i++)
*dst++ = (float)*src++ ;
}
// generic convert functions for multichannel operation
static int cvtShortShort(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
const short* src = (const short *)s ;
const short* srcEnd = src + n ;
short* dst = (short *)d ;
while (src != srcEnd)
{
*dst = *src ;
src += pState->incInput ;
dst += pState->incOutput ;
pState = pState->pNext ;
}
return dst - (short *)d ;
}
static int cvtFloatFloat(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
const float* src = (const float *)s ;
const float* srcEnd = src + n ;
float* dst = (float *)d ;
while (src != srcEnd)
{
*dst = *src ;
src += pState->incInput ;
dst += pState->incOutput ;
pState = pState->pNext ;
}
return dst - (float *)d ;
}
static int cvtIntFloat(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
const int* src = (const int *)s ;
const int* srcEnd = src + n ;
float* dst = (float *)d ;
while (src != srcEnd)
{
*dst = (1.0f/65536.0f) * (*src) ;
src += pState->incInput ;
dst += pState->incOutput ;
pState = pState->pNext ;
}
return dst - (float *)d ;
}
static int cvtIntShort(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
const int* src = (const int *)s ;
const int* srcEnd = src + n ;
short* dst = (short *)d ;
while (src != srcEnd)
{
int t = (int)(*src) ;
if (t < 0x7fff8000L) t += 0x8000L ; // rounding
*dst = (short)(t >> 16) ;
src += pState->incInput ;
dst += pState->incOutput ;
pState = pState->pNext ;
}
return dst - (short *)d ;
}
static int cvtShortFloat(void *d, const void *s, int n, const struct CVTSTATEMACHINE *pState)
{
const short* src = (const short *)s ;
const short* srcEnd = src + n ;
float* dst = (float *)d ;
while (src != srcEnd)
{
*dst = (float)(*src) ;
src += pState->incInput ;
dst += pState->incOutput ;
pState = pState->pNext ;
}
return dst - (float *)d ;
}
typedef int (*tfResample)(void *inbuf, int insamps, tConverter *pCvt, short *outbuf, int outstride, void *inst);
typedef void* (*tfInitResamplerCopy)(int nchans, const void *inst);
typedef void (*tfFreeResampler)(void *inst);
typedef int (*tfGetDelay)(void *inst);
typedef int (*tfGetDelay)(void *inst);
typedef int (*tfGetMinInput)(int nSamples, void *inst);
typedef int (*tfGetMaxOutput)(int nSamples, void *inst);
static cvtFunctionType const cvtFunc[3][3] = {
{cvtShortShort,0,cvtShortFloat},
{cvtIntShort, 0,cvtIntFloat},
{0, 0,cvtFloatFloat}
} ;
RAExactResampler::RAExactResampler() {}
RAExactResampler::~RAExactResampler() {}
class RAAnyResampler : public RAExactResampler
{
public:
int Resample(void *_inbuf, int insamps, signed short *outbuf) ;
int Resample(void *_inbuf, int insamps, signed int *outbuf) ;
int GetMinInput(int insamps)
{
int t = insamps / m_nChannelsTotal * m_nChannels[0] ;
t = m_pfGetMinInput(t, m_pResampler[0]) ;
return t / m_nChannels[0] * m_nChannelsTotal ;
}
int GetMaxOutput(int insamps)
{
int t = insamps / m_nChannelsTotal * m_nChannels[0] ;
t = m_pfGetMaxOutput(t, m_pResampler[0]) ;
return t / m_nChannels[0] * m_nChannelsTotal ;
}
int GetDelay()
{
return m_pfGetDelay(m_pResampler[0]) ;
}
protected:
RAAnyResampler() ;
virtual ~RAAnyResampler();
/*
* function pointers
*/
tfResample m_pfResample[2] ; // one for mono, one for stereo
tfGetMinInput m_pfGetMinInput ;
tfGetMaxOutput m_pfGetMaxOutput ;
tfGetDelay m_pfGetDelay ;
tfFreeResampler m_pfFreeResampler ;
tfInitResamplerCopy m_pfInitResamplerCopy ;
tConverter m_converter[2] ; // one for mono, one for stereo
int intype, resType;
enum {
MAX_RESAMPLERS = 5
} ;
int m_nResamplers ;
int m_nBytesPerSampleIn ;
int m_nChannelsTotal ;
void *m_pResampler[MAX_RESAMPLERS] ;
int m_nChannels[MAX_RESAMPLERS] ;
int m_nBlock[MAX_RESAMPLERS] ;
int m_nOffsetIn[MAX_RESAMPLERS] ;
HX_RESULT Init(int inrate, int outrate, int chans, int intype, float atten, float passband, float stopband, float dcgain) ;
// these need to be supplied by the base classes
virtual int GetResamplerInputType(void) = 0 ;
virtual HX_RESULT SetupFunctionPointers(void) = 0 ;
virtual void* InitResampler(int inrate, int outrate, int nchans, float atten, float passband, float stopband, float dcgain) = 0;
private:
HX_RESULT SetupNumChannels(int chans) ;
HX_RESULT SetupConverter(int intype, int restype) ;
} ;
RAAnyResampler::RAAnyResampler()
: m_nResamplers(0)
{
memset(m_pResampler, 0, sizeof(m_pResampler)) ;
}
HX_RESULT RAAnyResampler::Init(int inrate, int outrate, int chans, int intype, float atten, float passband, float stopband, float dcgain)
{
HX_RESULT res = HXR_OK ;
int i ;
if (SUCCEEDED(res))
res = SetupFunctionPointers() ;
if (SUCCEEDED(res))
res = SetupNumChannels(chans) ;
m_converter[0].pStateMachine = 0 ;
m_converter[1].pStateMachine = 0 ;
if (SUCCEEDED(res))
res = SetupConverter(intype, GetResamplerInputType()) ;
for (i = 0 ; i < m_nResamplers; i++)
{
m_pResampler[i] = 0 ;
if (SUCCEEDED(res))
{
if (i == 0)
m_pResampler[i] = InitResampler(inrate, outrate, m_nChannels[i], atten, passband, stopband, dcgain) ;
else
m_pResampler[i] = m_pfInitResamplerCopy(m_nChannels[i], m_pResampler[0]) ;
}
if (!m_pResampler[i])
res = HXR_OUTOFMEMORY ;
}
return res ;
}
RAAnyResampler::~RAAnyResampler()
{
int i ;
for (i = 0 ; i < MAX_RESAMPLERS ; i++)
{
if (m_pResampler[i]) m_pfFreeResampler(m_pResampler[i]) ;
m_pResampler[i] = 0; // paranoia code.
}
HX_VECTOR_DELETE(m_converter[0].pStateMachine) ;
HX_VECTOR_DELETE(m_converter[1].pStateMachine) ;
}
HX_RESULT RAAnyResampler::SetupNumChannels(int chans)
{
int i ;
if (chans > 2*MAX_RESAMPLERS)
return HXR_FAIL ;
m_nChannelsTotal = chans ;
m_nResamplers = 0 ;
for (i = 0 ; i < chans ; i+=2)
{
m_nChannels[m_nResamplers] = (i + 2 > chans) ? 1 : 2 ;
m_nBlock[m_nResamplers] = NBLOCK - NBLOCK % m_nChannels[m_nResamplers] ;
m_nOffsetIn[m_nResamplers] = 2 * m_nResamplers ;
m_nResamplers++ ;
}
return HXR_OK ;
}
HX_RESULT RAAnyResampler::SetupConverter(int _intype, int _restype)
{
static const int bps[] = {2,4,4} ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -