mirror of
https://github.com/librespot-org/librespot.git
synced 2025-10-04 18:29:45 +02:00
add SDL backend based on sdl2 crate
This commit is contained in:
parent
a41ab28540
commit
52c5b18825
6 changed files with 113 additions and 0 deletions
56
playback/src/audio_backend/sdl.rs
Normal file
56
playback/src/audio_backend/sdl.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use super::{Open, Sink};
|
||||
use sdl2::audio::{AudioQueue, AudioSpecDesired};
|
||||
use std::{io, thread, time};
|
||||
|
||||
type Channel = i16;
|
||||
|
||||
pub struct SdlSink {
|
||||
queue: AudioQueue<Channel>,
|
||||
}
|
||||
|
||||
impl Open for SdlSink {
|
||||
fn open(device: Option<String>) -> SdlSink {
|
||||
debug!("Using SDL sink");
|
||||
|
||||
if device.is_some() {
|
||||
panic!("SDL sink does not support specifyng a device name");
|
||||
}
|
||||
|
||||
let ctx = sdl2::init().expect("Could not init SDL");
|
||||
let audio = ctx.audio().expect("Could not init SDL audio subsystem");
|
||||
|
||||
let desired_spec = AudioSpecDesired {
|
||||
freq: Some(44_100),
|
||||
channels: Some(2),
|
||||
samples: None, // default
|
||||
};
|
||||
let queue = audio
|
||||
.open_queue(None, &desired_spec)
|
||||
.expect("Could not open SDL audio device");
|
||||
|
||||
SdlSink { queue: queue }
|
||||
}
|
||||
}
|
||||
|
||||
impl Sink for SdlSink {
|
||||
fn start(&mut self) -> io::Result<()> {
|
||||
self.queue.clear(); //
|
||||
self.queue.resume();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn stop(&mut self) -> io::Result<()> {
|
||||
self.queue.pause();
|
||||
self.queue.clear();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write(&mut self, data: &[i16]) -> io::Result<()> {
|
||||
while self.queue.size() > (2 * 2 * 44_100) {
|
||||
// sleep and wait for sdl thread to drain the queue a bit
|
||||
thread::sleep(time::Duration::from_millis(10));
|
||||
}
|
||||
self.queue.queue(data);
|
||||
Ok(())
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue