From 3f5be609eae6505159adff90b1393649dbc29322 Mon Sep 17 00:00:00 2001 From: _ <> Date: Sat, 13 Nov 2021 01:11:11 +0000 Subject: [PATCH] it's good and seamless except for that dang popping! --- src/decoder.rs | 20 ++++++------------- src/main.rs | 53 ++++++++++++++++++++++++++------------------------ todo.md | 3 +++ 3 files changed, 37 insertions(+), 39 deletions(-) create mode 100644 todo.md diff --git a/src/decoder.rs b/src/decoder.rs index 53f5a0e..e90884b 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -114,8 +114,8 @@ impl Decoder { pub fn fill_buffer (&mut self, pcm_buffers: &mut PcmBuffers) -> Result { match self.resampler.delay () { - Some (x) if x.milliseconds > 500 => { - // tracing::trace! ("flushing resampler ({} ms)", x.milliseconds); + Some (x) => { + tracing::trace! ("flushing resampler ({} ms)", x.milliseconds); let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty (); frame_resampled.set_channel_layout (ChannelLayout::STEREO); frame_resampled.set_format (Sample::F32 (sample::Type::Packed)); @@ -136,6 +136,10 @@ impl Decoder { if self.decoder.receive_frame (&mut frame_src).is_ok () { //eprintln! ("decoder.receive_frame"); let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty (); + frame_resampled.set_channel_layout (ChannelLayout::STEREO); + frame_resampled.set_format (Sample::F32 (sample::Type::Packed)); + frame_resampled.set_rate (48000); + self.resampler.run (&frame_src, &mut frame_resampled)?; pcm_buffers.produce_bytes (frame_resampled.data (0)); return Ok (true); @@ -151,18 +155,6 @@ impl Decoder { } } - //eprintln! ("Decoder ran out of work"); - - if self.resampler.delay ().is_some () { - tracing::trace! ("flushing resampler (out of compressed packets)"); - let mut frame_resampled = ffmpeg_next::util::frame::Audio::empty (); - - if self.resampler.flush (&mut frame_resampled).is_ok () { - pcm_buffers.produce_bytes (frame_resampled.data (0)); - return Ok (true); - } - } - Ok (false) } } diff --git a/src/main.rs b/src/main.rs index 01df327..4b0a14e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,9 +89,10 @@ fn cmd_debug (args: &[String]) -> Result <()> { fn cmd_play (args: &[String]) -> Result <()> { tracing_subscriber::fmt::init (); - let filename = args.get (1) + let filenames: Vec <_> = args.iter () + .skip (1) .map (|s| s.to_string ()) - .unwrap_or_else (|| "test-short.m4a".to_string ()); + .collect (); let pair = Arc::new ((Mutex::new (decoder::SharedState::default ()), Condvar::new ())); let pair2 = Arc::clone (&pair); @@ -100,29 +101,31 @@ fn cmd_play (args: &[String]) -> Result <()> { let thread_decoder = thread::spawn (move|| { let (lock, cvar) = &*pair2; - let mut decoder = decoder::Decoder::new (&filename)?; - - 'decoder_thread: loop { - // tracing::trace! ("decode thread parking"); + 'many_files: for filename in &filenames { + let mut decoder = decoder::Decoder::new (&filename)?; - let mut decoder_state = cvar.wait_while (lock.lock ().unwrap (), |decoder_state| { - decoder_state.pcm_buffers.samples_available () >= 12_000 && - ! decoder_state.quit - }).unwrap (); - - if decoder_state.quit { - break 'decoder_thread; - } - - //dbg! (resampler.delay ()); - - let pcm_buffers = &mut decoder_state.pcm_buffers; - - while pcm_buffers.samples_available () < 24_000 { - // tracing::trace! ("Decoder is trying to work..."); - if ! decoder.fill_buffer (pcm_buffers)? { - tracing::info! ("Decoder thread is out of work, quitting"); - break 'decoder_thread; + 'one_file: loop { + // tracing::trace! ("decode thread parking"); + + let mut decoder_state = cvar.wait_while (lock.lock ().unwrap (), |decoder_state| { + decoder_state.pcm_buffers.samples_available () >= 48_000 && + ! decoder_state.quit + }).unwrap (); + + if decoder_state.quit { + break 'many_files; + } + + //dbg! (resampler.delay ()); + + let pcm_buffers = &mut decoder_state.pcm_buffers; + + while pcm_buffers.samples_available () < 96_000 { + // tracing::trace! ("Decoder is trying to work..."); + if ! decoder.fill_buffer (pcm_buffers)? { + tracing::info! ("Finished decoding file"); + break 'one_file; + } } } } @@ -157,7 +160,7 @@ fn cmd_play (args: &[String]) -> Result <()> { let pcm_buffers = &mut decoder_state.pcm_buffers; if ! pcm_buffers.consume_exact (data) { - // tracing::warn! ("PCM buffer underflow"); + tracing::warn! ("PCM buffer underflow"); for x in data { *x = 0.0; diff --git a/todo.md b/todo.md new file mode 100644 index 0000000..07505b3 --- /dev/null +++ b/todo.md @@ -0,0 +1,3 @@ +# Critical + +- Popping every 1-3 seconds-ish