diff --git a/audio-core/src/sample.rs b/audio-core/src/sample.rs index 16ea18e..aa6b872 100644 --- a/audio-core/src/sample.rs +++ b/audio-core/src/sample.rs @@ -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; } @@ -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 Sample for [u8; N] { + const ZERO: Self = [0; N]; +} diff --git a/audio/src/tests/byte_arrays.rs b/audio/src/tests/byte_arrays.rs new file mode 100644 index 0000000..d1fe5c6 --- /dev/null +++ b/audio/src/tests/byte_arrays.rs @@ -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]); +} diff --git a/audio/src/tests/mod.rs b/audio/src/tests/mod.rs index 38af571..f45f7b6 100644 --- a/audio/src/tests/mod.rs +++ b/audio/src/tests/mod.rs @@ -1,3 +1,4 @@ +mod byte_arrays; mod copy_channel; mod dynamic; mod interleaved;