🚧 wip: I might have fixed the popping.

I was not supposed to be calling `flush` in the first place
main
_ 2021-11-13 11:05:32 -06:00
parent e6c157b556
commit 9c3368f28b
2 changed files with 41 additions and 15 deletions

View File

@ -121,6 +121,30 @@ impl Decoder {
}
pub fn next (&mut self) -> Result <Option <AudioFrame>> {
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 <Option <AudioFrame>> {
loop {
match self.pump_resampler ()? {
Some (x) => {
@ -151,15 +175,23 @@ impl Decoder {
}
pub fn pump_resampler (&mut self) -> Result <Option <AudioFrame>> {
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 {

View File

@ -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;