Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions examples/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ impl HostTrait for MyHost {
true
}

fn new() -> Result<Self, Error> {
Ok(Self)
}

fn devices(&self) -> Result<Self::Devices, Error> {
Ok(std::iter::once(MyDevice))
}
Expand Down
10 changes: 4 additions & 6 deletions src/host/aaudio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,14 @@ struct BufferTuningState {
pub use crate::iter::{SupportedInputConfigs, SupportedOutputConfigs};
pub type Devices = std::vec::IntoIter<Device>;

impl Host {
pub fn new() -> Result<Self, Error> {
Ok(Host)
}
}

impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
Ok(Host)
}

fn is_available() -> bool {
true
}
Expand Down
24 changes: 12 additions & 12 deletions src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ pub struct Host {
}

impl Host {
pub fn new() -> Result<Self, Error> {
let inner = AlsaContext::new().map_err(|e| {
Error::with_message(
ErrorKind::HostUnavailable,
format!("ALSA is not available: {e}"),
)
})?;
Ok(Self {
inner: Arc::new(inner),
})
}

// "default" is a virtual ALSA device that redirects to the configured default. We cannot
// determine its actual capabilities without opening it, so we return Unknown direction.
fn default_device(&self) -> Device {
Expand All @@ -65,6 +53,18 @@ impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
let inner = AlsaContext::new().map_err(|e| {
Error::with_message(
ErrorKind::HostUnavailable,
format!("ALSA is not available: {e}"),
)
})?;
Ok(Self {
inner: Arc::new(inner),
})
}

fn is_available() -> bool {
// Assume ALSA is always available on Linux and BSD.
true
Expand Down
12 changes: 5 additions & 7 deletions src/host/asio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@ pub struct Host {
asio: Arc<sys::Asio>,
}

impl Host {
pub fn new() -> Result<Self, Error> {
impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
com::com_initialized();
let asio = GLOBAL_ASIO
.get_or_init(|| Arc::new(sys::Asio::new()))
.clone();
let host = Host { asio };
Ok(host)
}
}

impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn is_available() -> bool {
true
Expand Down
12 changes: 5 additions & 7 deletions src/host/audioworklet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,11 @@ enum Command {
Pause,
}

impl Host {
pub fn new() -> Result<Self, Error> {
impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
if Self::is_available() {
Ok(Host)
} else {
Expand All @@ -214,11 +217,6 @@ impl Host {
))
}
}
}

impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn is_available() -> bool {
if let Some(window) = web_sys::window() {
Expand Down
10 changes: 4 additions & 6 deletions src/host/coreaudio/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ impl fmt::Display for Device {

pub struct Host;

impl Host {
pub fn new() -> Result<Self, Error> {
Ok(Host)
}
}

impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
Ok(Host)
}

fn is_available() -> bool {
true
}
Expand Down
10 changes: 4 additions & 6 deletions src/host/coreaudio/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ pub use device::Device;
#[derive(Debug)]
pub struct Host;

impl Host {
pub fn new() -> Result<Self, Error> {
Ok(Host)
}
}

impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
Ok(Host)
}

fn is_available() -> bool {
// Assume coreaudio is always available
true
Expand Down
9 changes: 4 additions & 5 deletions src/host/custom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ use crate::{
pub struct Host(Box<dyn HostErased>);

impl Host {
// this only exists for impl_platform_host, which requires it
pub(crate) fn new() -> Result<Self, Error> {
Err(Error::new(ErrorKind::HostUnavailable))
}

/// Construct a custom host from an arbitrary [`HostTrait`] implementation.
pub fn from_host<T>(host: T) -> Self
where
Expand Down Expand Up @@ -356,6 +351,10 @@ impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
Err(Error::new(ErrorKind::HostUnavailable))
}

fn is_available() -> bool {
false
}
Expand Down
23 changes: 12 additions & 11 deletions src/host/jack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,6 @@ pub struct Host {
}

impl Host {
pub fn new() -> Result<Self, Error> {
let mut host = Host {
name: format!("cpal_client_{}", std::process::id()),
connect_ports_automatically: true,
start_server_automatically: false,
devices_created: vec![],
};
// Devices don't exist for JACK, they have to be created
host.initialize_default_devices();
Ok(host)
}
/// Configures whether created ports should automatically connect to system playback/capture
/// ports.
///
Expand Down Expand Up @@ -129,6 +118,18 @@ impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
let mut host = Host {
name: format!("cpal_client_{}", std::process::id()),
connect_ports_automatically: true,
start_server_automatically: false,
devices_created: vec![],
};
// Devices don't exist for JACK, they have to be created
host.initialize_default_devices();
Ok(host)
}

/// JACK is available if
/// - the jack feature flag is set
/// - libjack is installed (wouldn't compile without it)
Expand Down
31 changes: 17 additions & 14 deletions src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ pub(crate) mod webaudio;
#[cfg(feature = "custom")]
pub(crate) mod custom;

#[cfg(not(any(
windows,
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_vendor = "apple",
target_os = "android",
all(
target_arch = "wasm32",
any(target_os = "emscripten", target_os = "unknown"),
feature = "wasm-bindgen"
),
)))]
#[cfg(all(
not(feature = "custom"),
not(any(
windows,
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_vendor = "apple",
target_os = "android",
all(
target_arch = "wasm32",
any(target_os = "emscripten", target_os = "unknown"),
feature = "wasm-bindgen"
),
)),
))]
pub(crate) mod null;

