euphorium 0.4.0

Painless audio playback for native and web platforms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
//! Renderer-agnostic waveform view state.
//!
//! [`WaveformView`] owns an optional [`WaveformBuilder`] plus the zoom/scroll window and scrub
//! flag that an interactive waveform widget needs. It performs the incremental decode pump
//! (`prioritize` + `advance_frames`) and the range math (visible range, seek mapping, playhead
//! and tick fractions) without depending on any UI toolkit. The application wires the result up
//! to its renderer (egui, iced, canvas, ...) and to a [`crate::Sound`] for playhead/seek.

#[cfg(not(target_arch = "wasm32"))]
use std::path::Path;
use std::{num::NonZeroUsize, ops::Range, time::Duration};

use rodio::Source;

use crate::{SoundAsset, SoundscapeError, Waveform, WaveformBuilder, WaveformSlice};

/// Result of [`WaveformView::poll_visible`] and
/// [`WaveformView::poll_visible_with_budget`].
#[derive(Clone, Debug, PartialEq)]
pub struct WaveformPoll {
    /// Visible time range in seconds after following the playhead.
    pub visible: Range<f64>,
    /// Whether the complete source has been decoded.
    ///
    /// When this is `false` the caller should schedule another update (for example
    /// `egui::Context::request_repaint`) and poll again.
    pub finished: bool,
}

/// Renderer-agnostic owner of waveform decoding and its visible window.
///
/// The view holds the [`WaveformBuilder`] for the current track, the zoom level
/// (`visible_secs`), the scroll position derived from the playhead, and the
/// pause-on-scrub resume flag. All time ranges are in seconds; conversions to
/// [`Duration`] for [`WaveformBuilder`] calls happen inside the view.
pub struct WaveformView {
    builder: Option<WaveformBuilder>,
    visible_secs: f64,
    view_start_secs: f64,
    resume_after_scrub: bool,
}

impl Default for WaveformView {
    /// Creates an empty view with the default zoom level.
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Debug for WaveformView {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WaveformView")
            .field("is_available", &self.is_available())
            .field("is_finished", &self.is_finished())
            .field("visible_secs", &self.visible_secs)
            .field("view_start_secs", &self.view_start_secs)
            .field("resume_after_scrub", &self.resume_after_scrub)
            .finish()
    }
}

impl WaveformView {
    /// Default visible window in seconds for a new view or a new track.
    pub const DEFAULT_VISIBLE_SECS: f64 = 20.0;

    /// Creates an empty view with the default zoom level.
    pub fn new() -> Self {
        Self {
            builder: None,
            visible_secs: Self::DEFAULT_VISIBLE_SECS,
            view_start_secs: 0.0,
            resume_after_scrub: false,
        }
    }

    /// Replaces the decoded track and resets the zoom/scroll/scrub state.
    pub fn set_builder(&mut self, builder: Option<WaveformBuilder>) {
        self.builder = builder;
        self.reset_view();
    }

    /// Removes the current track and resets the zoom/scroll/scrub state.
    pub fn clear(&mut self) {
        self.set_builder(None);
    }

    /// Removes and returns the current builder, resetting the view state.
    pub fn take_builder(&mut self) -> Option<WaveformBuilder> {
        let builder = self.builder.take();
        self.reset_view();
        builder
    }

    /// Resets zoom, scroll and scrub state while keeping the current track.
    pub fn reset_view(&mut self) {
        self.visible_secs = Self::DEFAULT_VISIBLE_SECS;
        self.view_start_secs = 0.0;
        self.resume_after_scrub = false;
    }

    /// Starts decoding from a rodio source, replacing the current track.
    pub fn set_source(&mut self, source: impl Source<Item = f32> + 'static) {
        self.set_builder(Some(Waveform::builder_from_source(source)));
    }

    /// Starts decoding from a rodio source with a custom finest resolution.
    pub fn set_source_with_base_frames(
        &mut self,
        source: impl Source<Item = f32> + 'static,
        base_frames_per_peak: NonZeroUsize,
    ) {
        self.set_builder(Some(Waveform::builder_from_source_with_base_frames(
            source,
            base_frames_per_peak,
        )));
    }

    /// Starts decoding static bytes, replacing the current track.
    ///
    /// On error the view is cleared and the error is returned.
    pub fn set_static_bytes(&mut self, bytes: &'static [u8]) -> Result<(), SoundscapeError> {
        match Waveform::builder_from_static_bytes(bytes) {
            Ok(builder) => {
                self.set_builder(Some(builder));
                Ok(())
            }
            Err(error) => {
                self.set_builder(None);
                Err(error)
            }
        }
    }

