Skip to content
Open
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
6 changes: 3 additions & 3 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,12 +407,12 @@ let device = host.device_by_id(&id);
```rust
// Before (v0.17)
for line in desc.extended() { // &[String]
println!("{}", line); // line: &String
println!("{line}"); // line: &String
}

// After (v0.18)
for line in desc.extended() { // impl Iterator<Item = &str>
println!("{}", line); // line: &str — Display, write!, format! all unchanged
println!("{line}"); // line: &str — Display, write!, format! all unchanged
}
```

Expand Down Expand Up @@ -540,7 +540,7 @@ let name = device.name()?;

// New: For user-facing display
let desc = device.description()?;
println!("Device: {}", desc); // or desc.name() for just the name
println!("Device: {desc}"); // or desc.name() for just the name

// New: For stable identification and persistence
let id = device.id()?;
Expand Down
2 changes: 1 addition & 1 deletion src/device_description.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl fmt::Display for DeviceDescription {
write!(f, "{}", self.name)?;

if let Some(mfr) = &self.manufacturer {
write!(f, " ({})", mfr)?;
write!(f, " ({mfr})")?;
}

if self.device_type != DeviceType::Unknown {
Expand Down
4 changes: 2 additions & 2 deletions src/host/alsa/enumerate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn format_device_description(phys_dev: &PhysicalDevice, prefix: &str) -> String
_ => "",
};

format!("{}\n{}", first_line, second_line)
format!("{first_line}\n{second_line}")
}

fn physical_devices() -> Vec<PhysicalDevice> {
Expand Down Expand Up @@ -133,7 +133,7 @@ fn physical_devices() -> Vec<PhysicalDevice> {
}
};

