audio: Properly round sound duration

Previously it was always rounded down. Change it to the nearest
integer.
This commit is contained in:
relrelb 2021-04-16 18:43:17 +03:00 committed by Mike Welsh
parent 6310e31023
commit 79bcee07f5
2 changed files with 8 additions and 6 deletions

View File

@ -427,9 +427,10 @@ impl AudioBackend for CpalAudioBackend {
fn get_sound_duration(&self, sound: SoundHandle) -> Option<u32> {
if let Some(sound) = self.sounds.get(sound) {
// AS duration does not subtract skip_sample_frames.
let num_sample_frames = u64::from(sound.num_sample_frames);
let ms = num_sample_frames * 1000 / u64::from(sound.format.sample_rate);
// AS duration does not subtract `skip_sample_frames`.
let num_sample_frames: f64 = sound.num_sample_frames.into();
let sample_rate: f64 = sound.format.sample_rate.into();
let ms = (num_sample_frames * 1000.0 / sample_rate).round();
Some(ms as u32)
} else {
None

View File

@ -1029,9 +1029,10 @@ impl AudioBackend for WebAudioBackend {
fn get_sound_duration(&self, sound: SoundHandle) -> Option<u32> {
if let Some(sound) = self.sounds.get(sound) {
// AS duration does not subtract skip_sample_frames.
let num_sample_frames = u64::from(sound.num_sample_frames);
let ms = num_sample_frames * 1000 / u64::from(sound.format.sample_rate);
// AS duration does not subtract `skip_sample_frames`.
let num_sample_frames: f64 = sound.num_sample_frames.into();
let sample_rate: f64 = sound.format.sample_rate.into();
let ms = (num_sample_frames * 1000.0 / sample_rate).round();
Some(ms as u32)
} else {
None