1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
use crate::handle; use std::{error, fmt, result}; /// Opaque physical device handle. pub type PhysicalDevice = handle::RawHandle; pub const DEFAULT_SAMPLE_RATE: usize = 0; /// Driver Implementations #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum DriverId { Wasapi, PulseAudio, OpenSLES, AAudio, } bitflags::bitflags! { /// Physical Devices may support different resource access modi. /// /// Other applications and instance may access the same physical device /// concurrently or the application requires exclusive access to the certain device. pub struct SharingModeFlags: u32 { /// Exclusive device access flag. const EXCLUSIVE = 0b01; /// Concurrent devices access shared by multiple processes flag. const CONCURRENT = 0b10; } } /// Physical device access. /// /// Sharing mode specifies system-wide access to a physical device resource. /// Access is not isolated to the current process or instance. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SharingMode { /// Exclusive device access. Exclusive, /// Concurrent devices access shared by multiple processes. Concurrent, } /// Device stream operation mode. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum StreamMode { /// Explicit polling. /// /// Users need to manually execute `submit_buffers` to poll the stream buffers. /// The users are also in control of the audio session in which the stream will be processed. Polling, /// Callback based stream. /// /// The device internally poll the stream buffers. Audio sessions are automatically created and maintained. /// The execution context of the stream callback is hidden from the users. Callback, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum FormFactor { /// Unknown, /// Remote Network Remote, /// LineLevel, /// Headphones, /// Headset, /// Microphone, } bitflags::bitflags! { pub struct ChannelMask: u32 { const FRONT_LEFT = 0b0001; const FRONT_RIGHT = 0b0010; const FRONT_CENTER = 0b0100; } } bitflags::bitflags! { pub struct StreamFlags: u32 { const INPUT = 0b01; const OUTPUT = 0b10; } } pub type Frames = usize; #[derive(Debug, Clone)] pub struct PhysicalDeviceProperties { pub device_name: String, pub streams: StreamFlags, pub form_factor: FormFactor, } #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Format { F32, I16, U32, } /// Sample description. #[derive(Debug, Copy, Clone, PartialEq, Eq)] pub struct SampleDesc { /// Sample Format. pub format: Format, /// Sample Rate. pub sample_rate: usize, } /// Frame description. /// /// Consists of a channel mask and a sample description. /// A frame is composed of one samples per channel. #[derive(Debug, Copy, Clone)] pub struct FrameDesc { /// Sample Format. pub format: Format, /// Sample Rate. pub sample_rate: usize, /// Channel Mask. pub channels: ChannelMask, } impl FrameDesc { /// Number of channels for the channel mask. pub fn num_channels(&self) -> usize { self.channels.bits().count_ones() as _ } /// Sample descriptor. pub fn sample_desc(&self) -> SampleDesc { SampleDesc { format: self.format, sample_rate: self.sample_rate, } } } /// Properties of the instance implementation. #[derive(Debug, Clone, Copy)] pub struct InstanceProperties { /// Driver identifier. pub driver_id: DriverId, /// Operation mode of the device stream. pub stream_mode: StreamMode, /// Device sharing modes. pub sharing: SharingModeFlags, } #[derive(Debug, Clone)] pub enum Error { /// Device Lost DeviceLost, /// Validation error. /// /// Denote errors caused by incorrect API usage. Validation { description: String }, /// Internal implementation errors. Internal { cause: String }, } impl error::Error for Error {} impl fmt::Display for Error { fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> { match *self { Error::DeviceLost => writeln!(fmt, "Device lost"), Error::Validation { ref description } => writeln!(fmt, "Validation error: {}", description), Error::Internal { ref cause } => writeln!(fmt, "Internal: {}", cause), } } } impl Error { pub(crate) fn validation<O, T: ToString>(description: T) -> Result<O> { Err(Error::Validation { description: description.to_string() }) } } pub type Result<T> = result::Result<T, Error>; #[derive(Debug, Clone)] pub enum Event { Added(PhysicalDevice), Removed(PhysicalDevice), DefaultInputDevice(Option<PhysicalDevice>), DefaultOutputDevice(Option<PhysicalDevice>), } #[derive(Debug, Clone)] pub struct DeviceDesc { pub physical_device: PhysicalDevice, pub sharing: SharingMode, pub sample_desc: SampleDesc, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Channels { pub input: ChannelMask, pub output: ChannelMask, } /// Device Stream properties. #[derive(Debug, Clone, Copy)] pub struct StreamProperties { pub channels: ChannelMask, pub sample_rate: usize, pub buffer_size: Frames, } impl StreamProperties { pub fn num_channels(&self) -> usize { self.channels.bits().count_ones() as _ } } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct StreamBuffers { /// Number of frames per buffer. pub frames: usize, /// Input frame buffer. /// /// For streams with empty input channels the pointer will be null. /// The buffer pointer is aligned according to the stream format requirements. pub input: *const (), /// Input frame buffer. /// /// For streams with empty output channels the pointer will be null. /// The buffer pointer is aligned according to the stream format requirements. pub output: *mut (), } pub struct Stream { pub properties: StreamProperties, pub buffers: StreamBuffers, } pub type StreamCallback = Box<dyn FnMut(Stream) + Send>; pub trait Instance { type Device: Device; /// Audio Session /// /// See more details on `create_session`. type Session; /// Get instance properties. unsafe fn properties() -> InstanceProperties; /// Create an instance object. /// /// ## Validation /// /// - The instance **must** outlive all its child objects. unsafe fn create(name: &str) -> Self; /// Retrieve a list of physical devices of the current instance. /// /// The list may vary over time when devices get added or removed. /// Users may track changes manually by registering an event handler. unsafe fn enumerate_physical_devices(&self) -> Vec<PhysicalDevice>; /// Get the default physical input device. unsafe fn default_physical_input_device(&self) -> Option<PhysicalDevice>; /// Get the default physical output device. unsafe fn default_physical_output_device(&self) -> Option<PhysicalDevice>; /// Get physical device properties. /// /// ## Validation /// /// - `physical_device` **must** be a valid handle. unsafe fn physical_device_properties( &self, physical_device: PhysicalDevice, ) -> Result<PhysicalDeviceProperties>; /// Check format support for a physical device. /// /// ## Validation /// /// - `physical_device` **must** be a valid handle. unsafe fn physical_device_supports_format( &self, physical_device: PhysicalDevice, sharing: SharingMode, frame_desc: FrameDesc, ) -> bool; /// Get default concurrent mode format. /// /// Returns the default format used for physical devices when /// used with concurrent sharing mode. /// /// ## Validation /// /// - `physical_device` **must** be a valid handle. unsafe fn physical_device_default_concurrent_format( &self, physical_device: PhysicalDevice, ) -> Result<FrameDesc>; /// Create a new logical device. /// /// A logical device with an associated stream will be created /// from a physical device. /// /// ## Validation /// /// - `physical_device` **must** be a valid handle. /// - If the device properties does not include `StreamFlags::INPUT`, the input channel mask must be empty. /// - If the device properties does not include `StreamFlags::OUTPUT`, the output channel mask must be empty. /// - If output channel mask is not empty, the format consisting of sample desc and output channel mask /// **must** be supported by this physical device. /// - If input channel mask is not empty, the format consisting of sample desc and input channel mask /// **must** be supported by this physical device. unsafe fn create_device( &self, desc: DeviceDesc, channels: Channels, callback: StreamCallback, ) -> Result<Self::Device>; /// Create an audio session. /// /// Audio sessions are needed for ensuring realtime properties for audio streaming. /// Callback based instances have an internal executor with the a properly configured audio session. /// After creating a session the current executor thread will have realtime properties for the lifetime of the session. /// /// All polling instances will expose a concurrent default format with a `sample_rate`, /// which is not equal to `DEFAULT_SAMPLE_RATE`. /// /// ## Validation /// /// - `sample_rate` **must** not be `DEFAULT_SAMPLE_RATE`. unsafe fn create_session(&self, sample_rate: usize) -> Result<Self::Session>; unsafe fn set_event_callback<F>(&mut self, callback: Option<F>) -> Result<()> where F: FnMut(Event) + Send + 'static; } pub trait Device { unsafe fn start(&self); unsafe fn stop(&self); unsafe fn stream_properties(&self) -> StreamProperties; /// Submit stream buffers. /// /// This function **must** be called only for devices of a polling instance. /// It will internally wait for acquiring the streaming buffers, call the stream callback /// for reading/writing the buffers and submit these to the audio engine. /// /// ## Validation /// /// - **Must** only be called for devices, which corresponding instance streaming properties are `Polling`. unsafe fn submit_buffers(&mut self, _timeout_ms: u32) -> Result<()> { Error::validation("`submit_buffers` not allowed for callback based instances") } }