Skip to content

Commit 8307def

Browse files
committed
new API
1 parent 7542fac commit 8307def

File tree

1 file changed

+34
-53
lines changed

1 file changed

+34
-53
lines changed

src/platform/asio.rs

Lines changed: 34 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
//! Implementations for ASIO-specific device functionality.
22
3+
#[allow(unused_imports)]
34
use crate::BackendSpecificError;
45
use crate::Device;
56

6-
/// Extension trait for ASIO-specific device functionality.
7+
/// Extension trait to get ASIO device.
78
pub trait AsioDeviceExt {
8-
/// Returns `true` if this device is an ASIO device.
9-
fn is_asio_device(&self) -> bool;
9+
fn as_asio(&self) -> Option<AsioDevice<'_>>;
10+
}
11+
12+
/// Struct containing for ASIO-specific device functionality.
13+
#[cfg(all(target_os = "windows", feature = "asio"))]
14+
pub struct AsioDevice<'a>(&'a crate::host::asio::Device);
1015

16+
#[cfg(not(all(target_os = "windows", feature = "asio")))]
17+
pub struct AsioDevice<'a>(std::marker::PhantomData<&'a ()>);
18+
19+
#[cfg(all(target_os = "windows", feature = "asio"))]
20+
impl AsioDevice<'_> {
1121
/// Opens the ASIO driver's control panel window.
1222
///
1323
/// This provides access to device-specific settings like buffer size,
@@ -21,63 +31,34 @@ pub trait AsioDeviceExt {
2131
/// # Errors
2232
///
2333
/// Returns an error if this device is not an ASIO device.
24-
fn asio_open_control_panel(&self) -> Result<(), BackendSpecificError>;
25-
}
26-
27-
#[cfg(all(target_os = "windows", feature = "asio"))]
28-
impl AsioDeviceExt for Device {
29-
fn is_asio_device(&self) -> bool {
30-
matches!(self.as_inner(), crate::platform::DeviceInner::Asio(_))
31-
}
32-
33-
fn asio_open_control_panel(&self) -> Result<(), BackendSpecificError> {
34+
pub fn open_control_panel(&self) -> Result<(), BackendSpecificError> {
3435
use crate::host::asio::GLOBAL_ASIO;
35-
use crate::platform::DeviceInner;
36-
37-
if let DeviceInner::Asio(ref asio_device) = self.as_inner() {
38-
let description = asio_device
39-
.description()
40-
.map_err(|e| BackendSpecificError {
41-
description: format!("Could not get device name: {:?}", e),
42-
})?;
4336

44-
let driver = GLOBAL_ASIO
45-
.get()
46-
.ok_or(BackendSpecificError {
47-
description: "ASIO not initialized.".into(),
48-
})?
49-
.load_driver(description.name())
50-
.map_err(|e| BackendSpecificError {
51-
description: format!("Failed to load driver: {:?}", e),
52-
})?;
37+
let description = self.0.description().map_err(|e| BackendSpecificError {
38+
description: format!("{e:?}"),
39+
})?;
40+
let driver_name = description.name();
5341

54-
driver
55-
.open_control_panel()
56-
.map_err(|e| BackendSpecificError {
57-
description: format!("Failed to open control panel: {:?}", e),
58-
})
59-
} else {
60-
Err(BackendSpecificError {
61-
description: "Not an ASIO device".to_string(),
42+
GLOBAL_ASIO
43+
.get()
44+
.expect("GLOBAL_ASIO is always set when an ASIO Device exists")
45+
.load_driver(driver_name)
46+
.map_err(|e| BackendSpecificError {
47+
description: format!("{e:?}"),
48+
})?
49+
.open_control_panel()
50+
.map_err(|e| BackendSpecificError {
51+
description: format!("{e:?}"),
6252
})
63-
}
6453
}
6554
}
6655

67-
#[cfg(not(all(target_os = "windows", feature = "asio")))]
6856
impl AsioDeviceExt for Device {
69-
fn is_asio_device(&self) -> bool {
70-
false
71-
}
72-
73-
fn asio_open_control_panel(&self) -> Result<(), BackendSpecificError> {
74-
Err(not_available())
75-
}
76-
}
77-
78-
#[cfg(not(all(target_os = "windows", feature = "asio")))]
79-
fn not_available() -> BackendSpecificError {
80-
BackendSpecificError {
81-
description: "ASIO is not available on this platform".to_string(),
57+
fn as_asio(&self) -> Option<AsioDevice<'_>> {
58+
match self.as_inner() {
59+
#[cfg(all(target_os = "windows", feature = "asio"))]
60+
crate::platform::DeviceInner::Asio(d) => Some(AsioDevice(d)),
61+
_ => None,
62+
}
8263
}
8364
}

0 commit comments

Comments
 (0)