Skip to main content

Converter

Struct Converter 

Source
pub struct Converter { /* private fields */ }
Expand description

Turns audio in the plan’s source format into the target format, a chunk at a time.

process takes whole frames from the front of the input and writes whole frames to the output, as many as fit in either; the caller keeps what was not consumed. After the last chunk call Converter::finish until it returns 0 to get the resampler’s tail, then Converter::reset to start another stream (a seek is a reset too: the filter’s history belongs to the old position).

Implementations§

Source§

impl Converter

Source

pub fn new(plan: &ConversionPlan) -> Result<Self, ConvertError>

Examples found in repository?
examples/convert_bench.rs (line 23)
16fn run(label: &str, source: PcmFormat, target: PcmFormat, quality: Quality) {
17    let options = ConvertOptions {
18        quality,
19        ..ConvertOptions::default()
20    };
21    let plan = ConversionPlan::pcm(source, target, &options).unwrap();
22    let build = Instant::now();
23    let mut converter = Converter::new(&plan).unwrap();
24    let build_ms = build.elapsed().as_secs_f64() * 1000.0;
25
26    let chunk_frames = source.sample_rate as usize / 100;
27    let chunk: Vec<u8> =
28        (0..chunk_frames * usize::from(source.channels) * source.sample.bytes_per_sample())
29            .map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8)
30            .collect();
31    let mut output = vec![0u8; converter.min_output_bytes().max(1 << 20)];
32    let seconds = 10;
33    let start = Instant::now();
34    let mut produced = 0usize;
35    for _ in 0..seconds * 100 {
36        let done = converter.process(&chunk, &mut output).unwrap();
37        produced += done.produced;
38    }
39    let elapsed = start.elapsed().as_secs_f64();
40    std::hint::black_box(produced);
41    println!(
42        "{label:<46} build {build_ms:6.1} ms   {:6.1}x realtime   {:5.2}% of a core",
43        seconds as f64 / elapsed,
44        elapsed / seconds as f64 * 100.0
45    );
46}
Source

pub fn plan(&self) -> &ConversionPlan

Source

pub fn source(&self) -> PcmFormat

Source

pub fn target(&self) -> PcmFormat

Source

pub fn source_frame_bytes(&self) -> usize

Bytes of one source frame: the unit process takes.

Source

pub fn target_frame_bytes(&self) -> usize

Bytes of one target frame: the unit process writes.

Source

pub fn delay_frames(&self) -> usize

Source frames the converter holds back before the output reflects them: the resampler’s algorithmic latency (a few milliseconds), 0 for every other plan. Output frame n is aligned with source time n * source_rate / target_rate exactly, so a position needs no correction; only the delay between feeding audio and hearing it grows by this much.

Source

pub fn min_output_bytes(&self) -> usize

The smallest output buffer process and finish make progress with: room for the frames one source frame can turn into (ceil(target_rate / source_rate) when resampling, 1 otherwise). With less, process can consume nothing and finish can return 0 before the end.

Examples found in repository?
examples/convert_bench.rs (line 31)
16fn run(label: &str, source: PcmFormat, target: PcmFormat, quality: Quality) {
17    let options = ConvertOptions {
18        quality,
19        ..ConvertOptions::default()
20    };
21    let plan = ConversionPlan::pcm(source, target, &options).unwrap();
22    let build = Instant::now();
23    let mut converter = Converter::new(&plan).unwrap();
24    let build_ms = build.elapsed().as_secs_f64() * 1000.0;
25
26    let chunk_frames = source.sample_rate as usize / 100;
27    let chunk: Vec<u8> =
28        (0..chunk_frames * usize::from(source.channels) * source.sample.bytes_per_sample())
29            .map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8)
30            .collect();
31    let mut output = vec![0u8; converter.min_output_bytes().max(1 << 20)];
32    let seconds = 10;
33    let start = Instant::now();
34    let mut produced = 0usize;
35    for _ in 0..seconds * 100 {
36        let done = converter.process(&chunk, &mut output).unwrap();
37        produced += done.produced;
38    }
39    let elapsed = start.elapsed().as_secs_f64();
40    std::hint::black_box(produced);
41    println!(
42        "{label:<46} build {build_ms:6.1} ms   {:6.1}x realtime   {:5.2}% of a core",
43        seconds as f64 / elapsed,
44        elapsed / seconds as f64 * 100.0
45    );
46}
Source

pub fn clipped_samples(&self) -> u64

Samples that exceeded full scale and were saturated, since the start or the last reset.

Source

pub fn process( &mut self, input: &[u8], output: &mut [u8], ) -> Result<Processed, ConvertError>

Converts whole frames. Declared integer padding is checked before any output or state change. Nonzero padding returns InvalidArgument; after finish, call reset first.

Examples found in repository?
examples/convert_bench.rs (line 36)
16fn run(label: &str, source: PcmFormat, target: PcmFormat, quality: Quality) {
17    let options = ConvertOptions {
18        quality,
19        ..ConvertOptions::default()
20    };
21    let plan = ConversionPlan::pcm(source, target, &options).unwrap();
22    let build = Instant::now();
23    let mut converter = Converter::new(&plan).unwrap();
24    let build_ms = build.elapsed().as_secs_f64() * 1000.0;
25
26    let chunk_frames = source.sample_rate as usize / 100;
27    let chunk: Vec<u8> =
28        (0..chunk_frames * usize::from(source.channels) * source.sample.bytes_per_sample())
29            .map(|i| (i.wrapping_mul(2_654_435_761) >> 13) as u8)
30            .collect();
31    let mut output = vec![0u8; converter.min_output_bytes().max(1 << 20)];
32    let seconds = 10;
33    let start = Instant::now();
34    let mut produced = 0usize;
35    for _ in 0..seconds * 100 {
36        let done = converter.process(&chunk, &mut output).unwrap();
37        produced += done.produced;
38    }
39    let elapsed = start.elapsed().as_secs_f64();
40    std::hint::black_box(produced);
41    println!(
42        "{label:<46} build {build_ms:6.1} ms   {:6.1}x realtime   {:5.2}% of a core",
43        seconds as f64 / elapsed,
44        elapsed / seconds as f64 * 100.0
45    );
46}
Source

pub fn finish(&mut self, output: &mut [u8]) -> Result<usize, ConvertError>

Writes the end of the stream: the frames the resampler still holds, as far as output has room. Call it until it returns 0. Nothing else follows a finish: Converter::process fails until Converter::reset.

Source

pub fn reset(&mut self)

Starts a new stream: the filter’s history, the dither state and the counters are cleared.

Source

pub fn output_len_for(&self, input_bytes: usize) -> usize

Output bytes process writes for input_bytes of input, if the output buffer is large enough, rounded down to whole frames. Exact for every plan; for a resampling one it depends on how much has been fed so far.

Auto Trait Implementations§

§

impl Forget for Converter

§

impl Freeze for Converter
where *const u8 is !null: Freeze, PhantomData<Step>: Freeze, PhantomData<u8>: Freeze, *const FloatEngine is !null: Freeze, PhantomData<FloatEngine>: Freeze,

§

impl Move for Converter

§

impl RefUnwindSafe for Converter

§

impl Send for Converter
where RawVecInner: Send, BoxRaw<FloatEngine>: Send,

§

impl Sync for Converter
where RawVecInner: Sync, BoxRaw<FloatEngine>: Sync,

§

impl Unpin for Converter
where *const u8 is !null: Unpin, Box<FloatEngine>: Unpin,

§

impl UnsafeUnpin for Converter

§

impl UnwindSafe for Converter
where RawVecInner: UnwindSafe, BoxRaw<FloatEngine>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.