Majority of ASIO host update following refactor
Currently not compiling - still need to address some global items within asio-sys, including the `set_callback` function and the double buffer globals.
This commit is contained in:
parent
efe683133c
commit
d739a5b79d
|
@ -13,3 +13,5 @@ extern crate num_derive;
|
||||||
pub mod bindings;
|
pub mod bindings;
|
||||||
#[cfg(asio)]
|
#[cfg(asio)]
|
||||||
pub use bindings::*;
|
pub use bindings::*;
|
||||||
|
#[cfg(asio)]
|
||||||
|
pub use bindings::errors::{AsioError, LoadDriverError};
|
||||||
|
|
|
@ -3,6 +3,8 @@ pub type SupportedInputFormats = std::vec::IntoIter<SupportedFormat>;
|
||||||
pub type SupportedOutputFormats = std::vec::IntoIter<SupportedFormat>;
|
pub type SupportedOutputFormats = std::vec::IntoIter<SupportedFormat>;
|
||||||
|
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
|
use std::sync::Arc;
|
||||||
|
use BackendSpecificError;
|
||||||
use DefaultFormatError;
|
use DefaultFormatError;
|
||||||
use DeviceNameError;
|
use DeviceNameError;
|
||||||
use DevicesError;
|
use DevicesError;
|
||||||
|
@ -14,16 +16,17 @@ use SupportedFormatsError;
|
||||||
use super::sys;
|
use super::sys;
|
||||||
|
|
||||||
/// A ASIO Device
|
/// A ASIO Device
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug)]
|
||||||
pub struct Device {
|
pub struct Device {
|
||||||
/// The drivers for this device
|
/// The drivers for this device
|
||||||
pub drivers: sys::Drivers,
|
pub driver: Arc<sys::Driver>,
|
||||||
/// The name of this device
|
/// The name of this device
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// All available devices
|
/// All available devices
|
||||||
pub struct Devices {
|
pub struct Devices {
|
||||||
|
asio: Arc<sys::Asio>,
|
||||||
drivers: std::vec::IntoIter<String>,
|
drivers: std::vec::IntoIter<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +65,7 @@ impl Device {
|
||||||
// Collect a format for every combination of supported sample rate and number of channels.
|
// Collect a format for every combination of supported sample rate and number of channels.
|
||||||
let mut supported_formats = vec![];
|
let mut supported_formats = vec![];
|
||||||
for &rate in ::COMMON_SAMPLE_RATES {
|
for &rate in ::COMMON_SAMPLE_RATES {
|
||||||
if !self.drivers.can_sample_rate(rate.0 as u32) {
|
if !self.driver.can_sample_rate(rate.0.into()).ok().unwrap_or(false) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for channels in 1..f.channels + 1 {
|
for channels in 1..f.channels + 1 {
|
||||||
|
@ -90,7 +93,7 @@ impl Device {
|
||||||
// Collect a format for every combination of supported sample rate and number of channels.
|
// Collect a format for every combination of supported sample rate and number of channels.
|
||||||
let mut supported_formats = vec![];
|
let mut supported_formats = vec![];
|
||||||
for &rate in ::COMMON_SAMPLE_RATES {
|
for &rate in ::COMMON_SAMPLE_RATES {
|
||||||
if !self.drivers.can_sample_rate(rate.0 as u32) {
|
if !self.driver.can_sample_rate(rate.0.into()).ok().unwrap_or(false) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for channels in 1..f.channels + 1 {
|
for channels in 1..f.channels + 1 {
|
||||||
|
@ -104,50 +107,36 @@ impl Device {
|
||||||
|
|
||||||
/// Returns the default input format
|
/// Returns the default input format
|
||||||
pub fn default_input_format(&self) -> Result<Format, DefaultFormatError> {
|
pub fn default_input_format(&self) -> Result<Format, DefaultFormatError> {
|
||||||
let channels = self.drivers.get_channels().ins as u16;
|
let channels = self.driver.channels().map_err(default_format_err)?.ins as u16;
|
||||||
let sample_rate = SampleRate(self.drivers.get_sample_rate().rate);
|
let sample_rate = SampleRate(self.driver.sample_rate().map_err(default_format_err)? as _);
|
||||||
// Map th ASIO sample type to a CPAL sample type
|
// Map th ASIO sample type to a CPAL sample type
|
||||||
match self.drivers.get_data_type() {
|
let data_type = self.driver.data_type().map_err(default_format_err)?;
|
||||||
Ok(sys::AsioSampleType::ASIOSTInt16MSB) => Ok(SampleFormat::I16),
|
let data_type = convert_data_type(data_type).ok_or(DefaultFormatError::StreamTypeNotSupported)?;
|
||||||
Ok(sys::AsioSampleType::ASIOSTInt32MSB) => Ok(SampleFormat::I16),
|
Ok(Format {
|
||||||
Ok(sys::AsioSampleType::ASIOSTFloat32MSB) => Ok(SampleFormat::F32),
|
|
||||||
Ok(sys::AsioSampleType::ASIOSTInt16LSB) => Ok(SampleFormat::I16),
|
|
||||||
Ok(sys::AsioSampleType::ASIOSTInt32LSB) => Ok(SampleFormat::I16),
|
|
||||||
Ok(sys::AsioSampleType::ASIOSTFloat32LSB) => Ok(SampleFormat::F32),
|
|
||||||
_ => Err(DefaultFormatError::StreamTypeNotSupported),
|
|
||||||
}.map(|dt| Format {
|
|
||||||
channels,
|
channels,
|
||||||
sample_rate,
|
sample_rate,
|
||||||
data_type: dt,
|
data_type,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the default output format
|
/// Returns the default output format
|
||||||
pub fn default_output_format(&self) -> Result<Format, DefaultFormatError> {
|
pub fn default_output_format(&self) -> Result<Format, DefaultFormatError> {
|
||||||
let channels = self.drivers.get_channels().outs as u16;
|
let channels = self.driver.channels().map_err(default_format_err)?.outs as u16;
|
||||||
let sample_rate = SampleRate(self.drivers.get_sample_rate().rate);
|
let sample_rate = SampleRate(self.driver.sample_rate().map_err(default_format_err)? as _);
|
||||||
match self.drivers.get_data_type() {
|
let data_type = self.driver.data_type().map_err(default_format_err)?;
|
||||||
// Map th ASIO sample type to a CPAL sample type
|
let data_type = convert_data_type(data_type).ok_or(DefaultFormatError::StreamTypeNotSupported)?;
|
||||||
Ok(sys::AsioSampleType::ASIOSTInt16MSB) => Ok(SampleFormat::I16),
|
Ok(Format {
|
||||||
Ok(sys::AsioSampleType::ASIOSTFloat32MSB) => Ok(SampleFormat::F32),
|
|
||||||
Ok(sys::AsioSampleType::ASIOSTInt16LSB) => Ok(SampleFormat::I16),
|
|
||||||
Ok(sys::AsioSampleType::ASIOSTInt32LSB) => Ok(SampleFormat::I16),
|
|
||||||
Ok(sys::AsioSampleType::ASIOSTFloat32LSB) => Ok(SampleFormat::F32),
|
|
||||||
_ => Err(DefaultFormatError::StreamTypeNotSupported),
|
|
||||||
}.map(|dt| Format {
|
|
||||||
channels,
|
channels,
|
||||||
sample_rate,
|
sample_rate,
|
||||||
data_type: dt,
|
data_type,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Devices {
|
impl Devices {
|
||||||
pub fn new() -> Result<Self, DevicesError> {
|
pub fn new(asio: Arc<sys::Asio>) -> Result<Self, DevicesError> {
|
||||||
let driver_names = online_devices();
|
let drivers = asio.driver_names().into_iter();
|
||||||
Ok(Devices {
|
Ok(Devices { asio, drivers })
|
||||||
drivers: driver_names.into_iter(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,14 +145,14 @@ impl Iterator for Devices {
|
||||||
|
|
||||||
/// Load drivers and return device
|
/// Load drivers and return device
|
||||||
fn next(&mut self) -> Option<Device> {
|
fn next(&mut self) -> Option<Device> {
|
||||||
|
loop {
|
||||||
match self.drivers.next() {
|
match self.drivers.next() {
|
||||||
Some(name) => sys::Drivers::load(&name)
|
Some(name) => match self.asio.load_driver(&name) {
|
||||||
.or_else(|e| {
|
Ok(driver) => return Some(Device { driver: Arc::new(driver), name }),
|
||||||
eprintln!("{}", e);
|
Err(_) => continue,
|
||||||
Err(e)
|
}
|
||||||
}).ok()
|
None => return None,
|
||||||
.map(|drivers| Device { drivers, name }),
|
}
|
||||||
None => None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,35 +161,40 @@ impl Iterator for Devices {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asio doesn't have a concept of default
|
fn convert_data_type(ty: sys::AsioSampleType) -> Option<SampleFormat> {
|
||||||
/// so returning first in list as default
|
let fmt = match ty {
|
||||||
pub fn default_input_device() -> Option<Device> {
|
sys::AsioSampleType::ASIOSTInt16MSB => SampleFormat::I16,
|
||||||
first_device()
|
sys::AsioSampleType::ASIOSTInt32MSB => SampleFormat::I16,
|
||||||
|
sys::AsioSampleType::ASIOSTFloat32MSB => SampleFormat::F32,
|
||||||
|
sys::AsioSampleType::ASIOSTInt16LSB => SampleFormat::I16,
|
||||||
|
sys::AsioSampleType::ASIOSTInt32LSB => SampleFormat::I16,
|
||||||
|
sys::AsioSampleType::ASIOSTFloat32LSB => SampleFormat::F32,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
Some(fmt)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Asio doesn't have a concept of default
|
fn default_format_err(e: sys::AsioError) -> DefaultFormatError {
|
||||||
/// so returning first in list as default
|
match e {
|
||||||
pub fn default_output_device() -> Option<Device> {
|
sys::AsioError::NoDrivers |
|
||||||
first_device()
|
sys::AsioError::HardwareMalfunction => DefaultFormatError::DeviceNotAvailable,
|
||||||
|
sys::AsioError::NoRate => DefaultFormatError::StreamTypeNotSupported,
|
||||||
|
err => {
|
||||||
|
let description = format!("{}", err);
|
||||||
|
BackendSpecificError { description }.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn first_device() -> Option<Device> {
|
|
||||||
let mut driver_list = online_devices();
|
|
||||||
match driver_list.pop() {
|
|
||||||
Some(name) => sys::Drivers::load(&name)
|
|
||||||
.or_else(|e| {
|
|
||||||
eprintln!("{}", e);
|
|
||||||
Err(e)
|
|
||||||
}).ok()
|
|
||||||
.map(|drivers| Device { drivers, name }),
|
|
||||||
None => None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove offline drivers
|
fn supported_formats_err(e: sys::AsioError) -> SupportedFormatsError {
|
||||||
fn online_devices() -> Vec<String> {
|
match e {
|
||||||
sys::get_driver_list()
|
sys::AsioError::NoDrivers |
|
||||||
.into_iter()
|
sys::AsioError::HardwareMalfunction => SupportedFormatsError::DeviceNotAvailable,
|
||||||
.filter(|name| sys::Drivers::load(&name).is_ok())
|
sys::AsioError::InvalidInput |
|
||||||
.collect()
|
sys::AsioError::BadMode => SupportedFormatsError::InvalidArgument,
|
||||||
|
err => {
|
||||||
|
let description = format!("{}", err);
|
||||||
|
BackendSpecificError { description }.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,9 @@ use {
|
||||||
SupportedFormatsError,
|
SupportedFormatsError,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use self::device::{Device, Devices, SupportedInputFormats, SupportedOutputFormats, default_input_device, default_output_device};
|
pub use self::device::{Device, Devices, SupportedInputFormats, SupportedOutputFormats};
|
||||||
pub use self::stream::{EventLoop, StreamId};
|
pub use self::stream::{EventLoop, StreamId};
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
mod device;
|
mod device;
|
||||||
mod stream;
|
mod stream;
|
||||||
|
@ -25,12 +26,15 @@ mod asio_utils;
|
||||||
|
|
||||||
/// The host for ASIO.
|
/// The host for ASIO.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Host;
|
pub struct Host {
|
||||||
|
asio: Arc<sys::Asio>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Host {
|
impl Host {
|
||||||
pub fn new() -> Result<Self, crate::HostUnavailable> {
|
pub fn new() -> Result<Self, crate::HostUnavailable> {
|
||||||
//unimplemented!("asio as an initialisation and termination process that needs to be impld");
|
let asio = Arc::new(sys::Asio::new());
|
||||||
Ok(Host)
|
let host = Host { asio };
|
||||||
|
Ok(host)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,15 +49,17 @@ impl HostTrait for Host {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn devices(&self) -> Result<Self::Devices, DevicesError> {
|
fn devices(&self) -> Result<Self::Devices, DevicesError> {
|
||||||
Devices::new()
|
Devices::new(self.asio.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_input_device(&self) -> Option<Self::Device> {
|
fn default_input_device(&self) -> Option<Self::Device> {
|
||||||
default_input_device()
|
// ASIO has no concept of a default device, so just use the first.
|
||||||
|
self.input_devices().ok().and_then(|mut ds| ds.next())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_output_device(&self) -> Option<Self::Device> {
|
fn default_output_device(&self) -> Option<Self::Device> {
|
||||||
default_output_device()
|
// ASIO has no concept of a default device, so just use the first.
|
||||||
|
self.output_devices().ok().and_then(|mut ds| ds.next())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event_loop(&self) -> Self::EventLoop {
|
fn event_loop(&self) -> Self::EventLoop {
|
||||||
|
|
|
@ -10,6 +10,7 @@ use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
use BackendSpecificError;
|
||||||
use BuildStreamError;
|
use BuildStreamError;
|
||||||
use Format;
|
use Format;
|
||||||
use PauseStreamError;
|
use PauseStreamError;
|
||||||
|
@ -45,6 +46,8 @@ pub struct StreamId(usize);
|
||||||
/// Each stream can be playing or paused.
|
/// Each stream can be playing or paused.
|
||||||
struct Stream {
|
struct Stream {
|
||||||
playing: bool,
|
playing: bool,
|
||||||
|
// The driver associated with this stream.
|
||||||
|
driver: Arc<sys::Driver>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -86,7 +89,7 @@ impl EventLoop {
|
||||||
|
|
||||||
fn check_format(
|
fn check_format(
|
||||||
&self,
|
&self,
|
||||||
drivers: &sys::Drivers,
|
driver: &sys::Driver,
|
||||||
format: &Format,
|
format: &Format,
|
||||||
num_asio_channels: u16,
|
num_asio_channels: u16,
|
||||||
) -> Result<(), BuildStreamError> {
|
) -> Result<(), BuildStreamError> {
|
||||||
|
@ -96,12 +99,12 @@ impl EventLoop {
|
||||||
data_type,
|
data_type,
|
||||||
} = format;
|
} = format;
|
||||||
// Try and set the sample rate to what the user selected.
|
// Try and set the sample rate to what the user selected.
|
||||||
let sample_rate = sample_rate.0;
|
let sample_rate = sample_rate.0.into();
|
||||||
if sample_rate != drivers.get_sample_rate().rate {
|
if sample_rate != driver.sample_rate().map_err(build_stream_err)? {
|
||||||
if drivers.can_sample_rate(sample_rate) {
|
if driver.can_sample_rate(sample_rate).map_err(build_stream_err)? {
|
||||||
drivers
|
driver
|
||||||
.set_sample_rate(sample_rate)
|
.set_sample_rate(sample_rate)
|
||||||
.expect("Unsupported sample rate");
|
.map_err(build_stream_err)?;
|
||||||
} else {
|
} else {
|
||||||
return Err(BuildStreamError::FormatNotSupported);
|
return Err(BuildStreamError::FormatNotSupported);
|
||||||
}
|
}
|
||||||
|
@ -122,14 +125,14 @@ impl EventLoop {
|
||||||
/// it will be created.
|
/// it will be created.
|
||||||
fn get_input_stream(
|
fn get_input_stream(
|
||||||
&self,
|
&self,
|
||||||
drivers: &sys::Drivers,
|
driver: &sys::Driver,
|
||||||
format: &Format,
|
format: &Format,
|
||||||
device: &Device,
|
device: &Device,
|
||||||
) -> Result<usize, BuildStreamError> {
|
) -> Result<usize, BuildStreamError> {
|
||||||
match device.default_input_format() {
|
match device.default_input_format() {
|
||||||
Ok(f) => {
|
Ok(f) => {
|
||||||
let num_asio_channels = f.channels;
|
let num_asio_channels = f.channels;
|
||||||
self.check_format(drivers, format, num_asio_channels)
|
self.check_format(driver, format, num_asio_channels)
|
||||||
},
|
},
|
||||||
Err(_) => Err(BuildStreamError::FormatNotSupported),
|
Err(_) => Err(BuildStreamError::FormatNotSupported),
|
||||||
}?;
|
}?;
|
||||||
|
@ -141,7 +144,7 @@ impl EventLoop {
|
||||||
Some(ref input) => Ok(input.buffer_size as usize),
|
Some(ref input) => Ok(input.buffer_size as usize),
|
||||||
None => {
|
None => {
|
||||||
let output = streams.output.take();
|
let output = streams.output.take();
|
||||||
drivers
|
driver
|
||||||
.prepare_input_stream(output, num_channels)
|
.prepare_input_stream(output, num_channels)
|
||||||
.map(|new_streams| {
|
.map(|new_streams| {
|
||||||
let bs = match new_streams.input {
|
let bs = match new_streams.input {
|
||||||
|
@ -163,14 +166,14 @@ impl EventLoop {
|
||||||
/// it will be created.
|
/// it will be created.
|
||||||
fn get_output_stream(
|
fn get_output_stream(
|
||||||
&self,
|
&self,
|
||||||
drivers: &sys::Drivers,
|
driver: &sys::Driver,
|
||||||
format: &Format,
|
format: &Format,
|
||||||
device: &Device,
|
device: &Device,
|
||||||
) -> Result<usize, BuildStreamError> {
|
) -> Result<usize, BuildStreamError> {
|
||||||
match device.default_output_format() {
|
match device.default_output_format() {
|
||||||
Ok(f) => {
|
Ok(f) => {
|
||||||
let num_asio_channels = f.channels;
|
let num_asio_channels = f.channels;
|
||||||
self.check_format(drivers, format, num_asio_channels)
|
self.check_format(driver, format, num_asio_channels)
|
||||||
},
|
},
|
||||||
Err(_) => Err(BuildStreamError::FormatNotSupported),
|
Err(_) => Err(BuildStreamError::FormatNotSupported),
|
||||||
}?;
|
}?;
|
||||||
|
@ -182,7 +185,7 @@ impl EventLoop {
|
||||||
Some(ref output) => Ok(output.buffer_size as usize),
|
Some(ref output) => Ok(output.buffer_size as usize),
|
||||||
None => {
|
None => {
|
||||||
let input = streams.input.take();
|
let input = streams.input.take();
|
||||||
drivers
|
driver
|
||||||
.prepare_output_stream(input, num_channels)
|
.prepare_output_stream(input, num_channels)
|
||||||
.map(|new_streams| {
|
.map(|new_streams| {
|
||||||
let bs = match new_streams.output {
|
let bs = match new_streams.output {
|
||||||
|
@ -205,11 +208,10 @@ impl EventLoop {
|
||||||
device: &Device,
|
device: &Device,
|
||||||
format: &Format,
|
format: &Format,
|
||||||
) -> Result<StreamId, BuildStreamError> {
|
) -> Result<StreamId, BuildStreamError> {
|
||||||
let Device { drivers, .. } = device;
|
let Device { driver, .. } = device;
|
||||||
let num_channels = format.channels.clone();
|
let num_channels = format.channels.clone();
|
||||||
let stream_type = drivers.get_data_type().expect("Couldn't load data type");
|
let stream_type = driver.data_type().map_err(build_stream_err)?;
|
||||||
let input_stream = self.get_input_stream(&drivers, format, device);
|
let stream_buffer_size = self.get_input_stream(&driver, format, device)?;
|
||||||
input_stream.map(|stream_buffer_size| {
|
|
||||||
let cpal_num_samples = stream_buffer_size * num_channels as usize;
|
let cpal_num_samples = stream_buffer_size * num_channels as usize;
|
||||||
let count = self.stream_count.fetch_add(1, Ordering::SeqCst);
|
let count = self.stream_count.fetch_add(1, Ordering::SeqCst);
|
||||||
let asio_streams = self.asio_streams.clone();
|
let asio_streams = self.asio_streams.clone();
|
||||||
|
@ -524,9 +526,9 @@ impl EventLoop {
|
||||||
self.cpal_streams
|
self.cpal_streams
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.push(Some(Stream { playing: false }));
|
.push(Some(Stream { driver: driver.clone(), playing: false }));
|
||||||
StreamId(count)
|
|
||||||
})
|
Ok(StreamId(count))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create the an output cpal stream.
|
/// Create the an output cpal stream.
|
||||||
|
@ -535,11 +537,10 @@ impl EventLoop {
|
||||||
device: &Device,
|
device: &Device,
|
||||||
format: &Format,
|
format: &Format,
|
||||||
) -> Result<StreamId, BuildStreamError> {
|
) -> Result<StreamId, BuildStreamError> {
|
||||||
let Device { drivers, .. } = device;
|
let Device { driver, .. } = device;
|
||||||
let num_channels = format.channels.clone();
|
let num_channels = format.channels.clone();
|
||||||
let stream_type = drivers.get_data_type().expect("Couldn't load data type");
|
let stream_type = driver.data_type().map_err(build_stream_err)?;
|
||||||
let output_stream = self.get_output_stream(&drivers, format, device);
|
let stream_buffer_size = self.get_output_stream(&driver, format, device)?;
|
||||||
output_stream.map(|stream_buffer_size| {
|
|
||||||
let cpal_num_samples = stream_buffer_size * num_channels as usize;
|
let cpal_num_samples = stream_buffer_size * num_channels as usize;
|
||||||
let count = self.stream_count.fetch_add(1, Ordering::SeqCst);
|
let count = self.stream_count.fetch_add(1, Ordering::SeqCst);
|
||||||
let asio_streams = self.asio_streams.clone();
|
let asio_streams = self.asio_streams.clone();
|
||||||
|
@ -875,45 +876,49 @@ impl EventLoop {
|
||||||
self.cpal_streams
|
self.cpal_streams
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.push(Some(Stream { playing: false }));
|
.push(Some(Stream { driver: driver.clone(), playing: false }));
|
||||||
|
|
||||||
// Give the ID based on the stream count
|
// Give the ID based on the stream count
|
||||||
StreamId(count)
|
Ok(StreamId(count))
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Play the cpal stream for the given ID.
|
/// Play the cpal stream for the given ID.
|
||||||
/// Also play The ASIO streams if they are not already.
|
///
|
||||||
|
/// TODO: This will also play all other paused streams... need a nicer way of addressing ASIO's
|
||||||
|
/// limitation of only being able to start/stop the entire driver.
|
||||||
pub fn play_stream(&self, stream_id: StreamId) -> Result<(), PlayStreamError> {
|
pub fn play_stream(&self, stream_id: StreamId) -> Result<(), PlayStreamError> {
|
||||||
let mut streams = self.cpal_streams.lock().unwrap();
|
let mut streams = self.cpal_streams.lock().unwrap();
|
||||||
if let Some(s) = streams.get_mut(stream_id.0).expect("Bad play stream index") {
|
if let Some(s) = streams.get_mut(stream_id.0).expect("Bad play stream index") {
|
||||||
s.playing = true;
|
s.playing = true;
|
||||||
}
|
|
||||||
// Calling play when already playing is a no-op
|
// Calling play when already playing is a no-op
|
||||||
sys::play();
|
s.driver.start().map_err(play_stream_err)?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Pause the cpal stream for the given ID.
|
/// Pause the cpal stream for the given ID.
|
||||||
/// Pause the ASIO streams if there are no CPAL streams palying.
|
///
|
||||||
|
/// Pause the ASIO streams if there are no other CPAL streams playing, as ASIO only allows
|
||||||
|
/// stopping the entire driver.
|
||||||
|
///
|
||||||
|
/// TODO: Come up with a nicer solution for this.
|
||||||
pub fn pause_stream(&self, stream_id: StreamId) -> Result<(), PauseStreamError> {
|
pub fn pause_stream(&self, stream_id: StreamId) -> Result<(), PauseStreamError> {
|
||||||
let mut streams = self.cpal_streams.lock().unwrap();
|
let mut streams = self.cpal_streams.lock().unwrap();
|
||||||
if let Some(s) = streams
|
let streams_playing = streams.iter()
|
||||||
.get_mut(stream_id.0)
|
.filter(|s| s.map(|s| s.playing).unwrap_or(false))
|
||||||
.expect("Bad pause stream index")
|
.count();
|
||||||
{
|
if let Some(s) = streams.get_mut(stream_id.0).expect("Bad pause stream index") {
|
||||||
|
if streams_playing <= 1 {
|
||||||
|
s.driver.stop().map_err(pause_stream_err)?;
|
||||||
s.playing = false;
|
s.playing = false;
|
||||||
}
|
}
|
||||||
let any_playing = streams
|
|
||||||
.iter()
|
|
||||||
.any(|s| if let Some(s) = s { s.playing } else { false });
|
|
||||||
if any_playing {
|
|
||||||
sys::stop();
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Destroy the cpal stream based on the ID.
|
/// Destroy the cpal stream based on the ID.
|
||||||
pub fn destroy_stream(&self, stream_id: StreamId) {
|
pub fn destroy_stream(&self, stream_id: StreamId) {
|
||||||
|
// TODO: Should we not also remove an ASIO stream here?
|
||||||
let mut streams = self.cpal_streams.lock().unwrap();
|
let mut streams = self.cpal_streams.lock().unwrap();
|
||||||
streams.get_mut(stream_id.0).take();
|
streams.get_mut(stream_id.0).take();
|
||||||
}
|
}
|
||||||
|
@ -942,7 +947,6 @@ impl Drop for EventLoop {
|
||||||
output: None,
|
output: None,
|
||||||
input: None,
|
input: None,
|
||||||
};
|
};
|
||||||
sys::clean_up();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -961,3 +965,38 @@ fn convert_endian_from<T: PrimInt>(sample: T, endian: Endian) -> T {
|
||||||
Endian::Little => T::from_le(sample),
|
Endian::Little => T::from_le(sample),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn build_stream_err(e: sys::AsioError) -> BuildStreamError {
|
||||||
|
match e {
|
||||||
|
sys::AsioError::NoDrivers |
|
||||||
|
sys::AsioError::HardwareMalfunction => BuildStreamError::DeviceNotAvailable,
|
||||||
|
sys::AsioError::InvalidInput |
|
||||||
|
sys::AsioError::BadMode => BuildStreamError::InvalidArgument,
|
||||||
|
err => {
|
||||||
|
let description = format!("{}", err);
|
||||||
|
BackendSpecificError { description }.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn pause_stream_err(e: sys::AsioError) -> PauseStreamError {
|
||||||
|
match e {
|
||||||
|
sys::AsioError::NoDrivers |
|
||||||
|
sys::AsioError::HardwareMalfunction => PauseStreamError::DeviceNotAvailable,
|
||||||
|
err => {
|
||||||
|
let description = format!("{}", err);
|
||||||
|
BackendSpecificError { description }.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn play_stream_err(e: sys::AsioError) -> PlayStreamError {
|
||||||
|
match e {
|
||||||
|
sys::AsioError::NoDrivers |
|
||||||
|
sys::AsioError::HardwareMalfunction => PlayStreamError::DeviceNotAvailable,
|
||||||
|
err => {
|
||||||
|
let description = format!("{}", err);
|
||||||
|
BackendSpecificError { description }.into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -377,6 +377,9 @@ pub enum BuildStreamError {
|
||||||
/// them immediately.
|
/// them immediately.
|
||||||
#[derive(Debug, Fail)]
|
#[derive(Debug, Fail)]
|
||||||
pub enum PlayStreamError {
|
pub enum PlayStreamError {
|
||||||
|
/// The device associated with the stream is no longer available.
|
||||||
|
#[fail(display = "the device associated with the stream is no longer available")]
|
||||||
|
DeviceNotAvailable,
|
||||||
/// See the `BackendSpecificError` docs for more information about this error variant.
|
/// See the `BackendSpecificError` docs for more information about this error variant.
|
||||||
#[fail(display = "{}", err)]
|
#[fail(display = "{}", err)]
|
||||||
BackendSpecific {
|
BackendSpecific {
|
||||||
|
@ -392,6 +395,9 @@ pub enum PlayStreamError {
|
||||||
/// them immediately.
|
/// them immediately.
|
||||||
#[derive(Debug, Fail)]
|
#[derive(Debug, Fail)]
|
||||||
pub enum PauseStreamError {
|
pub enum PauseStreamError {
|
||||||
|
/// The device associated with the stream is no longer available.
|
||||||
|
#[fail(display = "the device associated with the stream is no longer available")]
|
||||||
|
DeviceNotAvailable,
|
||||||
/// See the `BackendSpecificError` docs for more information about this error variant.
|
/// See the `BackendSpecificError` docs for more information about this error variant.
|
||||||
#[fail(display = "{}", err)]
|
#[fail(display = "{}", err)]
|
||||||
BackendSpecific {
|
BackendSpecific {
|
||||||
|
|
Loading…
Reference in New Issue