aboutsummaryrefslogtreecommitdiff
path: root/src/desktop/audio.rs
blob: 60d32dfeb00a63b6a069b65ea2c6bb335a2684cc (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
use rodio::{OutputStream, Sink, Source};

use crate::io::Audio;
use crate::audio::SAMPLE_RATE;
use std::time::Duration;

pub struct RodioAudio {
    _stream: OutputStream,
    _sink: Sink,
}

struct RodioWave<W: Iterator + Send + 'static>(W);

impl<W: Iterator + Send + 'static> Iterator for RodioWave<W> where <W as Iterator>::Item: rodio::Sample {
    type Item = W::Item;

    fn next(&mut self) -> Option<Self::Item> {
        self.0.next()
    }
}

impl<W: Iterator + Send + 'static> Source for RodioWave<W> where <W as Iterator>::Item: rodio::Sample  {
    fn current_frame_len(&self) -> Option<usize> {
        None
    }

    fn channels(&self) -> u16 {
        1
    }

    fn sample_rate(&self) -> u32 {
        SAMPLE_RATE
    }

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


impl Audio for RodioAudio {
    fn new<S: Iterator<Item = f32> + 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));

        
        RodioAudio {
            _stream: stream,
            _sink: sink,
        }
    }
}