Update cpal -> 0.10

* Provide device prevous method using `cpal::default_host()`
* Naively handle new cpal errors
This commit is contained in:
Alex Butler 2019-07-18 13:06:14 +01:00
parent 655d05100f
commit 0151398043
No known key found for this signature in database
GPG key ID: E1355A2F8E415521
3 changed files with 53 additions and 9 deletions

View file

@ -10,7 +10,7 @@ documentation = "http://docs.rs/rodio"
[dependencies] [dependencies]
claxon = { version = "0.4.2", optional = true } claxon = { version = "0.4.2", optional = true }
cpal = "0.8" cpal = "0.10"
hound = { version = "3.3.1", optional = true } hound = { version = "3.3.1", optional = true }
lazy_static = "1.0.0" lazy_static = "1.0.0"
lewton = { version = "0.9", optional = true } lewton = { version = "0.9", optional = true }

View file

@ -5,6 +5,7 @@ use std::sync::Mutex;
use std::sync::Weak; use std::sync::Weak;
use std::thread::Builder; use std::thread::Builder;
use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait};
use cpal::Device; use cpal::Device;
use cpal::EventLoop; use cpal::EventLoop;
use cpal::Sample as CpalSample; use cpal::Sample as CpalSample;
@ -24,7 +25,7 @@ where
lazy_static! { lazy_static! {
static ref ENGINE: Arc<Engine> = { static ref ENGINE: Arc<Engine> = {
let engine = Arc::new(Engine { let engine = Arc::new(Engine {
events_loop: EventLoop::new(), events_loop: cpal::default_host().event_loop(),
dynamic_mixers: Mutex::new(HashMap::with_capacity(1)), dynamic_mixers: Mutex::new(HashMap::with_capacity(1)),
end_points: Mutex::new(HashMap::with_capacity(1)), end_points: Mutex::new(HashMap::with_capacity(1)),
}); });
@ -37,7 +38,9 @@ where
let engine = engine.clone(); let engine = engine.clone();
move || { move || {
engine.events_loop.run(|stream_id, buffer| { engine.events_loop.run(|stream_id, buffer| {
audio_callback(&engine, stream_id, buffer); if let Ok(buf) = buffer {
audio_callback(&engine, stream_id, buf);
}
}) })
} }
}) })
@ -91,7 +94,7 @@ fn audio_callback(engine: &Arc<Engine>, stream_id: StreamId, buffer: StreamData)
} => for d in buffer.iter_mut() { } => for d in buffer.iter_mut() {
*d = mixer_rx.next().unwrap_or(0f32); *d = mixer_rx.next().unwrap_or(0f32);
}, },
StreamData::Input { buffer: _ } => { StreamData::Input { .. } => {
panic!("Can't play an input stream!"); panic!("Can't play an input stream!");
}, },
}; };
@ -107,7 +110,7 @@ where
let mixer = { let mixer = {
let mut end_points = engine.end_points.lock().unwrap(); let mut end_points = engine.end_points.lock().unwrap();
match end_points.entry(device.name()) { match end_points.entry(device.name().expect("No device name")) {
Entry::Vacant(e) => { Entry::Vacant(e) => {
let (mixer, stream) = new_output_stream(engine, device); let (mixer, stream) = new_output_stream(engine, device);
e.insert(Arc::downgrade(&mixer)); e.insert(Arc::downgrade(&mixer));
@ -128,7 +131,7 @@ where
}; };
if let Some(stream) = stream_to_start { if let Some(stream) = stream_to_start {
engine.events_loop.play_stream(stream); engine.events_loop.play_stream(stream).expect("play_stream failed");
} }
mixer.add(source); mixer.add(source);

View file

@ -96,9 +96,7 @@ extern crate lewton;
#[cfg(feature = "mp3")] #[cfg(feature = "mp3")]
extern crate minimp3; extern crate minimp3;
pub use cpal::{ pub use cpal::{traits::DeviceTrait, Device, Devices, DevicesError, InputDevices};
default_input_device, default_output_device, devices, input_devices, output_devices, Device,
};
pub use conversions::Sample; pub use conversions::Sample;
pub use decoder::Decoder; pub use decoder::Decoder;
@ -107,6 +105,7 @@ pub use sink::Sink;
pub use source::Source; pub use source::Source;
pub use spatial_sink::SpatialSink; pub use spatial_sink::SpatialSink;
use cpal::traits::HostTrait;
use std::io::{Read, Seek}; use std::io::{Read, Seek};
mod conversions; mod conversions;
@ -132,3 +131,45 @@ where
sink.append(input); sink.append(input);
Ok(sink) Ok(sink)
} }
/// The default input audio device on the system.
///
/// Returns `None` if no input device is available.
#[inline]
pub fn default_input_device() -> Option<Device> {
cpal::default_host().default_input_device()
}
/// The default output audio device on the system.
///
/// Returns `None` if no output device is available.
#[inline]
pub fn default_output_device() -> Option<Device> {
cpal::default_host().default_output_device()
}
/// An iterator yielding all `Device`s currently available to the host on the system.
///
/// Can be empty if the system does not support audio in general.
#[inline]
pub fn devices() -> Result<Devices, DevicesError> {
cpal::default_host().devices()
}
/// An iterator yielding all `Device`s currently available to the system that support one or more
/// input stream formats.
///
/// Can be empty if the system does not support audio input.
#[inline]
pub fn input_devices() -> Result<InputDevices<Devices>, DevicesError> {
cpal::default_host().input_devices()
}
/// An iterator yielding all `Device`s currently available to the system that support one or more
/// output stream formats.
///
/// Can be empty if the system does not support audio output.
#[inline]
pub fn output_devices() -> Result<InputDevices<Devices>, DevicesError> {
cpal::default_host().output_devices()
}