Dry SRC implementation for audio samples

parent 89d54de2
......@@ -32,30 +32,6 @@ void CITAAudioSample::Init( const int iNumChannels, const int iLength, const flo
ITASampleFrame::Init( iNumChannels, iLength, bZeroInit );
}
void CITAAudioSample::Load( const ITASampleFrame& sfSource, const float fSourceSampleRate )
{
if( fSourceSampleRate <= 0.0f )
ITA_EXCEPT_INVALID_PARAMETER( "Source sample rate must be greater zero." );
// Same sample rate
if( fabs( fSourceSampleRate - m_fSampleRate ) < 1e-20 )
{
Init( sfSource.GetNumChannels(), sfSource.GetLength(), m_fSampleRate );
ITASampleFrame::write( sfSource, sfSource.GetLength() );
return;
}
const int iConverter = SRC_SINC_MEDIUM_QUALITY;
int iSRCError;
SRC_STATE* pSRC = src_new( iConverter, sfSource.GetNumChannels(), &iSRCError );
if( pSRC == nullptr )
ITA_EXCEPT_INVALID_PARAMETER( "Could not create sample rate converter, samplerate error was: " + std::to_string( iSRCError ) );
const float fSRCRation = GetSampleRate() / fSourceSampleRate;
SRC_DATA src_data;
}
void CITAAudioSample::LoadWithSampleTypeConversion( const std::string& sFilePath )
{
if( m_fSampleRate <= 0.0f )
......@@ -73,3 +49,41 @@ float CITAAudioSample::GetSampleRate() const
{
return m_fSampleRate;
}
void CITAAudioSample::Load( const ITASampleFrame& sfSource, const float fSourceSampleRate )
{
if( fSourceSampleRate <= 0.0f )
ITA_EXCEPT_INVALID_PARAMETER( "Source sample rate must be greater zero." );
// Same sample rate
if( fabs( fSourceSampleRate - m_fSampleRate ) < 1e-20 )
{
Init( sfSource.GetNumChannels(), sfSource.GetLength(), m_fSampleRate );
ITASampleFrame::write( sfSource, sfSource.GetLength() );
return;
}
const float fSRCRation = GetSampleRate() / fSourceSampleRate;
const int iTargetLength = (int) ceil( sfSource.GetLength() * fSRCRation ); // @tbd test!
Init( sfSource.GetNumChannels(), iTargetLength, m_fSampleRate );
const int iConverter = SRC_SINC_MEDIUM_QUALITY;
for( int i = 0; i < sfSource.GetNumChannels(); i++ )
{
int iSRCError;
SRC_STATE* pSRCStace = src_new( iConverter, sfSource.GetNumChannels(), &iSRCError );
if( pSRCStace == nullptr )
ITA_EXCEPT_INVALID_PARAMETER( "Could not create sample rate converter, samplerate error was: " + std::string( src_strerror( iSRCError ) ) );
SRC_DATA oSRCData;
oSRCData.data_in = sfSource[ i ].GetData();
oSRCData.data_out = ( *this )[ i ].GetData();
oSRCData.input_frames = 1;
oSRCData.output_frames = 1;
oSRCData.src_ratio = fSRCRation;
if( ( iSRCError = src_process( pSRCStace, &oSRCData ) ) != 0 )
ITA_EXCEPT_INVALID_PARAMETER( "Could not convert sample rate: " + std::string( src_strerror( iSRCError ) ) );
src_delete( pSRCStace );
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment