diff options
author | Astatin <[email protected]> | 2025-04-03 18:35:03 +0200 |
---|---|---|
committer | Astatin <[email protected]> | 2025-04-03 18:35:03 +0200 |
commit | 9a8e4117be8d30109229600346e7d9561c52a3e3 (patch) | |
tree | 6d2531b675e609d3d5d734732f1328769dddf557 /src/desktop/audio.rs | |
parent | c1fb610e198d785aa57440b86c69587e5caaf563 (diff) |
Separate core from desktop target
Diffstat (limited to 'src/desktop/audio.rs')
-rw-r--r-- | src/desktop/audio.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/desktop/audio.rs b/src/desktop/audio.rs new file mode 100644 index 0000000..60d32df --- /dev/null +++ b/src/desktop/audio.rs @@ -0,0 +1,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, + } + } +} |