Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement device enumeration for WASAPI driver backend #1

Closed
wants to merge 2 commits into from
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
65 changes: 65 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ cfg_aliases = "0.2.1"
[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "netbsd"))'.dependencies]
alsa = "0.9.0"

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.58.0", features = [
"Win32_Media_Audio",
"Win32_Foundation",
"Win32_Devices_Properties",
"Win32_Media_KernelStreaming",
"Win32_System_Com_StructuredStorage",
"Win32_System_Threading",
"Win32_Security",
"Win32_System_SystemServices",
"Win32_System_Variant",
"Win32_Media_Multimedia",
"Win32_UI_Shell_PropertiesSystem"
]}

[[example]]
name = "enumerate_alsa"
path = "examples/enumerate_alsa.rs"

[[example]]
name = "enumerate_wasapi"
path = "examples/enumerate_wasapi.rs"

3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ fn main() {
cfg_aliases! {
wasm: { any(target_os = "wasm32") },
os_alsa: { any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd",
target_os = "netbsd") }
target_os = "netbsd") },
os_wasapi: { target_os = "windows" }
}
}
5 changes: 1 addition & 4 deletions examples/enumerate_alsa.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use std::error::Error;

mod util;

#[cfg(os_alsa)]
fn main() -> Result<(), Box<dyn Error>> {
fn main() -> Result<(), Box<dyn std::error::Error>> {
use crate::util::enumerate::enumerate_devices;
use interflow::backends::alsa::AlsaDriver;
enumerate_devices(AlsaDriver::default())
Expand All @@ -13,4 +11,3 @@ fn main() -> Result<(), Box<dyn Error>> {
fn main() {
println!("ALSA driver is not available on this platform");
}

13 changes: 13 additions & 0 deletions examples/enumerate_wasapi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod util;

#[cfg(os_wasapi)]
fn main() -> Result<(), Box<dyn std::error::Error>> {
use crate::util::enumerate::enumerate_devices;
use interflow::backends::wasapi::WasapiDriver;
enumerate_devices(WasapiDriver)
}

#[cfg(not(os_wasapi))]
fn main() {
println!("WASAPI driver is not available on this platform");
}
21 changes: 13 additions & 8 deletions examples/sine_wave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ fn main() -> Result<()> {
};
assert!(device.is_config_supported(&config));
println!("Using device {}", device.name());
let stream = device.create_output_stream(
config,
SineWave {
frequency: 440.,
phase: 0.,
},
).unwrap();
let stream = device
.create_output_stream(
config,
SineWave {
frequency: 440.,
phase: 0.,
},
)
.unwrap();
println!("Press Enter to stop");
std::io::stdin().read_line(&mut String::new())?;
stream.eject().unwrap();
Expand All @@ -33,7 +35,10 @@ struct SineWave {

impl AudioOutputCallback for SineWave {
fn on_output_data(&mut self, context: AudioCallbackContext, mut output: AudioOutput<f32>) {
eprintln!("Callback called, timestamp: {:2.3} s", context.timestamp.as_seconds());
eprintln!(
"Callback called, timestamp: {:2.3} s",
context.timestamp.as_seconds()
);
let sr = context.timestamp.samplerate as f32;
for i in 0..output.buffer.num_samples() {
output.buffer.set_mono(i, self.next_sample(sr));
Expand Down
7 changes: 5 additions & 2 deletions examples/util/enumerate.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use interflow::{AudioDevice, AudioDriver, DeviceType};
use std::error::Error;

pub fn enumerate_devices<Driver: AudioDriver>(driver: Driver) -> Result<(), Box<dyn Error>> where <Driver as AudioDriver>::Error: 'static {
pub fn enumerate_devices<Driver: AudioDriver>(driver: Driver) -> Result<(), Box<dyn Error>>
where
<Driver as AudioDriver>::Error: 'static,
{
eprintln!("Driver name : {}", Driver::DISPLAY_NAME);
eprintln!("Driver version: {}", driver.version()?);
eprintln!("Default device");
Expand All @@ -19,4 +22,4 @@ pub fn enumerate_devices<Driver: AudioDriver>(driver: Driver) -> Result<(), Box<
eprintln!("\t{} ({:?})", device.name(), device.device_type());
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion examples/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod enumerate;
pub mod enumerate;
11 changes: 4 additions & 7 deletions src/audio_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<S: DataOwned> Default for AudioBufferBase<S> {
}
}

impl<S: Data, S2: Data<Elem=S::Elem>> PartialEq<AudioBufferBase<S2>> for AudioBufferBase<S>
impl<S: Data, S2: Data<Elem = S::Elem>> PartialEq<AudioBufferBase<S2>> for AudioBufferBase<S>
where
S::Elem: PartialEq<S::Elem>,
{
Expand Down Expand Up @@ -178,7 +178,7 @@ impl<S: DataMut> AudioBufferBase<S> {
pub fn channels_mut(&mut self) -> impl '_ + Iterator<Item = ArrayViewMut1<S::Elem>> {
self.storage.rows_mut().into_iter()
}

pub fn as_interleaved_mut(&mut self) -> ArrayViewMut2<S::Elem> {
self.storage.view_mut().reversed_axes()
}
Expand Down Expand Up @@ -374,11 +374,8 @@ impl<'a, T: Sample> AudioMut<'a, T> {
}
}

pub fn mix(
&mut self,
other: AudioRef<T>,
other_amplitude: T::Float,
) where
pub fn mix(&mut self, other: AudioRef<T>, other_amplitude: T::Float)
where
T: AddAssign<T>,
{
for (mut ch_a, ch_b) in self.channels_mut().zip(other.channels()) {
Expand Down
12 changes: 6 additions & 6 deletions src/backends/alsa.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
use core::fmt;
use std::{borrow::Cow, convert::Infallible, ffi::CStr};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread::JoinHandle;
use std::time::Duration;
use std::{borrow::Cow, convert::Infallible, ffi::CStr};

use alsa::{device_name::HintIter, pcm, PCM};
use thiserror::Error;

use crate::audio_buffer::{AudioMut, AudioRef};
use crate::channel_map::{Bitset, ChannelMap32};
use crate::timestamp::Timestamp;
use crate::{
AudioCallbackContext, AudioDevice, AudioDriver, AudioInput, AudioInputCallback,
AudioInputDevice, AudioOutput, AudioOutputCallback, AudioOutputDevice, AudioStreamHandle,
Channel, DeviceType, StreamConfig,
};
use crate::audio_buffer::{AudioMut, AudioRef};
use crate::channel_map::{Bitset, ChannelMap32};
use crate::timestamp::Timestamp;

#[derive(Debug, Error)]
#[error("ALSA error: ")]
Expand Down Expand Up @@ -236,7 +236,7 @@ impl<Callback: 'static + Send + AudioInputCallback> AlsaStream<Callback> {
let input = AudioInput { buffer, timestamp };
callback.on_input_data(context, input);
timestamp += frames as u64;

match device.pcm.state() {
pcm::State::Suspended => {
if hwp.can_resume() {
Expand Down
19 changes: 15 additions & 4 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use crate::{AudioDevice, AudioDriver, AudioInputDevice, AudioOutputCallback, AudioOutputDevice, DeviceType};
use crate::{
AudioDevice, AudioDriver, AudioInputDevice, AudioOutputCallback, AudioOutputDevice, DeviceType,
};

#[cfg(os_alsa)]
pub mod alsa;

#[cfg(os_wasapi)]
pub mod wasapi;

pub fn default_driver() -> impl AudioDriver {
#[cfg(os_alsa)]
alsa::AlsaDriver
return alsa::AlsaDriver;
#[cfg(os_wasapi)]
return wasapi::WasapiDriver;
}

pub fn default_input_device_from<Driver: AudioDriver>(driver: &Driver) -> Driver::Device
Expand All @@ -24,7 +31,9 @@ where

pub fn default_input_device() -> impl AudioInputDevice {
#[cfg(os_alsa)]
default_input_device_from(&alsa::AlsaDriver)
return default_input_device_from(&alsa::AlsaDriver);
#[cfg(os_wasapi)]
return default_input_device_from(&wasapi::WasapiDriver);
}

pub fn default_output_device_from<Driver: AudioDriver>(driver: &Driver) -> Driver::Device
Expand All @@ -40,5 +49,7 @@ where

pub fn default_output_device() -> impl AudioOutputDevice {
#[cfg(os_alsa)]
default_output_device_from(&alsa::AlsaDriver)
return default_output_device_from(&alsa::AlsaDriver);
#[cfg(os_wasapi)]
return default_output_device_from(&wasapi::WasapiDriver);
}
Loading