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:
mitchmindtree 2019-06-28 04:43:58 +10:00
parent efe683133c
commit d739a5b79d
5 changed files with 788 additions and 741 deletions

View File

@ -12,4 +12,6 @@ extern crate num_derive;
#[cfg(asio)] #[cfg(asio)]
pub mod bindings; pub mod bindings;
#[cfg(asio)] #[cfg(asio)]
pub use bindings::*; pub use bindings::*;
#[cfg(asio)]
pub use bindings::errors::{AsioError, LoadDriverError};

View File

@ -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> {
match self.drivers.next() { loop {
Some(name) => sys::Drivers::load(&name) match self.drivers.next() {
.or_else(|e| { Some(name) => match self.asio.load_driver(&name) {
eprintln!("{}", e); Ok(driver) => return Some(Device { driver: Arc::new(driver), name }),
Err(e) Err(_) => continue,
}).ok() }
.map(|drivers| Device { drivers, name }), None => return None,
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 => {
fn first_device() -> Option<Device> { let description = format!("{}", err);
let mut driver_list = online_devices(); BackendSpecificError { description }.into()
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()
}
}
} }

View File

@ -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 {

File diff suppressed because it is too large Load Diff

View File

@ -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 {