blob: 2438d01ca4f058a84fe9105e6c3570822805568d (
plain)
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
|
use rodio::{OutputStream, Sink, Source};
use crate::audio::SAMPLE_RATE;
use crate::io::{Wave, Audio};
use std::time::Duration;
pub struct RodioAudio {
_stream: OutputStream,
_sink: Sink,
}
struct RodioWave<W: Wave + Send + 'static>(W, usize);
impl<W: Wave + Send + 'static> Iterator for RodioWave<W>
{
type Item = f32;
fn next(&mut self) -> Option<Self::Item> {
self.1 += 1;
let left = self.1 % 2 == 0;
let result = self.0.next(left);
result
}
}
impl<W: Wave + Send + 'static> Source for RodioWave<W>
{
fn current_frame_len(&self) -> Option<usize> {
None
}
fn channels(&self) -> u16 {
2
}
fn sample_rate(&self) -> u32 {
SAMPLE_RATE
}
fn total_duration(&self) -> Option<Duration> {
None
}
}
impl Audio for RodioAudio {
fn new<S: Wave + Send + 'static>(wave: S) -> Self {
let (stream, stream_handle) = OutputStream::try_default().unwrap();
let sink = Sink::try_new(&stream_handle).unwrap();
sink.append(RodioWave(wave, 0));
RodioAudio {
_stream: stream,
_sink: sink,
}
}
}
|