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

Use alsa-rs for alsa support rather than alsa-sys #308

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
18 changes: 0 additions & 18 deletions .rustfmt.toml

This file was deleted.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repository = "https://github.com/tomaka/cpal"
documentation = "https://docs.rs/cpal"
license = "Apache-2.0"
keywords = ["audio", "sound"]
edition = "2018"

[features]
asio = ["asio-sys"] # Only available on Windows. See README for setup instructions.
Expand All @@ -24,12 +25,16 @@ winapi = { version = "0.3", features = ["audiosessiontypes", "audioclient", "com
asio-sys = { version = "0.1", path = "asio-sys", optional = true }

[target.'cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))'.dependencies]
alsa-sys = { version = "0.1", path = "alsa-sys" }
libc = "0.2"
alsa = "0.3.0"
nix = "0.14.1"

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
coreaudio-rs = { version = "0.9.1", default-features = false, features = ["audio_unit", "core_audio"] }
core-foundation-sys = "0.6.2" # For linking to CoreFoundation.framework and handling device name `CFString`s.

[target.'cfg(target_os = "emscripten")'.dependencies]
stdweb = { version = "0.1.3", default-features = false }

[patch.crates-io]
alsa = { path = "../alsa-rs" }
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::env;
const CPAL_ASIO_DIR: &'static str = "CPAL_ASIO_DIR";

fn main() {
// If ASIO directory isn't set silently return early
// If ASIO directory isn't set silently return early
// otherwise set the asio config flag
match env::var(CPAL_ASIO_DIR) {
Err(_) => return,
Ok(_) => println!("cargo:rustc-cfg=asio"),
};
}
}
22 changes: 15 additions & 7 deletions examples/beep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use cpal::traits::{DeviceTrait, EventLoopTrait, HostTrait};

fn main() -> Result<(), failure::Error> {
let host = cpal::default_host();
let device = host.default_output_device().expect("failed to find a default output device");
let device = host
.default_output_device()
.expect("failed to find a default output device");
let format = device.default_output_format()?;
let event_loop = host.event_loop();
let stream_id = event_loop.build_output_stream(&device, &format)?;
Expand All @@ -30,30 +32,36 @@ fn main() -> Result<(), failure::Error> {
};

match data {
cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::U16(mut buffer) } => {
cpal::StreamData::Output {
buffer: cpal::UnknownTypeOutputBuffer::U16(mut buffer),
} => {
for sample in buffer.chunks_mut(format.channels as usize) {
let value = ((next_value() * 0.5 + 0.5) * std::u16::MAX as f32) as u16;
for out in sample.iter_mut() {
*out = value;
}
}
},
cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::I16(mut buffer) } => {
}
cpal::StreamData::Output {
buffer: cpal::UnknownTypeOutputBuffer::I16(mut buffer),
} => {
for sample in buffer.chunks_mut(format.channels as usize) {
let value = (next_value() * std::i16::MAX as f32) as i16;
for out in sample.iter_mut() {
*out = value;
}
}
},
cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer) } => {
}
cpal::StreamData::Output {
buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer),
} => {
for sample in buffer.chunks_mut(format.channels as usize) {
let value = next_value();
for out in sample.iter_mut() {
*out = value;
}
}
},
}
_ => (),
}
});
Expand Down
18 changes: 14 additions & 4 deletions examples/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ fn main() -> Result<(), failure::Error> {
Err(e) => {
println!("Error: {:?}", e);
continue;
},
}
};
if input_formats.peek().is_some() {
println!(" All supported input stream formats:");
for (format_index, format) in input_formats.enumerate() {
println!(" {}.{}. {:?}", device_index + 1, format_index + 1, format);
println!(
" {}.{}. {:?}",
device_index + 1,
format_index + 1,
format
);
}
}