    /// Starts decoding shared bytes, replacing the current track.
    ///
    /// On error the view is cleared and the error is returned.
    pub fn set_shared_bytes(&mut self, bytes: impl AsRef<[u8]>) -> Result<(), SoundscapeError> {
        match Waveform::builder_from_shared_bytes(bytes) {
            Ok(builder) => {
                self.set_builder(Some(builder));
                Ok(())
            }
            Err(error) => {
                self.set_builder(None);
                Err(error)
            }
        }
    }

    /// Starts decoding a file, replacing the current track.
    ///
    /// On error the view is cleared and the error is returned.
    #[cfg(not(target_arch = "wasm32"))]
    pub fn set_file(&mut self, path: impl AsRef<Path>) -> Result<(), SoundscapeError> {
        match Waveform::builder_from_file(path) {
            Ok(builder) => {
                self.set_builder(Some(builder));
                Ok(())
            }
            Err(error) => {
                self.set_builder(None);
                Err(error)
            }
        }
    }

    /// Starts decoding an audio asset, replacing the current track.
    ///
    /// On error the view is cleared and the error is returned.
    pub async fn set_asset(&mut self, asset: &SoundAsset) -> Result<(), SoundscapeError> {
        match Waveform::builder_from_asset(asset).await {
            Ok(builder) => {
                self.set_builder(Some(builder));
                Ok(())
            }
            Err(error) => {
                self.set_builder(None);
                Err(error)
            }
        }
    }

    /// Returns the current builder, if a track is loaded.
    pub fn builder(&self) -> Option<&WaveformBuilder> {
        self.builder.as_ref()
    }

    /// Returns the current builder mutably, if a track is loaded.
    pub fn builder_mut(&mut self) -> Option<&mut WaveformBuilder> {
        self.builder.as_mut()
    }

    /// Returns whether a track is loaded.
    pub fn is_available(&self) -> bool {
        self.builder.is_some()
    }

    /// Returns whether the complete source has been decoded.
    ///
    /// An empty view reports `true` so callers stop scheduling decode work.
    pub fn is_finished(&self) -> bool {
        self.builder
            .as_ref()
            .is_none_or(WaveformBuilder::is_finished)
    }

    /// Returns the source sample rate, if a track is loaded.
    pub fn sample_rate(&self) -> Option<u32> {
        self.builder.as_ref().map(WaveformBuilder::sample_rate)
    }

    /// Returns the track duration in seconds.
    ///
    /// Uses the source duration when known and falls back to the decoded
    /// duration while loading. Returns `0.0` when no track is loaded.
    pub fn duration_secs(&self) -> f64 {
        self.builder.as_ref().map_or(0.0, |builder| {
            builder
                .duration()
                .unwrap_or_else(|| builder.decoded_duration())
                .as_secs_f64()
        })
    }

    /// Returns the smallest useful visible window for the current track.
    pub fn minimum_visible_secs(&self) -> f64 {
        self.duration_secs().min(0.25)
    }

    /// Returns the current visible window size in seconds.
    pub fn visible_secs(&self) -> f64 {
        self.visible_secs
    }

    /// Returns the visible window mutably for direct binding to a slider.
    ///
    /// The value is clamped to the track duration on the next
    /// [`visible_range`](Self::visible_range) or [`poll_visible`](Self::poll_visible) call.
    pub fn visible_secs_mut(&mut self) -> &mut f64 {
        &mut self.visible_secs
    }

    /// Sets the desired visible window size in seconds.
    ///
    /// Non-finite values are ignored. Clamping to the track duration happens on
    /// the next [`visible_range`](Self::visible_range) or
    /// [`poll_visible`](Self::poll_visible) call.
    pub fn set_visible_secs(&mut self, visible_secs: f64) {
        if visible_secs.is_finite() {
            self.visible_secs = visible_secs;
        }
    }

    /// Zooms by a delta where values above `1.0` zoom in.
    ///
    /// Non-finite or non-positive deltas are ignored. This matches pinch/scroll
    /// zoom deltas: the visible window is divided by `delta`.
    pub fn zoom_by(&mut self, delta: f64) {
        if delta.is_finite() && delta > 0.0 {
            self.visible_secs /= delta;
        }
    }

    /// Halves the visible window (zoom in).
    pub fn zoom_in(&mut self) {
        self.zoom_by(2.0);
    }

    /// Doubles the visible window (zoom out).
    pub fn zoom_out(&mut self) {
        self.zoom_by(0.5);
    }

    /// Returns the visible time range in seconds, following the playhead.
    ///
    /// The window stays centered on `playhead_secs` where possible and is
    /// clamped to the track duration. Returns `0.0..0.0` when no track is
    /// loaded.
    pub fn visible_range(&mut self, playhead_secs: f64) -> Range<f64> {
        let duration_secs = self.duration_secs();
        self.visible_range_inner(duration_secs, playhead_secs)
    }

