mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 18:37:51 +00:00
FilmCraft's FLAC encoder behind `flac-encode`, kept over SoundCraft's: linear prediction, stereo decorrelation and the STREAMINFO MD5 give 0.1-37% smaller files on 12 test signals, and the output is bit-exact through `flac` and ffmpeg. It is 3-5x slower than SoundCraft's, still about 56x real time. `lossless-encode` is now `pcm-encode` plus `flac-encode`, and `EncodeFormat::Flac` writes it through `encode` and `FileEncoder`. With only one of the two features, the other's formats are refused as `Unsupported`. The tests that use FLAC files as decode fixtures (truncation, corruption, metadata bounds, zero-frame files, container preflight) come with it, as do the FLAC oracle tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
102 lines
4.5 KiB
Rust
102 lines
4.5 KiB
Rust
use craft_audio::{AudioBuffer, BitDepth, EncodeFormat, EncodeOptions, FileEncoder, SampleFormat, decode, encode};
|
|
use std::io::Cursor;
|
|
|
|
fn encoded(audio: &AudioBuffer, options: &EncodeOptions, streaming: bool) -> Vec<u8> {
|
|
if !streaming {
|
|
return encode(audio, options).unwrap();
|
|
}
|
|
let mut writer = FileEncoder::new(Cursor::new(Vec::new()), options, audio.sample_rate, audio.num_channels()).unwrap();
|
|
// Cross the writer's 4096-frame scratch boundary, then write a short final block.
|
|
for start in (0..audio.frames()).step_by(4097) {
|
|
let end = (start + 4097).min(audio.frames());
|
|
let block: Vec<Vec<f32>> = audio.channels.iter().map(|channel| channel[start..end].to_vec()).collect();
|
|
writer.write(&block, end - start).unwrap();
|
|
}
|
|
writer.finish().unwrap().into_inner()
|
|
}
|
|
|
|
fn assert_float_headroom(streaming: bool) {
|
|
let samples = [
|
|
-f32::MAX,
|
|
-2.0,
|
|
-1.25,
|
|
-1.0,
|
|
-0.5,
|
|
-f32::MIN_POSITIVE,
|
|
-f32::from_bits(1),
|
|
-0.0,
|
|
0.0,
|
|
f32::from_bits(1),
|
|
f32::MIN_POSITIVE,
|
|
0.5,
|
|
1.0,
|
|
1.25,
|
|
2.0,
|
|
f32::MAX,
|
|
];
|
|
for format in [EncodeFormat::Wav, EncodeFormat::Aiff] {
|
|
for dither in [false, true] {
|
|
for channels in [1, 2] {
|
|
let audio = AudioBuffer {
|
|
sample_rate: 48_000,
|
|
channels: (0..channels).map(|c| samples.iter().copied().cycle().skip(c).take(4103).collect()).collect(),
|
|
};
|
|
let options = EncodeOptions::new(format).with_bit_depth(BitDepth::Float32).with_dither(dither);
|
|
let bytes = encoded(&audio, &options, streaming);
|
|
let pcm: Vec<u8> =
|
|
audio.interleaved().iter().flat_map(|v| if format == EncodeFormat::Wav { v.to_le_bytes() } else { v.to_be_bytes() }).collect();
|
|
assert!(bytes.ends_with(&pcm), "{format:?}, dither={dither}: stored sample bytes changed");
|
|
let (info, decoded) = decode(&bytes, None).unwrap();
|
|
assert_eq!(info.sample_format, SampleFormat::Float32);
|
|
assert_eq!((decoded.sample_rate, decoded.num_channels(), decoded.frames()), (48_000, channels as u16, 4103));
|
|
for (channel, (actual, expected)) in decoded.channels.iter().zip(&audio.channels).enumerate() {
|
|
for (frame, (actual, expected)) in actual.iter().zip(expected).enumerate() {
|
|
assert_eq!(actual.to_bits(), expected.to_bits(), "{format:?}, dither={dither}, channel={channel}, frame={frame}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn whole_file_float32_preserves_finite_headroom() {
|
|
assert_float_headroom(false);
|
|
}
|
|
|
|
#[test]
|
|
fn streaming_float32_preserves_finite_headroom() {
|
|
assert_float_headroom(true);
|
|
}
|
|
|
|
#[test]
|
|
fn float32_keeps_non_finite_sample_handling() {
|
|
let audio = AudioBuffer { sample_rate: 48_000, channels: vec![vec![f32::NAN, f32::NEG_INFINITY, f32::INFINITY]] };
|
|
let expected = [0.0_f32, -1.0, 1.0];
|
|
for format in [EncodeFormat::Wav, EncodeFormat::Aiff] {
|
|
for streaming in [false, true] {
|
|
let options = EncodeOptions::new(format).with_bit_depth(BitDepth::Float32).with_dither(false);
|
|
let bytes = encoded(&audio, &options, streaming);
|
|
// Inspect stored samples too: the decoder itself replaces non-finite values.
|
|
let pcm: Vec<u8> = expected.iter().flat_map(|v| if format == EncodeFormat::Wav { v.to_le_bytes() } else { v.to_be_bytes() }).collect();
|
|
assert!(bytes.ends_with(&pcm), "{format:?}, streaming={streaming}");
|
|
assert_eq!(decode(&bytes, None).unwrap().1.channels[0], expected);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn integer_pcm_still_clips_at_full_scale() {
|
|
let audio = AudioBuffer { sample_rate: 48_000, channels: vec![vec![-2.0, -1.25, -1.0, -0.5, 0.0, 0.5, 1.0, 1.25, 2.0]] };
|
|
for (depth, max) in [(BitDepth::Int16, 32767.0 / 32768.0), (BitDepth::Int24, 8_388_607.0 / 8_388_608.0), (BitDepth::Int32, 1.0)] {
|
|
for format in [EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac] {
|
|
for streaming in [false, true] {
|
|
let options = EncodeOptions::new(format).with_bit_depth(depth).with_dither(false);
|
|
let bytes = encoded(&audio, &options, streaming);
|
|
let (_, decoded) = decode(&bytes, None).unwrap();
|
|
assert_eq!(decoded.channels[0], [-1.0, -1.0, -1.0, -0.5, 0.0, 0.5, max, max, max], "{format:?} {depth:?}, streaming={streaming}");
|
|
}
|
|
}
|
|
}
|
|
}
|