mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Switch to latest alsa-rs
crate
This commit is contained in:
parent
a67048c3d7
commit
08cfb1516d
3 changed files with 56 additions and 20 deletions
|
@ -15,7 +15,7 @@ futures = "0.1.8"
|
|||
log = "0.3.5"
|
||||
byteorder = "1.2.1"
|
||||
|
||||
alsa = { version = "0.1.5", optional = true }
|
||||
alsa = { git = "https://github.com/diwic/alsa-rs.git", optional = true }
|
||||
portaudio-rs = { version = "0.3.0", optional = true }
|
||||
libpulse-sys = { version = "0.0.0", optional = true }
|
||||
jack = { version = "0.5.3", optional = true }
|
||||
|
|
|
@ -1,17 +1,34 @@
|
|||
use super::{Open, Sink};
|
||||
use alsa::{Direction, Error, ValueOr};
|
||||
use alsa::device_name::HintIter;
|
||||
use std::ffi::{CStr, CString};
|
||||
use alsa::pcm::{Access, Format, HwParams, PCM};
|
||||
use std::io;
|
||||
use std::process::exit;
|
||||
|
||||
use std::ffi::CString;
|
||||
use alsa::{Direction, ValueOr};
|
||||
use alsa::pcm::{PCM, HwParams, Format, Access};
|
||||
|
||||
pub struct AlsaSink(Option<PCM>, String);
|
||||
|
||||
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) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Open for AlsaSink {
|
||||
fn open(device: Option<String>) -> AlsaSink {
|
||||
info!("Using alsa sink");
|
||||
|
||||
let name = device.unwrap_or("default".to_string());
|
||||
let name = match device.as_ref().map(AsRef::as_ref) {
|
||||
Some("?") => {
|
||||
list_outputs();
|
||||
exit(0)
|
||||
}
|
||||
Some(device) => device,
|
||||
None => "default",
|
||||
}.to_string();
|
||||
|
||||
AlsaSink(None, name)
|
||||
}
|
||||
|
@ -19,11 +36,8 @@ impl Open for AlsaSink {
|
|||
|
||||
impl Sink for AlsaSink {
|
||||
fn start(&mut self) -> io::Result<()> {
|
||||
if self.0.is_some() {
|
||||
} else {
|
||||
let pcm = PCM::open(&*CString::new(self.1.to_owned().into_bytes()).unwrap(),
|
||||
Direction::Playback,
|
||||
false).unwrap();
|
||||
if self.0.is_none() {
|
||||
let pcm = PCM::new(&*self.1, Direction::Playback, false).unwrap();
|
||||
{
|
||||
// Set hardware parameters: 44100 Hz / Stereo / 16 bit
|
||||
let hwp = HwParams::any(&pcm).unwrap();
|
||||
|
@ -32,7 +46,9 @@ impl Sink for AlsaSink {
|
|||
hwp.set_format(Format::s16()).unwrap();
|
||||
hwp.set_access(Access::RWInterleaved).unwrap();
|
||||
pcm.hw_params(&hwp).unwrap();
|
||||
}
|
||||
println!("PCM status: {:?}, {:?}", pcm.state(), pcm.hw_params_current().unwrap())
|
||||
}
|
||||
PCM::prepare(&pcm).unwrap();
|
||||
|
||||
self.0 = Some(pcm);
|
||||
}
|
||||
|
@ -41,6 +57,10 @@ impl Sink for AlsaSink {
|
|||
}
|
||||
|
||||
fn stop(&mut self) -> io::Result<()> {
|
||||
{
|
||||
let pcm = self.0.as_mut().unwrap();
|
||||
pcm.drain().unwrap();
|
||||
}
|
||||
self.0 = None;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -51,7 +71,8 @@ impl Sink for AlsaSink {
|
|||
|
||||
match io.writei(&data) {
|
||||
Ok(_) => (),
|
||||
Err(err) => pcm.recover(err.code(), false).unwrap(),
|
||||
Err(err) => pcm.try_recover(err, false).unwrap(),
|
||||
// Err(err) => println!("{:?}",err),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -5,14 +5,29 @@ use alsa;
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct AlsaMixer {
|
||||
name: String
|
||||
card: String,
|
||||
mixer: String,
|
||||
}
|
||||
|
||||
// Doesn't work - Selem is borrowed from Mixer
|
||||
// impl AlsaMixer {
|
||||
// fn get_selem(&self ) -> Result<(alsa::mixer::Selem), Box<Error>> {
|
||||
//
|
||||
// let selem_id = alsa::mixer::SelemId::new(self.mixer, 0);
|
||||
// let mixer = alsa::mixer::Mixer::new(self.card, false)?;
|
||||
// let selem = mixer.find_selem(&selem_id).unwrap();
|
||||
//
|
||||
// Ok((selem))
|
||||
// }
|
||||
// }
|
||||
|
||||
impl Mixer for AlsaMixer {
|
||||
fn open(device: Option<String>) -> AlsaMixer {
|
||||
let name = device.unwrap_or("default".to_string());
|
||||
let card = device.unwrap_or(String::from("default"));
|
||||
let mixer = String::from("PCM");
|
||||
AlsaMixer {
|
||||
name: name
|
||||
card: card,
|
||||
mixer: mixer,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -23,8 +38,8 @@ impl Mixer for AlsaMixer {
|
|||
}
|
||||
|
||||
fn volume(&self) -> u16 {
|
||||
let mixer = alsa::mixer::Mixer::new(&self.name, false).unwrap();
|
||||
let selem_id = alsa::mixer::SelemId::new("Master", 0);
|
||||
let mixer = alsa::mixer::Mixer::new(&self.card, false).unwrap();
|
||||
let selem_id = alsa::mixer::SelemId::new(&self.mixer, 0);
|
||||
let selem = mixer.find_selem(&selem_id).unwrap();
|
||||
let (min, max) = selem.get_playback_volume_range();
|
||||
let volume: i64 = selem.get_playback_volume(alsa::mixer::SelemChannelId::FrontLeft).unwrap();
|
||||
|
@ -40,8 +55,8 @@ impl Mixer for AlsaMixer {
|
|||
}
|
||||
|
||||
fn set_volume(&self, volume: u16) {
|
||||
let mixer = alsa::mixer::Mixer::new(&self.name, false).unwrap();
|
||||
let selem_id = alsa::mixer::SelemId::new("Master", 0);
|
||||
let mixer = alsa::mixer::Mixer::new(&self.card, false).unwrap();
|
||||
let selem_id = alsa::mixer::SelemId::new(&self.mixer, 0);
|
||||
let selem = mixer.find_selem(&selem_id).unwrap();
|
||||
let (min, max) = selem.get_playback_volume_range();
|
||||
|
||||
|
@ -52,7 +67,7 @@ impl Mixer for AlsaMixer {
|
|||
let resolution = max - min + 1;
|
||||
let factor: u16 = (((0xFFFF + 1) / resolution) - 1) as u16;
|
||||
let volume: i64 = (volume / factor) as i64;
|
||||
|
||||
info!("Setting volume: {:?}", volume);
|
||||
selem.set_playback_volume_all(volume).unwrap();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue