Skip to content

Commit

Permalink
Get default device with cpal
Browse files Browse the repository at this point in the history
  • Loading branch information
HEnquist committed Jan 28, 2021
1 parent 8a72fcd commit 3a576c5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ devices:
* `Stdin` (capture only)
* `Stdout` (playback only)
* `channels`: number of channels
* `device`: device name (for Alsa, Pulse, Wasapi, CoreAudio)
* `device`: device name (for Alsa, Pulse, Wasapi, CoreAudio). For CoreAudio and Wasapi, "default" will give the default device.
* `filename` path the the file (for File)
* `format`: sample format.

Expand Down
2 changes: 1 addition & 1 deletion devices.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Available formats:
- S16_LE
- S32_LE
```
The interesting fields are FORMAT, RATE and CHANNELS. In this example the sample formats this device can use are S16_LE and S32_LE (corresponding to S16LE and S32LE in CamillaDSP, see the table in the main README for all formats). The sample rate can be either 44,1, or 48 kHz. And it supports only stereo playback.
The interesting fields are FORMAT, RATE and CHANNELS. In this example the sample formats this device can use are S16_LE and S32_LE (corresponding to S16LE and S32LE in CamillaDSP, see the table in the main README for all formats). The sample rate can be either 44.1, or 48 kHz. And it supports only stereo playback.


Capture parameters are determined in the same way with `arecord`:
Expand Down
18 changes: 10 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -804,10 +804,11 @@ fn replace_tokens_in_config(config: &mut Configuration) {
let num_channels = config.devices.capture.channels();
//let mut new_config = config.clone();
for (_name, filter) in config.filters.iter_mut() {
if let Filter::Conv { parameters } = filter {
if let ConvParameters::File { filename, .. } = parameters {
*filename = replace_tokens(filename, samplerate, num_channels);
}
if let Filter::Conv {
parameters: ConvParameters::File { filename, .. },
} = filter
{
*filename = replace_tokens(filename, samplerate, num_channels);
}
}
for mut step in config.pipeline.iter_mut() {
Expand All @@ -829,10 +830,11 @@ fn replace_relative_paths_in_config(config: &mut Configuration, configname: &str
if let Ok(config_file) = PathBuf::from(configname.to_owned()).canonicalize() {
if let Some(config_dir) = config_file.parent() {
for (_name, filter) in config.filters.iter_mut() {
if let Filter::Conv { parameters } = filter {
if let ConvParameters::File { filename, .. } = parameters {
check_and_replace_relative_path(filename, config_dir);
}
if let Filter::Conv {
parameters: ConvParameters::File { filename, .. },
} = filter
{
check_and_replace_relative_path(filename, config_dir);
}
}
} else {
Expand Down
57 changes: 39 additions & 18 deletions src/cpaldevice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,25 @@ fn open_cpal_playback(
CpalHost::Wasapi => HostId::Wasapi,
};
let host = cpal::host_from_id(host_id)?;
let mut devices = host.devices()?;
let device = match devices.find(|dev| match dev.name() {
Ok(n) => n == devname,
_ => false,
}) {
Some(dev) => dev,
None => {
let msg = format!("Could not find device '{}'", devname);
return Err(ConfigError::new(&msg).into());
let device = if devname == "default" {
match host.default_output_device() {
Some(dev) => dev,
None => {
let msg = "Could not get default playback device".to_string();
return Err(ConfigError::new(&msg).into());
}
}
} else {
let mut devices = host.devices()?;
match devices.find(|dev| match dev.name() {
Ok(n) => n == devname,
_ => false,
}) {
Some(dev) => dev,
None => {
let msg = format!("Could not find playback device '{}'", devname);
return Err(ConfigError::new(&msg).into());
}
}
};
let cpal_format = match sample_format {
Expand Down Expand Up @@ -114,17 +124,28 @@ fn open_cpal_capture(
CpalHost::Wasapi => HostId::Wasapi,
};
let host = cpal::host_from_id(host_id)?;
let mut devices = host.devices()?;
let device = match devices.find(|dev| match dev.name() {
Ok(n) => n == devname,
_ => false,
}) {
Some(dev) => dev,
None => {
let msg = format!("Could not find device '{}'", devname);
return Err(ConfigError::new(&msg).into());
let device = if devname == "default" {
match host.default_input_device() {
Some(dev) => dev,
None => {
let msg = "Could not get default capture device".to_string();
return Err(ConfigError::new(&msg).into());
}
}
} else {
let mut devices = host.devices()?;
match devices.find(|dev| match dev.name() {
Ok(n) => n == devname,
_ => false,
}) {
Some(dev) => dev,
None => {
let msg = format!("Could not find capture device '{}'", devname);
return Err(ConfigError::new(&msg).into());
}
}
};

let cpal_format = match sample_format {
SampleFormat::S16LE => cpal::SampleFormat::I16,
SampleFormat::FLOAT32LE => cpal::SampleFormat::F32,
Expand Down

0 comments on commit 3a576c5

Please sign in to comment.