let device_name = device_name.unwrap_or_else(|| format!("Device {}", device_index));
let device_name = device_name.unwrap_or_else(|| format!("Device {device_index}"));
devices.push(PhysicalDevice {
card_index,
card_name: card_name.clone(),
Expand Down
5 changes: 2 additions & 3 deletions src/host/equilibrium.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ pub fn fill_equilibrium(buffer: &mut [u8], sample_format: SampleFormat) {
($sample_type:ty) => {{
let sample_size = std::mem::size_of::<$sample_type>();

debug_assert_eq!(
buffer.len() % sample_size,
0,
debug_assert!(
buffer.len() % sample_size == 0,
"Buffer size must be aligned to sample size for format {:?}",
sample_format
);
Expand Down
4 changes: 2 additions & 2 deletions src/host/jack/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Device {
connect_ports_automatically: bool,
start_server_automatically: bool,
) -> Result<Self, Error> {
let output_client_name = format!("{}_out", name);
let output_client_name = format!("{name}_out");
Device::new_device(
output_client_name,
connect_ports_automatically,
Expand All @@ -101,7 +101,7 @@ impl Device {
connect_ports_automatically: bool,
start_server_automatically: bool,
) -> Result<Self, Error> {
let input_client_name = format!("{}_in", name);
let input_client_name = format!("{name}_in");
Device::new_device(
input_client_name,
connect_ports_automatically,
Expand Down
4 changes: 2 additions & 2 deletions src/host/jack/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Stream {
let mut port_names: Vec<String> = vec![];
for i in 0..channels {
let port = client
.register_port(&format!("in_{}", i), jack::AudioIn::default())
.register_port(&format!("in_{i}"), jack::AudioIn::default())
.context(format!("Failed to register input port {i}"))?;
if let Ok(port_name) = port.name() {
port_names.push(port_name);
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Stream {
let mut port_names: Vec<String> = vec![];
for i in 0..channels {
let port = client
.register_port(&format!("out_{}", i), jack::AudioOut::default())
.register_port(&format!("out_{i}"), jack::AudioOut::default())
.context(format!("Failed to register output port {i}"))?;
if let Ok(port_name) = port.name() {
port_names.push(port_name);
Expand Down
10 changes: 7 additions & 3 deletions src/host/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,19 +249,23 @@ pub(crate) use error_emit::try_emit_error;
feature = "audioworklet",
))]
#[inline]
pub(crate) fn frames_to_duration(
pub(crate) const fn frames_to_duration(
frames: crate::FrameCount,
rate: crate::SampleRate,
) -> std::time::Duration {
if rate == 0 {
return std::time::Duration::ZERO;
}

let frames = frames as u64;
let rate = rate as u64;
let secs = frames as u64 / rate;

let secs = frames / rate;
// rem_frames < rate <= u32::MAX, so rem_frames * 1_000_000_000 < u64::MAX
let rem_frames = frames as u64 % rate;
let rem_frames = frames % rate;
// Round to nearest so the duration isn't biased.
let nanos = (rem_frames * 1_000_000_000 + rate / 2) / rate;

std::time::Duration::new(secs, nanos as u32)
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@
//! # let host = cpal::default_host();
//! # let device = host.default_output_device().unwrap();
//! # let supported_config = device.default_output_config().unwrap();
//! let err_fn = |err| eprintln!("an error occurred on the output audio stream: {}", err);
//! let err_fn = |err| eprintln!("an error occurred on the output audio stream: {err}");
//! let sample_format = supported_config.sample_format();
//! let config = supported_config.into();
//! let stream = match sample_format {
Expand Down Expand Up @@ -284,7 +284,7 @@ pub type FrameCount = u32;
///
/// // Serialize to string (e.g., for storage in config file)
/// let id_string = device_id.to_string();
/// println!("Device ID: {}", id_string); // e.g., "wasapi:device_identifier"
/// println!("Device ID: {id_string}"); // e.g., "wasapi:device_identifier"
///
/// // Deserialize from string
/// match DeviceId::from_str(&id_string) {
Expand All @@ -294,7 +294,7 @@ pub type FrameCount = u32;
/// println!("Found device: {:?}", device.id());
/// }
/// }
/// Err(e) => eprintln!("Failed to parse device ID: {}", e),
/// Err(e) => eprintln!("Failed to parse device ID: {e}"),
/// }
/// ```
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -400,7 +400,7 @@ impl std::str::FromStr for DeviceId {
/// // Check supported buffer size range
/// match config.buffer_size() {
/// SupportedBufferSize::Range { min, max } => {
/// println!("Buffer size range: {} - {}", min, max);
/// println!("Buffer size range: {min} - {max}");
/// // Request a small buffer for low latency
/// let mut stream_config = config.config();
/// stream_config.buffer_size = BufferSize::Fixed(256);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ macro_rules! impl_platform_host {
///
/// // Parse host string (may fail if host is not available on this platform)
/// if let Ok(host_id) = HostId::from_str(host_string) {
/// println!("Successfully parsed: {}", host_id);
/// println!("Successfully parsed: {host_id}");
/// }
/// }
/// ```
Expand Down
115 changes: 34 additions & 81 deletions src/sample_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ pub use dasp_sample::{FromSample, Sample};
))]
use wasm_bindgen::prelude::*;

// I48 and U48 are not currently supported by cpal but available in dasp_sample:
// pub use dasp_sample::{I48, U48};

/// Format that each sample has. Usually, this corresponds to the sampling
/// depth of the audio source. For example, 16 bit quantized samples can be
/// encoded in `i16` or `u16`. Note that the quantized sampling depth is not
Expand Down Expand Up @@ -78,8 +75,6 @@ pub enum SampleFormat {
/// `i32` with a valid range of `i32::MIN..=i32::MAX` with `0` being the origin.
I32,

// /// `I48` with a valid range of '-(1 << 47)..(1 << 47)' with `0` being the origin
// I48,
/// `i64` with a valid range of `i64::MIN..=i64::MAX` with `0` being the origin.
I64,

Expand All @@ -97,9 +92,6 @@ pub enum SampleFormat {
/// `u32` with a valid range of `u32::MIN..=u32::MAX` with `1 << 31` being the origin.
U32,

/// `U48` with a valid range of '0..(1 << 48)' with `1 << 47` being the origin
// U48,

/// `u64` with a valid range of `u64::MIN..=u64::MAX` with `1 << 63` being the origin.
U64,

Expand All @@ -125,8 +117,8 @@ impl SampleFormat {
/// sample format (e.g., i24 has size of i32).
#[inline]
#[must_use]
pub fn sample_size(&self) -> usize {
match *self {
pub const fn sample_size(self) -> usize {
match self {
SampleFormat::I8 => mem::size_of::<i8>(),
SampleFormat::U8 => mem::size_of::<u8>(),
SampleFormat::I16 => mem::size_of::<i16>(),
Expand All @@ -135,8 +127,6 @@ impl SampleFormat {
SampleFormat::U24 => mem::size_of::<i32>(),
SampleFormat::I32 => mem::size_of::<i32>(),
SampleFormat::U32 => mem::size_of::<u32>(),
// SampleFormat::I48 => mem::size_of::<i64>(),
// SampleFormat::U48 => mem::size_of::<i64>(),
SampleFormat::I64 => mem::size_of::<i64>(),
SampleFormat::U64 => mem::size_of::<u64>(),
SampleFormat::F32 => mem::size_of::<f32>(),
Expand All @@ -152,8 +142,8 @@ impl SampleFormat {
/// this sample format (e.g., I24 has size of i32 but 24 bits per sample).
#[inline]
#[must_use]
pub fn bits_per_sample(&self) -> u32 {
match *self {
pub const fn bits_per_sample(self) -> u32 {
match self {
SampleFormat::I8 => i8::BITS,
SampleFormat::U8 => u8::BITS,
SampleFormat::I16 => i16::BITS,
Expand All @@ -162,55 +152,51 @@ impl SampleFormat {
SampleFormat::U24 => 24,
SampleFormat::I32 => i32::BITS,
SampleFormat::U32 => u32::BITS,
// SampleFormat::I48 => 48,
// SampleFormat::U48 => 48,
SampleFormat::I64 => i64::BITS,
SampleFormat::U64 => u64::BITS,
SampleFormat::F32 => 32,
SampleFormat::F32 => 32, // f32/64::BITS is currently unstable, so we hardcode the values here.
SampleFormat::F64 => 64,
SampleFormat::DsdU8 | SampleFormat::DsdU16 | SampleFormat::DsdU32 => 1,
}
}

#[inline]
#[must_use]
pub fn is_int(&self) -> bool {
pub const fn is_int(self) -> bool {
matches!(
*self,
self,
SampleFormat::I8
| SampleFormat::I16
| SampleFormat::I24
| SampleFormat::I32
// | SampleFormat::I48
| SampleFormat::I64
)
}

#[inline]
#[must_use]
pub fn is_uint(&self) -> bool {
pub const fn is_uint(self) -> bool {
matches!(
*self,
self,
SampleFormat::U8
| SampleFormat::U16
| SampleFormat::U24
| SampleFormat::U32
// | SampleFormat::U48
| SampleFormat::U64
)
}

#[inline]
#[must_use]
pub fn is_float(&self) -> bool {
matches!(*self, SampleFormat::F32 | SampleFormat::F64)
pub const fn is_float(self) -> bool {
matches!(self, SampleFormat::F32 | SampleFormat::F64)
}

#[inline]
#[must_use]
pub fn is_dsd(&self) -> bool {
pub const fn is_dsd(self) -> bool {
matches!(
*self,
self,
SampleFormat::DsdU8 | SampleFormat::DsdU16 | SampleFormat::DsdU32
)
}
Expand All @@ -223,13 +209,11 @@ impl Display for SampleFormat {
SampleFormat::I16 => "i16",
SampleFormat::I24 => "i24",
SampleFormat::I32 => "i32",
// SampleFormat::I48 => "i48",
SampleFormat::I64 => "i64",
SampleFormat::U8 => "u8",
SampleFormat::U16 => "u16",
SampleFormat::U24 => "u24",
SampleFormat::U32 => "u32",
// SampleFormat::U48 => "u48",
SampleFormat::U64 => "u64",
SampleFormat::F32 => "f32",
SampleFormat::F64 => "f64",
Expand Down Expand Up @@ -264,58 +248,27 @@ pub trait SizedSample: Sample {
const FORMAT: SampleFormat;
}

impl SizedSample for i8 {
const FORMAT: SampleFormat = SampleFormat::I8;
}

impl SizedSample for i16 {
const FORMAT: SampleFormat = SampleFormat::I16;
}

impl SizedSample for I24 {
const FORMAT: SampleFormat = SampleFormat::I24;
}

impl SizedSample for i32 {
const FORMAT: SampleFormat = SampleFormat::I32;
}

// impl SizedSample for I48 {
// const FORMAT: SampleFormat = SampleFormat::I48;
// }

impl SizedSample for i64 {
const FORMAT: SampleFormat = SampleFormat::I64;
}

impl SizedSample for u8 {
const FORMAT: SampleFormat = SampleFormat::U8;
}

impl SizedSample for u16 {
const FORMAT: SampleFormat = SampleFormat::U16;
}

impl SizedSample for U24 {
const FORMAT: SampleFormat = SampleFormat::U24;
}

impl SizedSample for u32 {
const FORMAT: SampleFormat = SampleFormat::U32;
}

// impl SizedSample for U48 {
// const FORMAT: SampleFormat = SampleFormat::U48;
// }

impl SizedSample for u64 {
const FORMAT: SampleFormat = SampleFormat::U64;
}

impl SizedSample for f32 {
const FORMAT: SampleFormat = SampleFormat::F32;
macro_rules! impl_sized_sample {
($($sample_type:ty => $format:expr),* $(,)?) => {
$(
impl SizedSample for $sample_type {
const FORMAT: SampleFormat = $format;
}
)*
};
}

impl SizedSample for f64 {
const FORMAT: SampleFormat = SampleFormat::F64;
impl_sized_sample! {
i8 => SampleFormat::I8,
i16 => SampleFormat::I16,
I24 => SampleFormat::I24,
i32 => SampleFormat::I32,
i64 => SampleFormat::I64,
u8 => SampleFormat::U8,
u16 => SampleFormat::U16,
U24 => SampleFormat::U24,
u32 => SampleFormat::U32,
u64 => SampleFormat::U64,
f32 => SampleFormat::F32,
f64 => SampleFormat::F64,
}