mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
- FLAC files read their layout from the channel mask comment, or else the one decoders assume for the count, which is what the writer omits the comment for - an unbuilt format is Unsupported before the layout is checked - FLAC encode no longer reserves half the PCM size up front - flac-encode's lib tests stream a file and compare it with encode - the seek test compares samples after the seek Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
277 lines
12 KiB
Rust
277 lines
12 KiB
Rust
use craft_audio::{AudioBuffer, AudioError, BitDepth, BwfInfo, ChannelLayout, EncodeFormat, EncodeOptions, FileEncoder, encode};
|
|
use std::io::{Cursor, Seek, SeekFrom, Write};
|
|
|
|
fn tone(channels: usize, frames: usize) -> AudioBuffer {
|
|
let channels = (0..channels).map(|c| (0..frames).map(|i| ((i * (c + 3)) as f32 * 0.013).sin() * 1.2).collect()).collect();
|
|
AudioBuffer { sample_rate: 44_100, channels }
|
|
}
|
|
|
|
fn streamed(buf: &AudioBuffer, opts: &EncodeOptions) -> Vec<u8> {
|
|
let mut out = Cursor::new(b"pre".to_vec());
|
|
out.set_position(3);
|
|
let mut enc = FileEncoder::new(out, opts, buf.sample_rate, buf.channels.len() as u16).unwrap();
|
|
let mut start = 0;
|
|
for len in [1, 7, 4095, 4097, 333].into_iter().cycle() {
|
|
if start >= buf.frames() {
|
|
break;
|
|
}
|
|
let len = len.min(buf.frames() - start);
|
|
// Borrowed slices, as a caller with its own buffers passes them.
|
|
let block: Vec<&[f32]> = buf.channels.iter().map(|c| &c[start..start + len]).collect();
|
|
enc.write(&block, len).unwrap();
|
|
start += len;
|
|
}
|
|
let out = enc.finish().unwrap();
|
|
// the sink is left at the end of the file
|
|
assert_eq!(out.position(), out.get_ref().len() as u64);
|
|
out.into_inner()
|
|
}
|
|
|
|
#[test]
|
|
fn streamed_files_match_whole_buffer_encodes() {
|
|
let bwf = BwfInfo::default().with_description("take").with_time_reference(123_456);
|
|
let cases = [
|
|
(EncodeFormat::Wav, BitDepth::Int24, true, None, None),
|
|
(EncodeFormat::Wav, BitDepth::Float32, false, None, None),
|
|
(EncodeFormat::Wav, BitDepth::Int16, true, Some(bwf), Some(ChannelLayout::SURROUND_5_1_SIDE)),
|
|
(EncodeFormat::Aiff, BitDepth::Int24, true, None, None),
|
|
(EncodeFormat::Aiff, BitDepth::Float32, false, None, None),
|
|
(EncodeFormat::Flac, BitDepth::Int16, true, None, None),
|
|
(EncodeFormat::Flac, BitDepth::Int24, false, None, None),
|
|
(EncodeFormat::Flac, BitDepth::Int16, false, None, Some(ChannelLayout::SURROUND_5_1_SIDE)),
|
|
];
|
|
for (format, bit_depth, dither, bwf, layout) in cases {
|
|
let base = EncodeOptions::new(format).with_bit_depth(bit_depth).with_dither(dither);
|
|
let base = match bwf {
|
|
Some(b) => base.with_bwf(b),
|
|
None => base,
|
|
};
|
|
for channels in [1, 6] {
|
|
let buf = tone(channels, 20_001);
|
|
// A layout applies to the buffer with as many channels.
|
|
let opts = match layout.filter(|l: &ChannelLayout| usize::from(l.channels()) == channels) {
|
|
Some(l) => base.clone().with_layout(l),
|
|
None => base.clone(),
|
|
};
|
|
let whole = encode(&buf, &opts).unwrap();
|
|
assert_eq!(streamed(&buf, &opts), [b"pre".as_slice(), &whole].concat(), "{opts:?}, {channels} channels");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A `Write + Seek` sink that accepts `budget` bytes in total and then fails every write, so
|
|
/// a `write_all` on it appends a prefix of the block and only then reports the error.
|
|
struct FailAfter {
|
|
inner: Cursor<Vec<u8>>,
|
|
budget: usize,
|
|
}
|
|
|
|
impl Write for FailAfter {
|
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
|
if self.budget == 0 {
|
|
return Err(std::io::Error::other("sink is full"));
|
|
}
|
|
let n = buf.len().min(self.budget);
|
|
self.budget -= n;
|
|
self.inner.write(&buf[..n])
|
|
}
|
|
|
|
fn flush(&mut self) -> std::io::Result<()> {
|
|
self.inner.flush()
|
|
}
|
|
}
|
|
|
|
impl Seek for FailAfter {
|
|
fn seek(&mut self, pos: SeekFrom) -> std::io::Result<u64> {
|
|
self.inner.seek(pos)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_failed_write_poisons_the_encoder() {
|
|
let buf = tone(2, 20_001);
|
|
for format in [EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac] {
|
|
let opts = EncodeOptions::new(format).with_bit_depth(BitDepth::Int16).with_dither(false);
|
|
let sink = FailAfter { inner: Cursor::new(Vec::new()), budget: 256 };
|
|
let mut enc = FileEncoder::new(sink, &opts, buf.sample_rate, 2).unwrap();
|
|
// The sink accepts a prefix of the first block, then fails.
|
|
assert!(enc.write(&buf.channels, buf.frames()).is_err(), "{format:?}");
|
|
// Later calls must not claim success: the bytes on the sink no longer match the
|
|
// encoder's counters (and for FLAC the failed frame's samples are already gone).
|
|
assert!(matches!(enc.write(&buf.channels, 16), Err(AudioError::Encode(_))), "{format:?}");
|
|
assert!(matches!(enc.finish(), Err(AudioError::Encode(_))), "{format:?}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn blocks_past_a_format_limit_are_refused_before_anything_is_written() {
|
|
let buf = tone(2, 100);
|
|
for (format, limit) in [(EncodeFormat::Aiff, 1u64 << 30), (EncodeFormat::Flac, 1u64 << 36)] {
|
|
let opts = EncodeOptions::new(format).with_bit_depth(BitDepth::Int16).with_dither(false);
|
|
let mut enc = FileEncoder::new(Cursor::new(Vec::new()), &opts, buf.sample_rate, 2).unwrap();
|
|
enc.write(&buf.channels, 100).unwrap();
|
|
assert!(enc.check_room(limit / 2).is_ok(), "{format:?}");
|
|
assert!(matches!(enc.check_room(limit), Err(AudioError::TooLarge(_))), "{format:?}");
|
|
if format == EncodeFormat::Flac {
|
|
// 100 frames are in: STREAMINFO counts up to 2^36 - 1 in all.
|
|
assert!(enc.check_room(limit - 101).is_ok());
|
|
assert!(matches!(enc.check_room(limit - 100), Err(AudioError::TooLarge(_))));
|
|
}
|
|
// A block that does not hold the frames it claims is refused before the limit is checked.
|
|
if let Ok(frames) = usize::try_from(limit) {
|
|
assert!(matches!(enc.write::<Vec<f32>>(&[], frames), Err(AudioError::Encode(_))), "{format:?}");
|
|
}
|
|
assert_eq!(enc.finish().unwrap().into_inner(), encode(&buf, &opts).unwrap());
|
|
}
|
|
}
|
|
|
|
// A source that stops serving reads past `fail_from`, as a network-backed file might.
|
|
struct FlakySource {
|
|
bytes: Vec<u8>,
|
|
fail_from: u64,
|
|
}
|
|
|
|
use craft_media::ByteSource;
|
|
|
|
impl ByteSource for FlakySource {
|
|
fn len(&self) -> u64 {
|
|
self.bytes.len() as u64
|
|
}
|
|
fn read_at(&self, offset: u64, buf: &mut [u8]) -> std::io::Result<()> {
|
|
if offset + buf.len() as u64 > self.fail_from {
|
|
return Err(std::io::Error::other("connection reset"));
|
|
}
|
|
self.bytes.as_slice().read_at(offset, buf)
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn decoder_reads_any_read_seek_source_and_reports_source_failures() {
|
|
use craft_audio::{DecodeOptions, Decoder};
|
|
use craft_media::ReadSeekSource;
|
|
let wav = encode(&tone(2, 48_000), &EncodeOptions::new(EncodeFormat::Wav)).unwrap();
|
|
|
|
// `ReadSeekSource` (any `Read + Seek`) is accepted, so a `BufReader<File>` works the same way.
|
|
let source = ReadSeekSource::new(Cursor::new(wav.clone())).unwrap();
|
|
let info = Decoder::open(source, Some("wav"), &DecodeOptions::default()).unwrap().probe().unwrap();
|
|
assert_eq!((info.frames, info.damaged), (48_000, 0));
|
|
|
|
// A source failing half way through gives an I/O error, not concealed silence.
|
|
let half = wav.len() as u64 / 2;
|
|
let mut dec = Decoder::open(FlakySource { bytes: wav, fail_from: half }, Some("wav"), &DecodeOptions::default()).unwrap();
|
|
let err = loop {
|
|
match dec.next_block() {
|
|
Ok(Some(_)) => continue,
|
|
Ok(None) => panic!("the source failure was not reported"),
|
|
Err(e) => break e,
|
|
}
|
|
};
|
|
assert!(matches!(err, AudioError::Io(_)), "{err}");
|
|
assert_eq!(dec.info().damaged, 0);
|
|
}
|
|
|
|
#[test]
|
|
fn seeking_to_or_past_the_end_ends_the_stream() {
|
|
use craft_audio::{DecodeOptions, Decoder};
|
|
let buf = tone(2, 4800);
|
|
for format in [EncodeFormat::Wav, EncodeFormat::Aiff, EncodeFormat::Flac] {
|
|
let file = encode(&buf, &EncodeOptions::new(format).with_bit_depth(BitDepth::Int16)).unwrap();
|
|
for target in [4800, 4801, 1_000_000] {
|
|
let mut dec = Decoder::from_bytes(&file, None, &DecodeOptions::default()).unwrap();
|
|
assert_eq!(dec.seek(target).unwrap(), 4800, "{format:?} seek({target})");
|
|
assert!(dec.next_block().unwrap().is_none(), "{format:?} seek({target})");
|
|
// Seeking back still works.
|
|
assert_eq!(dec.seek(4000).unwrap(), 4000, "{format:?}");
|
|
let mut left = vec![Vec::new(); buf.channels.len()];
|
|
while let Some(b) = dec.next_block().unwrap() {
|
|
for (l, c) in left.iter_mut().zip(b.channels) {
|
|
l.extend(c);
|
|
}
|
|
}
|
|
for (l, source) in left.iter().zip(&buf.channels) {
|
|
assert_eq!(l.len(), 800, "{format:?}");
|
|
assert!(l.iter().zip(&source[4000..]).all(|(a, b)| (a - b.clamp(-1.0, 1.0)).abs() <= 2.0 / 32768.0), "{format:?}");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn single_track_files_refuse_other_track_indexes() {
|
|
use craft_audio::{DecodeOptions, decode_with};
|
|
let buf = tone(1, 1000);
|
|
for format in [EncodeFormat::Wav, EncodeFormat::Aiff] {
|
|
let file = encode(&buf, &EncodeOptions::new(format)).unwrap();
|
|
assert!(decode_with(&file, None, &DecodeOptions::default().with_track(0)).is_ok(), "{format:?}");
|
|
let e = decode_with(&file, None, &DecodeOptions::default().with_track(1)).unwrap_err();
|
|
assert!(matches!(e, AudioError::Unsupported(_)), "{format:?}: {e}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn short_or_missing_channels_are_refused_without_writing() {
|
|
let opts = EncodeOptions::new(EncodeFormat::Wav).with_bit_depth(BitDepth::Int16);
|
|
let mut enc = FileEncoder::new(Cursor::new(Vec::new()), &opts, 48_000, 2).unwrap();
|
|
let left = vec![0.1f32; 4];
|
|
// More frames than the channels hold, or too few channels: refused, nothing written.
|
|
assert!(matches!(enc.write(&[&left, &left], 10), Err(AudioError::Encode(_))));
|
|
assert!(matches!(enc.write(&[&left], 4), Err(AudioError::Encode(_))));
|
|
// The encoder is still usable.
|
|
enc.write(&[&left, &left], 4).unwrap();
|
|
let wav = enc.finish().unwrap().into_inner();
|
|
assert_eq!(craft_audio::probe(&wav, None).unwrap().frames, 4);
|
|
}
|
|
|
|
/// A 48 kHz mono float PCM Matroska file: ten 10 ms blocks from `first_ns`, with block durations
|
|
/// when `durations` is set (otherwise its length is unknown), and a `CodecDelay` of `delay_ns`.
|
|
fn pcm_mkv(first_ns: i64, durations: bool, delay_ns: u64) -> Vec<u8> {
|
|
use craft_media::matroska::{LacingMode, MkvWriter, MuxOptions, TrackKind, TrackSpec};
|
|
let mut spec = TrackSpec::new(TrackKind::Audio, "A_PCM/FLOAT/IEEE");
|
|
spec.audio = Some((48_000.0, 1, Some(32)));
|
|
spec.codec_delay_ns = delay_ns;
|
|
let opts = MuxOptions { lacing: LacingMode::None, ..MuxOptions::default() };
|
|
let mut w = MkvWriter::new(Cursor::new(Vec::new()), vec![spec], opts).unwrap();
|
|
for k in 0..10i64 {
|
|
let data: Vec<u8> = (0..480).flat_map(|i| ((k * 480 + i) as f32 * 0.001).sin().to_le_bytes()).collect();
|
|
w.write_frame(0, first_ns + k * 10_000_000, true, &data, durations.then_some(10_000_000)).unwrap();
|
|
}
|
|
w.finish().unwrap().into_inner()
|
|
}
|
|
|
|
#[test]
|
|
fn a_matroska_track_that_starts_late_counts_only_its_own_frames() {
|
|
use craft_audio::{DecodeOptions, Decoder};
|
|
let file = pcm_mkv(1_000_000_000, true, 0);
|
|
let dec = Decoder::from_bytes(&file, None, &DecodeOptions::default()).unwrap();
|
|
assert!(dec.length_known());
|
|
assert_eq!(dec.info().frames, 4800);
|
|
let decoded: usize = dec.probe().map(|i| i.frames as usize).unwrap();
|
|
assert_eq!(decoded, 4800);
|
|
}
|
|
|
|
#[test]
|
|
fn a_matroska_codec_delay_is_taken_off_the_length() {
|
|
use craft_audio::{decode, probe};
|
|
// `probe` reports the length the container gives, before anything is decoded; `decode`
|
|
// reports what it presented. 100 ms are decoded and the 20 ms of codec delay dropped, wherever
|
|
// the first block is stored.
|
|
for first_ns in [-20_000_000, 0, 50_000_000] {
|
|
let file = pcm_mkv(first_ns, true, 20_000_000);
|
|
let (_, buf) = decode(&file, None).unwrap();
|
|
assert_eq!((probe(&file, None).unwrap().frames, buf.frames()), (3840, 3840), "first block at {first_ns} ns");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn seeking_far_past_an_unknown_end_does_not_overflow() {
|
|
use craft_audio::{DecodeOptions, Decoder};
|
|
let file = pcm_mkv(0, false, 1_000_000);
|
|
let mut dec = Decoder::from_bytes(&file, None, &DecodeOptions::default()).unwrap();
|
|
assert!(!dec.length_known());
|
|
dec.seek(u64::MAX).unwrap();
|
|
assert!(dec.next_block().unwrap().is_none());
|
|
// and back to the start
|
|
assert_eq!(dec.seek(0).unwrap(), 0);
|
|
assert!(dec.next_block().unwrap().is_some_and(|b| b.frames() > 0));
|
|
}
|