Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement Sample for byte arrays #35

Merged
merged 3 commits into from
Nov 6, 2023
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
7 changes: 6 additions & 1 deletion audio-core/src/sample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
///
/// Implementor must make sure that a bit-pattern of all-zeros is a legal
/// bit-pattern for the implemented type.
pub unsafe trait Sample: Copy + Default {
pub unsafe trait Sample: Copy {
/// The zero pattern for the sample.
const ZERO: Self;
}
Expand Down Expand Up @@ -69,3 +69,8 @@ impl_int!(i64);
impl_int!(i128);
impl_int!(usize);
impl_int!(isize);

// Implement for byte arrays of any length
unsafe impl<const N: usize> Sample for [u8; N] {
const ZERO: Self = [0; N];
}
11 changes: 11 additions & 0 deletions audio/src/tests/byte_arrays.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[test]
fn test_byte_array() {
let buf: crate::buf::Interleaved<[u8; 2]> =
crate::interleaved![[[1, 2], [3, 4]], [[5, 6], [7, 8]]];

assert_eq!(buf.channels(), 2);
assert_eq!(buf.sample(0, 0).unwrap(), [1, 2]);
assert_eq!(buf.sample(0, 1).unwrap(), [3, 4]);
assert_eq!(buf.sample(1, 0).unwrap(), [5, 6]);
assert_eq!(buf.sample(1, 1).unwrap(), [7, 8]);
}
1 change: 1 addition & 0 deletions audio/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod byte_arrays;
mod copy_channel;
mod dynamic;
mod interleaved;
Expand Down