Compare commits

...

3 Commits

2 changed files with 51 additions and 50 deletions

View File

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

View File

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