mirror of
https://github.com/librespot-org/librespot.git
synced 2025-01-27 17:44:04 +00:00
Forgot to add some files in previous commit.
This commit is contained in:
parent
9274a6bfb3
commit
0973469120
2 changed files with 42 additions and 33 deletions
|
@ -1,4 +1,3 @@
|
||||||
use portaudio;
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
pub trait Sink {
|
pub trait Sink {
|
||||||
|
@ -7,40 +6,54 @@ pub trait Sink {
|
||||||
fn write(&self, data: &[i16]) -> io::Result<()>;
|
fn write(&self, data: &[i16]) -> io::Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct PortAudioSink<'a>(portaudio::stream::Stream<'a, i16, i16>);
|
mod portaudio_sink {
|
||||||
|
use audio_sink::Sink;
|
||||||
|
use std::io;
|
||||||
|
use portaudio;
|
||||||
|
pub struct PortAudioSink<'a>(portaudio::stream::Stream<'a, i16, i16>);
|
||||||
|
|
||||||
impl <'a> PortAudioSink<'a> {
|
impl <'a> PortAudioSink<'a> {
|
||||||
pub fn open() -> PortAudioSink<'a> {
|
pub fn open() -> PortAudioSink<'a> {
|
||||||
portaudio::initialize().unwrap();
|
portaudio::initialize().unwrap();
|
||||||
|
|
||||||
let stream = portaudio::stream::Stream::open_default(
|
let stream = portaudio::stream::Stream::open_default(
|
||||||
0, 2, 44100.0,
|
0, 2, 44100.0,
|
||||||
portaudio::stream::FRAMES_PER_BUFFER_UNSPECIFIED,
|
portaudio::stream::FRAMES_PER_BUFFER_UNSPECIFIED,
|
||||||
None
|
None
|
||||||
).unwrap();
|
).unwrap();
|
||||||
|
|
||||||
PortAudioSink(stream)
|
PortAudioSink(stream)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <'a> Sink for PortAudioSink<'a> {
|
||||||
|
fn start(&self) -> io::Result<()> {
|
||||||
|
self.0.start().unwrap();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn stop(&self) -> io::Result<()> {
|
||||||
|
self.0.stop().unwrap();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
fn write(&self, data: &[i16]) -> io::Result<()> {
|
||||||
|
match self.0.write(&data) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(portaudio::PaError::OutputUnderflowed) => eprintln!("Underflow"),
|
||||||
|
Err(e) => panic!("PA Error {}", e),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl <'a> Drop for PortAudioSink<'a> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
portaudio::terminate().unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a> Sink for PortAudioSink<'a> {
|
|
||||||
fn start(&self) -> io::Result<()> {
|
|
||||||
self.0.start().unwrap();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
fn stop(&self) -> io::Result<()> {
|
|
||||||
self.0.stop().unwrap();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
fn write(&self, data: &[i16]) -> io::Result<()> {
|
|
||||||
self.0.write(&data).unwrap();
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl <'a> Drop for PortAudioSink<'a> {
|
pub type DefaultSink = portaudio_sink::PortAudioSink<'static>;
|
||||||
fn drop(&mut self) {
|
|
||||||
portaudio::terminate().unwrap();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -107,14 +107,10 @@ fn main() {
|
||||||
let reusable_credentials = session.login(credentials).unwrap();
|
let reusable_credentials = session.login(credentials).unwrap();
|
||||||
reusable_credentials.save_to_file(credentials_path);
|
reusable_credentials.save_to_file(credentials_path);
|
||||||
|
|
||||||
portaudio::initialize().unwrap();
|
|
||||||
|
|
||||||
let player = Player::new(session.clone(), || DefaultSink::open());
|
let player = Player::new(session.clone(), || DefaultSink::open());
|
||||||
let spirc = SpircManager::new(session.clone(), player);
|
let spirc = SpircManager::new(session.clone(), player);
|
||||||
thread::spawn(move || spirc.run());
|
thread::spawn(move || spirc.run());
|
||||||
|
|
||||||
portaudio::terminate().unwrap();
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
session.poll();
|
session.poll();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue