it's good and seamless except for that dang popping!
parent
336c94e39c
commit
3f5be609ea
|
@ -114,8 +114,8 @@ impl Decoder {
|
|||
|
||||
pub fn fill_buffer (&mut self, pcm_buffers: &mut PcmBuffers) -> Result <bool> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
53
src/main.rs
53
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;
|
||||
|
|
Loading…
Reference in New Issue