core: Report sound duration as `f64` and round down in AS1

This commit is contained in:
David Wendt 2021-08-13 18:50:15 -04:00 committed by kmeisthax
parent e4c6e29b8a
commit 3b6eea2a1d
4 changed files with 20 additions and 12 deletions

View File

@ -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 {

View File

@ -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<u32>;
fn get_sound_duration(&self, sound: SoundHandle) -> Option<f64>;
/// 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<u32> {
None
}
fn get_sound_duration(&self, sound: SoundHandle) -> Option<u32> {
fn get_sound_duration(&self, sound: SoundHandle) -> Option<f64> {
if let Some(sound) = self.sounds.get(sound) {
Some(sound.duration)
} else {

View File

@ -401,13 +401,13 @@ impl AudioBackend for CpalAudioBackend {
sound_instances.get(instance).map(|_| 0)
}
fn get_sound_duration(&self, sound: SoundHandle) -> Option<u32> {
fn get_sound_duration(&self, sound: SoundHandle) -> Option<f64> {
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
}

View File

@ -1027,13 +1027,13 @@ impl AudioBackend for WebAudioBackend {
})
}
fn get_sound_duration(&self, sound: SoundHandle) -> Option<u32> {
fn get_sound_duration(&self, sound: SoundHandle) -> Option<f64> {
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
}