Skip to content

Commit

Permalink
Merge pull request #867 from s09bQ5/multichannel
Browse files Browse the repository at this point in the history
AudioConverter: fix output buffer size calculation
  • Loading branch information
barbeque-squared authored Jul 13, 2024
2 parents 77467b2 + adc857e commit f2214ab
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/media/UAudioConverter.pas
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ function TAudioConverter_SWResample.Init(SrcFormatInfo: TAudioFormatInfo;
av_opt_set_sample_fmt(SwrContext, 'out_sample_fmt', DstFormat, 0);
swr_init(SwrContext);
// calculate ratio
Ratio := (dstFormatInfo.Channels / srcFormatInfo.Channels) *
(dstFormatInfo.SampleRate / srcFormatInfo.SampleRate);
Ratio := srcFormatInfo.GetRatio(dstFormatInfo);

Result := true;
end;
Expand Down Expand Up @@ -320,15 +319,15 @@ function TAudioConverter_SWResample.Convert(InputBuffer: PByteArray; OutputBuffe
end;

InputSampleCount := InputSize div SrcFormatInfo.FrameSize;
OutputSampleCount := GetOutputBufferSize(InputSampleCount);
OutputSampleCount := GetOutputBufferSize(InputSize) div DstFormatInfo.FrameSize;
InBufPtr := Pcuint8(@InputBuffer[0]);
OutBufPtr := Pcuint8(@OutputBuffer[0]);
OutputSampleCount:= swr_convert(SwrContext, OutBufPtr, OutputSampleCount,
InBufPtr, InputSampleCount);
if (OutputSampleCount < 0) then
begin
Log.LogError('swr_convert failed ' + inttostr(OutputSampleCount), 'TAudioConverter_SWResample.Init');
OutputSampleCount := GetOutputBufferSize(InputSampleCount);
OutputSampleCount := GetOutputBufferSize(InputSize) div DstFormatInfo.FrameSize;
end;
Result := OutputSampleCount * DstFormatInfo.FrameSize;
end;
Expand Down Expand Up @@ -410,8 +409,7 @@ function TAudioConverter_FFmpeg.Init(SrcFormatInfo: TAudioFormatInfo;
end;

// calculate ratio
Ratio := (dstFormatInfo.Channels / srcFormatInfo.Channels) *
(dstFormatInfo.SampleRate / srcFormatInfo.SampleRate);
Ratio := srcFormatInfo.GetRatio(dstFormatInfo);

Result := true;
end;
Expand Down

0 comments on commit f2214ab

Please sign in to comment.