commenting and rustfmt for device

This commit is contained in:
Tom Gowan 2018-11-05 12:51:55 +11:00 committed by mitchmindtree
parent 5248455cb7
commit 1f24c76e6a
1 changed files with 81 additions and 73 deletions

View File

@ -3,21 +3,25 @@ use std;
pub type SupportedInputFormats = std::vec::IntoIter<SupportedFormat>; 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 DefaultFormatError;
use Format; use Format;
use FormatsEnumerationError; use FormatsEnumerationError;
use DefaultFormatError;
use SupportedFormat;
use SampleFormat; use SampleFormat;
use SampleRate; use SampleRate;
use std::hash::{Hash, Hasher}; use SupportedFormat;
/// A ASIO Device
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Device{ pub struct Device {
/// The drivers for this device
pub drivers: sys::Drivers, pub drivers: sys::Drivers,
/// The name of this device
pub name: String, pub name: String,
} }
pub struct Devices{ /// All available devices
pub struct Devices {
drivers: std::vec::IntoIter<String>, drivers: std::vec::IntoIter<String>,
} }
@ -40,40 +44,46 @@ impl Device {
self.name.clone() self.name.clone()
} }
// Just supporting default for now /// Gets the supported input formats.
pub fn supported_input_formats(&self) -> Result<SupportedInputFormats, /// TODO currently only supports the default.
FormatsEnumerationError> { /// Need to find all possible formats.
match self.default_input_format() { pub fn supported_input_formats(
Ok(f) => { &self,
) -> Result<SupportedInputFormats, FormatsEnumerationError> {
match self.default_input_format() {
Ok(f) => {
// Can this device support both 44100 and 48000
let supported_formats: Vec<SupportedFormat> = [44100, 48000] let supported_formats: Vec<SupportedFormat> = [44100, 48000]
.into_iter() .into_iter()
.filter(|rate| self.drivers.can_sample_rate(**rate as u32) ) .filter(|rate| self.drivers.can_sample_rate(**rate as u32))
.map(|rate| { .map(|rate| {
let mut format = f.clone(); let mut format = f.clone();
format.sample_rate = SampleRate(*rate); format.sample_rate = SampleRate(*rate);
SupportedFormat::from(format) SupportedFormat::from(format)
}) }).collect();
.collect();
//Ok(vec![SupportedFormat::from(f)].into_iter())
Ok(supported_formats.into_iter()) Ok(supported_formats.into_iter())
}, },
Err(_) => Err(FormatsEnumerationError::DeviceNotAvailable), Err(_) => Err(FormatsEnumerationError::DeviceNotAvailable),
} }
} }
pub fn supported_output_formats(&self) -> Result<SupportedOutputFormats, /// Gets the supported output formats.
FormatsEnumerationError> { /// TODO currently only supports the default.
/// Need to find all possible formats.
pub fn supported_output_formats(
&self,
) -> Result<SupportedOutputFormats, FormatsEnumerationError> {
match self.default_output_format() { match self.default_output_format() {
Ok(f) => { Ok(f) => {
// Can this device support both 44100 and 48000
let supported_formats: Vec<SupportedFormat> = [44100, 48000] let supported_formats: Vec<SupportedFormat> = [44100, 48000]
.into_iter() .into_iter()
.filter(|rate| self.drivers.can_sample_rate(**rate as u32) ) .filter(|rate| self.drivers.can_sample_rate(**rate as u32))
.map(|rate| { .map(|rate| {
let mut format = f.clone(); let mut format = f.clone();
format.sample_rate = SampleRate(*rate); format.sample_rate = SampleRate(*rate);
SupportedFormat::from(format) SupportedFormat::from(format)
}) }).collect();
.collect();
//Ok(vec![SupportedFormat::from(f)].into_iter()) //Ok(vec![SupportedFormat::from(f)].into_iter())
Ok(supported_formats.into_iter()) Ok(supported_formats.into_iter())
}, },
@ -81,39 +91,42 @@ impl Device {
} }
} }
/// 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.drivers.get_channels().ins as u16;
let sample_rate = SampleRate(self.drivers.get_sample_rate().rate); let sample_rate = SampleRate(self.drivers.get_sample_rate().rate);
// Map th ASIO sample type to a CPAL sample type
match self.drivers.get_data_type() { match self.drivers.get_data_type() {
Ok(sys::AsioSampleType::ASIOSTInt16MSB) => Ok(SampleFormat::I16), Ok(sys::AsioSampleType::ASIOSTInt16MSB) => Ok(SampleFormat::I16),
Ok(sys::AsioSampleType::ASIOSTInt32MSB) => Ok(SampleFormat::I16), Ok(sys::AsioSampleType::ASIOSTInt32MSB) => Ok(SampleFormat::I16),
Ok(sys::AsioSampleType::ASIOSTFloat32MSB) => Ok(SampleFormat::F32), Ok(sys::AsioSampleType::ASIOSTFloat32MSB) => Ok(SampleFormat::F32),
Ok(sys::AsioSampleType::ASIOSTInt16LSB) => Ok(SampleFormat::I16), Ok(sys::AsioSampleType::ASIOSTInt16LSB) => Ok(SampleFormat::I16),
Ok(sys::AsioSampleType::ASIOSTInt32LSB) => Ok(SampleFormat::I16), Ok(sys::AsioSampleType::ASIOSTInt32LSB) => Ok(SampleFormat::I16),
Ok(sys::AsioSampleType::ASIOSTFloat32LSB) => Ok(SampleFormat::F32), Ok(sys::AsioSampleType::ASIOSTFloat32LSB) => Ok(SampleFormat::F32),
_ => Err(DefaultFormatError::StreamTypeNotSupported), _ => Err(DefaultFormatError::StreamTypeNotSupported),
}.map(|dt| { }.map(|dt| Format {
Format{channels, channels,
sample_rate, sample_rate,
data_type: dt } data_type: dt,
}) })
} }
/// 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.drivers.get_channels().outs as u16;
let sample_rate = SampleRate(self.drivers.get_sample_rate().rate); let sample_rate = SampleRate(self.drivers.get_sample_rate().rate);
match self.drivers.get_data_type() { match self.drivers.get_data_type() {
// Map th ASIO sample type to a CPAL sample type
Ok(sys::AsioSampleType::ASIOSTInt16MSB) => Ok(SampleFormat::I16), Ok(sys::AsioSampleType::ASIOSTInt16MSB) => Ok(SampleFormat::I16),
Ok(sys::AsioSampleType::ASIOSTFloat32MSB) => Ok(SampleFormat::F32), Ok(sys::AsioSampleType::ASIOSTFloat32MSB) => Ok(SampleFormat::F32),
Ok(sys::AsioSampleType::ASIOSTInt16LSB) => Ok(SampleFormat::I16), Ok(sys::AsioSampleType::ASIOSTInt16LSB) => Ok(SampleFormat::I16),
// TODO This should not be set to 16bit but is for testing Ok(sys::AsioSampleType::ASIOSTInt32LSB) => Ok(SampleFormat::I16),
Ok(sys::AsioSampleType::ASIOSTInt32LSB) => Ok(SampleFormat::I16),
Ok(sys::AsioSampleType::ASIOSTFloat32LSB) => Ok(SampleFormat::F32), Ok(sys::AsioSampleType::ASIOSTFloat32LSB) => Ok(SampleFormat::F32),
_ => Err(DefaultFormatError::StreamTypeNotSupported), _ => Err(DefaultFormatError::StreamTypeNotSupported),
}.map(|dt|{ }.map(|dt| Format {
Format{channels, channels,
sample_rate, sample_rate,
data_type: dt } data_type: dt,
}) })
} }
} }
@ -125,25 +138,24 @@ impl Default for Devices {
.into_iter() .into_iter()
.filter(|name| sys::Drivers::load(&name).is_ok()) .filter(|name| sys::Drivers::load(&name).is_ok())
.collect(); .collect();
Devices{ drivers: driver_names.into_iter() } Devices {
drivers: driver_names.into_iter(),
}
} }
} }
impl Iterator for Devices { impl Iterator for Devices {
type Item = Device; type Item = Device;
/// Load drivers and return device
fn next(&mut self) -> Option<Device> { fn next(&mut self) -> Option<Device> {
match self.drivers.next() { match self.drivers.next() {
Some(name) => { Some(name) => sys::Drivers::load(&name)
sys::Drivers::load(&name) .or_else(|e| {
.or_else(|e| { eprintln!("{}", e);
eprintln!("{}", e); Err(e)
Err(e) }).ok()
}) .map(|drivers| Device { drivers, name }),
.ok()
.map(|drivers| Device{drivers, name} )
},
None => None, None => None,
} }
} }
@ -153,36 +165,32 @@ impl Iterator for Devices {
} }
} }
// Asio doesn't have a concept of default /// Asio doesn't have a concept of default
// so returning first in list as default /// so returning first in list as default
pub fn default_input_device() -> Option<Device> { pub fn default_input_device() -> Option<Device> {
let mut driver_list = sys::get_driver_list(); let mut driver_list = sys::get_driver_list();
match driver_list.pop() { match driver_list.pop() {
Some(name) => { Some(name) => sys::Drivers::load(&name)
sys::Drivers::load(&name) .or_else(|e| {
.or_else(|e| { eprintln!("{}", e);
eprintln!("{}", e); Err(e)
Err(e) }).ok()
}) .map(|drivers| Device { drivers, name }),
.ok()
.map(|drivers| Device{drivers, name} )
},
None => None, None => None,
} }
} }
/// Asio doesn't have a concept of default
/// so returning first in list as default
pub fn default_output_device() -> Option<Device> { pub fn default_output_device() -> Option<Device> {
let mut driver_list = sys::get_driver_list(); let mut driver_list = sys::get_driver_list();
match driver_list.pop() { match driver_list.pop() {
Some(name) => { Some(name) => sys::Drivers::load(&name)
sys::Drivers::load(&name) .or_else(|e| {
.or_else(|e| { eprintln!("{}", e);
eprintln!("{}", e); Err(e)
Err(e) }).ok()
}) .map(|drivers| Device { drivers, name }),
.ok()
.map(|drivers| Device{drivers, name} )
},
None => None, None => None,
} }
} }