forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli4 days ago4021 commits

rf64.rs182 lines, 6.4 KiBHistoryRawDownload
1//! RF64 headers: the container choice, the ds64 sizes, and the reader's side.
2//!
3//! The threshold is injectable ([`WavOptions::rf64_threshold`]), so the RF64
4//! path runs on kilobytes here. The one test that writes past 4 GiB is
5//! `#[ignore]`d and runs by hand; it writes under `std::env::temp_dir()`,
6//! which honours `TMPDIR`.
7
8use std::io::Cursor;
9
10use ec_riff::{SampleType, WavOptions, WavReader, WavSpec, WavWriter};
11
12fn spec(channels: u16, bits: u16) -> WavSpec {
13 WavSpec {
14 channels,
15 sample_rate: 48_000,
16 bits_per_sample: bits,
17 sample_format: SampleType::Int,
18 }
19}
20
21fn le64(bytes: &[u8], at: usize) -> u64 {
22 u64::from_le_bytes(bytes[at..at + 8].try_into().unwrap())
23}
24
25fn le32(bytes: &[u8], at: usize) -> u32 {
26 u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap())
27}
28
29/// Write `frames` frames of a ramp and return the finished file.
30fn write_file(s: WavSpec, frames: usize, options: WavOptions) -> Vec<u8> {
31 let mut w = WavWriter::with_options(Cursor::new(Vec::new()), s, options).unwrap();
32 let ch = usize::from(s.channels);
33 let limit = 1i64 << (s.bits_per_sample - 1);
34 for i in 0..frames * ch {
35 w.write_sample((i as i64 % (limit - 1)) as i32).unwrap();
36 }
37 w.finalize().unwrap().into_inner()
38}
39
40fn opts(frames: u64, threshold: u64) -> WavOptions {
41 WavOptions {
42 expected_frames: frames,
43 rf64_threshold: threshold,
44 }
45}
46
47#[test]
48fn over_threshold_writes_rf64_with_exact_ds64_sizes() {
49 let s = spec(2, 16);
50 let frames = 1000;
51 let bytes = write_file(s, frames, opts(frames as u64, 64));
52
53 assert_eq!(&bytes[0..4], b"RF64");
54 assert_eq!(le32(&bytes, 4), u32::MAX, "RIFF size is ds64's to hold");
55 assert_eq!(&bytes[8..12], b"WAVE");
56 assert_eq!(&bytes[12..16], b"ds64");
57 assert_eq!(le32(&bytes, 16), 28, "ds64 body without a table");
58
59 let data_bytes = (frames * 4) as u64;
60 assert_eq!(le64(&bytes, 20), bytes.len() as u64 - 8, "riffSize");
61 assert_eq!(le64(&bytes, 28), data_bytes, "dataSize");
62 assert_eq!(le64(&bytes, 36), frames as u64, "sampleCount");
63 assert_eq!(le32(&bytes, 44), 0, "tableLength");
64
65 // Stereo 16-bit is a plain PCM fmt with no fact chunk, then data with the sentinel.
66 let fmt = 48;
67 assert_eq!(&bytes[fmt..fmt + 4], b"fmt ");
68 // The `data` id sits right before its payload, at a fixed distance from the end.
69 let data = bytes.len() - 8 - data_bytes as usize;
70 assert_eq!(&bytes[data..data + 4], b"data");
71 assert_eq!(
72 le32(&bytes, data + 4),
73 u32::MAX,
74 "data size is ds64's to hold"
75 );
76
77 let mut r = WavReader::new(Cursor::new(&bytes[..])).unwrap();
78 assert_eq!(r.spec(), s);
79 assert_eq!(r.duration(), Some(frames as u64));
80 assert_eq!(r.read_all_i32().unwrap().len(), frames * 2);
81}
82
83#[test]
84fn rf64_samples_match_the_riff_file_for_the_same_input() {
85 for (channels, bits) in [(1u16, 8u16), (6, 24), (2, 16), (8, 32)] {
86 let s = spec(channels, bits);
87 let frames = 333; // odd sample counts for the mono 8-bit pad case
88 let riff = write_file(s, frames, WavOptions::default());
89 let rf64 = write_file(s, frames, opts(frames as u64, 64));
90 assert_eq!(&riff[0..4], b"RIFF");
91 assert_eq!(&rf64[0..4], b"RF64");
92 let a = WavReader::new(Cursor::new(&riff[..]))
93 .unwrap()
94 .read_all_i32()
95 .unwrap();
96 let b = WavReader::new(Cursor::new(&rf64[..]))
97 .unwrap()
98 .read_all_i32()
99 .unwrap();
100 assert_eq!(a, b, "{channels}ch {bits}b");
101 }
102}
103
104#[test]
105fn odd_rf64_data_gets_a_pad_byte_outside_its_size() {
106 let s = spec(1, 8);
107 let frames = 5;
108 let bytes = write_file(s, frames, opts(frames as u64, 1));
109 // 5 data bytes: one pad byte follows, counted by riffSize but not dataSize.
110 assert_eq!(bytes.len() % 2, 0);
111 assert_eq!(le64(&bytes, 20), bytes.len() as u64 - 8);
112 assert_eq!(le64(&bytes, 28), 5);
113 let r = WavReader::new(Cursor::new(&bytes[..])).unwrap();
114 assert_eq!(r.duration(), Some(5));
115}
116
117#[test]
118fn extensible_rf64_keeps_fact_and_its_saturating_count() {
119 let s = spec(6, 24);
120 let frames = 50;
121 let bytes = write_file(s, frames, opts(frames as u64, 1));
122 assert_eq!(&bytes[0..4], b"RF64");
123 // Extensible fmt is 40 bytes, so `fact` follows it at 48 + 8 + 40.
124 let fact = 48 + 8 + 40;
125 assert_eq!(&bytes[fact..fact + 4], b"fact");
126 assert_eq!(le32(&bytes, fact + 8), frames as u32);
127 let r = WavReader::new(Cursor::new(&bytes[..])).unwrap();
128 assert_eq!(r.spec(), s);
129 assert_eq!(r.duration(), Some(frames as u64));
130}
131
132#[test]
133fn under_threshold_stays_riff_byte_for_byte() {
134 let s = spec(2, 16);
135 let riff = write_file(s, 100, WavOptions::default());
136 let below = write_file(s, 100, opts(100, 400 + 1));
137 assert_eq!(riff, below);
138 assert_eq!(&riff[0..4], b"RIFF");
139 assert_eq!(le32(&riff, 4) as usize, riff.len() - 8);
140}
141
142#[test]
143fn unknown_length_is_riff_and_oversize_riff_is_refused() {
144 let s = spec(2, 16);
145 let bytes = write_file(s, 10, WavOptions::default());
146 assert_eq!(&bytes[0..4], b"RIFF");
147 assert_eq!(le32(&bytes, 4) as usize, bytes.len() - 8);
148}
149
150/// Manual: writes 4.1 GiB of 16-bit stereo and reads its header back. Run with
151/// `cargo test -p ec-riff --test it rf64:: -- --ignored --nocapture`.
152#[test]
153#[ignore = "writes ~4.1 GiB under TMPDIR; run by hand"]
154fn writes_past_four_gib_as_rf64() {
155 let s = spec(2, 16);
156 // 4.4e9 bytes: 4.1 GiB, past the 4 GiB line the default threshold guards.
157 let frames: u64 = 1_100_000_000;
158 let path = std::env::temp_dir().join(format!("ec-riff-rf64-{}.wav", std::process::id()));
159 let mut w = WavWriter::create_with_options(&path, s, opts(frames, 0xFFFF_F000)).unwrap();
160 let block = vec![1234i32; 1 << 20];
161 let mut left = frames * 2;
162 while left > 0 {
163 let n = (block.len() as u64).min(left) as usize;
164 w.write_samples(&block[..n]).unwrap();
165 left -= n as u64;
166 }
167 w.finalize().unwrap();
168
169 let len = std::fs::metadata(&path).unwrap().len();
170 let mut head = vec![0u8; 60];
171 use std::io::Read;
172 std::fs::File::open(&path)
173 .unwrap()
174 .read_exact(&mut head)
175 .unwrap();
176 assert_eq!(&head[0..4], b"RF64");
177 assert_eq!(le64(&head, 20), len - 8);
178 assert_eq!(le64(&head, 28), frames * 4);
179 let r = WavReader::open(&path).unwrap();
180 assert_eq!(r.duration(), Some(frames));
181 let _ = std::fs::remove_file(&path);
182}