Skip to content
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- **WASAPI**: `Send` and `Sync` implementations to `Stream`.
- **WebAudio**: `Send` and `Sync` implementations to `Stream`.
- **WebAudio**: `BufferSize::Fixed` validation against supported range.
- **ALSA**: Add support for native DSD playback.

### Changed

Expand Down
4 changes: 3 additions & 1 deletion examples/record_wav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ fn main() -> Result<(), anyhow::Error> {
}

fn sample_format(format: cpal::SampleFormat) -> hound::SampleFormat {
if format.is_float() {
if format.is_dsd() {
panic!("DSD formats cannot be written to WAV files");
} else if format.is_float() {
hound::SampleFormat::Float
} else {
hound::SampleFormat::Int
Expand Down
20 changes: 19 additions & 1 deletion src/host/alsa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ impl Device {
// Test both LE and BE formats to detect what the hardware actually supports.
// LE is listed first as it's the common case for most audio hardware.
// Hardware reports its supported formats regardless of CPU endianness.
const FORMATS: [(SampleFormat, alsa::pcm::Format); 18] = [
const FORMATS: [(SampleFormat, alsa::pcm::Format); 23] = [
(SampleFormat::I8, alsa::pcm::Format::S8),
(SampleFormat::U8, alsa::pcm::Format::U8),
(SampleFormat::I16, alsa::pcm::Format::S16LE),
Expand All @@ -501,6 +501,11 @@ impl Device {
(SampleFormat::F32, alsa::pcm::Format::FloatBE),
(SampleFormat::F64, alsa::pcm::Format::Float64LE),
(SampleFormat::F64, alsa::pcm::Format::Float64BE),
(SampleFormat::DsdU8, alsa::pcm::Format::DSDU8),
(SampleFormat::DsdU16, alsa::pcm::Format::DSDU16LE),
(SampleFormat::DsdU16, alsa::pcm::Format::DSDU16BE),
(SampleFormat::DsdU32, alsa::pcm::Format::DSDU32LE),
(SampleFormat::DsdU32, alsa::pcm::Format::DSDU32BE),
//SND_PCM_FORMAT_IEC958_SUBFRAME_LE,
//SND_PCM_FORMAT_IEC958_SUBFRAME_BE,
//SND_PCM_FORMAT_MU_LAW,
Expand Down Expand Up @@ -1268,6 +1273,7 @@ fn fill_with_equilibrium(buffer: &mut [u8], sample_format: SampleFormat) {
}
}};
}
const DSD_SILENCE_BYTE: u8 = 0x69;

match sample_format {
SampleFormat::I8 => fill_typed!(i8),
Expand All @@ -1284,6 +1290,9 @@ fn fill_with_equilibrium(buffer: &mut [u8], sample_format: SampleFormat) {
SampleFormat::U64 => fill_typed!(u64),
SampleFormat::F32 => fill_typed!(f32),
SampleFormat::F64 => fill_typed!(f64),
SampleFormat::DsdU8 | SampleFormat::DsdU16 | SampleFormat::DsdU32 => {
buffer.fill(DSD_SILENCE_BYTE)
}
}
}

Expand Down Expand Up @@ -1350,6 +1359,15 @@ fn sample_format_to_alsa_format(
SampleFormat::F64 => (Format::Float64LE, Format::Float64BE),
#[cfg(target_endian = "big")]
SampleFormat::F64 => (Format::Float64BE, Format::Float64LE),
SampleFormat::DsdU8 => return Ok(Format::DSDU8),
#[cfg(target_endian = "little")]
SampleFormat::DsdU16 => (Format::DSDU16LE, Format::DSDU16BE),
#[cfg(target_endian = "big")]
SampleFormat::DsdU16 => (Format::DSDU16BE, Format::DSDU16LE),
#[cfg(target_endian = "little")]
SampleFormat::DsdU32 => (Format::DSDU32LE, Format::DSDU32BE),
#[cfg(target_endian = "big")]
SampleFormat::DsdU32 => (Format::DSDU32BE, Format::DSDU32LE),
_ => {
return Err(BackendSpecificError {
description: format!("Sample format '{sample_format}' is not supported"),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@ impl From<SupportedStreamConfig> for StreamConfig {
#[allow(dead_code)]
pub(crate) const COMMON_SAMPLE_RATES: &[SampleRate] = &[
5512, 8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000,
176400, 192000, 352800, 384000,
176400, 192000, 352800, 384000, 705600, 768000, 1411200, 1536000,
];

#[test]
Expand Down
25 changes: 25 additions & 0 deletions src/samples_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ pub enum SampleFormat {

/// `f64` with a valid range of `-1.0..=1.0` with `0.0` being the origin.
F64,

/// DSD 1-bit stream in u8 container (8 bits = 8 DSD samples) with 0x69 being the silence byte pattern.
DsdU8,

/// DSD 1-bit stream in u16 container (16 bits = 16 DSD samples) with 0x69 being the silence byte pattern.
DsdU16,

/// DSD 1-bit stream in u32 container (32 bits = 32 DSD samples) with 0x69 being the silence byte pattern.
DsdU32,
}

impl SampleFormat {
Expand All @@ -129,6 +138,9 @@ impl SampleFormat {
SampleFormat::U64 => mem::size_of::<u64>(),
SampleFormat::F32 => mem::size_of::<f32>(),
SampleFormat::F64 => mem::size_of::<f64>(),
SampleFormat::DsdU8 => mem::size_of::<u8>(),
SampleFormat::DsdU16 => mem::size_of::<u16>(),
SampleFormat::DsdU32 => mem::size_of::<u32>(),
}
}

Expand All @@ -153,6 +165,7 @@ impl SampleFormat {
SampleFormat::U64 => u64::BITS,
SampleFormat::F32 => 32,
SampleFormat::F64 => 64,
SampleFormat::DsdU8 | SampleFormat::DsdU16 | SampleFormat::DsdU32 => 1,
}
}

Expand Down Expand Up @@ -189,6 +202,15 @@ impl SampleFormat {
pub fn is_float(&self) -> bool {
matches!(*self, SampleFormat::F32 | SampleFormat::F64)
}

#[inline]
#[must_use]
pub fn is_dsd(&self) -> bool {
matches!(
*self,
SampleFormat::DsdU8 | SampleFormat::DsdU16 | SampleFormat::DsdU32
)
}
}

impl Display for SampleFormat {
Expand All @@ -208,6 +230,9 @@ impl Display for SampleFormat {
SampleFormat::U64 => "u64",
SampleFormat::F32 => "f32",
SampleFormat::F64 => "f64",
SampleFormat::DsdU8 => "dsdu8",
SampleFormat::DsdU16 => "dsdu16",
SampleFormat::DsdU32 => "dsdu32",
}
.fmt(f)
}
Expand Down