🚧 wip: I might have fixed the popping.
I was not supposed to be calling `flush` in the first placemain
parent
e6c157b556
commit
9c3368f28b
|
@ -121,6 +121,30 @@ impl Decoder {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next (&mut self) -> Result <Option <AudioFrame>> {
|
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 {
|
loop {
|
||||||
match self.pump_resampler ()? {
|
match self.pump_resampler ()? {
|
||||||
Some (x) => {
|
Some (x) => {
|
||||||
|
@ -151,15 +175,23 @@ impl Decoder {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pump_resampler (&mut self) -> Result <Option <AudioFrame>> {
|
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 ();
|
let mut frame_resampled = Self::new_frame ();
|
||||||
|
|
||||||
// dbg! (self.resampler.delay ());
|
frame_src.set_rate (44100);
|
||||||
|
|
||||||
if self.resampler.delay ().is_none () {
|
tracing::trace! ("Flushing resampler");
|
||||||
return Ok (None);
|
tracing::trace! ("delay: {:?}", delay);
|
||||||
}
|
let _rc = self.resampler.run (&frame_src, &mut frame_resampled)?;
|
||||||
|
|
||||||
let _rc = self.resampler.flush (&mut frame_resampled)?;
|
|
||||||
// dbg! (&frame_resampled, rc);
|
// dbg! (&frame_resampled, rc);
|
||||||
|
|
||||||
Ok (if frame_resampled.samples () > 0 {
|
Ok (if frame_resampled.samples () > 0 {
|
||||||
|
|
12
src/main.rs
12
src/main.rs
|
@ -19,11 +19,6 @@ use anyhow::{
|
||||||
Result,
|
Result,
|
||||||
};
|
};
|
||||||
|
|
||||||
use byteorder::{
|
|
||||||
ByteOrder,
|
|
||||||
LittleEndian,
|
|
||||||
};
|
|
||||||
|
|
||||||
use cpal::traits::{
|
use cpal::traits::{
|
||||||
DeviceTrait,
|
DeviceTrait,
|
||||||
HostTrait,
|
HostTrait,
|
||||||
|
@ -57,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 (0))?;
|
f.write_all (&frame.data (0) [0..frame.samples () * 4 * 2])?;
|
||||||
|
// f.write_all (frame.data (0))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok (())
|
Ok (())
|
||||||
|
@ -75,8 +71,6 @@ fn cmd_debug_pipe (args: &[String]) -> Result <()> {
|
||||||
|
|
||||||
while let Some (frame) = decoder.next ()? {
|
while let Some (frame) = decoder.next ()? {
|
||||||
sample_count += frame.samples ();
|
sample_count += frame.samples ();
|
||||||
assert_eq! (frame.rate (), 48000);
|
|
||||||
assert! (frame.samples () > 0);
|
|
||||||
// dbg! (frame, sample_count);
|
// dbg! (frame, sample_count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,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 (0)),
|
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;
|
||||||
|
|
Loading…
Reference in New Issue