From 858d1cde8a7e70d88f262cb636871e3ce9e1f60b Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 02:38:58 +0000 Subject: [PATCH 1/2] Add neutral XFL frame sound schema views --- crates/xfl/src/lib.rs | 2 + crates/xfl/src/sound.rs | 144 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 crates/xfl/src/sound.rs diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 6b43420e2..4248b98a7 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -35,6 +35,8 @@ pub mod media; pub mod shape; /// Borrowed views of native XFL shape-hint records. pub mod shape_hints; +/// Borrowed views of native XFL frame-sound and envelope records. +pub mod sound; /// Borrowed views of native XFL tween and custom-ease records. pub mod tween; diff --git a/crates/xfl/src/sound.rs b/crates/xfl/src/sound.rs new file mode 100644 index 000000000..0c3a69d66 --- /dev/null +++ b/crates/xfl/src/sound.rs @@ -0,0 +1,144 @@ +//! Borrowed views of native XFL frame-sound and envelope records. +//! +//! These parse the frame fields, effect-name spelling, and envelope wire values. Resolving sound +//! library items and mapping effects/envelopes into a consumer's audio model remain application +//! responsibilities. + +use crate::shape::XflElement; + +/// Native frame-sound synchronization mode. Unknown values use Animate's event behavior. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum SyncMode { + Start, + Stop, + Stream, + #[default] + Event, +} + +/// A native sound instruction stored on a `DOMFrame`. +#[derive(Clone, Copy, Debug)] +pub struct FrameSound<'a, E: XflElement> { + frame: &'a E, +} + +impl<'a, E: XflElement> FrameSound<'a, E> { + pub fn new(frame: &'a E) -> Self { + Self { frame } + } + + pub fn name(&self) -> Option<&'a str> { + self.frame.attr("soundName") + } + + pub fn sync_mode(&self) -> SyncMode { + match self.frame.attr("soundSync").map(str::trim) { + Some("start") => SyncMode::Start, + Some("stop") => SyncMode::Stop, + Some("stream") => SyncMode::Stream, + _ => SyncMode::Event, + } + } + + pub fn looping(&self) -> bool { + self.frame.attr("soundLoopMode").map(str::trim) == Some("loop") + } + + pub fn repeat(&self) -> Option { + integer(self.frame, "soundLoop") + } + + pub fn has_in_point(&self) -> bool { + self.frame.attr("inPoint44").is_some() + } + + pub fn has_out_point(&self) -> bool { + self.frame.attr("outPoint44").is_some() + } + + /// The effect menu label normalized to lowercase ASCII letters and digits, matching the + /// legacy reader's recognition of labels such as `Fade Left to Right`. + pub fn effect_key(&self) -> String { + self.frame + .attr("soundEffect") + .unwrap_or("") + .chars() + .filter(char::is_ascii_alphanumeric) + .map(|character| character.to_ascii_lowercase()) + .collect() + } + + pub fn envelope_points(&self) -> impl Iterator> + 'a { + self.frame + .child("SoundEnvelope") + .into_iter() + .flat_map(|envelope| envelope.elements_named("SoundEnvelopePoint")) + .map(|element| EnvelopePoint { element }) + } +} + +/// One native `SoundEnvelopePoint` record. Levels are raw SWF/XFL integer units. +#[derive(Clone, Copy, Debug)] +pub struct EnvelopePoint<'a, E: XflElement> { + element: &'a E, +} + +impl EnvelopePoint<'_, E> { + pub fn mark44(&self) -> Option { + number(self.element, "mark44") + } + + pub fn level0(&self) -> Option { + number(self.element, "level0") + } + + pub fn level1(&self) -> Option { + number(self.element, "level1") + } +} + +fn number(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) +} + +fn integer(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(source: &[u8]) -> craft_xml::El { + craft_xml::parse(source).expect("well-formed test XML") + } + + #[test] + fn reads_frame_sound_modes_repeat_trim_and_effect_name() { + let frame = parse(br#""#); + let sound = FrameSound::new(&frame); + assert_eq!(sound.name(), Some("Music")); + assert_eq!(sound.sync_mode(), SyncMode::Stream); + assert!(sound.looping()); + assert_eq!(sound.repeat(), Some(12)); + assert!(sound.has_in_point()); + assert!(sound.has_out_point()); + assert_eq!(sound.effect_key(), "fadelefttoright"); + } + + #[test] + fn defaults_and_envelope_records_keep_native_optional_values() { + let frame = parse(br#""#); + let sound = FrameSound::new(&frame); + assert_eq!(sound.sync_mode(), SyncMode::Event); + assert!(!sound.looping()); + assert_eq!(sound.repeat(), None); + assert!(!sound.has_in_point()); + assert!(!sound.has_out_point()); + assert_eq!(sound.effect_key(), "custom"); + let points: Vec<_> = sound.envelope_points().collect(); + assert_eq!(points.len(), 2); + assert_eq!((points[0].mark44(), points[0].level0(), points[0].level1()), (Some(44100.0), Some(32768.0), None)); + assert_eq!((points[1].mark44(), points[1].level0(), points[1].level1()), (None, Some(0.0), None)); + } +} From afad374d74cf5f85366b381a7191cf0aee6a02d1 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 02:46:53 +0000 Subject: [PATCH 2/2] Reuse XFL wire scalar parsers --- crates/xfl/src/sound.rs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/crates/xfl/src/sound.rs b/crates/xfl/src/sound.rs index 0c3a69d66..408b000f1 100644 --- a/crates/xfl/src/sound.rs +++ b/crates/xfl/src/sound.rs @@ -4,7 +4,7 @@ //! library items and mapping effects/envelopes into a consumer's audio model remain application //! responsibilities. -use crate::shape::XflElement; +use crate::shape::{XflElement, integer, number}; /// Native frame-sound synchronization mode. Unknown values use Animate's event behavior. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] @@ -97,14 +97,6 @@ impl EnvelopePoint<'_, E> { } } -fn number(element: &impl XflElement, name: &str) -> Option { - element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) -} - -fn integer(element: &impl XflElement, name: &str) -> Option { - element.attr(name)?.trim().parse::().ok() -} - #[cfg(test)] mod tests { use super::*;