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
impl Converter
Sourcepub fn new(plan: &ConversionPlan) -> Result<Self, ConvertError>
pub fn new(plan: &ConversionPlan) -> Result<Self, ConvertError>
Examples found in repository?
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}pub fn plan(&self) -> &ConversionPlan
pub fn source(&self) -> PcmFormat
pub fn target(&self) -> PcmFormat
Sourcepub fn source_frame_bytes(&self) -> usize
pub fn source_frame_bytes(&self) -> usize
Bytes of one source frame: the unit process takes.
Sourcepub fn target_frame_bytes(&self) -> usize
pub fn target_frame_bytes(&self) -> usize
Bytes of one target frame: the unit process writes.
Sourcepub fn delay_frames(&self) -> usize
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.
Sourcepub fn min_output_bytes(&self) -> usize
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?
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}Sourcepub fn clipped_samples(&self) -> u64
pub fn clipped_samples(&self) -> u64
Samples that exceeded full scale and were saturated, since the start or the last reset.
Sourcepub fn process(
&mut self,
input: &[u8],
output: &mut [u8],
) -> Result<Processed, ConvertError>
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?
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}Sourcepub fn finish(&mut self, output: &mut [u8]) -> Result<usize, ConvertError>
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.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Starts a new stream: the filter’s history, the dither state and the counters are cleared.
Sourcepub fn output_len_for(&self, input_bytes: usize) -> usize
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.