Expand All @@ -48,12 +53,17 @@ fn main() -> Result<(), failure::Error> {
Err(e) => {
println!("Error: {:?}", e);
continue;
},
}
};
if output_formats.peek().is_some() {
println!(" All supported output stream formats:");
for (format_index, format) in output_formats.enumerate() {
println!(" {}.{}. {:?}", device_index + 1, format_index + 1, format);
println!(
" {}.{}. {:?}",
device_index + 1,
format_index + 1,
format
);
}
}
}
Expand Down
27 changes: 19 additions & 8 deletions examples/feedback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ fn main() -> Result<(), failure::Error> {
let event_loop = host.event_loop();

// Default devices.
let input_device = host.default_input_device().expect("failed to get default input device");
let output_device = host.default_output_device().expect("failed to get default output device");
let input_device = host
.default_input_device()
.expect("failed to get default input device");
let output_device = host
.default_output_device()
.expect("failed to get default output device");
println!("Using default input device: \"{}\"", input_device.name()?);
println!("Using default output device: \"{}\"", output_device.name()?);

Expand All @@ -46,7 +50,10 @@ fn main() -> Result<(), failure::Error> {
}

// Play the streams.
println!("Starting the input and output streams with `{}` milliseconds of latency.", LATENCY_MS);
println!(
"Starting the input and output streams with `{}` milliseconds of latency.",
LATENCY_MS
);
event_loop.play_stream(input_stream_id.clone())?;
event_loop.play_stream(output_stream_id.clone())?;

Expand All @@ -62,7 +69,9 @@ fn main() -> Result<(), failure::Error> {
};

match data {
cpal::StreamData::Input { buffer: cpal::UnknownTypeInputBuffer::F32(buffer) } => {
cpal::StreamData::Input {
buffer: cpal::UnknownTypeInputBuffer::F32(buffer),
} => {
assert_eq!(id, input_stream_id);
let mut output_fell_behind = false;
for &sample in buffer.iter() {
Expand All @@ -73,8 +82,10 @@ fn main() -> Result<(), failure::Error> {
if output_fell_behind {
eprintln!("output stream fell behind: try increasing latency");
}
},
cpal::StreamData::Output { buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer) } => {
}
cpal::StreamData::Output {
buffer: cpal::UnknownTypeOutputBuffer::F32(mut buffer),
} => {
assert_eq!(id, output_stream_id);
let mut input_fell_behind = None;
for sample in buffer.iter_mut() {
Expand All @@ -83,13 +94,13 @@ fn main() -> Result<(), failure::Error> {
Err(err) => {
input_fell_behind = Some(err);
0.0
},
}
};
}
if let Some(err) = input_fell_behind {
eprintln!("input stream fell behind: {}: try increasing latency", err);
}
},
}
_ => panic!("we're expecting f32 data"),
}
});
Expand Down
26 changes: 18 additions & 8 deletions examples/record_wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ fn main() -> Result<(), failure::Error> {
let host = cpal::default_host();

// Setup the default input device and stream with the default input format.
let device = host.default_input_device().expect("Failed to get default input device");
let device = host
.default_input_device()
.expect("Failed to get default input device");
println!("Default input device: {}", device.name()?);
let format = device.default_input_format().expect("Failed to get default input format");
let format = device
.default_input_format()
.expect("Failed to get default input format");
println!("Default input format: {:?}", format);
let event_loop = host.event_loop();
let stream_id = event_loop.build_input_stream(&device, &format)?;
Expand Down Expand Up @@ -50,7 +54,9 @@ fn main() -> Result<(), failure::Error> {
}
// Otherwise write to the wav writer.
match data {
cpal::StreamData::Input { buffer: cpal::UnknownTypeInputBuffer::U16(buffer) } => {
cpal::StreamData::Input {
buffer: cpal::UnknownTypeInputBuffer::U16(buffer),
} => {
if let Ok(mut guard) = writer_2.try_lock() {
if let Some(writer) = guard.as_mut() {
for sample in buffer.iter() {
Expand All @@ -59,25 +65,29 @@ fn main() -> Result<(), failure::Error> {
}
}
}
},
cpal::StreamData::Input { buffer: cpal::UnknownTypeInputBuffer::I16(buffer) } => {
}
cpal::StreamData::Input {
buffer: cpal::UnknownTypeInputBuffer::I16(buffer),
} => {
if let Ok(mut guard) = writer_2.try_lock() {
if let Some(writer) = guard.as_mut() {
for &sample in buffer.iter() {
writer.write_sample(sample).ok();
}
}
}
},
cpal::StreamData::Input { buffer: cpal::UnknownTypeInputBuffer::F32(buffer) } => {
}
cpal::StreamData::Input {
buffer: cpal::UnknownTypeInputBuffer::F32(buffer),
} => {
if let Ok(mut guard) = writer_2.try_lock() {
if let Some(writer) = guard.as_mut() {
for &sample in buffer.iter() {
writer.write_sample(sample).ok();
}
}
}
},
}
_ => (),
}
});
Expand Down
Loading