From 8831dea166a11d3de22e4265876ab15c09017871 Mon Sep 17 00:00:00 2001 From: Paul Lietar Date: Sat, 31 Dec 2016 13:24:18 +0100 Subject: [PATCH] Add missing file :( --- src/audio_backend/pipe.rs | 40 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/audio_backend/pipe.rs diff --git a/src/audio_backend/pipe.rs b/src/audio_backend/pipe.rs new file mode 100644 index 00000000..9761c5cb --- /dev/null +++ b/src/audio_backend/pipe.rs @@ -0,0 +1,40 @@ +use super::{Open, Sink}; +use std::fs::OpenOptions; +use std::io::{self, Write}; +use std::mem; +use std::slice; + +pub struct StdoutSink(Box); + +impl Open for StdoutSink { + fn open(path: Option<&str>) -> StdoutSink { + if let Some(path) = path { + let file = OpenOptions::new().write(true).open(path).unwrap(); + StdoutSink(Box::new(file)) + } else { + StdoutSink(Box::new(io::stdout())) + } + } +} + +impl Sink for StdoutSink { + fn start(&mut self) -> io::Result<()> { + Ok(()) + } + + fn stop(&mut self) -> io::Result<()> { + Ok(()) + } + + fn write(&mut self, data: &[i16]) -> io::Result<()> { + let data: &[u8] = unsafe { + slice::from_raw_parts(data.as_ptr() as *const u8, data.len() * mem::size_of::()) + }; + + self.0.write_all(data)?; + self.0.flush()?; + + Ok(()) + } +} +