mirror of
https://github.com/librespot-org/librespot.git
synced 2024-12-18 17:11:53 +00:00
Merge pull request #113 from joerg-krause/alsa-backend
Add ALSA backend
This commit is contained in:
commit
5a4297551d
6 changed files with 60 additions and 0 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -2,6 +2,7 @@
|
||||||
name = "librespot"
|
name = "librespot"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"alsa 0.0.1 (git+https://github.com/plietar/rust-alsa)",
|
||||||
"bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"bit-set 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
"byteorder 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"clippy 0.0.78 (registry+https://github.com/rust-lang/crates.io-index)",
|
"clippy 0.0.78 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -46,6 +47,14 @@ dependencies = [
|
||||||
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
"memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "alsa"
|
||||||
|
version = "0.0.1"
|
||||||
|
source = "git+https://github.com/plietar/rust-alsa#8c63543fa0ccd971cf15f5675293d19febd6f79e"
|
||||||
|
dependencies = [
|
||||||
|
"libc 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aster"
|
name = "aster"
|
||||||
version = "0.20.0"
|
version = "0.20.0"
|
||||||
|
|
|
@ -48,6 +48,7 @@ shannon = { git = "https://github.com/plietar/rust-shannon" }
|
||||||
vorbis = "~0.0.14"
|
vorbis = "~0.0.14"
|
||||||
tremor = { git = "https://github.com/plietar/rust-tremor", optional = true }
|
tremor = { git = "https://github.com/plietar/rust-tremor", optional = true }
|
||||||
|
|
||||||
|
alsa = { git = "https://github.com/plietar/rust-alsa", optional = true }
|
||||||
portaudio = { git = "https://github.com/mvdnes/portaudio-rs", optional = true }
|
portaudio = { git = "https://github.com/mvdnes/portaudio-rs", optional = true }
|
||||||
libpulse-sys = { git = "https://github.com/astro/libpulse-sys", optional = true }
|
libpulse-sys = { git = "https://github.com/astro/libpulse-sys", optional = true }
|
||||||
|
|
||||||
|
@ -72,6 +73,7 @@ nightly = ["serde_macros"]
|
||||||
|
|
||||||
with-tremor = ["tremor"]
|
with-tremor = ["tremor"]
|
||||||
facebook = ["hyper/ssl", "openssl"]
|
facebook = ["hyper/ssl", "openssl"]
|
||||||
|
alsa-backend = ["alsa"]
|
||||||
portaudio-backend = ["portaudio"]
|
portaudio-backend = ["portaudio"]
|
||||||
pulseaudio-backend= ["libpulse-sys"]
|
pulseaudio-backend= ["libpulse-sys"]
|
||||||
default = ["with-syntex", "portaudio-backend"]
|
default = ["with-syntex", "portaudio-backend"]
|
||||||
|
|
|
@ -66,6 +66,7 @@ target/release/librespot [...] --backend portaudio
|
||||||
```
|
```
|
||||||
|
|
||||||
The following backends are currently available :
|
The following backends are currently available :
|
||||||
|
- ALSA
|
||||||
- PortAudio
|
- PortAudio
|
||||||
- PulseAudio
|
- PulseAudio
|
||||||
|
|
||||||
|
|
38
src/audio_backend/alsa.rs
Normal file
38
src/audio_backend/alsa.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use super::{Open, Sink};
|
||||||
|
use std::io;
|
||||||
|
use alsa::{PCM, Stream, Mode, Format, Access};
|
||||||
|
|
||||||
|
pub struct AlsaSink(Option<PCM>, String);
|
||||||
|
|
||||||
|
impl Open for AlsaSink {
|
||||||
|
fn open(device: Option<&str>) -> AlsaSink {
|
||||||
|
println!("Using alsa sink");
|
||||||
|
|
||||||
|
let name = device.unwrap_or("default").to_string();
|
||||||
|
|
||||||
|
AlsaSink(None, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Sink for AlsaSink {
|
||||||
|
fn start(&mut self) -> io::Result<()> {
|
||||||
|
if self.0.is_some() {
|
||||||
|
} else {
|
||||||
|
self.0 = Some(PCM::open(&*self.1,
|
||||||
|
Stream::Playback, Mode::Blocking,
|
||||||
|
Format::Signed16, Access::Interleaved,
|
||||||
|
2, 44100).ok().unwrap());
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn stop(&mut self) -> io::Result<()> {
|
||||||
|
self.0 = None;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write(&mut self, data: &[i16]) -> io::Result<()> {
|
||||||
|
self.0.as_mut().unwrap().write_interleaved(&data).unwrap();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -54,6 +54,11 @@ fn mk_sink<S: Sink + Open + 'static>(device: Option<&str>) -> Box<Sink> {
|
||||||
Box::new(S::open(device))
|
Box::new(S::open(device))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "alsa-backend")]
|
||||||
|
mod alsa;
|
||||||
|
#[cfg(feature = "alsa-backend")]
|
||||||
|
use self::alsa::AlsaSink;
|
||||||
|
|
||||||
#[cfg(feature = "portaudio-backend")]
|
#[cfg(feature = "portaudio-backend")]
|
||||||
mod portaudio;
|
mod portaudio;
|
||||||
#[cfg(feature = "portaudio-backend")]
|
#[cfg(feature = "portaudio-backend")]
|
||||||
|
@ -70,6 +75,8 @@ declare_backends! {
|
||||||
(&'static str,
|
(&'static str,
|
||||||
&'static (Fn(Option<&str>) -> Box<Sink> + Sync + Send + 'static))
|
&'static (Fn(Option<&str>) -> Box<Sink> + Sync + Send + 'static))
|
||||||
] = &[
|
] = &[
|
||||||
|
#[cfg(feature = "alsa-backend")]
|
||||||
|
("alsa", &mk_sink::<AlsaSink>),
|
||||||
#[cfg(feature = "portaudio-backend")]
|
#[cfg(feature = "portaudio-backend")]
|
||||||
("portaudio", &mk_sink::<PortAudioSink>),
|
("portaudio", &mk_sink::<PortAudioSink>),
|
||||||
#[cfg(feature = "pulseaudio-backend")]
|
#[cfg(feature = "pulseaudio-backend")]
|
||||||
|
|
|
@ -42,6 +42,9 @@ extern crate tremor as vorbis;
|
||||||
#[cfg(feature = "openssl")]
|
#[cfg(feature = "openssl")]
|
||||||
extern crate openssl;
|
extern crate openssl;
|
||||||
|
|
||||||
|
#[cfg(feature = "alsa-backend")]
|
||||||
|
extern crate alsa;
|
||||||
|
|
||||||
#[cfg(feature = "portaudio")]
|
#[cfg(feature = "portaudio")]
|
||||||
extern crate portaudio;
|
extern crate portaudio;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue