Skip to content

Commit f7206e9

Browse files
authored
feat: add Sample::bits_per_sample() (#1008)
1 parent 4e547f5 commit f7206e9

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Unreleased
22

3-
- ASIO: Fix linker flags for MinGW cross-compilation.
3+
- Added `Sample::bits_per_sample` method.
44
- ALSA(process_output): Pass `silent=true` to `PCM.try_recover`, so it doesn't write to stderr.
55
- ALSA: Fix buffer and period size by selecting the closest supported values.
66
- ALSA: Change ALSA periods from 4 to 2.
77
- ALSA: Change card enumeration to work like `aplay -L` does.
8+
- ASIO: Fix linker flags for MinGW cross-compilation.
89
- CoreAudio: Change `Device::supported_configs` to return a single element containing the available sample rate range when all elements have the same `mMinimum` and `mMaximum` values.
910
- CoreAudio: Change default audio device detection to be lazy when building a stream, instead of during device enumeration.
1011
- iOS: Fix example by properly activating audio session.

src/samples_formats.rs

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ pub use dasp_sample::{FromSample, Sample, I24, I48, U24, U48};
66

77
/// Format that each sample has. Usually, this corresponds to the sampling
88
/// depth of the audio source. For example, 16 bit quantized samples can be
9-
/// encoded in `i16` or `u16`. Note that the sampling depth is not directly
10-
/// visible for formats where [`is_float`] is true.
9+
/// encoded in `i16` or `u16`. Note that the quantized sampling depth is not
10+
/// directly visible for formats where [`is_float`] is true.
1111
///
1212
/// Also note that the backend must support the encoding of the quantized
1313
/// samples in the given format, as there is no generic transformation from one
@@ -71,20 +71,47 @@ impl SampleFormat {
7171
#[must_use]
7272
pub fn sample_size(&self) -> usize {
7373
match *self {
74-
SampleFormat::I8 | SampleFormat::U8 => mem::size_of::<i8>(),
75-
SampleFormat::I16 | SampleFormat::U16 => mem::size_of::<i16>(),
74+
SampleFormat::I8 => mem::size_of::<i8>(),
75+
SampleFormat::U8 => mem::size_of::<u8>(),
76+
SampleFormat::I16 => mem::size_of::<i16>(),
77+
SampleFormat::U16 => mem::size_of::<u16>(),
7678
SampleFormat::I24 => mem::size_of::<i32>(),
7779
// SampleFormat::U24 => mem::size_of::<i32>(),
78-
SampleFormat::I32 | SampleFormat::U32 => mem::size_of::<i32>(),
79-
80+
SampleFormat::I32 => mem::size_of::<i32>(),
81+
SampleFormat::U32 => mem::size_of::<u32>(),
8082
// SampleFormat::I48 => mem::size_of::<i64>(),
8183
// SampleFormat::U48 => mem::size_of::<i64>(),
82-
SampleFormat::I64 | SampleFormat::U64 => mem::size_of::<i64>(),
84+
SampleFormat::I64 => mem::size_of::<i64>(),
85+
SampleFormat::U64 => mem::size_of::<u64>(),
8386
SampleFormat::F32 => mem::size_of::<f32>(),
8487
SampleFormat::F64 => mem::size_of::<f64>(),
8588
}
8689
}
8790

91+
/// Returns the number of bits of a sample of this format. Note that this is
92+
/// not necessarily the same as the size of the primitive used to represent
93+
/// this sample format (e.g., I24 has size of i32 but 24 bits per sample).
94+
#[inline]
95+
#[must_use]
96+
pub fn bits_per_sample(&self) -> u32 {
97+
match *self {
98+
SampleFormat::I8 => i8::BITS,
99+
SampleFormat::U8 => u8::BITS,
100+
SampleFormat::I16 => i16::BITS,
101+
SampleFormat::U16 => u16::BITS,
102+
SampleFormat::I24 => 24,
103+
// SampleFormat::U24 => 24,
104+
SampleFormat::I32 => i32::BITS,
105+
SampleFormat::U32 => u32::BITS,
106+
// SampleFormat::I48 => 48,
107+
// SampleFormat::U48 => 48,
108+
SampleFormat::I64 => i64::BITS,
109+
SampleFormat::U64 => u64::BITS,
110+
SampleFormat::F32 => 32,
111+
SampleFormat::F64 => 64,
112+
}
113+
}
114+
88115
#[inline]
89116
#[must_use]
90117
pub fn is_int(&self) -> bool {

0 commit comments

Comments
 (0)