use std::io::Cursor; use ec_riff::{SampleType, WavOptions, WavReader, WavSpec, WavWriter}; fn spec(channels: u16, bits: u16) -> WavSpec { WavSpec { channels, sample_rate: 48_000, bits_per_sample: bits, sample_format: SampleType::Int, } } fn le64(bytes: &[u8], at: usize) -> u64 { u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap()) } fn write_file(s: WavSpec, frames: usize, options: WavOptions) -> Vec { let mut w = WavWriter::with_options(Cursor::new(Vec::new()), s, options).unwrap(); let ch = usize::from(s.channels); let limit = 1i64 << (s.bits_per_sample - 1); for i in 0..frames * ch { w.write_sample((i as i64 % (limit - 1)) as i32).unwrap(); } w.finalize().unwrap().into_inner() } fn opts(frames: u64, threshold: u64) -> WavOptions { WavOptions { expected_frames: frames, rf64_threshold: threshold, } } #[test] fn rf64_samples_match_the_riff_file_for_the_same_input() { for (channels, bits) in [(1u16, 8u16), (6, 24), (2, 16), (8, 32)] { let s = spec(channels, bits); let frames = 333; let riff = write_file(s, frames, WavOptions::default()); let rf64 = write_file(s, frames, opts(frames as u64, 64)); assert_eq!(&riff[0..4], b"RIFF"); assert_eq!(&rf64[0..4], b"RF64"); let a = WavReader::new(Cursor::new(&riff[..])) .unwrap() .read_all_i32() .unwrap(); let b = WavReader::new(Cursor::new(&rf64[..])) .unwrap() .read_all_i32() .unwrap(); assert_eq!(a, b, "{channels}ch {bits}b"); } } #[test] #[ignore = "writes ~4.1 GiB under TMPDIR; run by hand"] fn writes_past_four_gib_as_rf64() { let s = spec(2, 16); let frames: u64 = 1_100_000_000; let path = std::env::temp_dir().join(format!("ec-riff-rf64-{}.wav", std::process::id())); let mut w = WavWriter::create_with_options(&path, s, opts(frames, 0xFFFF_F000)).unwrap(); let block = vec![1234i32; 1 << 20]; let mut left = frames * 2; while left > 0 { let n = (block.len() as u64).min(left) as usize; w.write_samples(&block[..n]).unwrap(); left -= n as u64; } w.finalize().unwrap(); let len = std::fs::metadata(&path).unwrap().len(); let mut head = vec![0u8; 60]; use std::io::Read; std::fs::File::open(&path) .unwrap() .read_exact(&mut head) .unwrap(); assert_eq!(&head[0..4], b"RF64"); assert_eq!(le64(&head, 20), len - 8); assert_eq!(le64(&head, 28), frames * 4); let r = WavReader::open(&path).unwrap(); assert_eq!(r.duration(), Some(frames)); let _ = std::fs::remove_file(&path); }