Skip to content

Commit

Permalink
Merge pull request #3 from SolarLiner/coreaudio
Browse files Browse the repository at this point in the history
feat(backends): coreaudio initial impl
  • Loading branch information
SolarLiner authored Nov 10, 2024
2 parents c2df47a + 2b9c513 commit a26b727
Show file tree
Hide file tree
Showing 18 changed files with 718 additions and 156 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ env:
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
147 changes: 146 additions & 1 deletion Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT"
duplicate = "1.0.0"
log = "0.4.22"
ndarray = "0.15.6"
oneshot = "0.1.8"
thiserror = "1.0.63"

[dev-dependencies]
Expand All @@ -22,6 +23,9 @@ 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(any(target_os = "macos", target_os = "ios"))'.dependencies]
coreaudio-rs = "0.12.0"

[[example]]
name = "enumerate_alsa"
path = "examples/enumerate_alsa.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_coreaudio: { any (target_os = "macos", target_os = "ios") }
}
}
5 changes: 2 additions & 3 deletions examples/enumerate_alsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ mod util;
fn main() -> Result<(), Box<dyn Error>> {
use crate::util::enumerate::enumerate_devices;
use interflow::backends::alsa::AlsaDriver;

env_logger::init();

enumerate_devices(AlsaDriver::default())
}

#[cfg(not(os_alsa))]
fn main() {
println!("ALSA driver is not available on this platform");
}

16 changes: 16 additions & 0 deletions examples/enumerate_coreaudio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::error::Error;

mod util;

#[cfg(os_coreaudio)]
fn main() -> Result<(), Box<dyn Error>> {
use crate::util::enumerate::enumerate_devices;
use interflow::backends::coreaudio::CoreAudioDriver;

enumerate_devices(CoreAudioDriver)
}

#[cfg(not(os_coreaudio))]
fn main() {
println!("CoreAudio is not available on this platform");
}
14 changes: 3 additions & 11 deletions examples/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@ use interflow::timestamp::Timestamp;

fn main() -> Result<()> {
env_logger::init();

let device = default_input_device();
let config = StreamConfig {
samplerate: 48000.,
channels: 0b1,
buffer_size_range: (Some(16384), None),
};
assert!(device.is_config_supported(&config));
let stream = device
.create_input_stream(config, RmsMeter::default())
.unwrap();
let stream = device.default_input_stream(RmsMeter::default()).unwrap();
println!("Press Enter to stop");
std::io::stdin().read_line(&mut String::new()).unwrap();
let meter = stream.eject().unwrap();
meter.progress.finish();
meter.progress.finish_and_clear();
Ok(())
}

Expand Down
22 changes: 9 additions & 13 deletions examples/sine_wave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,15 @@ use interflow::prelude::*;

fn main() -> Result<()> {
env_logger::init();

let device = default_output_device();
let config = StreamConfig {
samplerate: 48000.,
channels: 0b11,
buffer_size_range: (None, None),
};
assert!(device.is_config_supported(&config));
println!("Using device {}", device.name());
let stream = device.create_output_stream(
config,
SineWave {
let stream = device
.default_output_stream(SineWave {
frequency: 440.,
phase: 0.,
},
).unwrap();
})
.unwrap();
println!("Press Enter to stop");
std::io::stdin().read_line(&mut String::new())?;
stream.eject().unwrap();
Expand All @@ -35,7 +28,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
Loading

0 comments on commit a26b727

Please sign in to comment.