alsa.rs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. use super::{Open, Sink};
  2. use std::io;
  3. use alsa::{PCM, Stream, Mode, Format, Access};
  4. pub struct AlsaSink(Option<PCM>, String);
  5. impl Open for AlsaSink {
  6. fn open(device: Option<String>) -> AlsaSink {
  7. info!("Using alsa sink");
  8. let name = device.unwrap_or("default".to_string());
  9. AlsaSink(None, name)
  10. }
  11. }
  12. impl Sink for AlsaSink {
  13. fn start(&mut self) -> io::Result<()> {
  14. if self.0.is_none() {
  15. match PCM::open(&*self.1,
  16. Stream::Playback, Mode::Blocking,
  17. Format::Signed16, Access::Interleaved,
  18. 2, 44100) {
  19. Ok(f) => self.0 = Some(f),
  20. Err(e) => {
  21. error!("Alsa error PCM open {}", e);
  22. return Err(io::Error::new(io::ErrorKind::Other, "Alsa error: PCM open failed"));
  23. }
  24. }
  25. }
  26. Ok(())
  27. }
  28. fn stop(&mut self) -> io::Result<()> {
  29. self.0 = None;
  30. Ok(())
  31. }
  32. fn write(&mut self, data: &[i16]) -> io::Result<()> {
  33. self.0.as_mut().unwrap().write_interleaved(&data).unwrap();
  34. Ok(())
  35. }
  36. }