Skip to content

Commit

Permalink
Cleaner handling of Playback Stop
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyCoolSlug committed Aug 6, 2024
1 parent 170f75f commit dcd4454
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions audio/src/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,10 @@ impl Player {
0
};

let mut break_playback = false;
let mut mono_playback = false;

// Loop over the input file..
let result = loop {
let result = 'main: loop {
let packet = match reader.next_packet() {
Ok(packet) => packet,
Err(err) => {
Expand Down Expand Up @@ -294,34 +293,27 @@ impl Player {
if self.force_stop.load(Ordering::Relaxed) {
// Don't care about the buffer, just end it.
debug!("Force Stop Requested, terminating.");

if let Some(audio_output) = &mut audio_output {
audio_output.stop();
}

break Ok(());
break 'main Ok(());
}

if let Some(fade_amount) = fade_amount {
// Technically, this is a little weird, we don't do a 'per-channel' check on the samples,
// so each channel will have a slightly different volume, for now it's small enough to not
// actually notice :p

for i in 0..=samples.len() - 1 {
samples[i] *= self.volume;
for sample in samples.iter_mut() {
*sample *= self.volume;

// Has the fade amount dropped below 0?
self.volume -= fade_amount;
if self.volume < 0.0 {
// We've reached the end, ensure already processed samples make it
samples = samples[0..i].to_vec();
break_playback = true;
break;
break 'main Ok(());
}
}
} else {
// No fade duration, clear out sample buffer and end.
debug!("Stop Requested, No Fade Out set, Stopping Playback.");
samples = vec![];
break_playback = true;
break 'main Ok(());
}
}

Expand Down Expand Up @@ -365,23 +357,23 @@ impl Player {
// Set the back to FALSE
self.restart_track.store(false, Ordering::Relaxed);
}

// If we've been instructed to break, end it here.
if break_playback {
break Ok(());
}
}
}
Err(err) => break Err(err),
}
};
if !self.force_stop.load(Ordering::Relaxed) {
if let Some(mut audio_output) = audio_output {
if let Some(ref mut audio_output) = audio_output {
// We should always flush the last samples, unless forced to stop
audio_output.flush();
}
}

// Stop the playback handler..
if let Some(mut audio_output) = audio_output {
audio_output.stop();
}

if let Some(ebu_r128) = ebu_r128 {
// Calculate Gain..
let mut loudness = ebu_r128.loudness_global()?;
Expand Down

0 comments on commit dcd4454

Please sign in to comment.