From c9ddc76add5cc660ce3e72c2350e4122248d7772 Mon Sep 17 00:00:00 2001 From: Rom's Date: Sat, 18 Jan 2025 08:10:01 +0700 Subject: [PATCH] add Static/StreamingSoundData::unsliced_duration (#109) * add total duration * add tests for total duration * rename total duration as unsliced duration --- crates/kira/src/sound/static_sound/data.rs | 6 ++++++ crates/kira/src/sound/static_sound/data/test.rs | 11 +++++++++++ crates/kira/src/sound/streaming/data.rs | 8 ++++++++ crates/kira/src/sound/streaming/data/test.rs | 10 ++++++++++ 4 files changed, 35 insertions(+) diff --git a/crates/kira/src/sound/static_sound/data.rs b/crates/kira/src/sound/static_sound/data.rs index 92297e46..a924c284 100644 --- a/crates/kira/src/sound/static_sound/data.rs +++ b/crates/kira/src/sound/static_sound/data.rs @@ -313,6 +313,12 @@ impl StaticSoundData { Duration::from_secs_f64(self.num_frames() as f64 / self.sample_rate as f64) } + /// Returns the total duration of the audio, regardless of its slice. + #[must_use] + pub fn unsliced_duration(&self) -> Duration { + Duration::from_secs_f64(self.frames.len() as f64 / self.sample_rate as f64) + } + /// Returns the nth [`Frame`] of audio in the [`StaticSoundData`]. /// /// If [`StaticSoundData::slice`] is `Some`, this will behave as if the [`StaticSoundData`] diff --git a/crates/kira/src/sound/static_sound/data/test.rs b/crates/kira/src/sound/static_sound/data/test.rs index 9f9cdcfd..6d4d37ab 100644 --- a/crates/kira/src/sound/static_sound/data/test.rs +++ b/crates/kira/src/sound/static_sound/data/test.rs @@ -15,6 +15,17 @@ fn duration() { assert_eq!(static_sound.duration(), Duration::from_secs(4)); } +#[test] +fn unsliced_duration() { + let static_sound = StaticSoundData { + sample_rate: 1, + frames: Arc::new([Frame::from_mono(0.0); 4]), + settings: Default::default(), + slice: Some((2, 3)), + }; + assert_eq!(static_sound.unsliced_duration(), Duration::from_secs(4)); +} + #[test] fn sliced_duration() { let static_sound = StaticSoundData { diff --git a/crates/kira/src/sound/streaming/data.rs b/crates/kira/src/sound/streaming/data.rs index 07921c6e..f16149f4 100644 --- a/crates/kira/src/sound/streaming/data.rs +++ b/crates/kira/src/sound/streaming/data.rs @@ -291,6 +291,14 @@ impl StreamingSoundData { Duration::from_secs_f64(self.num_frames() as f64 / self.decoder.sample_rate() as f64) } + /// Returns the total duration of the audio, regardless of its slice. + #[must_use] + pub fn unsliced_duration(&self) -> Duration { + Duration::from_secs_f64( + self.decoder.num_frames() as f64 / self.decoder.sample_rate() as f64, + ) + } + /** Sets the portion of the audio this [`StreamingSoundData`] represents. */ diff --git a/crates/kira/src/sound/streaming/data/test.rs b/crates/kira/src/sound/streaming/data/test.rs index 557615a7..0665716d 100644 --- a/crates/kira/src/sound/streaming/data/test.rs +++ b/crates/kira/src/sound/streaming/data/test.rs @@ -15,6 +15,16 @@ fn duration() { assert_eq!(sound.duration(), Duration::from_secs(4)); } +#[test] +fn unsliced_duration() { + let sound = StreamingSoundData { + decoder: Box::new(MockDecoder::new(vec![Frame::from_mono(0.5); 4])), + settings: Default::default(), + slice: Some((2, 3)), + }; + assert_eq!(sound.unsliced_duration(), Duration::from_secs(4)); +} + #[test] fn sliced_duration() { let sound = StreamingSoundData {