From 3b6eea2a1d3c28f610ccda013331c8c8421c0174 Mon Sep 17 00:00:00 2001 From: David Wendt Date: Fri, 13 Aug 2021 18:50:15 -0400 Subject: [PATCH] core: Report sound duration as `f64` and round down in AS1 --- core/src/avm1/globals/sound.rs | 6 +++++- core/src/backend/audio.rs | 14 +++++++++----- desktop/src/audio.rs | 6 +++--- web/src/audio.rs | 6 +++--- 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/core/src/avm1/globals/sound.rs b/core/src/avm1/globals/sound.rs index 95d34af19..1a8003476 100644 --- a/core/src/avm1/globals/sound.rs +++ b/core/src/avm1/globals/sound.rs @@ -85,7 +85,11 @@ fn attach_sound<'gc>( sound_object.set_sound(activation.context.gc_context, Some(*sound)); sound_object.set_duration( activation.context.gc_context, - activation.context.audio.get_sound_duration(*sound), + activation + .context + .audio + .get_sound_duration(*sound) + .map(|d| d.round() as u32), ); sound_object.set_position(activation.context.gc_context, 0); } else { diff --git a/core/src/backend/audio.rs b/core/src/backend/audio.rs index d23d489ff..a730bcd0d 100644 --- a/core/src/backend/audio.rs +++ b/core/src/backend/audio.rs @@ -87,7 +87,7 @@ pub trait AudioBackend: Downcast { /// Get the duration of a sound in milliseconds. /// Returns `None` if sound is not registered. - fn get_sound_duration(&self, sound: SoundHandle) -> Option; + fn get_sound_duration(&self, sound: SoundHandle) -> Option; /// Get the size of the data stored within a given sound. /// @@ -113,8 +113,12 @@ pub trait AudioBackend: Downcast { impl_downcast!(AudioBackend); +/// Information about a sound provided to `NullAudioBackend`. struct NullSound { - duration: u32, + /// The duration of the sound in milliseconds. + duration: f64, + + /// The compressed size of the sound data, excluding MP3 latency seek data. size: u32, } @@ -145,10 +149,10 @@ impl AudioBackend for NullAudioBackend { // AS duration does not subtract `skip_sample_frames`. let num_sample_frames: f64 = sound.num_samples.into(); let sample_rate: f64 = sound.format.sample_rate.into(); - let ms = (num_sample_frames * 1000.0 / sample_rate).round(); + let duration = num_sample_frames * 1000.0 / sample_rate; Ok(self.sounds.insert(NullSound { - duration: ms as u32, + duration, size: data.len() as u32, })) } @@ -177,7 +181,7 @@ impl AudioBackend for NullAudioBackend { fn get_sound_position(&self, _instance: SoundInstanceHandle) -> Option { None } - fn get_sound_duration(&self, sound: SoundHandle) -> Option { + fn get_sound_duration(&self, sound: SoundHandle) -> Option { if let Some(sound) = self.sounds.get(sound) { Some(sound.duration) } else { diff --git a/desktop/src/audio.rs b/desktop/src/audio.rs index 74bb7e666..98353cded 100644 --- a/desktop/src/audio.rs +++ b/desktop/src/audio.rs @@ -401,13 +401,13 @@ impl AudioBackend for CpalAudioBackend { sound_instances.get(instance).map(|_| 0) } - fn get_sound_duration(&self, sound: SoundHandle) -> Option { + fn get_sound_duration(&self, sound: SoundHandle) -> Option { if let Some(sound) = self.sounds.get(sound) { // 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) + let ms = num_sample_frames * 1000.0 / sample_rate; + Some(ms) } else { None } diff --git a/web/src/audio.rs b/web/src/audio.rs index ece6a4177..15df5fbed 100644 --- a/web/src/audio.rs +++ b/web/src/audio.rs @@ -1027,13 +1027,13 @@ impl AudioBackend for WebAudioBackend { }) } - fn get_sound_duration(&self, sound: SoundHandle) -> Option { + fn get_sound_duration(&self, sound: SoundHandle) -> Option { if let Some(sound) = self.sounds.get(sound) { // 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) + let ms = num_sample_frames * 1000.0 / sample_rate; + Some(ms) } else { None }