Skip to content
Closed
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
7 changes: 3 additions & 4 deletions src/decoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -727,12 +727,11 @@ where

/// Returns the total duration of this audio source.
///
/// Always returns `None` for looped decoders since they have no fixed end point -
/// they will continue playing indefinitely by seeking back to the start when reaching
/// the end of the audio data.
/// Always returns `Duration::MAX` for looped decoders since they play
/// indefinitely by seeking back to the start when reaching the end.
#[inline]
fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

/// Attempts to seek to a specific position in the audio stream.
Expand Down
4 changes: 2 additions & 2 deletions src/microphone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ impl Source for Microphone {
}

fn total_duration(&self) -> Option<std::time::Duration> {
None
Some(std::time::Duration::MAX)
}
}

Expand All @@ -219,7 +219,7 @@ impl crate::FixedSource for Microphone {
}

fn total_duration(&self) -> Option<std::time::Duration> {
None
Some(std::time::Duration::MAX)
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,10 @@ pub trait Source: Iterator<Item = Sample> {

/// Returns the total duration of this source, if known.
///
/// `None` indicates at the same time "infinite" or "unknown".
/// `None` indicates that the duration is unknown. Infinite sources
/// (e.g. sine wave generators, repeating sources) return
/// `Some(Duration::MAX)` to distinguish them from sources with an
/// unknown but finite duration.
fn total_duration(&self) -> Option<Duration>;

/// Stores the source in a buffer in addition to returning it. This iterator can be cloned.
Expand Down
2 changes: 1 addition & 1 deletion src/source/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro_rules! impl_noise_source {
}

fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

fn try_seek(&mut self, _pos: Duration) -> Result<(), crate::source::SeekError> {
Expand Down
2 changes: 1 addition & 1 deletion src/source/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ where

#[inline]
fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/source/sawtooth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Source for SawtoothWave {

#[inline]
fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/source/signal_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ impl Source for SignalGenerator {

#[inline]
fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/source/sine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Source for SineWave {

#[inline]
fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/source/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Source for SquareWave {

#[inline]
fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/source/take.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ where
Some(self.requested_duration)
}
} else {
None
Some(self.requested_duration)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/source/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Source for TriangleWave {

#[inline]
fn total_duration(&self) -> Option<Duration> {
None
Some(Duration::MAX)
}

#[inline]
Expand Down
28 changes: 27 additions & 1 deletion tests/total_duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
#![allow(unused_imports)]

use std::io::{Read, Seek};
use std::num::NonZero;
use std::path::Path;
use std::time::Duration;

use rodio::{Decoder, Source};
use rodio::source::{SineWave, Source};
use rodio::Decoder;

use rstest::rstest;
use rstest_reuse::{self, *};
Expand Down Expand Up @@ -105,3 +107,27 @@ fn decoder_returns_total_duration(
"decoder got {res}, correct is: {correct_duration}"
);
}

#[test]
fn infinite_sources_return_duration_max() {
let sine = SineWave::new(440.0);
assert_eq!(sine.total_duration(), Some(Duration::MAX));
}

#[test]
fn take_duration_on_infinite_source() {
let sine = SineWave::new(440.0);
let take = sine.take_duration(Duration::from_secs(5));
assert_eq!(take.total_duration(), Some(Duration::from_secs(5)));
}

#[test]
fn repeat_returns_duration_max() {
let buf = rodio::buffer::SamplesBuffer::new(
NonZero::new(1).unwrap(),
NonZero::new(44100).unwrap(),
vec![0.0f32; 44100],
);
let repeated = buf.repeat_infinite();
assert_eq!(repeated.total_duration(), Some(Duration::MAX));
}
Loading