Skip to content

Commit c9ddc76

Browse files
authored
add Static/StreamingSoundData::unsliced_duration (#109)
* add total duration * add tests for total duration * rename total duration as unsliced duration
1 parent a9a1886 commit c9ddc76

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

crates/kira/src/sound/static_sound/data.rs

+6
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ impl StaticSoundData {
313313
Duration::from_secs_f64(self.num_frames() as f64 / self.sample_rate as f64)
314314
}
315315

316+
/// Returns the total duration of the audio, regardless of its slice.
317+
#[must_use]
318+
pub fn unsliced_duration(&self) -> Duration {
319+
Duration::from_secs_f64(self.frames.len() as f64 / self.sample_rate as f64)
320+
}
321+
316322
/// Returns the nth [`Frame`] of audio in the [`StaticSoundData`].
317323
///
318324
/// If [`StaticSoundData::slice`] is `Some`, this will behave as if the [`StaticSoundData`]

crates/kira/src/sound/static_sound/data/test.rs

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ fn duration() {
1515
assert_eq!(static_sound.duration(), Duration::from_secs(4));
1616
}
1717

18+
#[test]
19+
fn unsliced_duration() {
20+
let static_sound = StaticSoundData {
21+
sample_rate: 1,
22+
frames: Arc::new([Frame::from_mono(0.0); 4]),
23+
settings: Default::default(),
24+
slice: Some((2, 3)),
25+
};
26+
assert_eq!(static_sound.unsliced_duration(), Duration::from_secs(4));
27+
}
28+
1829
#[test]
1930
fn sliced_duration() {
2031
let static_sound = StaticSoundData {

crates/kira/src/sound/streaming/data.rs

+8
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ impl<Error: Send> StreamingSoundData<Error> {
291291
Duration::from_secs_f64(self.num_frames() as f64 / self.decoder.sample_rate() as f64)
292292
}
293293

294+
/// Returns the total duration of the audio, regardless of its slice.
295+
#[must_use]
296+
pub fn unsliced_duration(&self) -> Duration {
297+
Duration::from_secs_f64(
298+
self.decoder.num_frames() as f64 / self.decoder.sample_rate() as f64,
299+
)
300+
}
301+
294302
/**
295303
Sets the portion of the audio this [`StreamingSoundData`] represents.
296304
*/

crates/kira/src/sound/streaming/data/test.rs

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ fn duration() {
1515
assert_eq!(sound.duration(), Duration::from_secs(4));
1616
}
1717

18+
#[test]
19+
fn unsliced_duration() {
20+
let sound = StreamingSoundData {
21+
decoder: Box::new(MockDecoder::new(vec![Frame::from_mono(0.5); 4])),
22+
settings: Default::default(),
23+
slice: Some((2, 3)),
24+
};
25+
assert_eq!(sound.unsliced_duration(), Duration::from_secs(4));
26+
}
27+
1828
#[test]
1929
fn sliced_duration() {
2030
let sound = StreamingSoundData {

0 commit comments

Comments
 (0)