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

Add support for OpenBSD platform via sndio host #493

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
16 changes: 16 additions & 0 deletions .github/workflows/cpal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ jobs:
run: sudo apt-get install libasound2-dev
- name: Install libjack
run: sudo apt-get install libjack-jackd2-dev libjack-jackd2-0
- name: Install sndio
run: |
sudo apt-get install build-essential &&
git clone https://caoua.org/git/sndio &&
cd sndio &&
./configure &&
make &&
sudo mkdir /var/run/sndiod &&
sudo useradd -r -g audio -s /sbin/nologin -d /var/run/sndiod sndiod &&
sudo make install &&
sudo ldconfig
- name: Install stable
uses: actions-rs/toolchain@v1
with:
Expand All @@ -99,6 +110,11 @@ jobs:
with:
command: test
args: --all --all-features --verbose
- name: Run with jack feature
uses: actions-rs/cargo@v1
with:
command: test
args: --all --features jack --verbose

linux-check-and-test-armv7:
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.DS_Store
recorded.wav
rls*.log
rusty-tags.*
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ keywords = ["audio", "sound"]

[features]
asio = ["asio-sys", "num-traits"] # Only available on Windows. See README for setup instructions.
linux-sndio = ["sndio-sys", "libc"] # sndio on Linux (normally this is only used on OpenBSD)

[dependencies]
thiserror = "1.0.2"
Expand All @@ -34,6 +35,14 @@ libc = "0.2.65"
parking_lot = "0.11"
jack = { version = "0.7.0", optional = true }

[target.'cfg(target_os = "linux")'.dependencies]
sndio-sys = { version = "0.0.*", optional = true }
libc = { version = "0.2.65", optional = true }

[target.'cfg(target_os = "openbsd")'.dependencies]
sndio-sys = "0.0.*"
libc = "0.2.65"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
core-foundation-sys = "0.8.2" # For linking to CoreFoundation.framework and handling device name `CFString`s.
mach = "0.3" # For access to mach_timebase type.
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Currently, supported hosts include:
- macOS (via CoreAudio)
- iOS (via CoreAudio)
- Android (via Oboe)
- OpenBSD (via sndio)
- Emscripten

Note that on Linux, the ALSA development files are required. These are provided
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ pub enum PauseStreamError {
}

/// Errors that might occur while a stream is running.
#[derive(Debug, Error)]
#[derive(Debug, Error, Clone)]
pub enum StreamError {
/// The device no longer exists. This can happen if the device is disconnected while the
/// program is running.
Expand Down
5 changes: 5 additions & 0 deletions src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ pub(crate) mod jack;
pub(crate) mod null;
#[cfg(target_os = "android")]
pub(crate) mod oboe;
#[cfg(any(
target_os = "openbsd",
all(target_os = "linux", feature = "linux-sndio")
))]
pub(crate) mod sndio;
#[cfg(windows)]
pub(crate) mod wasapi;
#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
Expand Down
70 changes: 70 additions & 0 deletions src/host/sndio/adapters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use crate::{Data, FrameCount, InputCallbackInfo, OutputCallbackInfo, Sample};
use samples_formats::TypeSampleFormat;

/// When given an input data callback that expects samples in the specified sample format, return
/// an input data callback that expects samples in the I16 sample format. The `buffer_size` is in
/// samples.
pub(super) fn input_adapter_callback<T, D>(
mut original_data_callback: D,
buffer_size: FrameCount,
) -> Box<dyn FnMut(&Data, &InputCallbackInfo) + Send + 'static>
where
D: FnMut(&Data, &InputCallbackInfo) + Send + 'static,
T: Sample + TypeSampleFormat + Copy + Send + Default + 'static,
{
let mut adapted_buf = vec![T::default(); buffer_size as usize];

Box::new(move |data: &Data, info: &InputCallbackInfo| {
let data_slice: &[i16] = data.as_slice().unwrap(); // unwrap OK because data is always i16
let adapted_slice = &mut adapted_buf;
assert_eq!(data_slice.len(), adapted_slice.len());
for (adapted_ref, data_element) in adapted_slice.iter_mut().zip(data_slice.iter()) {
*adapted_ref = T::from(data_element);
}

// Note: we construct adapted_data here instead of in the parent function because adapted_buf needs
// to be owned by the closure.
let adapted_data = unsafe { data_from_vec(&mut adapted_buf) };
original_data_callback(&adapted_data, info);
})
}

/// When given an output data callback that expects a place to write samples in the specified
/// sample format, return an output data callback that expects a place to write samples in the I16
/// sample format. The `buffer_size` is in samples.
pub(super) fn output_adapter_callback<T, D>(
mut original_data_callback: D,
buffer_size: FrameCount,
) -> Box<dyn FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static>
where
D: FnMut(&mut Data, &OutputCallbackInfo) + Send + 'static,
T: Sample + TypeSampleFormat + Copy + Send + Default + 'static,
{
let mut adapted_buf = vec![T::default(); buffer_size as usize];
Box::new(move |data: &mut Data, info: &OutputCallbackInfo| {
// Note: we construct adapted_data here instead of in the parent function because
// adapted_buf needs to be owned by the closure.
let mut adapted_data = unsafe { data_from_vec(&mut adapted_buf) };

// Populate adapted_buf / adapted_data.
original_data_callback(&mut adapted_data, info);

let data_slice: &mut [i16] = data.as_slice_mut().unwrap(); // unwrap OK because data is always i16
let adapted_slice = &adapted_buf;
assert_eq!(data_slice.len(), adapted_slice.len());
for (data_ref, adapted_element) in data_slice.iter_mut().zip(adapted_slice.iter()) {
*data_ref = adapted_element.to_i16();
}
})
}

unsafe fn data_from_vec<T>(adapted_buf: &mut Vec<T>) -> Data
where
T: TypeSampleFormat,
{
Data::from_parts(
adapted_buf.as_mut_ptr() as *mut _,
adapted_buf.len(), // TODO: this is converting a FrameCount to a number of samples; invalid for stereo!
T::sample_format(),
)
}
Loading