Skip to main content

ffmpeg_pipeline/audio/
resampling.rs

1use super::*;
2use ffmpeg_next::software::resampling::context::Context;
3
4pub struct Resampler {
5    resampler: Context,
6}
7
8impl Resampler {
9    pub fn new<S, D>(src: S, dst: D) -> FFmpegResult<Self>
10    where
11        S: TryInto<AudioSpec, Error = FFmpegError>,
12        D: TryInto<AudioSpec, Error = FFmpegError>,
13    {
14        let src: AudioSpec = src.try_into()?;
15        let dst: AudioSpec = dst.try_into()?;
16        Ok(Self {
17            resampler: Context::get(
18                src.format,
19                src.channel_layout,
20                src.sample_rate,
21                dst.format,
22                dst.channel_layout,
23                dst.sample_rate,
24            )?,
25        })
26    }
27
28    pub fn resample(&mut self, frame: &AudioFrame) -> FFmpegResult<AudioFrame> {
29        let mut resampled_frame = AudioFrame::empty();
30        self.resampler.run(frame, &mut resampled_frame)?;
31        Ok(resampled_frame)
32    }
33}