Skip to content

Commit a5855e4

Browse files
Sim-huclaude
andcommitted
feat: infinite sources return Duration::MAX for total_duration
Previously, `total_duration()` returned `None` for both infinite and unknown-duration sources, making them indistinguishable. Infinite sources (generators, repeat, looped decoders, microphone) now return `Some(Duration::MAX)`, so wrapping them with `TakeDuration` correctly reports the requested duration instead of `None`. Fixes #702 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a352fb5 commit a5855e4

12 files changed

Lines changed: 44 additions & 16 deletions

File tree

src/decoder/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -727,12 +727,11 @@ where
727727

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

738737
/// Attempts to seek to a specific position in the audio stream.

src/microphone.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl Source for Microphone {
204204
}
205205

206206
fn total_duration(&self) -> Option<std::time::Duration> {
207-
None
207+
Some(std::time::Duration::MAX)
208208
}
209209
}
210210

@@ -219,7 +219,7 @@ impl crate::FixedSource for Microphone {
219219
}
220220

221221
fn total_duration(&self) -> Option<std::time::Duration> {
222-
None
222+
Some(std::time::Duration::MAX)
223223
}
224224
}
225225

src/source/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,10 @@ pub trait Source: Iterator<Item = Sample> {
197197

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

203206
/// Stores the source in a buffer in addition to returning it. This iterator can be cloned.

src/source/noise.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ macro_rules! impl_noise_source {
8383
}
8484

8585
fn total_duration(&self) -> Option<Duration> {
86-
None
86+
Some(Duration::MAX)
8787
}
8888

8989
fn try_seek(&mut self, _pos: Duration) -> Result<(), crate::source::SeekError> {

src/source/repeat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ where
8383

8484
#[inline]
8585
fn total_duration(&self) -> Option<Duration> {
86-
None
86+
Some(Duration::MAX)
8787
}
8888

8989
#[inline]

src/source/sawtooth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Source for SawtoothWave {
5656

5757
#[inline]
5858
fn total_duration(&self) -> Option<Duration> {
59-
None
59+
Some(Duration::MAX)
6060
}
6161

6262
#[inline]

src/source/signal_generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl Source for SignalGenerator {
153153

154154
#[inline]
155155
fn total_duration(&self) -> Option<Duration> {
156-
None
156+
Some(Duration::MAX)
157157
}
158158

159159
#[inline]

src/source/sine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Source for SineWave {
5656

5757
#[inline]
5858
fn total_duration(&self) -> Option<Duration> {
59-
None
59+
Some(Duration::MAX)
6060
}
6161

6262
#[inline]

src/source/square.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Source for SquareWave {
5656

5757
#[inline]
5858
fn total_duration(&self) -> Option<Duration> {
59-
None
59+
Some(Duration::MAX)
6060
}
6161

6262
#[inline]

src/source/take.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ where
165165
Some(self.requested_duration)
166166
}
167167
} else {
168-
None
168+
Some(self.requested_duration)
169169
}
170170
}
171171

0 commit comments

Comments
 (0)