Skip to content

Commit

Permalink
add Static/StreamingSoundData::unsliced_duration (#109)
Browse files Browse the repository at this point in the history
* add total duration

* add tests for total duration

* rename total duration as unsliced duration
  • Loading branch information
Roms1383 authored Jan 18, 2025
1 parent a9a1886 commit c9ddc76
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions crates/kira/src/sound/static_sound/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`]
Expand Down
11 changes: 11 additions & 0 deletions crates/kira/src/sound/static_sound/data/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions crates/kira/src/sound/streaming/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,14 @@ impl<Error: Send> StreamingSoundData<Error> {
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.
*/
Expand Down
10 changes: 10 additions & 0 deletions crates/kira/src/sound/streaming/data/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c9ddc76

Please sign in to comment.