#[cfg(any(
Expand Down
39 changes: 19 additions & 20 deletions src/host/null/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ use std::fmt;
use std::time::Duration;

use crate::{
CallbackInfo, Data, DeviceDescription, DeviceDescriptionBuilder, DeviceId, Error, FrameCount,
SampleFormat, StreamConfig, StreamInstant, SupportedStreamConfig, SupportedStreamConfigRange,
CallbackInfo, Data, DeviceDescription, DeviceDescriptionBuilder, DeviceId, Error,
ErrorKind::DeviceNotAvailable,
FrameCount, SampleFormat, StreamConfig, StreamInstant, SupportedStreamConfig,
SupportedStreamConfigRange,
traits::{DeviceTrait, HostTrait, StreamTrait},
};

Expand All @@ -33,13 +35,6 @@ pub struct SupportedInputConfigs;
#[derive(Clone)]
pub struct SupportedOutputConfigs;

impl Host {
#[allow(dead_code)]
pub fn new() -> Result<Self, Error> {
Ok(Self)
}
}

impl DeviceTrait for Device {
type SupportedInputConfigs = SupportedInputConfigs;
type SupportedOutputConfigs = SupportedOutputConfigs;
Expand All @@ -54,19 +49,19 @@ impl DeviceTrait for Device {
}

fn supported_input_configs(&self) -> Result<SupportedInputConfigs, Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

fn supported_output_configs(&self) -> Result<SupportedOutputConfigs, Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

fn default_input_config(&self) -> Result<SupportedStreamConfig, Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

fn default_output_config(&self) -> Result<SupportedStreamConfig, Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

fn build_input_stream_raw<D, E>(
Expand All @@ -81,7 +76,7 @@ impl DeviceTrait for Device {
D: FnMut(&Data, &CallbackInfo) + Send + 'static,
E: FnMut(Error) + Send + 'static,
{
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

/// Create an output stream.
Expand All @@ -97,14 +92,18 @@ impl DeviceTrait for Device {
D: FnMut(&mut Data, &CallbackInfo) + Send + 'static,
E: FnMut(Error) + Send + 'static,
{
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}
}

impl HostTrait for Host {
type Devices = Devices;
type Device = Device;

fn new() -> Result<Self, Error> {
Ok(Self)
}

fn is_available() -> bool {
false
}
Expand All @@ -124,23 +123,23 @@ impl HostTrait for Host {

impl StreamTrait for Stream {
fn start(&self) -> Result<(), Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

fn pause(&self) -> Result<(), Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

fn stop(&self, _timeout: Option<std::time::Duration>) -> Result<(), Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}

fn now(&self) -> StreamInstant {
unimplemented!()
StreamInstant::ZERO
}

fn buffer_size(&self) -> Result<FrameCount, Error> {
unimplemented!()
Err(Error::with_message(DeviceNotAvailable, "Null host in use"))
}
}

Expand Down
Loading