    /// Returns the visible time range as [`Duration`]s for decoding and slicing.
    pub fn waveform_range(&mut self, playhead_secs: f64) -> Range<Duration> {
        let visible = self.visible_range(playhead_secs);
        Self::durations_from_secs(&visible)
    }

    /// Prioritizes the playhead-visible range and decodes about one second of audio.
    ///
    /// This is the per-frame pump previously embedded in the example app: it
    /// computes the follow-playhead range, calls
    /// [`WaveformBuilder::prioritize`] for it, then
    /// [`WaveformBuilder::advance_frames`] with a budget of one second of
    /// frames (`sample_rate`). When the returned [`WaveformPoll::finished`] is
    /// `false`, schedule another update and poll again.
    pub fn poll_visible(&mut self, playhead_secs: f64) -> Result<WaveformPoll, SoundscapeError> {
        let budget = self.sample_rate().map_or(0, |rate| rate as usize);
        self.poll_visible_with_budget(playhead_secs, budget)
    }

    /// Prioritizes the playhead-visible range and decodes at most `max_frames`.
    ///
    /// A frame contains one sample per channel. See [`poll_visible`](Self::poll_visible).
    pub fn poll_visible_with_budget(
        &mut self,
        playhead_secs: f64,
        max_frames: usize,
    ) -> Result<WaveformPoll, SoundscapeError> {
        if self.builder.is_none() {
            return Ok(WaveformPoll {
                visible: 0.0..0.0,
                finished: true,
            });
        }

        let duration_secs = self.duration_secs();
        let visible = self.visible_range_inner(duration_secs, playhead_secs);
        let range = Self::durations_from_secs(&visible);

        let Some(builder) = self.builder.as_mut() else {
            return Ok(WaveformPoll {
                visible: 0.0..0.0,
                finished: true,
            });
        };

        builder.prioritize(range)?;
        if !builder.is_finished() {
            builder.advance_frames(max_frames);
        }

        Ok(WaveformPoll {
            visible,
            finished: builder.is_finished(),
        })
    }

    /// Borrows peaks covering `visible`, using no more than `max_peaks` when possible.
    ///
    /// Returns `None` when no track is loaded. The range is clamped to the
    /// track duration; pass the range from [`visible_range`](Self::visible_range)
    /// or [`WaveformPoll::visible`].
    pub fn slice(&self, visible: Range<f64>, max_peaks: usize) -> Option<WaveformSlice<'_>> {
        let builder = self.builder.as_ref()?;
        let duration_secs = self.duration_secs();
        let start_secs = sanitize_secs(visible.start, duration_secs);
        let end_secs = sanitize_secs(visible.end, duration_secs);
        let (start, end) = if end_secs >= start_secs {
            (start_secs, end_secs)
        } else {
            (end_secs, start_secs)
        };

        Some(builder.slice(
            Duration::from_secs_f64(start)..Duration::from_secs_f64(end),
            max_peaks,
        ))
    }

    /// Maps a `0.0..=1.0` pointer fraction inside `visible` to track seconds.
    ///
    /// Out-of-range or non-finite fractions are clamped to the visible range.
    pub fn seek_secs(visible: &Range<f64>, fraction: f64) -> f64 {
        if !visible.start.is_finite() || !visible.end.is_finite() {
            return 0.0;
        }
        let fraction = if fraction.is_finite() {
            fraction.clamp(0.0, 1.0)
        } else {
            0.0
        };
        visible.start + (visible.end - visible.start).max(0.0) * fraction
    }

    /// Maps a track time to a `0.0..=1.0` fraction inside `visible` for drawing.
    ///
    /// Returns `0.0` for empty or invalid ranges.
    pub fn playhead_fraction(playhead_secs: f64, visible: &Range<f64>) -> f32 {
        if !playhead_secs.is_finite() || !visible.start.is_finite() || !visible.end.is_finite() {
            return 0.0;
        }
        let len = visible.end - visible.start;
        if !len.is_finite() || len <= 0.0 {
            return 0.0;
        }
        ((playhead_secs - visible.start) / len).clamp(0.0, 1.0) as f32
    }

    /// Returns the track time of tick `index` out of `tick_count` evenly spaced ticks.
    ///
    /// Returns the visible start when `tick_count <= 1`. Indices beyond the
    /// last tick clamp to the visible end.
    pub fn tick_secs(visible: &Range<f64>, index: usize, tick_count: usize) -> f64 {
        if tick_count <= 1 {
            return if visible.start.is_finite() {
                visible.start.max(0.0)
            } else {
                0.0
            };
        }
        let last = tick_count - 1;
        let fraction = index.min(last) as f64 / last as f64;
        Self::seek_secs(visible, fraction)
    }

    /// Records whether playback was active when a scrub gesture started.
    pub fn begin_scrub(&mut self, was_playing: bool) {
        self.resume_after_scrub = was_playing;
    }

    /// Clears the scrub flag, returning whether the caller should resume playback.
    pub fn end_scrub(&mut self) -> bool {
        let resume = self.resume_after_scrub;
        self.resume_after_scrub = false;
        resume
    }

    /// Returns the pending scrub-resume flag without clearing it.
    pub fn resume_after_scrub(&self) -> bool {
        self.resume_after_scrub
    }

    fn visible_range_inner(&mut self, duration_secs: f64, playhead_secs: f64) -> Range<f64> {
        if !duration_secs.is_finite() || duration_secs <= 0.0 {
            return 0.0..0.0;
        }
        if !self.visible_secs.is_finite() {
            self.visible_secs = Self::DEFAULT_VISIBLE_SECS;
        }

        let minimum_visible_secs = duration_secs.min(0.25);
        self.visible_secs = self.visible_secs.clamp(minimum_visible_secs, duration_secs);
        let visible_secs = self.visible_secs;
        let max_start = (duration_secs - visible_secs).max(0.0);
        let playhead_secs = if playhead_secs.is_finite() {
            playhead_secs
        } else {
            0.0
        };

        self.view_start_secs = (playhead_secs - visible_secs * 0.5).clamp(0.0, max_start);

        self.view_start_secs..self.view_start_secs + visible_secs
    }

    fn durations_from_secs(visible: &Range<f64>) -> Range<Duration> {
        let start = if visible.start.is_finite() {
            visible.start.max(0.0)
        } else {
            0.0
        };
        let end = if visible.end.is_finite() {
            visible.end.max(start)
        } else {
            start
        };

        Duration::from_secs_f64(start)..Duration::from_secs_f64(end)
    }
}

