cpal/lib.rs
1//! # How to use cpal
2//!
3//! Here are some concepts cpal exposes:
4//!
5//! - A [`Host`] provides access to the available audio devices on the system.
6//! Some platforms have more than one host available, but every platform supported by CPAL has at
7//! least one [default_host] that is guaranteed to be available.
8//! - A [`Device`] is an audio device that may have any number of input and
9//! output streams.
10//! - A [`Stream`] is an open flow of audio data. Input streams allow you to
11//! receive audio data, output streams allow you to play audio data. You must choose which
12//! [Device] will run your stream before you can create one. Often, a default device can be
13//! retrieved via the [Host].
14//!
15//! The first step is to initialise the [`Host`]:
16//!
17//! ```
18//! use cpal::traits::HostTrait;
19//! let host = cpal::default_host();
20//! ```
21//!
22//! Then choose an available [`Device`]. The easiest way is to use the default input or output
23//! `Device` via the [`default_input_device()`] or [`default_output_device()`] methods on `host`.
24//!
25//! Alternatively, you can enumerate all the available devices with the [`devices()`] method.
26//! Beware that the `default_*_device()` functions return an `Option<Device>` in case no device
27//! is available for that stream type on the system.
28//!
29//! ```no_run
30//! # use cpal::traits::HostTrait;
31//! # let host = cpal::default_host();
32//! let device = host.default_output_device().expect("no output device available");
33//! ```
34//!
35//! Before we can create a stream, we must decide what the configuration of the audio stream is
36//! going to be.
37//! You can query all the supported configurations with the
38//! [`supported_input_configs()`] and [`supported_output_configs()`] methods.
39//! These produce a list of [`SupportedStreamConfigRange`] structs which can later be turned into
40//! actual [`SupportedStreamConfig`] structs.
41//!
42//! If you don't want to query the list of configs,
43//! you can also build your own [`StreamConfig`] manually, but doing so could lead to an error when
44//! building the stream if the config is not supported by the device.
45//!
46//! > **Note**: the `supported_input/output_configs()` methods
47//! > could return an error for example if the device has been disconnected.
48//!
49//! ```no_run
50//! use cpal::traits::{DeviceTrait, HostTrait};
51//! # let host = cpal::default_host();
52//! # let device = host.default_output_device().unwrap();
53//! let mut supported_configs_range = device.supported_output_configs()
54//! .expect("error while querying configs");
55//! let supported_config = supported_configs_range.next()
56//! .expect("no supported config?!")
57//! .with_max_sample_rate();
58//! ```
59//!
60//! Now that we have everything for the stream, we are ready to create it from our selected device:
61//!
62//! ```no_run
63//! use cpal::Data;
64//! use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
65//! # let host = cpal::default_host();
66//! # let device = host.default_output_device().unwrap();
67//! # let config = device.default_output_config().unwrap().into();
68//! let stream = device.build_output_stream(
69//! &config,
70//! move |data: &mut [f32], _: &cpal::OutputCallbackInfo| {
71//! // react to stream events and read or write stream data here.
72//! },
73//! move |err| {
74//! // react to errors here.
75//! },
76//! None // None=blocking, Some(Duration)=timeout
77//! );
78//! ```
79//!
80//! While the stream is running, the selected audio device will periodically call the data callback
81//! that was passed to the function. For input streams, the callback receives `&`[`Data`] containing
82//! captured audio samples. For output streams, the callback receives `&mut`[`Data`] to be filled
83//! with audio samples for playback.
84//!
85//! > **Note**: Creating and running a stream will *not* block the thread. On modern platforms, the
86//! > given callback is called by a dedicated, high-priority thread responsible for delivering
87//! > audio data to the system's audio device in a timely manner. On older platforms that only
88//! > provide a blocking API (e.g. ALSA), CPAL will create a thread in order to consistently
89//! > provide non-blocking behaviour (currently this is a thread per stream, but this may change to
90//! > use a single thread for all streams). *If this is an issue for your platform or design,
91//! > please share your issue and use-case with the CPAL team on the GitHub issue tracker for
92//! > consideration.*
93//!
94//! In this example, we simply fill the given output buffer with silence.
95//!
96//! ```no_run
97//! use cpal::{Data, Sample, SampleFormat, FromSample};
98//! use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
99//! # let host = cpal::default_host();
100//! # let device = host.default_output_device().unwrap();
101//! # let supported_config = device.default_output_config().unwrap();
102//! let err_fn = |err| eprintln!("an error occurred on the output audio stream: {}", err);
103//! let sample_format = supported_config.sample_format();
104//! let config = supported_config.into();
105//! let stream = match sample_format {
106//! SampleFormat::F32 => device.build_output_stream(&config, write_silence::<f32>, err_fn, None),
107//! SampleFormat::I16 => device.build_output_stream(&config, write_silence::<i16>, err_fn, None),
108//! SampleFormat::U16 => device.build_output_stream(&config, write_silence::<u16>, err_fn, None),
109//! sample_format => panic!("Unsupported sample format '{sample_format}'")
110//! }.unwrap();
111//!
112//! fn write_silence<T: Sample>(data: &mut [T], _: &cpal::OutputCallbackInfo) {
113//! for sample in data.iter_mut() {
114//! *sample = Sample::EQUILIBRIUM;
115//! }
116//! }
117//! ```
118//!
119//! Not all platforms automatically run the stream upon creation. To ensure the stream has started,
120//! we can use [`Stream::play`](traits::StreamTrait::play).
121//!
122//! ```no_run
123//! # use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
124//! # let host = cpal::default_host();
125//! # let device = host.default_output_device().unwrap();
126//! # let supported_config = device.default_output_config().unwrap();
127//! # let sample_format = supported_config.sample_format();
128//! # let config = supported_config.into();
129//! # let data_fn = move |_data: &mut cpal::Data, _: &cpal::OutputCallbackInfo| {};
130//! # let err_fn = move |_err| {};
131//! # let stream = device.build_output_stream_raw(&config, sample_format, data_fn, err_fn, None).unwrap();
132//! stream.play().unwrap();
133//! ```
134//!
135//! Some devices support pausing the audio stream. This can be useful for saving energy in moments
136//! of silence.
137//!
138//! ```no_run
139//! # use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
140//! # let host = cpal::default_host();
141//! # let device = host.default_output_device().unwrap();
142//! # let supported_config = device.default_output_config().unwrap();
143//! # let sample_format = supported_config.sample_format();
144//! # let config = supported_config.into();
145//! # let data_fn = move |_data: &mut cpal::Data, _: &cpal::OutputCallbackInfo| {};
146//! # let err_fn = move |_err| {};
147//! # let stream = device.build_output_stream_raw(&config, sample_format, data_fn, err_fn, None).unwrap();
148//! stream.pause().unwrap();
149//! ```
150//!
151//! [`default_input_device()`]: traits::HostTrait::default_input_device
152//! [`default_output_device()`]: traits::HostTrait::default_output_device
153//! [`devices()`]: traits::HostTrait::devices
154//! [`supported_input_configs()`]: traits::DeviceTrait::supported_input_configs
155//! [`supported_output_configs()`]: traits::DeviceTrait::supported_output_configs
156
157#![cfg_attr(docsrs, feature(doc_cfg))]
158
159// Extern crate declarations with `#[macro_use]` must unfortunately be at crate root.
160#[cfg(all(
161 target_arch = "wasm32",
162 any(target_os = "emscripten", feature = "wasm-bindgen")
163))]
164extern crate js_sys;
165#[cfg(all(
166 target_arch = "wasm32",
167 any(target_os = "emscripten", feature = "wasm-bindgen")
168))]
169extern crate wasm_bindgen;
170#[cfg(all(
171 target_arch = "wasm32",
172 any(target_os = "emscripten", feature = "wasm-bindgen")
173))]
174extern crate web_sys;
175
176#[cfg(all(
177 target_arch = "wasm32",
178 any(target_os = "emscripten", feature = "wasm-bindgen")
179))]
180use wasm_bindgen::prelude::*;
181
182pub use device_description::{
183 DeviceDescription, DeviceDescriptionBuilder, DeviceDirection, DeviceType, InterfaceType,
184};
185pub use error::*;
186pub use platform::{
187 available_hosts, default_host, host_from_id, Device, Devices, Host, HostId, Stream,
188 SupportedInputConfigs, SupportedOutputConfigs, ALL_HOSTS,
189};
190pub use samples_formats::{FromSample, Sample, SampleFormat, SizedSample, I24, U24};
191use std::convert::TryInto;
192use std::time::Duration;
193
194pub mod device_description;
195mod error;
196mod host;
197pub mod platform;
198mod samples_formats;
199pub mod traits;
200
201/// Iterator of devices wrapped in a filter to only include certain device types
202pub type DevicesFiltered<I> = std::iter::Filter<I, fn(&<I as Iterator>::Item) -> bool>;
203
204/// A host's device iterator yielding only *input* devices.
205pub type InputDevices<I> = DevicesFiltered<I>;
206
207/// A host's device iterator yielding only *output* devices.
208pub type OutputDevices<I> = DevicesFiltered<I>;
209
210/// Number of channels.
211pub type ChannelCount = u16;
212
213/// The number of samples processed per second for a single channel of audio.
214pub type SampleRate = u32;
215
216/// A frame represents one sample for each channel. For example, with stereo audio,
217/// one frame contains two samples (left and right channels).
218pub type FrameCount = u32;
219
220/// A stable identifier for an audio device across all supported platforms.
221///
222/// Device IDs should remain stable across application restarts and can be serialized using `Display`/`FromStr`.
223///
224/// A device ID consists of a [`HostId`] identifying the audio backend and a device-specific identifier string.
225///
226/// # Example
227///
228/// ```no_run
229/// use cpal::traits::{HostTrait, DeviceTrait};
230/// use cpal::DeviceId;
231/// use std::str::FromStr;
232///
233/// let host = cpal::default_host();
234/// let device = host.default_output_device().unwrap();
235/// let device_id = device.id().unwrap();
236///
237/// // Serialize to string (e.g., for storage in config file)
238/// let id_string = device_id.to_string();
239/// println!("Device ID: {}", id_string); // e.g., "wasapi:device_identifier"
240///
241/// // Deserialize from string
242/// match DeviceId::from_str(&id_string) {
243/// Ok(parsed_id) => {
244/// // Retrieve the device by its ID
245/// if let Some(device) = host.device_by_id(&parsed_id) {
246/// println!("Found device: {:?}", device.id());
247/// }
248/// }
249/// Err(e) => eprintln!("Failed to parse device ID: {}", e),
250/// }
251/// ```
252#[derive(Clone, Debug, PartialEq, Eq, Hash)]
253pub struct DeviceId(pub crate::platform::HostId, pub String);
254
255impl std::fmt::Display for DeviceId {
256 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
257 write!(f, "{}:{}", self.0, self.1)
258 }
259}
260
261impl std::str::FromStr for DeviceId {
262 type Err = DeviceIdError;
263
264 fn from_str(s: &str) -> Result<Self, Self::Err> {
265 let (host_str, device_str) = s.split_once(':').ok_or(DeviceIdError::BackendSpecific {
266 err: BackendSpecificError {
267 description: format!(
268 "Failed to parse device ID from: {s}\nCheck if format matches \"host:device_id\""
269 ),
270 },
271 })?;
272
273 let host_id = crate::platform::HostId::from_str(host_str)
274 .map_err(|_| DeviceIdError::UnsupportedPlatform)?;
275
276 Ok(DeviceId(host_id, device_str.to_string()))
277 }
278}
279
280/// The buffer size requests the callback size for audio streams.
281///
282/// This controls the approximate size of the audio buffer passed to your callback.
283/// The actual callback size depends on the host/platform implementation and hardware
284/// constraints, and may differ from or vary around the requested size.
285///
286/// ## Callback Size Expectations
287///
288/// When you specify [`BufferSize::Fixed(x)`], you are **requesting** that callbacks
289/// receive approximately `x` frames of audio data. However, **no guarantees can be
290/// made** about the actual callback size:
291///
292/// - The host may round to hardware-supported values
293/// - Different devices have different constraints
294/// - The callback size may vary between calls (especially on mobile platforms)
295/// - The actual size might be larger or smaller than requested
296///
297/// ## Latency Considerations
298///
299/// [`BufferSize::Default`] uses the host's default buffer size, which may be
300/// surprisingly large, leading to higher latency. If low latency is desired,
301/// [`BufferSize::Fixed`] should be used with a small value in accordance with
302/// the [`SupportedBufferSize`] range from [`SupportedStreamConfig`].
303///
304/// Smaller buffer sizes reduce latency but may increase CPU usage and risk audio
305/// dropouts if the callback cannot process audio quickly enough.
306///
307/// # Example
308///
309/// ```no_run
310/// use cpal::traits::{DeviceTrait, HostTrait};
311/// use cpal::{BufferSize, SupportedBufferSize};
312///
313/// let host = cpal::default_host();
314/// let device = host.default_output_device().unwrap();
315/// let config = device.default_output_config().unwrap();
316///
317/// // Check supported buffer size range
318/// match config.buffer_size() {
319/// SupportedBufferSize::Range { min, max } => {
320/// println!("Buffer size range: {} - {}", min, max);
321/// // Request a small buffer for low latency
322/// let mut stream_config = config.config();
323/// stream_config.buffer_size = BufferSize::Fixed(256);
324/// }
325/// SupportedBufferSize::Unknown => {
326/// // Platform doesn't expose buffer size control
327/// println!("Buffer size cannot be queried on this platform");
328/// }
329/// }
330/// ```
331///
332/// [`BufferSize::Default`]: BufferSize::Default
333/// [`BufferSize::Fixed`]: BufferSize::Fixed
334/// [`BufferSize::Fixed(x)`]: BufferSize::Fixed
335/// [`SupportedBufferSize`]: SupportedStreamConfig::buffer_size
336/// [`SupportedStreamConfig`]: SupportedStreamConfig
337#[derive(Clone, Copy, Debug, Eq, PartialEq)]
338pub enum BufferSize {
339 Default,
340 Fixed(FrameCount),
341}
342
343#[cfg(all(
344 target_arch = "wasm32",
345 any(target_os = "emscripten", feature = "wasm-bindgen")
346))]
347impl wasm_bindgen::describe::WasmDescribe for BufferSize {
348 fn describe() {
349 <Option<FrameCount> as wasm_bindgen::describe::WasmDescribe>::describe();
350 }
351}
352
353#[cfg(all(
354 target_arch = "wasm32",
355 any(target_os = "emscripten", feature = "wasm-bindgen")
356))]
357impl wasm_bindgen::convert::IntoWasmAbi for BufferSize {
358 type Abi = <Option<FrameCount> as wasm_bindgen::convert::IntoWasmAbi>::Abi;
359
360 fn into_abi(self) -> Self::Abi {
361 match self {
362 Self::Default => None,
363 Self::Fixed(fc) => Some(fc),
364 }
365 .into_abi()
366 }
367}
368
369#[cfg(all(
370 target_arch = "wasm32",
371 any(target_os = "emscripten", feature = "wasm-bindgen")
372))]
373impl wasm_bindgen::convert::FromWasmAbi for BufferSize {
374 type Abi = <Option<FrameCount> as wasm_bindgen::convert::FromWasmAbi>::Abi;
375
376 unsafe fn from_abi(js: Self::Abi) -> Self {
377 match Option::<FrameCount>::from_abi(js) {
378 None => Self::Default,
379 Some(fc) => Self::Fixed(fc),
380 }
381 }
382}
383
384/// The set of parameters used to describe how to open a stream.
385///
386/// The sample format is omitted in favour of using a sample type.
387///
388/// See also [`BufferSize`] for details on buffer size behavior and latency considerations.
389#[cfg_attr(
390 all(
391 target_arch = "wasm32",
392 any(target_os = "emscripten", feature = "wasm-bindgen")
393 ),
394 wasm_bindgen
395)]
396#[derive(Clone, Debug, Eq, PartialEq)]
397pub struct StreamConfig {
398 pub channels: ChannelCount,
399 pub sample_rate: SampleRate,
400 pub buffer_size: BufferSize,
401}
402
403/// Describes the minimum and maximum supported buffer size for the device
404#[derive(Clone, Copy, Debug, Eq, PartialEq)]
405pub enum SupportedBufferSize {
406 Range {
407 min: FrameCount,
408 max: FrameCount,
409 },
410 /// In the case that the platform provides no way of getting the default
411 /// buffer size before starting a stream.
412 Unknown,
413}
414
415/// Describes a range of supported stream configurations, retrieved via the
416/// [`Device::supported_input/output_configs`](traits::DeviceTrait#required-methods) method.
417#[derive(Debug, Clone, Copy, PartialEq, Eq)]
418pub struct SupportedStreamConfigRange {
419 pub(crate) channels: ChannelCount,
420 /// Minimum value for the sample rate of the supported formats.
421 pub(crate) min_sample_rate: SampleRate,
422 /// Maximum value for the sample rate of the supported formats.
423 pub(crate) max_sample_rate: SampleRate,
424 /// Buffer size ranges supported by the device
425 pub(crate) buffer_size: SupportedBufferSize,
426 /// Type of data expected by the device.
427 pub(crate) sample_format: SampleFormat,
428}
429
430/// Common iterator types used by backend implementations.
431///
432/// All backends use these same concrete iterator types for supported stream configurations.
433#[allow(dead_code)]
434pub(crate) mod iter {
435 use super::SupportedStreamConfigRange;
436
437 /// Iterator type for supported input stream configurations.
438 ///
439 /// This is the iterator type returned by all backend implementations of
440 /// [`DeviceTrait::supported_input_configs`](crate::traits::DeviceTrait::supported_input_configs).
441 pub type SupportedInputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>;
442
443 /// Iterator type for supported output stream configurations.
444 ///
445 /// This is the iterator type returned by all backend implementations of
446 /// [`DeviceTrait::supported_output_configs`](crate::traits::DeviceTrait::supported_output_configs).
447 pub type SupportedOutputConfigs = std::vec::IntoIter<SupportedStreamConfigRange>;
448}
449
450/// Describes a single supported stream configuration, retrieved via either a
451/// [`SupportedStreamConfigRange`] instance or one of the
452/// [`Device::default_input/output_config`](traits::DeviceTrait#required-methods) methods.
453#[derive(Debug, Clone, PartialEq, Eq)]
454pub struct SupportedStreamConfig {
455 channels: ChannelCount,
456 sample_rate: SampleRate,
457 buffer_size: SupportedBufferSize,
458 sample_format: SampleFormat,
459}
460
461/// A buffer of dynamically typed audio data, passed to raw stream callbacks.
462///
463/// Raw input stream callbacks receive `&Data`, while raw output stream callbacks expect `&mut Data`.
464#[cfg_attr(target_os = "emscripten", wasm_bindgen)]
465#[derive(Debug)]
466pub struct Data {
467 data: *mut (),
468 len: usize,
469 sample_format: SampleFormat,
470}
471
472/// A monotonic time instance associated with a stream, retrieved from either:
473///
474/// 1. A timestamp provided to the stream's underlying audio data callback or
475/// 2. The same time source used to generate timestamps for a stream's underlying audio data
476/// callback.
477///
478/// `StreamInstant` represents a duration since an unspecified origin point. The origin
479/// is guaranteed to occur at or before the stream starts, and remains consistent for the
480/// lifetime of that stream. Different streams may have different origins.
481///
482/// ## Host `StreamInstant` Sources
483///
484/// | Host | Source |
485/// | ---- | ------ |
486/// | alsa | `snd_pcm_status_get_htstamp` |
487/// | coreaudio | `mach_absolute_time` |
488/// | wasapi | `QueryPerformanceCounter` |
489/// | asio | `timeGetTime` |
490/// | emscripten | `AudioContext.getOutputTimestamp` |
491#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
492pub struct StreamInstant {
493 secs: i64,
494 nanos: u32,
495}
496
497/// A timestamp associated with a call to an input stream's data callback.
498#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
499pub struct InputStreamTimestamp {
500 /// The instant the stream's data callback was invoked.
501 pub callback: StreamInstant,
502 /// The instant that data was captured from the device.
503 ///
504 /// E.g. The instant data was read from an ADC.
505 pub capture: StreamInstant,
506}
507
508/// A timestamp associated with a call to an output stream's data callback.
509#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
510pub struct OutputStreamTimestamp {
511 /// The instant the stream's data callback was invoked.
512 pub callback: StreamInstant,
513 /// The predicted instant that data written will be delivered to the device for playback.
514 ///
515 /// E.g. The instant data will be played by a DAC.
516 pub playback: StreamInstant,
517}
518
519/// Information relevant to a single call to the user's input stream data callback.
520#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
521pub struct InputCallbackInfo {
522 timestamp: InputStreamTimestamp,
523}
524
525/// Information relevant to a single call to the user's output stream data callback.
526#[cfg_attr(target_os = "emscripten", wasm_bindgen)]
527#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
528pub struct OutputCallbackInfo {
529 timestamp: OutputStreamTimestamp,
530}
531
532impl SupportedStreamConfig {
533 pub fn new(
534 channels: ChannelCount,
535 sample_rate: SampleRate,
536 buffer_size: SupportedBufferSize,
537 sample_format: SampleFormat,
538 ) -> Self {
539 Self {
540 channels,
541 sample_rate,
542 buffer_size,
543 sample_format,
544 }
545 }
546
547 pub fn channels(&self) -> ChannelCount {
548 self.channels
549 }
550
551 pub fn sample_rate(&self) -> SampleRate {
552 self.sample_rate
553 }
554
555 pub fn buffer_size(&self) -> &SupportedBufferSize {
556 &self.buffer_size
557 }
558
559 pub fn sample_format(&self) -> SampleFormat {
560 self.sample_format
561 }
562
563 pub fn config(&self) -> StreamConfig {
564 StreamConfig {
565 channels: self.channels,
566 sample_rate: self.sample_rate,
567 buffer_size: BufferSize::Default,
568 }
569 }
570}
571
572impl StreamInstant {
573 /// The amount of time elapsed from another instant to this one.
574 ///
575 /// Returns `None` if `earlier` is later than self.
576 pub fn duration_since(&self, earlier: &Self) -> Option<Duration> {
577 if self < earlier {
578 None
579 } else {
580 (self.as_nanos() - earlier.as_nanos())
581 .try_into()
582 .ok()
583 .map(Duration::from_nanos)
584 }
585 }
586
587 /// Returns the instant in time after the given duration has passed.
588 ///
589 /// Returns `None` if the resulting instant would exceed the bounds of the underlying data
590 /// structure.
591 pub fn add(&self, duration: Duration) -> Option<Self> {
592 self.as_nanos()
593 .checked_add(duration.as_nanos() as i128)
594 .and_then(Self::from_nanos_i128)
595 }
596
597 /// Returns the instant in time one `duration` ago.
598 ///
599 /// Returns `None` if the resulting instant would underflow. As a result, it is important to
600 /// consider that on some platforms the [`StreamInstant`] may begin at `0` from the moment the
601 /// source stream is created.
602 pub fn sub(&self, duration: Duration) -> Option<Self> {
603 self.as_nanos()
604 .checked_sub(duration.as_nanos() as i128)
605 .and_then(Self::from_nanos_i128)
606 }
607
608 fn as_nanos(&self) -> i128 {
609 (self.secs as i128 * 1_000_000_000) + self.nanos as i128
610 }
611
612 #[allow(dead_code)]
613 fn from_nanos(nanos: i64) -> Self {
614 let secs = nanos / 1_000_000_000;
615 let subsec_nanos = nanos - secs * 1_000_000_000;
616 Self::new(secs, subsec_nanos as u32)
617 }
618
619 #[allow(dead_code)]
620 fn from_nanos_i128(nanos: i128) -> Option<Self> {
621 let secs = nanos / 1_000_000_000;
622 if secs > i64::MAX as i128 || secs < i64::MIN as i128 {
623 None
624 } else {
625 let subsec_nanos = nanos - secs * 1_000_000_000;
626 debug_assert!(subsec_nanos < u32::MAX as i128);
627 Some(Self::new(secs as i64, subsec_nanos as u32))
628 }
629 }
630
631 #[allow(dead_code)]
632 fn from_secs_f64(secs: f64) -> crate::StreamInstant {
633 let s = secs.floor() as i64;
634 let ns = ((secs - s as f64) * 1_000_000_000.0) as u32;
635 Self::new(s, ns)
636 }
637
638 pub fn new(secs: i64, nanos: u32) -> Self {
639 StreamInstant { secs, nanos }
640 }
641}
642
643impl InputCallbackInfo {
644 pub fn new(timestamp: InputStreamTimestamp) -> Self {
645 Self { timestamp }
646 }
647
648 /// The timestamp associated with the call to an input stream's data callback.
649 pub fn timestamp(&self) -> InputStreamTimestamp {
650 self.timestamp
651 }
652}
653
654impl OutputCallbackInfo {
655 pub fn new(timestamp: OutputStreamTimestamp) -> Self {
656 Self { timestamp }
657 }
658
659 /// The timestamp associated with the call to an output stream's data callback.
660 pub fn timestamp(&self) -> OutputStreamTimestamp {
661 self.timestamp
662 }
663}
664
665// Note: Data does not implement `is_empty()` because it always contains a valid audio buffer
666// by design. The buffer may contain silence, but it is never structurally empty.
667#[allow(clippy::len_without_is_empty)]
668impl Data {
669 /// Constructor for host implementations to use.
670 ///
671 /// # Safety
672 /// The following requirements must be met in order for the safety of `Data`'s API.
673 /// - The `data` pointer must point to the first sample in the slice containing all samples.
674 /// - The `len` must describe the length of the buffer as a number of samples in the expected
675 /// format specified via the `sample_format` argument.
676 /// - The `sample_format` must correctly represent the underlying sample data delivered/expected
677 /// by the stream.
678 pub unsafe fn from_parts(data: *mut (), len: usize, sample_format: SampleFormat) -> Self {
679 Data {
680 data,
681 len,
682 sample_format,
683 }
684 }
685
686 /// The sample format of the internal audio data.
687 pub fn sample_format(&self) -> SampleFormat {
688 self.sample_format
689 }
690
691 /// The full length of the buffer in samples.
692 ///
693 /// The returned length is the same length as the slice of type `T` that would be returned via
694 /// [`as_slice`](Self::as_slice) given a sample type that matches the inner sample format.
695 pub fn len(&self) -> usize {
696 self.len
697 }
698
699 /// The raw slice of memory representing the underlying audio data as a slice of bytes.
700 ///
701 /// It is up to the user to interpret the slice of memory based on [`Data::sample_format`].
702 pub fn bytes(&self) -> &[u8] {
703 let len = self.len * self.sample_format.sample_size();
704 // The safety of this block relies on correct construction of the `Data` instance.
705 // See the unsafe `from_parts` constructor for these requirements.
706 unsafe { std::slice::from_raw_parts(self.data as *const u8, len) }
707 }
708
709 /// The raw slice of memory representing the underlying audio data as a slice of bytes.
710 ///
711 /// It is up to the user to interpret the slice of memory based on [`Data::sample_format`].
712 pub fn bytes_mut(&mut self) -> &mut [u8] {
713 let len = self.len * self.sample_format.sample_size();
714 // The safety of this block relies on correct construction of the `Data` instance. See
715 // the unsafe `from_parts` constructor for these requirements.
716 unsafe { std::slice::from_raw_parts_mut(self.data as *mut u8, len) }
717 }
718
719 /// Access the data as a slice of sample type `T`.
720 ///
721 /// Returns `None` if the sample type does not match the expected sample format.
722 pub fn as_slice<T>(&self) -> Option<&[T]>
723 where
724 T: SizedSample,
725 {
726 if T::FORMAT == self.sample_format {
727 // The safety of this block relies on correct construction of the `Data` instance. See
728 // the unsafe `from_parts` constructor for these requirements.
729 unsafe { Some(std::slice::from_raw_parts(self.data as *const T, self.len)) }
730 } else {
731 None
732 }
733 }
734
735 /// Access the data as a slice of sample type `T`.
736 ///
737 /// Returns `None` if the sample type does not match the expected sample format.
738 pub fn as_slice_mut<T>(&mut self) -> Option<&mut [T]>
739 where
740 T: SizedSample,
741 {
742 if T::FORMAT == self.sample_format {
743 // The safety of this block relies on correct construction of the `Data` instance. See
744 // the unsafe `from_parts` constructor for these requirements.
745 unsafe {
746 Some(std::slice::from_raw_parts_mut(
747 self.data as *mut T,
748 self.len,
749 ))
750 }
751 } else {
752 None
753 }
754 }
755}
756
757impl SupportedStreamConfigRange {
758 pub fn new(
759 channels: ChannelCount,
760 min_sample_rate: SampleRate,
761 max_sample_rate: SampleRate,
762 buffer_size: SupportedBufferSize,
763 sample_format: SampleFormat,
764 ) -> Self {
765 Self {
766 channels,
767 min_sample_rate,
768 max_sample_rate,
769 buffer_size,
770 sample_format,
771 }
772 }
773
774 pub fn channels(&self) -> ChannelCount {
775 self.channels
776 }
777
778 pub fn min_sample_rate(&self) -> SampleRate {
779 self.min_sample_rate
780 }
781
782 pub fn max_sample_rate(&self) -> SampleRate {
783 self.max_sample_rate
784 }
785
786 pub fn buffer_size(&self) -> &SupportedBufferSize {
787 &self.buffer_size
788 }
789
790 pub fn sample_format(&self) -> SampleFormat {
791 self.sample_format
792 }
793
794 /// Retrieve a [`SupportedStreamConfig`] with the given sample rate and buffer size.
795 ///
796 /// # Panics
797 ///
798 /// Panics if the given `sample_rate` is outside the range specified within
799 /// this [`SupportedStreamConfigRange`] instance. For a non-panicking
800 /// variant, use [`try_with_sample_rate`](#method.try_with_sample_rate).
801 pub fn with_sample_rate(self, sample_rate: SampleRate) -> SupportedStreamConfig {
802 self.try_with_sample_rate(sample_rate)
803 .expect("sample rate out of range")
804 }
805
806 /// Retrieve a [`SupportedStreamConfig`] with the given sample rate and buffer size.
807 ///
808 /// Returns `None` if the given sample rate is outside the range specified
809 /// within this [`SupportedStreamConfigRange`] instance.
810 pub fn try_with_sample_rate(self, sample_rate: SampleRate) -> Option<SupportedStreamConfig> {
811 if self.min_sample_rate <= sample_rate && sample_rate <= self.max_sample_rate {
812 Some(SupportedStreamConfig {
813 channels: self.channels,
814 sample_rate,
815 sample_format: self.sample_format,
816 buffer_size: self.buffer_size,
817 })
818 } else {
819 None
820 }
821 }
822
823 /// Turns this [`SupportedStreamConfigRange`] into a [`SupportedStreamConfig`] corresponding to the maximum sample rate.
824 #[inline]
825 pub fn with_max_sample_rate(self) -> SupportedStreamConfig {
826 SupportedStreamConfig {
827 channels: self.channels,
828 sample_rate: self.max_sample_rate,
829 sample_format: self.sample_format,
830 buffer_size: self.buffer_size,
831 }
832 }
833
834 /// A comparison function which compares two [`SupportedStreamConfigRange`]s in terms of their priority of
835 /// use as a default stream format.
836 ///
837 /// Some backends do not provide a default stream format for their audio devices. In these
838 /// cases, CPAL attempts to decide on a reasonable default format for the user. To do this we
839 /// use the "greatest" of all supported stream formats when compared with this method.
840 ///
841 /// SupportedStreamConfigs are prioritised by the following heuristics:
842 ///
843 /// **Channels**:
844 ///
845 /// - Stereo
846 /// - Mono
847 /// - Max available channels
848 ///
849 /// **Sample format**:
850 /// - f32
851 /// - i16
852 /// - u16
853 ///
854 /// **Sample rate**:
855 ///
856 /// - 44100 (cd quality)
857 /// - Max sample rate
858 pub fn cmp_default_heuristics(&self, other: &Self) -> std::cmp::Ordering {
859 use std::cmp::Ordering::Equal;
860 use SampleFormat::{F32, I16, I24, I32, U16, U24, U32};
861
862 let cmp_stereo = (self.channels == 2).cmp(&(other.channels == 2));
863 if cmp_stereo != Equal {
864 return cmp_stereo;
865 }
866
867 let cmp_mono = (self.channels == 1).cmp(&(other.channels == 1));
868 if cmp_mono != Equal {
869 return cmp_mono;
870 }
871
872 let cmp_channels = self.channels.cmp(&other.channels);
873 if cmp_channels != Equal {
874 return cmp_channels;
875 }
876
877 let cmp_f32 = (self.sample_format == F32).cmp(&(other.sample_format == F32));
878 if cmp_f32 != Equal {
879 return cmp_f32;
880 }
881
882 let cmp_i32 = (self.sample_format == I32).cmp(&(other.sample_format == I32));
883 if cmp_i32 != Equal {
884 return cmp_i32;
885 }
886
887 let cmp_u32 = (self.sample_format == U32).cmp(&(other.sample_format == U32));
888 if cmp_u32 != Equal {
889 return cmp_u32;
890 }
891
892 let cmp_i24 = (self.sample_format == I24).cmp(&(other.sample_format == I24));
893 if cmp_i24 != Equal {
894 return cmp_i24;
895 }
896
897 let cmp_u24 = (self.sample_format == U24).cmp(&(other.sample_format == U24));
898 if cmp_u24 != Equal {
899 return cmp_u24;
900 }
901
902 let cmp_i16 = (self.sample_format == I16).cmp(&(other.sample_format == I16));
903 if cmp_i16 != Equal {
904 return cmp_i16;
905 }
906
907 let cmp_u16 = (self.sample_format == U16).cmp(&(other.sample_format == U16));
908 if cmp_u16 != Equal {
909 return cmp_u16;
910 }
911
912 const HZ_44100: SampleRate = 44_100;
913 let r44100_in_self = self.min_sample_rate <= HZ_44100 && HZ_44100 <= self.max_sample_rate;
914 let r44100_in_other =
915 other.min_sample_rate <= HZ_44100 && HZ_44100 <= other.max_sample_rate;
916 let cmp_r44100 = r44100_in_self.cmp(&r44100_in_other);
917 if cmp_r44100 != Equal {
918 return cmp_r44100;
919 }
920
921 self.max_sample_rate.cmp(&other.max_sample_rate)
922 }
923}
924
925#[test]
926fn test_cmp_default_heuristics() {
927 let mut formats = [
928 SupportedStreamConfigRange {
929 buffer_size: SupportedBufferSize::Range { min: 256, max: 512 },
930 channels: 2,
931 min_sample_rate: 1,
932 max_sample_rate: 96000,
933 sample_format: SampleFormat::F32,
934 },
935 SupportedStreamConfigRange {
936 buffer_size: SupportedBufferSize::Range { min: 256, max: 512 },
937 channels: 1,
938 min_sample_rate: 1,
939 max_sample_rate: 96000,
940 sample_format: SampleFormat::F32,
941 },
942 SupportedStreamConfigRange {
943 buffer_size: SupportedBufferSize::Range { min: 256, max: 512 },
944 channels: 2,
945 min_sample_rate: 1,
946 max_sample_rate: 96000,
947 sample_format: SampleFormat::I16,
948 },
949 SupportedStreamConfigRange {
950 buffer_size: SupportedBufferSize::Range { min: 256, max: 512 },
951 channels: 2,
952 min_sample_rate: 1,
953 max_sample_rate: 96000,
954 sample_format: SampleFormat::U16,
955 },
956 SupportedStreamConfigRange {
957 buffer_size: SupportedBufferSize::Range { min: 256, max: 512 },
958 channels: 2,
959 min_sample_rate: 1,
960 max_sample_rate: 22050,
961 sample_format: SampleFormat::F32,
962 },
963 ];
964
965 formats.sort_by(|a, b| a.cmp_default_heuristics(b));
966
967 // lowest-priority first:
968 assert_eq!(formats[0].sample_format(), SampleFormat::F32);
969 assert_eq!(formats[0].min_sample_rate(), 1);
970 assert_eq!(formats[0].max_sample_rate(), 96000);
971 assert_eq!(formats[0].channels(), 1);
972
973 assert_eq!(formats[1].sample_format(), SampleFormat::U16);
974 assert_eq!(formats[1].min_sample_rate(), 1);
975 assert_eq!(formats[1].max_sample_rate(), 96000);
976 assert_eq!(formats[1].channels(), 2);
977
978 assert_eq!(formats[2].sample_format(), SampleFormat::I16);
979 assert_eq!(formats[2].min_sample_rate(), 1);
980 assert_eq!(formats[2].max_sample_rate(), 96000);
981 assert_eq!(formats[2].channels(), 2);
982
983 assert_eq!(formats[3].sample_format(), SampleFormat::F32);
984 assert_eq!(formats[3].min_sample_rate(), 1);
985 assert_eq!(formats[3].max_sample_rate(), 22050);
986 assert_eq!(formats[3].channels(), 2);
987
988 assert_eq!(formats[4].sample_format(), SampleFormat::F32);
989 assert_eq!(formats[4].min_sample_rate(), 1);
990 assert_eq!(formats[4].max_sample_rate(), 96000);
991 assert_eq!(formats[4].channels(), 2);
992}
993
994impl From<SupportedStreamConfig> for StreamConfig {
995 fn from(conf: SupportedStreamConfig) -> Self {
996 conf.config()
997 }
998}
999
1000// If a backend does not provide an API for retrieving supported formats, we query it with a bunch
1001// of commonly used rates. This is always the case for WASAPI and is sometimes the case for ALSA.
1002#[allow(dead_code)]
1003pub(crate) const COMMON_SAMPLE_RATES: &[SampleRate] = &[
1004 5512, 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000,
1005 176400, 192000, 352800, 384000, 705600, 768000, 1411200, 1536000,
1006];
1007
1008#[test]
1009fn test_stream_instant() {
1010 let a = StreamInstant::new(2, 0);
1011 let b = StreamInstant::new(-2, 0);
1012 let min = StreamInstant::new(i64::MIN, 0);
1013 let max = StreamInstant::new(i64::MAX, 0);
1014 assert_eq!(
1015 a.sub(Duration::from_secs(1)),
1016 Some(StreamInstant::new(1, 0))
1017 );
1018 assert_eq!(
1019 a.sub(Duration::from_secs(2)),
1020 Some(StreamInstant::new(0, 0))
1021 );
1022 assert_eq!(
1023 a.sub(Duration::from_secs(3)),
1024 Some(StreamInstant::new(-1, 0))
1025 );
1026 assert_eq!(min.sub(Duration::from_secs(1)), None);
1027 assert_eq!(
1028 b.add(Duration::from_secs(1)),
1029 Some(StreamInstant::new(-1, 0))
1030 );
1031 assert_eq!(
1032 b.add(Duration::from_secs(2)),
1033 Some(StreamInstant::new(0, 0))
1034 );
1035 assert_eq!(
1036 b.add(Duration::from_secs(3)),
1037 Some(StreamInstant::new(1, 0))
1038 );
1039 assert_eq!(max.add(Duration::from_secs(1)), None);
1040}