@@ -39,16 +39,15 @@ AudioTransform::AudioTransform()
39
39
40
40
bool AudioTransform::init ( const Frame& srcFrame, const Frame& dstFrame )
41
41
{
42
- const AudioFrame& src = static_cast <const AudioFrame&>( srcFrame );
43
- const AudioFrame& dst = static_cast <const AudioFrame&>( dstFrame );
44
-
45
42
_audioConvertContext = AllocResampleContext ();
46
-
47
43
if ( !_audioConvertContext )
48
44
{
49
45
throw std::runtime_error ( " unable to create audio convert context" );
50
46
}
51
-
47
+
48
+ const AudioFrame& src = static_cast <const AudioFrame&>( srcFrame );
49
+ const AudioFrame& dst = static_cast <const AudioFrame&>( dstFrame );
50
+
52
51
av_opt_set_int ( _audioConvertContext, " in_channel_layout" , av_get_default_channel_layout ( src.desc ().getChannels () ), 0 );
53
52
av_opt_set_int ( _audioConvertContext, " out_channel_layout" , av_get_default_channel_layout ( dst.desc ().getChannels () ), 0 );
54
53
av_opt_set_int ( _audioConvertContext, " in_sample_rate" , src.desc ().getSampleRate (), 0 );
@@ -65,15 +64,19 @@ bool AudioTransform::init( const Frame& srcFrame, const Frame& dstFrame )
65
64
return true ;
66
65
}
67
66
68
- bool AudioTransform::initFrames ( const Frame& srcFrame, Frame& dstFrame )
67
+ void AudioTransform::initFrames ( const Frame& srcFrame, Frame& dstFrame )
69
68
{
70
69
const AudioFrame& src = static_cast <const AudioFrame&>( srcFrame );
71
- const AudioFrame& dst = static_cast <const AudioFrame&>( dstFrame );
70
+ AudioFrame& dst = static_cast <AudioFrame&>( dstFrame );
72
71
72
+ // resize buffer of output frame
73
73
int dstSampleSize = av_get_bytes_per_sample ( dst.desc ().getSampleFormat () );
74
- dstFrame.resize ( src.getNbSamples () * dstSampleSize );
74
+ dstFrame.resize ( src.getNbSamples () * src.desc ().getChannels () * dstSampleSize );
75
+
76
+ // set nbSamples of output frame
77
+ dst.setNbSamples ( src.getNbSamples () );
78
+
75
79
_previousProcessedAudioFrameSize = srcFrame.getSize ();
76
- return true ;
77
80
}
78
81
79
82
void AudioTransform::convert ( const Frame& srcFrame, Frame& dstFrame )
@@ -98,12 +101,6 @@ void AudioTransform::convert( const Frame& srcFrame, Frame& dstFrame )
98
101
{
99
102
throw std::runtime_error ( " unable to convert audio samples" );
100
103
}
101
-
102
- size_t nbOutputSamples = nbOutputSamplesPerChannel * static_cast <const AudioFrame&>( dstFrame ).desc ().getChannels ();
103
-
104
- if ( dstFrame.getSize () != nbOutputSamples )
105
- dstFrame.resize ( nbOutputSamples );
106
- static_cast <AudioFrame&>( dstFrame ).setNbSamples ( static_cast <const AudioFrame&>( srcFrame ).getNbSamples () );
107
104
}
108
105
109
106
}
0 commit comments