Update to a more general Device and Stream API. Add support for input streams (E.g. microphone). Add default format methods. (#201)
* Update to a more general Device and Stream API This update prepares for adding input stream support by removing the `Endpoint` type (which only supports output streams) in favour of a more general `Device` type which may support any number of input or output streams. Previously discussed at #117. The name `Voice` has been replaced with the more ubiquitous name `Stream`. See #118 for justification. Also introduces a new `StreamData` which is now passed to the `EventLoop::run` callback rather than the `UnknownTypeBuffer`. `StreamData` allows for passing either `Input` data to be read, or `Output` data to be written. The `beep.rs` example has been updated for the API changes. None of the backends have been updated for this API change yet. Backends will be updated in the following commits. Closes #117. Closes #118. * Update ALSA backend for new `Device` and `Stream` API. * Update wasapi backend for new `Device` and `Stream` API. * Update enumerate.rs example for new `Device` and `Stream` API. * Update coreaudio backend for new `Device` and `Stream` API. * Fix lib doc tests for Device and Stream API update * Update emscripten backend for new `Device` and `Stream` API. * Update null backend for new `Device` and `Stream` API. * Merge match exprs in beep.rs example * Add Input variant along with UnknownTypeInputBuffer and InputBuffer UnknownTypeBuffer and Buffer have been renamed to UnknownTypeOutputBuffer and OutputBuffer respectively. No backends have yet been updated for this name change or the addition of the InputBuffer. * Update null backend for introduction of InputBuffer * Update emscripten backend for introduction of InputBuffer * Make InputBuffer inner field an option to call finish in drop * Update alsa backend for introduction of InputBuffer * Update wasapi backend for introduction of InputBuffer * Update coreaudio backend for introduction of InputBuffer * Update enumerate.rs example to provide more detail about devices The enumerate.rs example now also displays: - Supported input stream formats. - Default input stream format. - Default output stream format. This should also be useful for testing the progress of #201. * Add `record_wav.rs` example for demonstrating input streams Records a ~3 second WAV file to `$CARGO_MANIFEST_DIR/recorded.wav` using the default input device and default input format. Uses hound 3.0 to create and write to the WAV file. This should also be useful for testing the input stream implementations for each different cpal backend. * Implement input stream support for coreaudio backend This implements the following for the coreaudio backend: - Device::supported_input_formats - Device::default_input_format - Device::default_output_format - EventLoop::build_input_stream The `enumerate.rs` and `record_wav.rs` examples now work successfully on macos. * Add `SupportedFormat::cmp_default_heuristics` method This adds a comparison function which compares two `SupportedFormat`s in terms of their priority of use as a default stream format. Some backends (such as ALSA) do not provide a default stream format for their audio devices. In these cases, CPAL attempts to decide on a reasonable default format for the user. To do this we use the "greatest" of all supported stream formats when compared with this method. * Implement input stream support for ALSA backend This implements the following for the ALSA backend: - Device::supported_input_formats - Device::default_input_format - Device::default_output_format - EventLoop::build_input_stream Note that ALSA itself does not give default stream formats for its devices. Thus the newly added `SupportedFormat::cmp_default_heuristics` method is used to determine the most suitable, supported stream format to use as the default. The `enumerate.rs` and `record_wav.rs` examples now work successfully on my linux machine. * Implement input stream support for wasapi backend This implements the following for the wasapi backend: - Device::supported_input_formats - Device::default_input_format - Device::default_output_format - EventLoop::build_input_stream Note that wasapi does not enumerate supported input/output stream formats for its devices. Instead, we query the `IsFormatSupported` method for supported formats ourselves. * Fix some warnings in the alsa backend * Update CHANGELOG for introduction of input streams and related items * Update README to show latest features supported by CPAL * Simplify beep example using Device::default_output_format * Remove old commented code from wasapi/stream.rs
This commit is contained in:
parent
b47e46a4ac
commit
c38bbb26e4
19 changed files with 3441 additions and 1891 deletions
|
|
@ -1,14 +1,11 @@
|
|||
use super::Endpoint;
|
||||
use super::Device;
|
||||
use super::alsa;
|
||||
use super::check_errors;
|
||||
use super::libc;
|
||||
|
||||
use std::ffi::CStr;
|
||||
use std::ffi::CString;
|
||||
use std::mem;
|
||||
|
||||
/// ALSA implementation for `EndpointsIterator`.
|
||||
pub struct EndpointsIterator {
|
||||
/// ALSA implementation for `Devices`.
|
||||
pub struct Devices {
|
||||
// we keep the original list so that we can pass it to the free function
|
||||
global_list: *const *const u8,
|
||||
|
||||
|
|
@ -16,12 +13,12 @@ pub struct EndpointsIterator {
|
|||
next_str: *const *const u8,
|
||||
}
|
||||
|
||||
unsafe impl Send for EndpointsIterator {
|
||||
unsafe impl Send for Devices {
|
||||
}
|
||||
unsafe impl Sync for EndpointsIterator {
|
||||
unsafe impl Sync for Devices {
|
||||
}
|
||||
|
||||
impl Drop for EndpointsIterator {
|
||||
impl Drop for Devices {
|
||||
#[inline]
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
|
|
@ -30,8 +27,8 @@ impl Drop for EndpointsIterator {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for EndpointsIterator {
|
||||
fn default() -> EndpointsIterator {
|
||||
impl Default for Devices {
|
||||
fn default() -> Devices {
|
||||
unsafe {
|
||||
let mut hints = mem::uninitialized();
|
||||
// TODO: check in which situation this can fail
|
||||
|
|
@ -40,7 +37,7 @@ impl Default for EndpointsIterator {
|
|||
|
||||
let hints = hints as *const *const u8;
|
||||
|
||||
EndpointsIterator {
|
||||
Devices {
|
||||
global_list: hints,
|
||||
next_str: hints,
|
||||
}
|
||||
|
|
@ -48,10 +45,10 @@ impl Default for EndpointsIterator {
|
|||
}
|
||||
}
|
||||
|
||||
impl Iterator for EndpointsIterator {
|
||||
type Item = Endpoint;
|
||||
impl Iterator for Devices {
|
||||
type Item = Device;
|
||||
|
||||
fn next(&mut self) -> Option<Endpoint> {
|
||||
fn next(&mut self) -> Option<Device> {
|
||||
loop {
|
||||
unsafe {
|
||||
if (*self.next_str).is_null() {
|
||||
|
|
@ -62,10 +59,9 @@ impl Iterator for EndpointsIterator {
|
|||
let n_ptr = alsa::snd_device_name_get_hint(*self.next_str as *const _,
|
||||
b"NAME\0".as_ptr() as *const _);
|
||||
if !n_ptr.is_null() {
|
||||
let n = CStr::from_ptr(n_ptr).to_bytes().to_vec();
|
||||
let n = String::from_utf8(n).unwrap();
|
||||
libc::free(n_ptr as *mut _);
|
||||
Some(n)
|
||||
let bytes = CString::from_raw(n_ptr).into_bytes();
|
||||
let string = String::from_utf8(bytes).unwrap();
|
||||
Some(string)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
@ -75,10 +71,9 @@ impl Iterator for EndpointsIterator {
|
|||
let n_ptr = alsa::snd_device_name_get_hint(*self.next_str as *const _,
|
||||
b"IOID\0".as_ptr() as *const _);
|
||||
if !n_ptr.is_null() {
|
||||
let n = CStr::from_ptr(n_ptr).to_bytes().to_vec();
|
||||
let n = String::from_utf8(n).unwrap();
|
||||
libc::free(n_ptr as *mut _);
|
||||
Some(n)
|
||||
let bytes = CString::from_raw(n_ptr).into_bytes();
|
||||
let string = String::from_utf8(bytes).unwrap();
|
||||
Some(string)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
|
@ -92,24 +87,46 @@ impl Iterator for EndpointsIterator {
|
|||
}
|
||||
}
|
||||
|
||||
if let Some(name) = name {
|
||||
// trying to open the PCM device to see if it can be opened
|
||||
let name_zeroed = CString::new(name.clone()).unwrap();
|
||||
let mut playback_handle = mem::uninitialized();
|
||||
if alsa::snd_pcm_open(&mut playback_handle,
|
||||
name_zeroed.as_ptr() as *const _,
|
||||
alsa::SND_PCM_STREAM_PLAYBACK,
|
||||
alsa::SND_PCM_NONBLOCK) == 0
|
||||
{
|
||||
alsa::snd_pcm_close(playback_handle);
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
let name = match name {
|
||||
Some(name) => {
|
||||
// Ignoring the `null` device.
|
||||
if name == "null" {
|
||||
continue;
|
||||
}
|
||||
name
|
||||
},
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
// ignoring the `null` device
|
||||
if name != "null" {
|
||||
return Some(Endpoint(name));
|
||||
}
|
||||
// trying to open the PCM device to see if it can be opened
|
||||
let name_zeroed = CString::new(&name[..]).unwrap();
|
||||
|
||||
// See if the device has an available output stream.
|
||||
let mut playback_handle = mem::uninitialized();
|
||||
let has_available_output = alsa::snd_pcm_open(
|
||||
&mut playback_handle,
|
||||
name_zeroed.as_ptr() as *const _,
|
||||
alsa::SND_PCM_STREAM_PLAYBACK,
|
||||
alsa::SND_PCM_NONBLOCK,
|
||||
) == 0;
|
||||
if has_available_output {
|
||||
alsa::snd_pcm_close(playback_handle);
|
||||
}
|
||||
|
||||
// See if the device has an available input stream.
|
||||
let mut capture_handle = mem::uninitialized();
|
||||
let has_available_input = alsa::snd_pcm_open(
|
||||
&mut capture_handle,
|
||||
name_zeroed.as_ptr() as *const _,
|
||||
alsa::SND_PCM_STREAM_CAPTURE,
|
||||
alsa::SND_PCM_NONBLOCK,
|
||||
) == 0;
|
||||
if has_available_input {
|
||||
alsa::snd_pcm_close(capture_handle);
|
||||
}
|
||||
|
||||
if has_available_output || has_available_input {
|
||||
return Some(Device(name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -117,6 +134,11 @@ impl Iterator for EndpointsIterator {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn default_endpoint() -> Option<Endpoint> {
|
||||
Some(Endpoint("default".to_owned()))
|
||||
pub fn default_input_device() -> Option<Device> {
|
||||
Some(Device("default".to_owned()))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn default_output_device() -> Option<Device> {
|
||||
Some(Device("default".to_owned()))
|
||||
}
|
||||
|
|
|
|||
899
src/alsa/mod.rs
899
src/alsa/mod.rs
File diff suppressed because it is too large
Load diff
Loading…
Add table
Add a link
Reference in a new issue