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
use std::{iter::Iterator, mem, time::Duration};
use rodio::{Sample, Source};
pub struct EndSignalSource<I: Source, F: FnOnce()>
where
<I as Iterator>::Item: Sample,
{
input: I,
f: Option<F>,
}
impl<I: Source, F: FnOnce()> EndSignalSource<I, F>
where
<I as Iterator>::Item: Sample,
{
pub fn new(input: I, f: F) -> EndSignalSource<I, F> {
EndSignalSource { input, f: Some(f) }
}
}
impl<I: Source, F: FnOnce()> Iterator for EndSignalSource<I, F>
where
<I as Iterator>::Item: Sample,
{
type Item = <I as Iterator>::Item;
fn next(&mut self) -> Option<Self::Item> {
let next = self.input.next();
if next.is_none() {
let f = mem::replace(&mut self.f, None);
if let Some(f) = f {
f()
}
}
next
}
}
impl<I: Source, F: FnOnce()> Source for EndSignalSource<I, F>
where
<I as Iterator>::Item: Sample,
{
fn current_frame_len(&self) -> Option<usize> {
self.input.current_frame_len()
}
fn channels(&self) -> u16 {
self.input.channels()
}
fn sample_rate(&self) -> u32 {
self.input.sample_rate()
}
fn total_duration(&self) -> Option<Duration> {
self.input.total_duration()
}
}