Skip to content

Commit 97e2382

Browse files
author
Clement Champetier
committed
AudioTransform: support resampling
* Use default interpolation (nothing to set). * The default interpolation may be not optimized for some audio files: need to set parameters used to resample (filter_size, phase_shift, linear_interp, cutoff, etc...) before initialize the SWR context.
1 parent 8ed360c commit 97e2382

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/AvTranscoder/EssenceTransform/AudioEssenceTransform.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,26 @@ void AudioEssenceTransform::convert( const Frame& srcFrame, Frame& dstFrame )
7171
{
7272
if( ! _isInit )
7373
_isInit = init( srcFrame, dstFrame );
74-
75-
if( dstFrame.getSize() != srcFrame.getSize() )
76-
dstFrame.getBuffer().resize( srcFrame.getSize(), 0 );
7774

7875
const unsigned char* srcData = srcFrame.getPtr();
7976
unsigned char* dstData = dstFrame.getPtr();
80-
77+
78+
int nbOutputSamplesPerChannel;
8179
#ifdef AV_RESAMPLE_LIBRARY
82-
avresample_convert( _audioConvertContext, (uint8_t**)&dstData, 0, dstFrame.getSize(), (uint8_t**)&srcData, 0, srcFrame.getSize() );
80+
nbOutputSamplesPerChannel = avresample_convert( _audioConvertContext, (uint8_t**)&dstData, 0, dstFrame.getSize(), (uint8_t**)&srcData, 0, srcFrame.getSize() );
8381
#else
84-
swr_convert( _audioConvertContext, &dstData, dstFrame.getSize(), &srcData, srcFrame.getSize() );
82+
nbOutputSamplesPerChannel = swr_convert( _audioConvertContext, &dstData, dstFrame.getSize(), &srcData, srcFrame.getSize() );
8583
#endif
8684

85+
if( nbOutputSamplesPerChannel < 0 )
86+
{
87+
throw std::runtime_error( "unable to convert audio samples" );
88+
}
89+
90+
int nbOutputSamples = nbOutputSamplesPerChannel * static_cast<const AudioFrame&>( dstFrame ).desc().getChannels();
91+
92+
if( dstFrame.getSize() != nbOutputSamples )
93+
dstFrame.getBuffer().resize( nbOutputSamples, 0 );
8794
static_cast<AudioFrame&>( dstFrame ).setNbSamples( static_cast<const AudioFrame&>( srcFrame ).getNbSamples() );
8895
}
8996

0 commit comments

Comments
 (0)