audio: Fix skipping initial frame of MP3 audio

The changes in #5498 caused the samples from the initial MP3 frame
to be skipped. This was noticable in:
https://www.newgrounds.com/portal/view/1

This change properly removes the redundant `num_samples` variable
and considers the samples from the initial MP3 frame.
This commit is contained in:
Mike Welsh 2021-10-21 01:01:10 -07:00
parent 969fab7619
commit 11ae981890
1 changed files with 3 additions and 6 deletions

View File

@ -14,7 +14,6 @@ pub mod minimp3 {
sample_rate: u16, sample_rate: u16,
num_channels: u8, num_channels: u8,
cur_sample: usize, cur_sample: usize,
num_samples: usize,
} }
impl<R: Read> Mp3Decoder<R> { impl<R: Read> Mp3Decoder<R> {
@ -29,16 +28,14 @@ pub mod minimp3 {
sample_rate, sample_rate,
num_channels, num_channels,
cur_sample: 0, cur_sample: 0,
num_samples: 0,
}) })
} }
fn next_frame(&mut self) { fn next_frame(&mut self) {
if let Ok(frame) = self.decoder.next_frame() { if let Ok(frame) = self.decoder.next_frame() {
self.num_samples = frame.data.len();
self.frame = frame; self.frame = frame;
} else { } else {
self.num_samples = 0; self.frame.data.clear();
} }
self.cur_sample = 0; self.cur_sample = 0;
} }
@ -50,11 +47,11 @@ pub mod minimp3 {
#[inline] #[inline]
#[allow(unknown_lints, clippy::branches_sharing_code)] #[allow(unknown_lints, clippy::branches_sharing_code)]
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.cur_sample >= self.num_samples { if self.cur_sample >= self.frame.data.len() {
self.next_frame(); self.next_frame();
} }
if self.num_samples > 0 { if !self.frame.data.is_empty() {
if self.num_channels() == 2 { if self.num_channels() == 2 {
let left = self.frame.data[self.cur_sample]; let left = self.frame.data[self.cur_sample];
let right = self.frame.data[self.cur_sample + 1]; let right = self.frame.data[self.cur_sample + 1];