Compare commits

..

No commits in common. "3b45f4309fc224ba14cfe4952f7a6bb5cd4cb040" and "68461866d806d9ceee03d70049b75ca6e492aa8a" have entirely different histories.

2 changed files with 50 additions and 51 deletions

View File

@ -1,11 +1,15 @@
use std::{
io::Cursor,
};
use anyhow::{ use anyhow::{
anyhow, anyhow,
Result, Result,
}; };
use byteorder::{ use byteorder::{
ByteOrder,
LittleEndian, LittleEndian,
ReadBytesExt,
}; };
// This crate flitters between being very convenient and being a type labyrinth. // This crate flitters between being very convenient and being a type labyrinth.
@ -20,42 +24,15 @@ use ffmpeg_next::{
self, self,
Sample, Sample,
}, },
frame::Audio as FfAudioFrame, frame::Audio as AudioFrame,
}, },
}; };
pub const SAMPLE_RATE: u32 = 48000; pub const SAMPLE_RATE: u32 = 48000;
pub struct AudioFrame {
frame: FfAudioFrame,
}
impl AudioFrame {
pub fn data (&self) -> &[u8] {
// Hard-coded because I'm only support f32 interleaved stereo
&self.frame.data (0) [0..self.frame.samples () * 4 * 2]
}
pub fn rate (&self) -> u32 {
self.frame.rate ()
}
pub fn samples (&self) -> usize {
self.frame.samples ()
}
}
impl From <FfAudioFrame> for AudioFrame {
fn from (frame: FfAudioFrame) -> Self {
Self {
frame,
}
}
}
#[derive (Default)] #[derive (Default)]
pub struct PcmBuffers { pub struct PcmBuffers {
buffers: Vec <Vec <u8>>, buffers: Vec <Vec <f32>>,
// Always points into the first buffer, if any // Always points into the first buffer, if any
consumer_cursor: usize, consumer_cursor: usize,
@ -63,7 +40,7 @@ pub struct PcmBuffers {
impl PcmBuffers { impl PcmBuffers {
pub fn samples_available (&self) -> usize { pub fn samples_available (&self) -> usize {
(self.buffers.iter ().map (|b| b.len ()).sum::<usize> () - self.consumer_cursor) / 8 self.buffers.iter ().map (|b| b.len ()).sum::<usize> () - self.consumer_cursor
} }
#[warn(unused_must_use)] #[warn(unused_must_use)]
@ -78,16 +55,31 @@ impl PcmBuffers {
self.consumer_cursor = 0; self.consumer_cursor = 0;
} }
*x = LittleEndian::read_f32 (&self.buffers [0][self.consumer_cursor..]); *x = self.buffers [0][self.consumer_cursor];
self.consumer_cursor += 4; self.consumer_cursor += 1;
} }
true true
} }
pub fn produce_bytes (&mut self, new_buffer: Vec <u8>) { pub fn produce (&mut self, new_buffer: Vec <f32>) {
self.buffers.push (new_buffer); self.buffers.push (new_buffer);
} }
pub fn produce_bytes (&mut self, new_buffer: &[u8]) {
let mut b = vec! [0.0f32; new_buffer.len () / 4];
let mut rdr = Cursor::new (new_buffer);
rdr.read_f32_into::<LittleEndian> (&mut b).unwrap ();
self.produce (b);
}
}
#[derive (Default)]
pub struct SharedState {
pub pcm_buffers: PcmBuffers,
pub quit: bool,
} }
pub struct Decoder { pub struct Decoder {
@ -96,7 +88,7 @@ pub struct Decoder {
pub resampler: ResamplingContext, pub resampler: ResamplingContext,
best_stream_idx: usize, best_stream_idx: usize,
dummy_frame: Option <FfAudioFrame>, dummy_frame: Option <AudioFrame>,
} }
impl Decoder { impl Decoder {
@ -125,8 +117,8 @@ impl Decoder {
}) })
} }
fn new_frame () -> FfAudioFrame { fn new_frame () -> AudioFrame {
let mut x = FfAudioFrame::empty (); let mut x = AudioFrame::empty ();
x.set_channel_layout (ChannelLayout::STEREO); x.set_channel_layout (ChannelLayout::STEREO);
x.set_format (Sample::F32 (sample::Type::Packed)); x.set_format (Sample::F32 (sample::Type::Packed));
x x
@ -137,6 +129,18 @@ impl Decoder {
assert_eq! (frame.rate (), 48000); assert_eq! (frame.rate (), 48000);
assert! (frame.samples () > 0); 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) Some (frame)
} }
else { else {
@ -187,7 +191,7 @@ impl Decoder {
Ok (if frame_resampled.samples () > 0 { Ok (if frame_resampled.samples () > 0 {
// tracing::trace! ("Pulled from resampler FIFO"); // tracing::trace! ("Pulled from resampler FIFO");
Some (frame_resampled.into ()) Some (frame_resampled)
} }
else { else {
None None
@ -195,13 +199,13 @@ impl Decoder {
} }
pub fn pump_decoder (&mut self) -> Result <Option <AudioFrame>> { pub fn pump_decoder (&mut self) -> Result <Option <AudioFrame>> {
let mut frame_src = FfAudioFrame::empty (); let mut frame_src = AudioFrame::empty ();
if let Err (_) = self.decoder.receive_frame (&mut frame_src) { if let Err (_) = self.decoder.receive_frame (&mut frame_src) {
return Ok (None); return Ok (None);
}; };
if self.dummy_frame.is_none () { if self.dummy_frame.is_none () {
let mut dummy_frame = FfAudioFrame::new ( let mut dummy_frame = AudioFrame::new (
frame_src.format (), frame_src.format (),
0, 0,
frame_src.channel_layout (), frame_src.channel_layout (),
@ -212,14 +216,14 @@ impl Decoder {
let nb_output_samples = frame_src.samples (); let nb_output_samples = frame_src.samples ();
let mut frame_resampled = FfAudioFrame::new ( let mut frame_resampled = AudioFrame::new (
Sample::F32 (sample::Type::Packed), Sample::F32 (sample::Type::Packed),
nb_output_samples, nb_output_samples,
ChannelLayout::STEREO ChannelLayout::STEREO
); );
self.resampler.run (&frame_src, &mut frame_resampled)?; self.resampler.run (&frame_src, &mut frame_resampled)?;
Ok (Some (frame_resampled.into ())) Ok (Some (frame_resampled))
} }
pub fn pump_demuxer (&mut self) -> Result <bool> { pub fn pump_demuxer (&mut self) -> Result <bool> {

View File

@ -52,7 +52,8 @@ fn cmd_debug_dump (args: &[String]) -> Result <()> {
let mut f = File::create ("pcm-dump.data")?; let mut f = File::create ("pcm-dump.data")?;
while let Some (frame) = decoder.next ()? { while let Some (frame) = decoder.next ()? {
f.write_all (frame.data ())?; f.write_all (&frame.data (0) [0..frame.samples () * 4 * 2])?;
// f.write_all (frame.data (0))?;
} }
Ok (()) Ok (())
@ -78,12 +79,6 @@ fn cmd_debug_pipe (args: &[String]) -> Result <()> {
Ok (()) Ok (())
} }
#[derive (Default)]
pub struct SharedState {
pub pcm_buffers: decoder::PcmBuffers,
pub quit: bool,
}
fn cmd_play (args: &[String]) -> Result <()> { fn cmd_play (args: &[String]) -> Result <()> {
tracing_subscriber::fmt::init (); tracing_subscriber::fmt::init ();
@ -92,7 +87,7 @@ fn cmd_play (args: &[String]) -> Result <()> {
.map (|s| s.to_string ()) .map (|s| s.to_string ())
.collect (); .collect ();
let pair = Arc::new ((Mutex::new (SharedState::default ()), Condvar::new ())); let pair = Arc::new ((Mutex::new (decoder::SharedState::default ()), Condvar::new ()));
let pair2 = Arc::clone (&pair); let pair2 = Arc::clone (&pair);
let pair3 = Arc::clone (&pair); let pair3 = Arc::clone (&pair);
@ -121,7 +116,7 @@ fn cmd_play (args: &[String]) -> Result <()> {
while pcm_buffers.samples_available () < 12_000 { while pcm_buffers.samples_available () < 12_000 {
// tracing::trace! ("Decoder is trying to work..."); // tracing::trace! ("Decoder is trying to work...");
match decoder.next ()? { match decoder.next ()? {
Some (frame) => pcm_buffers.produce_bytes (frame.data ().into ()), Some (frame) => pcm_buffers.produce_bytes (&frame.data (0) [0..frame.samples () * 4 * 2]),
None => { None => {
tracing::info! ("Finished decoding file"); tracing::info! ("Finished decoding file");
break 'one_file; break 'one_file;