Skip to content

Commit 3fd2c84

Browse files
committed
Implement duplex stream
1 parent dbc2ad8 commit 3fd2c84

8 files changed

Lines changed: 431 additions & 210 deletions

File tree

src/backends/asio/device.rs

Lines changed: 398 additions & 172 deletions
Large diffs are not rendered by default.

src/backends/asio/driver.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ use std::{borrow::Cow, sync::Arc};
22

33
use asio_sys as asio;
44

5-
6-
use crate::{device::{AudioDevice, DeviceType}, driver::AudioDriver};
5+
use crate::{
6+
device::{AudioDevice, DeviceType},
7+
driver::AudioDriver,
8+
};
79

810
use super::{device::AsioDevice, error::AsioError};
911

@@ -14,6 +16,7 @@ pub struct AsioDriver {
1416
}
1517

1618
impl AsioDriver {
19+
/// Create a new ASIO driver.
1720
pub fn new() -> Result<Self, AsioError> {
1821
let asio = Arc::new(asio::Asio::new());
1922
Ok(AsioDriver { asio })
@@ -32,15 +35,14 @@ impl AudioDriver for AsioDriver {
3235

3336
fn default_device(&self, device_type: DeviceType) -> Result<Option<Self::Device>, Self::Error> {
3437
let iter = AsioDeviceList::new(self.asio.clone())?;
35-
36-
let dd = iter.filter(|device| {
37-
match (device.device_type(), device_type) {
38+
39+
let dd = iter
40+
.filter(|device| match (device.device_type(), device_type) {
3841
(DeviceType::Input | DeviceType::Duplex, DeviceType::Input) => true,
3942
(DeviceType::Output | DeviceType::Duplex, DeviceType::Output) => true,
4043
(a, b) => a == b,
41-
42-
}
43-
}).next();
44+
})
45+
.next();
4446
Ok(dd)
4547
}
4648

@@ -79,5 +81,3 @@ impl Iterator for AsioDeviceList {
7981
}
8082
}
8183
}
82-
83-

src/backends/asio/error.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use thiserror::Error;
21
use asio_sys::AsioError as AsioSysError;
2+
use thiserror::Error;
33

44
/// Type of errors from the ASIO backend.
55
#[derive(Debug, Error)]
@@ -13,4 +13,6 @@ pub enum AsioError {
1313
ConfigurationNotAvailable,
1414
#[error("Device unavailable")]
1515
DeviceUnavailable,
16+
#[error("Multiple streams not supported")]
17+
MultipleStreams,
1618
}

src/backends/asio/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
2-
mod device;
1+
pub(crate) mod device;
32
pub(crate) mod driver;
4-
mod error;
5-
mod stream;
3+
pub use driver::AsioDriver;
4+
pub(crate) mod error;
65
pub mod prelude;
7-
pub use prelude::*;
6+
pub(crate) mod stream;

src/backends/asio/prelude.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
pub use super::{
2-
device::AsioDevice, driver::AsioDriver,
3-
};
1+
pub use super::{device::AsioDevice, driver::AsioDriver, error::AsioError, stream::AsioStream};

src/backends/asio/stream.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,26 @@
1-
use std::{marker::PhantomData, sync::{atomic::AtomicBool, Arc}};
1+
use std::sync::Arc;
22

3-
use asio_sys::{self as asio, CallbackId};
3+
use asio_sys as asio;
44

55
use crate::stream::AudioStreamHandle;
66

77
use super::error::AsioError;
88

99
pub struct AsioStream<Callback> {
10-
playing: Arc<AtomicBool>,
11-
// driver: Arc<asio::Driver>,
12-
// streams: Arc<asio::AsioStreams>,
13-
callback_id: asio::CallbackId,
14-
callback: PhantomData<Callback>,
10+
pub driver: Arc<asio::Driver>,
11+
pub callback_id: asio::CallbackId,
12+
pub callback_retrieve: oneshot::Sender<oneshot::Sender<Callback>>,
1513
}
1614

1715
impl<Callback> AudioStreamHandle<Callback> for AsioStream<Callback> {
18-
1916
type Error = AsioError;
20-
17+
2118
fn eject(self) -> Result<Callback, Self::Error> {
22-
todo!()
19+
let (tx, rx) = oneshot::channel();
20+
self.callback_retrieve.send(tx).unwrap();
21+
let callback = rx.recv().unwrap();
22+
self.driver.stop()?;
23+
self.driver.remove_callback(self.callback_id);
24+
Ok(callback)
2325
}
2426
}
25-
26-
impl<Callback> AsioStream<Callback> {
27-
pub fn new(playing: Arc<AtomicBool>, callback_id: CallbackId) -> Self {
28-
AsioStream { playing, callback_id, callback: PhantomData }
29-
}
30-
31-
32-
}

src/backends/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,5 @@ pub fn default_output_device() -> impl AudioOutputDevice {
116116
// #[cfg(os_wasapi)]
117117
// return default_output_device_from(&wasapi::WasapiDriver);
118118
#[cfg(os_asio)]
119-
return default_output_device_from(&asio::AsioDriver::new().unwrap());
119+
return default_output_device_from(&asio::driver::AsioDriver::new().unwrap());
120120
}

src/prelude.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![allow(unused)]
22
//! Prelude module for `interflow`. Use as a star-import.
33
4+
#[cfg(os_asio)]
5+
pub use crate::backends::asio::prelude::*;
46
#[cfg(os_wasapi)]
57
pub use crate::backends::wasapi::prelude::*;
68
pub use crate::backends::*;

0 commit comments

Comments
 (0)