Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avm1: Various Sound fixes #4026

Merged
merged 4 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions core/src/avm1/globals/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ pub fn create_proto<'gc>(
Attribute::DONT_DELETE | Attribute::READ_ONLY | Attribute::DONT_ENUM,
);

object.as_script_object().unwrap().force_set_function(
"getDuration",
duration,
gc_context,
Attribute::DONT_DELETE | Attribute::READ_ONLY | Attribute::DONT_ENUM,
Some(fn_proto),
);

object.as_script_object().unwrap().force_set_function(
"setDuration",
|_, _, _| Ok(Value::Undefined),
gc_context,
Attribute::DONT_DELETE | Attribute::READ_ONLY | Attribute::DONT_ENUM,
Some(fn_proto),
);

object.add_property(
gc_context,
"id3",
Expand Down Expand Up @@ -200,11 +216,7 @@ 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)
.unwrap_or(0),
activation.context.audio.get_sound_duration(*sound),
);
sound_object.set_position(activation.context.gc_context, 0);
} else {
Expand All @@ -230,7 +242,9 @@ fn duration<'gc>(
) -> Result<Value<'gc>, Error<'gc>> {
if activation.current_swf_version() >= 6 {
if let Some(sound_object) = this.as_sound_object() {
return Ok(sound_object.duration().into());
return Ok(sound_object
.duration()
.map_or(Value::Undefined, |d| d.into()));
} else {
avm_warn!(activation, "Sound.duration: this is not a Sound");
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/avm1/object/sound_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct SoundObjectData<'gc> {
position: u32,

/// Duration of the currently attached sound in milliseconds.
duration: u32,
duration: Option<u32>,
}

impl fmt::Debug for SoundObject<'_> {
Expand All @@ -65,16 +65,16 @@ impl<'gc> SoundObject<'gc> {
sound_instance: None,
owner: None,
position: 0,
duration: 0,
duration: None,
},
))
}

pub fn duration(self) -> u32 {
pub fn duration(self) -> Option<u32> {
self.0.read().duration
}

pub fn set_duration(self, gc_context: MutationContext<'gc, '_>, duration: u32) {
pub fn set_duration(self, gc_context: MutationContext<'gc, '_>, duration: Option<u32>) {
self.0.write(gc_context).duration = duration;
}

Expand Down
3 changes: 3 additions & 0 deletions core/tests/swfs/avm1/sound/output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ left-to-right: 0
right-to-left: 0
right-to-right: 100

sound.duration: undefined
sound.getDuration(): undefined

Sound.setVolume(50.5)
volume: 50
pan: 0
Expand Down
Binary file modified core/tests/swfs/avm1/sound/test.fla
Binary file not shown.
Binary file modified core/tests/swfs/avm1/sound/test.swf
Binary file not shown.
7 changes: 4 additions & 3 deletions desktop/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions web/src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down