diff --git a/src/decoder.rs b/src/decoder.rs index 1776ce8..3b77d8c 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -121,6 +121,30 @@ impl Decoder { } pub fn next (&mut self) -> Result > { + Ok (if let Some (frame) = self.pump ()? { + assert_eq! (frame.rate (), 48000); + assert! (frame.samples () > 0); + + let actual_bytes = frame.data (0).len (); + let expected_bytes = frame.samples () * 4 * 2; + + assert! (actual_bytes >= expected_bytes); + + if actual_bytes > expected_bytes { + let extra_bytes = actual_bytes - expected_bytes; + let extra_samples = extra_bytes / 4 / 2; + tracing::debug! ("Extra bytes: {}", extra_bytes); + tracing::debug! ("Extra samples: {}", extra_samples); + } + + Some (frame) + } + else { + None + }) + } + + fn pump (&mut self) -> Result > { loop { match self.pump_resampler ()? { Some (x) => { @@ -151,15 +175,23 @@ impl Decoder { } pub fn pump_resampler (&mut self) -> Result > { + let delay = match self.resampler.delay () { + None => return Ok (None), + Some (x) => x, + }; + + let mut frame_src = AudioFrame::new ( + Sample::F32 (sample::Type::Planar), + 0, + ChannelLayout::STEREO + ); let mut frame_resampled = Self::new_frame (); - // dbg! (self.resampler.delay ()); + frame_src.set_rate (44100); - if self.resampler.delay ().is_none () { - return Ok (None); - } - - let _rc = self.resampler.flush (&mut frame_resampled)?; + tracing::trace! ("Flushing resampler"); + tracing::trace! ("delay: {:?}", delay); + let _rc = self.resampler.run (&frame_src, &mut frame_resampled)?; // dbg! (&frame_resampled, rc); Ok (if frame_resampled.samples () > 0 { diff --git a/src/main.rs b/src/main.rs index 9cf5328..2a20fc6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,11 +19,6 @@ use anyhow::{ Result, }; -use byteorder::{ - ByteOrder, - LittleEndian, -}; - use cpal::traits::{ DeviceTrait, HostTrait, @@ -57,7 +52,8 @@ fn cmd_debug_dump (args: &[String]) -> Result <()> { let mut f = File::create ("pcm-dump.data")?; while let Some (frame) = decoder.next ()? { - f.write_all (frame.data (0))?; + f.write_all (&frame.data (0) [0..frame.samples () * 4 * 2])?; + // f.write_all (frame.data (0))?; } Ok (()) @@ -75,8 +71,6 @@ fn cmd_debug_pipe (args: &[String]) -> Result <()> { while let Some (frame) = decoder.next ()? { sample_count += frame.samples (); - assert_eq! (frame.rate (), 48000); - assert! (frame.samples () > 0); // dbg! (frame, sample_count); } @@ -122,7 +116,7 @@ fn cmd_play (args: &[String]) -> Result <()> { while pcm_buffers.samples_available () < 12_000 { // tracing::trace! ("Decoder is trying to work..."); match decoder.next ()? { - Some (frame) => pcm_buffers.produce_bytes (frame.data (0)), + Some (frame) => pcm_buffers.produce_bytes (&frame.data (0) [0..frame.samples () * 4 * 2]), None => { tracing::info! ("Finished decoding file"); break 'one_file;