2019-10-13 10:36:49 +00:00
|
|
|
extern crate anyhow;
|
2014-12-11 15:28:26 +00:00
|
|
|
extern crate cpal;
|
2016-09-30 16:18:28 +00:00
|
|
|
|
2020-01-13 14:27:41 +00:00
|
|
|
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
[WIP] Introduce a `Host` API
This is an implementation of the API described at #204. Please see that
issue for more details on the motivation.
-----
A **Host** provides access to the available audio devices on the system.
Some platforms have more than one host available, e.g.
wasapi/asio/dsound on windows, alsa/pulse/jack on linux and so on. As a
result, some audio devices are only available on certain hosts, while
others are only available on other hosts. Every platform supported by
CPAL has at least one **DefaultHost** that is guaranteed to be available
(alsa, wasapi and coreaudio). Currently, the default hosts are the only
hosts supported by CPAL, however this will change as of landing #221 (cc
@freesig). These changes should also accommodate support for other hosts
such as jack #250 (cc @derekdreery) and pulseaudio (cc @knappador) #259.
This introduces a suite of traits allowing for both compile time and
runtime dispatch of different hosts and their uniquely associated device
and event loop types.
A new private **host** module has been added containing the individual
host implementations, each in their own submodule gated to the platforms
on which they are available.
A new **platform** module has been added containing platform-specific
items, including a dynamically dispatched host type that allows for
easily switching between hosts at runtime.
The **ALL_HOSTS** slice contains a **HostId** for each host supported on
the current platform. The **available_hosts** function produces a
**HostId** for each host that is currently *available* on the platform.
The **host_from_id** function allows for initialising a host from its
associated ID, failing with a **HostUnavailable** error. The
**default_host** function returns the default host and should never
fail.
Please see the examples for a demonstration of the change in usage. For
the most part, things look the same at the surface level, however the
role of device enumeration and creating the event loop have been moved
from global functions to host methods. The enumerate.rs example has been
updated to enumerate all devices for each host, not just the default.
**TODO**
- [x] Add the new **Host** API
- [x] Update examples for the new API.
- [x] ALSA host
- [ ] WASAPI host
- [ ] CoreAudio host
- [ ] Emscripten host **Follow-up PR**
- [ ] ASIO host #221
cc @ishitatsuyuki more to review for you if you're interested, but it
might be easier after #288 lands and this gets rebased.
2019-06-23 13:49:48 +00:00
|
|
|
|
2019-10-13 10:36:49 +00:00
|
|
|
fn main() -> Result<(), anyhow::Error> {
|
[WIP] Introduce a `Host` API
This is an implementation of the API described at #204. Please see that
issue for more details on the motivation.
-----
A **Host** provides access to the available audio devices on the system.
Some platforms have more than one host available, e.g.
wasapi/asio/dsound on windows, alsa/pulse/jack on linux and so on. As a
result, some audio devices are only available on certain hosts, while
others are only available on other hosts. Every platform supported by
CPAL has at least one **DefaultHost** that is guaranteed to be available
(alsa, wasapi and coreaudio). Currently, the default hosts are the only
hosts supported by CPAL, however this will change as of landing #221 (cc
@freesig). These changes should also accommodate support for other hosts
such as jack #250 (cc @derekdreery) and pulseaudio (cc @knappador) #259.
This introduces a suite of traits allowing for both compile time and
runtime dispatch of different hosts and their uniquely associated device
and event loop types.
A new private **host** module has been added containing the individual
host implementations, each in their own submodule gated to the platforms
on which they are available.
A new **platform** module has been added containing platform-specific
items, including a dynamically dispatched host type that allows for
easily switching between hosts at runtime.
The **ALL_HOSTS** slice contains a **HostId** for each host supported on
the current platform. The **available_hosts** function produces a
**HostId** for each host that is currently *available* on the platform.
The **host_from_id** function allows for initialising a host from its
associated ID, failing with a **HostUnavailable** error. The
**default_host** function returns the default host and should never
fail.
Please see the examples for a demonstration of the change in usage. For
the most part, things look the same at the surface level, however the
role of device enumeration and creating the event loop have been moved
from global functions to host methods. The enumerate.rs example has been
updated to enumerate all devices for each host, not just the default.
**TODO**
- [x] Add the new **Host** API
- [x] Update examples for the new API.
- [x] ALSA host
- [ ] WASAPI host
- [ ] CoreAudio host
- [ ] Emscripten host **Follow-up PR**
- [ ] ASIO host #221
cc @ishitatsuyuki more to review for you if you're interested, but it
might be easier after #288 lands and this gets rebased.
2019-06-23 13:49:48 +00:00
|
|
|
let host = cpal::default_host();
|
2020-01-13 14:27:41 +00:00
|
|
|
let device = host
|
|
|
|
.default_output_device()
|
|
|
|
.expect("failed to find a default output device");
|
2020-01-27 20:28:07 +00:00
|
|
|
let config = device.default_output_config()?;
|
2020-05-21 07:41:23 +00:00
|
|
|
println!("{:#?}", &config);
|
2020-02-02 17:28:38 +00:00
|
|
|
match config.sample_format() {
|
2020-01-27 20:28:07 +00:00
|
|
|
cpal::SampleFormat::F32 => run::<f32>(&device, &config.into())?,
|
|
|
|
cpal::SampleFormat::I16 => run::<i16>(&device, &config.into())?,
|
|
|
|
cpal::SampleFormat::U16 => run::<u16>(&device, &config.into())?,
|
2020-01-22 06:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-01-27 20:28:07 +00:00
|
|
|
fn run<T>(device: &cpal::Device, config: &cpal::StreamConfig) -> Result<(), anyhow::Error>
|
2020-01-22 06:36:02 +00:00
|
|
|
where
|
|
|
|
T: cpal::Sample,
|
|
|
|
{
|
2020-01-27 20:28:07 +00:00
|
|
|
let sample_rate = config.sample_rate.0 as f32;
|
|
|
|
let channels = config.channels as usize;
|
2017-10-20 15:12:01 +00:00
|
|
|
|
|
|
|
// Produce a sinusoid of maximum amplitude.
|
2020-01-13 14:27:41 +00:00
|
|
|
let mut sample_clock = 0f32;
|
2019-07-09 06:47:33 +00:00
|
|
|
let mut next_value = move || {
|
2018-02-04 12:02:16 +00:00
|
|
|
sample_clock = (sample_clock + 1.0) % sample_rate;
|
|
|
|
(sample_clock * 440.0 * 2.0 * 3.141592 / sample_rate).sin()
|
2017-10-20 15:12:01 +00:00
|
|
|
};
|
2014-12-11 16:51:58 +00:00
|
|
|
|
2020-01-19 14:06:19 +00:00
|
|
|
let err_fn = |err| eprintln!("an error occurred on stream: {}", err);
|
|
|
|
|
2020-01-22 06:36:02 +00:00
|
|
|
let stream = device.build_output_stream(
|
2020-01-27 20:28:07 +00:00
|
|
|
config,
|
2020-04-16 12:50:36 +00:00
|
|
|
move |data: &mut [T], _: &cpal::OutputCallbackInfo| {
|
|
|
|
write_data(data, channels, &mut next_value)
|
|
|
|
},
|
2020-01-22 06:36:02 +00:00
|
|
|
err_fn,
|
|
|
|
)?;
|
2019-07-09 06:47:33 +00:00
|
|
|
stream.play()?;
|
2019-07-09 06:47:33 +00:00
|
|
|
|
2019-07-09 06:47:33 +00:00
|
|
|
std::thread::sleep(std::time::Duration::from_millis(1000));
|
|
|
|
|
|
|
|
Ok(())
|
2014-12-11 15:28:26 +00:00
|
|
|
}
|
2020-01-13 14:27:41 +00:00
|
|
|
|
2020-01-22 06:01:49 +00:00
|
|
|
fn write_data<T>(output: &mut [T], channels: usize, next_sample: &mut dyn FnMut() -> f32)
|
2020-01-13 14:27:41 +00:00
|
|
|
where
|
|
|
|
T: cpal::Sample,
|
|
|
|
{
|
|
|
|
for frame in output.chunks_mut(channels) {
|
|
|
|
let value: T = cpal::Sample::from::<f32>(&next_sample());
|
|
|
|
for sample in frame.iter_mut() {
|
|
|
|
*sample = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|