2016-03-14 02:16:59 +00:00
|
|
|
use super::{Open, Sink};
|
2018-03-21 21:18:37 +00:00
|
|
|
use alsa::{Direction, Error, ValueOr};
|
|
|
|
use alsa::device_name::HintIter;
|
|
|
|
use std::ffi::{CStr, CString};
|
|
|
|
use alsa::pcm::{Access, Format, HwParams, PCM};
|
2016-03-14 02:16:59 +00:00
|
|
|
use std::io;
|
2018-03-21 21:18:37 +00:00
|
|
|
use std::process::exit;
|
2016-03-14 02:16:59 +00:00
|
|
|
|
2018-03-20 15:32:43 +00:00
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
pub struct AlsaSink(Option<PCM>, String);
|
|
|
|
|
2018-03-21 21:18:37 +00:00
|
|
|
fn list_outputs() {
|
|
|
|
for t in &["pcm", "ctl", "rawmidi", "timer", "seq", "hwdep"] {
|
|
|
|
println!("{} devices:", t);
|
|
|
|
let i = HintIter::new(None, &*CString::new(*t).unwrap()).unwrap();
|
|
|
|
for a in i { println!(" {:?}", a) }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
impl Open for AlsaSink {
|
2018-03-20 15:32:43 +00:00
|
|
|
fn open(device: Option<String>) -> AlsaSink {
|
2017-01-06 14:21:44 +00:00
|
|
|
info!("Using alsa sink");
|
2016-03-14 02:16:59 +00:00
|
|
|
|
2018-03-21 21:18:37 +00:00
|
|
|
let name = match device.as_ref().map(AsRef::as_ref) {
|
|
|
|
Some("?") => {
|
|
|
|
list_outputs();
|
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
Some(device) => device,
|
|
|
|
None => "default",
|
|
|
|
}.to_string();
|
2016-03-14 02:16:59 +00:00
|
|
|
|
|
|
|
AlsaSink(None, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sink for AlsaSink {
|
|
|
|
fn start(&mut self) -> io::Result<()> {
|
2018-03-21 21:18:37 +00:00
|
|
|
if self.0.is_none() {
|
|
|
|
let pcm = PCM::new(&*self.1, Direction::Playback, false).unwrap();
|
2018-03-20 15:32:43 +00:00
|
|
|
{
|
|
|
|
// Set hardware parameters: 44100 Hz / Stereo / 16 bit
|
|
|
|
let hwp = HwParams::any(&pcm).unwrap();
|
|
|
|
hwp.set_channels(2).unwrap();
|
|
|
|
hwp.set_rate(44100, ValueOr::Nearest).unwrap();
|
|
|
|
hwp.set_format(Format::s16()).unwrap();
|
|
|
|
hwp.set_access(Access::RWInterleaved).unwrap();
|
|
|
|
pcm.hw_params(&hwp).unwrap();
|
2018-03-21 21:18:37 +00:00
|
|
|
println!("PCM status: {:?}, {:?}", pcm.state(), pcm.hw_params_current().unwrap())
|
|
|
|
}
|
|
|
|
PCM::prepare(&pcm).unwrap();
|
2018-03-20 15:32:43 +00:00
|
|
|
|
|
|
|
self.0 = Some(pcm);
|
2016-03-14 02:16:59 +00:00
|
|
|
}
|
2018-03-20 15:32:43 +00:00
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(&mut self) -> io::Result<()> {
|
2018-03-21 21:18:37 +00:00
|
|
|
{
|
|
|
|
let pcm = self.0.as_mut().unwrap();
|
|
|
|
pcm.drain().unwrap();
|
|
|
|
}
|
2016-03-14 02:16:59 +00:00
|
|
|
self.0 = None;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write(&mut self, data: &[i16]) -> io::Result<()> {
|
2018-03-20 15:32:43 +00:00
|
|
|
let pcm = self.0.as_mut().unwrap();
|
|
|
|
let io = pcm.io_i16().unwrap();
|
|
|
|
|
|
|
|
match io.writei(&data) {
|
|
|
|
Ok(_) => (),
|
2018-03-21 21:18:37 +00:00
|
|
|
Err(err) => pcm.try_recover(err, false).unwrap(),
|
|
|
|
// Err(err) => println!("{:?}",err),
|
2018-03-20 15:32:43 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 02:16:59 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|