From 933f3de71600d000013db6de3438c681443a85bd Mon Sep 17 00:00:00 2001 From: obsoleszenz Date: Sun, 17 Apr 2022 16:40:53 +0200 Subject: [PATCH 1/2] Add simple test to decode_symphonia for decoding the first frame --- core/src/read/data.rs | 2 +- decode_symphonia/Cargo.toml | 3 + decode_symphonia/src/lib.rs | 112 ++++++++++++++++++++++++++++++++---- 3 files changed, 104 insertions(+), 13 deletions(-) diff --git a/core/src/read/data.rs b/core/src/read/data.rs index fd9c087..7a80b9a 100644 --- a/core/src/read/data.rs +++ b/core/src/read/data.rs @@ -4,7 +4,7 @@ pub struct DataBlock { } impl DataBlock { - pub(crate) fn new(num_channels: usize, block_size: usize) -> Self { + pub fn new(num_channels: usize, block_size: usize) -> Self { let mut block: Vec> = Vec::with_capacity(num_channels); for _ in 0..num_channels { let mut data: Vec = Vec::with_capacity(block_size); diff --git a/decode_symphonia/Cargo.toml b/decode_symphonia/Cargo.toml index 5957f45..6a13aec 100644 --- a/decode_symphonia/Cargo.toml +++ b/decode_symphonia/Cargo.toml @@ -17,6 +17,9 @@ repository = "https://github.com/RustyDAW/creek" creek-core = { version = "0.1", path = "../core" } symphonia = "0.5" +[dev-dependencies] +float-cmp = "*" + [features] #aac = [ "symphonia/aac" ] #alac = [ "symphonia/alac" ] diff --git a/decode_symphonia/src/lib.rs b/decode_symphonia/src/lib.rs index d185e04..e057db4 100644 --- a/decode_symphonia/src/lib.rs +++ b/decode_symphonia/src/lib.rs @@ -341,23 +341,24 @@ impl Drop for SymphoniaDecoder { #[cfg(test)] mod tests { use super::*; + use float_cmp::*; #[test] fn decoder_new() { let files = vec![ // file | num_channels | num_frames | sample_rate - ("./test_files/wav_u8_mono.wav", 1, 1323000, Some(44100)), - ("./test_files/wav_i16_mono.wav", 1, 1323000, Some(44100)), - ("./test_files/wav_i24_mono.wav", 1, 1323000, Some(44100)), - ("./test_files/wav_i32_mono.wav", 1, 1323000, Some(44100)), - ("./test_files/wav_f32_mono.wav", 1, 1323000, Some(44100)), - ("./test_files/wav_i24_stereo.wav", 2, 1323000, Some(44100)), - //"./test_files/ogg_mono.ogg", - //"./test_files/ogg_stereo.ogg", - //"./test_files/mp3_constant_mono.mp3", - //"./test_files/mp3_constant_stereo.mp3", - //"./test_files/mp3_variable_mono.mp3", - //"./test_files/mp3_variable_stereo.mp3", + ("../test_files/wav_u8_mono.wav", 1, 1323000, Some(44100)), + ("../test_files/wav_i16_mono.wav", 1, 1323000, Some(44100)), + ("../test_files/wav_i24_mono.wav", 1, 1323000, Some(44100)), + ("../test_files/wav_i32_mono.wav", 1, 1323000, Some(44100)), + ("../test_files/wav_f32_mono.wav", 1, 1323000, Some(44100)), + ("../test_files/wav_i24_stereo.wav", 2, 1323000, Some(44100)), + //"../test_files/ogg_mono.ogg", + //"../test_files/ogg_stereo.ogg", + //"../test_files/mp3_constant_mono.mp3", + //"../test_files/mp3_constant_stereo.mp3", + //"../test_files/mp3_variable_mono.mp3", + //"../test_files/mp3_variable_stereo.mp3", ]; for file in files { @@ -376,4 +377,91 @@ mod tests { } } } + + #[test] + fn decode_first_frame() { + let block_size = 10; + + let decoder = + SymphoniaDecoder::new("../test_files/wav_u8_mono.wav".into(), 0, block_size, ()); + + + let (mut decoder, file_info) = decoder.unwrap(); + println!("{:?}", file_info.num_frames); + + let mut data_block = DataBlock::new(1, block_size); + unsafe { + decoder.decode(&mut data_block).unwrap(); + } + + let samples = &mut data_block.block[0]; + assert_eq!(samples.len(), block_size); + + + let first_frame = [ + 0.0, + 0.046875, + 0.09375, + 0.1484375, + 0.1953125, + 0.2421875, + 0.2890625, + 0.3359375, + 0.3828125, + 0.421875 + ]; + + + for i in 0..samples.len() { + assert!(approx_eq!(f32, first_frame[i], samples[i], ulps = 2)); + } + + let second_frame = [ + 0.46875, + 0.5078125, + 0.5390625, + 0.578125, + 0.609375, + 0.640625, + 0.671875, + 0.6953125, + 0.71875, + 0.7421875, + ]; + + unsafe { + decoder.decode(&mut data_block).unwrap(); + } + + let samples = &mut data_block.block[0]; + for i in 0..samples.len() { + assert!(approx_eq!(f32, second_frame[i], samples[i], ulps = 2)); + } + + let last_frame = [ + -0.0625, + -0.046875, + -0.0234375, + -0.0078125, + 0.015625, + 0.03125, + 0.046875, + 0.0625, + 0.078125, + 0.0859375, + ]; + + // Seek to last frame + decoder.seek(file_info.num_frames - 1 - block_size).unwrap(); + + unsafe { + decoder.decode(&mut data_block).unwrap(); + } + let samples = &mut data_block.block[0]; + for i in 0..samples.len() { + assert!(approx_eq!(f32, last_frame[i], samples[i], ulps = 2)); + } + + assert_eq!(decoder.current_frame, file_info.num_frames - 1); + } } From eb6d8d5af95fe11779e608991f9140383697d79c Mon Sep 17 00:00:00 2001 From: obsoleszenz Date: Sun, 17 Apr 2022 16:44:50 +0200 Subject: [PATCH 2/2] Add rust ci --- .github/workflows/ci.yml | 51 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..77dc459 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,51 @@ +name: Rust CI + +on: + pull_request: + push: + branches: + - master + - staging + - trying + +jobs: + build_and_test: + name: Build and test + strategy: + matrix: + include: + # Currently used Rust version, same as in `rust-toolchain` file. + - os: ubuntu-latest + rust: 1.60.0 + + # Minimum Supported Rust Version = 1.56.0 + # + - os: ubuntu-latest + rust: 1.56.0 + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@master + + - name: Install alsa + run: sudo apt install alsa libasound2-dev + + - name: Install ${{ matrix.rust }} + uses: actions-rs/toolchain@v1 + with: + toolchain: ${{ matrix.rust }} + override: true + + - name: Cache rust cargo artifacts + uses: swatinem/rust-cache@v1 + + - name: check + uses: actions-rs/cargo@v1 + with: + command: check + args: -p creek-core -p creek-decode-symphonia -p creek-encode-wav --tests --features decode-all + + - name: tests + uses: actions-rs/cargo@v1 + with: + command: test + args: -p creek-core -p creek-decode-symphonia -p creek-encode-wav