forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB682c080Tahinli2 months ago2434 commits

lib.rs135 lines, 5.0 KiBHistoryRawDownload
1//! Drop-in replacement for the `flacenc` 0.5.1 surface the replica consumes,
2//! implemented over [`ec_flac`].
3//!
4//! It carries the incumbent's package name and version so the swap is a
5//! `[patch.crates-io]` entry and nothing else. The scope is exactly the API
6//! `engine/src/export.rs` calls (`export.rs:67-68,2930-2954`):
7//!
8//! ```no_run
9//! use flacenc::component::BitRepr;
10//! use flacenc::error::Verify;
11//!
12//! let config = flacenc::config::Encoder::default().into_verified().unwrap();
13//! let source = flacenc::source::MemSource::from_samples(&[0i32; 32], 2, 16, 44100);
14//! let mut stream =
15//! flacenc::encode_with_fixed_block_size(&config, source, config.block_size).unwrap();
16//! stream
17//! .stream_info_mut()
18//! .set_block_sizes(config.block_size, config.block_size)
19//! .unwrap();
20//! let mut sink = flacenc::bitsink::ByteSink::new();
21//! stream.write(&mut sink).unwrap();
22//! let _bytes: &[u8] = sink.as_slice();
23//! ```
24//!
25//! Signatures match the incumbent's (types, argument order, error shapes) so
26//! call sites compile unchanged; the implementation behind them is ours.
27//! Deliberately *not* covered: the `par` feature knobs, `serde` config
28//! round-tripping and the component tree (`Frame`/`SubFrame`/`Residual` as
29//! addressable values) — the replica constructs none of them.
30
31#![forbid(unsafe_code)]
32#![warn(missing_docs)]
33
34pub mod bitsink;
35pub mod component;
36pub mod config;
37pub mod error;
38pub mod source;
39
40pub use component::Stream;
41pub use error::EncodeError;
42
43use error::Verified;
44use source::Source;
45
46/// Encode `src` into a stream of fixed-size blocks.
47///
48/// Mirrors `flacenc::encode_with_fixed_block_size`: the verified config, a
49/// source, and the block size (which the caller reads off the config).
50pub fn encode_with_fixed_block_size<T: Source>(
51 config: &Verified<config::Encoder>,
52 src: T,
53 block_size: usize,
54) -> Result<Stream, EncodeError> {
55 let encoder = ec_flac::EncoderConfig {
56 block_size,
57 stereo_decorrelation: config.stereo_coding.use_midside
58 || config.stereo_coding.use_leftside
59 || config.stereo_coding.use_rightside,
60 ..ec_flac::EncoderConfig::default()
61 };
62 let bytes = ec_flac::encode(
63 &encoder,
64 src.as_raw_slice(),
65 src.channels(),
66 src.bits_per_sample() as u32,
67 src.sample_rate() as u32,
68 )
69 .map_err(|e| EncodeError(e.to_string()))?;
70 Ok(Stream::from_bytes(bytes))
71}
72
73#[cfg(test)]
74mod tests {
75 use super::*;
76 use crate::component::BitRepr;
77 use crate::error::Verify;
78
79 /// The replica's own call sequence, verbatim from `export.rs:2994-3025`,
80 /// with the result decoded back to prove it is a real FLAC stream.
81 #[test]
82 fn the_replica_call_sequence_produces_a_decodable_stream() {
83 let samples: Vec<i32> = (0..8192)
84 .map(|i| ((i as f64 * 0.07).sin() * 12000.0) as i32)
85 .collect();
86
87 let config = config::Encoder::default()
88 .into_verified()
89 .map_err(|(_, e)| format!("flac encoder config: {e}"))
90 .unwrap();
91 let source = source::MemSource::from_samples(&samples, 2, 16, 44100);
92 let mut stream = encode_with_fixed_block_size(&config, source, config.block_size)
93 .map_err(|e| format!("flac encode: {e}"))
94 .unwrap();
95 stream
96 .stream_info_mut()
97 .set_block_sizes(config.block_size, config.block_size)
98 .map_err(|e| format!("flac block size: {e}"))
99 .unwrap();
100 let mut sink = bitsink::ByteSink::new();
101 stream
102 .write(&mut sink)
103 .map_err(|e| format!("flac write: {e}"))
104 .unwrap();
105
106 let bytes = sink.as_slice();
107 assert_eq!(&bytes[..4], b"fLaC");
108 assert_eq!(stream.count_bits(), bytes.len() * 8);
109 let mut reader = ec_flac::FlacReader::new(bytes).expect("our own output opens");
110 let info = reader.stream_info().expect("streaminfo").clone();
111 assert_eq!(info.min_block_size, config.block_size as u16);
112 assert_eq!(info.max_block_size, config.block_size as u16);
113 let decoded = reader.decode_all().expect("decode");
114 assert_eq!(decoded.interleaved(), samples, "the shim must be lossless");
115 }
116
117 #[test]
118 fn a_block_size_the_stream_cannot_hold_is_an_error_not_a_panic() {
119 let config = config::Encoder::default().into_verified().unwrap();
120 let source = source::MemSource::from_samples(&[0i32; 64], 2, 16, 44100);
121 let mut stream = encode_with_fixed_block_size(&config, source, 32).unwrap();
122 assert!(stream.stream_info_mut().set_block_sizes(0, 70000).is_err());
123 }
124
125 #[test]
126 fn an_invalid_config_is_refused_with_its_original_value() {
127 let config = config::Encoder {
128 block_size: 4,
129 ..config::Encoder::default()
130 };
131 let (returned, error) = config.into_verified().unwrap_err();
132 assert_eq!(returned.block_size, 4);
133 assert!(format!("{error}").contains("block_size"));
134 }
135}