fn sanitize_secs(secs: f64, duration_secs: f64) -> f64 {
    if secs.is_nan() {
        return 0.0;
    }
    if !secs.is_finite() {
        return if secs.is_sign_positive() {
            duration_secs.max(0.0)
        } else {
            0.0
        };
    }
    secs.clamp(0.0, duration_secs.max(0.0))
}

#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests {
    use rodio::buffer::SamplesBuffer;

    use super::WaveformView;

    fn test_view() -> WaveformView {
        let samples = vec![-0.25, 0.5, -1.25, 0.75, -0.5, 0.25, -0.75, 1.5];
        let source = SamplesBuffer::new(
            std::num::NonZeroU16::new(1).unwrap(),
            std::num::NonZeroU32::new(8).unwrap(),
            samples,
        );
        let mut view = WaveformView::new();
        view.set_source(source);
        view
    }

    #[test]
    fn visible_range_follows_the_playhead_and_clamps_to_duration() {
        let mut view = test_view();
        assert_eq!(view.duration_secs(), 1.0);

        let visible = view.visible_range(0.5);
        assert_eq!(visible, 0.0..1.0);

        view.set_visible_secs(0.5);
        let visible = view.visible_range(0.5);
        assert_eq!(visible, 0.25..0.75);

        let start = view.visible_range(0.0);
        assert_eq!(start, 0.0..0.5);
        let end = view.visible_range(10.0);
        assert_eq!(end, 0.5..1.0);
    }

    #[test]
    fn zoom_and_seek_mapping_round_trip() {
        let mut view = test_view();
        view.set_visible_secs(0.5);
        view.zoom_in();
        assert_eq!(view.visible_secs(), 0.25);
        view.zoom_out();
        assert_eq!(view.visible_secs(), 0.5);

        let visible = view.visible_range(0.5);
        assert_eq!(WaveformView::seek_secs(&visible, 0.0), visible.start);
        assert_eq!(WaveformView::seek_secs(&visible, 1.0), visible.end);
        assert_eq!(WaveformView::seek_secs(&visible, 0.5), 0.5);
        assert_eq!(WaveformView::tick_secs(&visible, 0, 5), visible.start);
        assert_eq!(WaveformView::tick_secs(&visible, 4, 5), visible.end);
        assert_eq!(WaveformView::playhead_fraction(0.5, &visible), 0.5);
    }

    #[test]
    fn poll_decodes_to_completion_and_scrub_flag_round_trips() {
        let mut view = test_view();
        view.begin_scrub(true);
        assert!(view.resume_after_scrub());

        let mut finished = false;
        for _ in 0..16 {
            let poll = view
                .poll_visible(0.0)
                .expect("buffer source should prioritize");
            finished = poll.finished;
            if finished {
                break;
            }
        }
        assert!(finished);
        assert!(view.is_finished());
        assert!(view.end_scrub());
        assert!(!view.resume_after_scrub());
    }
}