mirror of
https://github.com/storytold/AniCraft.git
synced 2026-11-28 09:09:32 +01:00
refactor(audio): delegate streaming WAV serialization to craft-audio (M5) (#395)
This commit is contained in:
1 parent
19ce5ee962
commit
41a0da7166
4 files changed
+36
-65
No files matched your search
Generated
+15
-4
@@ -427,7 +427,7 @@ name = "anicraft-xml"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"craft-xfl",
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b67b9f90c50091a3090f93fa054c34e409edba11)",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
@@ -1219,9 +1219,10 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-audio"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2bf53f2bdba4828e21097774a568ed9c74b0004#e2bf53f2bdba4828e21097774a568ed9c74b0004"
|
||||
dependencies = [
|
||||
"craft-media",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=e2bf53f2bdba4828e21097774a568ed9c74b0004)",
|
||||
"log",
|
||||
"symphonia",
|
||||
"thiserror 2.0.21",
|
||||
@@ -1322,7 +1323,7 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-media"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=f0f4758c01581d79577b9bf5046c06f4e77b1f18#f0f4758c01581d79577b9bf5046c06f4e77b1f18"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2bf53f2bdba4828e21097774a568ed9c74b0004#e2bf53f2bdba4828e21097774a568ed9c74b0004"
|
||||
dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1417,7 +1418,7 @@ name = "craft-xfl"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=b67b9f90c50091a3090f93fa054c34e409edba11#b67b9f90c50091a3090f93fa054c34e409edba11"
|
||||
dependencies = [
|
||||
"craft-xml",
|
||||
"craft-xml 0.1.0 (git+https://github.com/storytold/craft-libs?rev=b67b9f90c50091a3090f93fa054c34e409edba11)",
|
||||
"miniz_oxide 0.8.9",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
@@ -1432,6 +1433,16 @@ dependencies = [
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-xml"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=e2bf53f2bdba4828e21097774a568ed9c74b0004#e2bf53f2bdba4828e21097774a568ed9c74b0004"
|
||||
dependencies = [
|
||||
"quick-xml 0.41.0",
|
||||
"serde",
|
||||
"thiserror 2.0.21",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "craft-zip"
|
||||
version = "0.1.0"
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ anicraft-guide = { path = "crates/guide" }
|
||||
craft-anicraft-format = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" }
|
||||
craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" }
|
||||
craft-html = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18", default-features = false }
|
||||
craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18", default-features = false, features = ["decode", "pcm-encode"] }
|
||||
craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "e2bf53f2bdba4828e21097774a568ed9c74b0004", default-features = false, features = ["decode", "pcm-encode"] }
|
||||
craft-gif = { git = "https://github.com/storytold/craft-libs", rev = "f0f4758c01581d79577b9bf5046c06f4e77b1f18" }
|
||||
# Isolate the already-merged APNG container from the existing raster-codec pin.
|
||||
craft-apng-container = { package = "craft-image-codecs", version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "55b1d7f3c598b600a130ae63f4a40334a230ae7b", default-features = false }
|
||||
|
||||
+16
-60
@@ -1,65 +1,6 @@
|
||||
//! WAV (RIFF WAVE) writing via `craft-audio`.
|
||||
|
||||
/// Sample formats written.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Bits {
|
||||
Int16,
|
||||
Int24,
|
||||
Float32,
|
||||
}
|
||||
|
||||
impl Bits {
|
||||
fn bytes(self) -> u16 {
|
||||
match self {
|
||||
Bits::Int16 => 2,
|
||||
Bits::Int24 => 3,
|
||||
Bits::Float32 => 4,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const PCM: u16 = 1;
|
||||
const FLOAT: u16 = 3;
|
||||
|
||||
/// A WAV header for `frames` frames of `channels` channels.
|
||||
pub fn header(rate: u32, channels: u16, bits: Bits, frames: u64) -> Option<Vec<u8>> {
|
||||
let b = bits.bytes();
|
||||
let data = u32::try_from(frames.checked_mul(u64::from(b) * u64::from(channels))?).ok()?;
|
||||
let riff = data.checked_add(36)?;
|
||||
let block = channels.checked_mul(b)?;
|
||||
let tag = if bits == Bits::Float32 { FLOAT } else { PCM };
|
||||
let mut h = Vec::with_capacity(44);
|
||||
h.extend_from_slice(b"RIFF");
|
||||
h.extend_from_slice(&riff.to_le_bytes());
|
||||
h.extend_from_slice(b"WAVEfmt ");
|
||||
h.extend_from_slice(&16u32.to_le_bytes());
|
||||
h.extend_from_slice(&tag.to_le_bytes());
|
||||
h.extend_from_slice(&channels.to_le_bytes());
|
||||
h.extend_from_slice(&rate.to_le_bytes());
|
||||
h.extend_from_slice(&(rate.saturating_mul(u32::from(block))).to_le_bytes());
|
||||
h.extend_from_slice(&block.to_le_bytes());
|
||||
h.extend_from_slice(&(b * 8).to_le_bytes());
|
||||
h.extend_from_slice(b"data");
|
||||
h.extend_from_slice(&data.to_le_bytes());
|
||||
Some(h)
|
||||
}
|
||||
|
||||
/// Appends samples in `bits` format. Integers are rounded and clipped to range; `x · 32768`
|
||||
/// for 16-bit, so 16-bit sources read by [`decode`] come back bit-identical.
|
||||
pub fn append(out: &mut Vec<u8>, samples: &[f32], bits: Bits) {
|
||||
out.reserve(samples.len() * usize::from(bits.bytes()));
|
||||
for &s in samples {
|
||||
let s = if s.is_finite() { s } else { 0.0 };
|
||||
match bits {
|
||||
Bits::Int16 => out.extend_from_slice(&((s * 32_768.0).round().clamp(-32_768.0, 32_767.0) as i16).to_le_bytes()),
|
||||
Bits::Int24 => {
|
||||
let v = (f64::from(s) * 8_388_608.0).round().clamp(-8_388_608.0, 8_388_607.0) as i32;
|
||||
out.extend_from_slice(v.to_le_bytes().get(..3).unwrap_or(&[0, 0, 0]));
|
||||
}
|
||||
Bits::Float32 => out.extend_from_slice(&s.to_le_bytes()),
|
||||
}
|
||||
}
|
||||
}
|
||||
pub use craft_audio::wav_stream::{Bits, append, header};
|
||||
|
||||
/// A whole WAV file in memory (empty when it would not fit a WAV's 4 GB limit).
|
||||
pub fn encode(rate: u32, channels: u16, samples: &[f32], bits: Bits) -> Vec<u8> {
|
||||
@@ -79,8 +20,23 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::{AudioError, decode};
|
||||
|
||||
const PCM: u16 = 1;
|
||||
const EXTENSIBLE: u16 = 0xFFFE;
|
||||
|
||||
#[test]
|
||||
fn streaming_blocks_round_trip_all_formats() {
|
||||
let samples = [0.25, -0.5, 0.0, 0.75, -1.0, 0.5];
|
||||
for bits in [Bits::Int16, Bits::Int24, Bits::Float32] {
|
||||
let mut bytes = header(48_000, 2, bits, 3).unwrap();
|
||||
for block in samples.chunks(2) {
|
||||
append(&mut bytes, block, bits);
|
||||
}
|
||||
let decoded = decode(&bytes).unwrap();
|
||||
assert_eq!((decoded.rate, decoded.channels, decoded.frames()), (48_000, 2, 3));
|
||||
assert_eq!(decoded.samples, samples);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sixteen_bit_round_trips_bit_exactly() {
|
||||
let src: Vec<i16> = vec![0, 1, -1, 32_767, -32_768, 12_345, -54, 7];
|
||||
|
||||
@@ -6,6 +6,10 @@ milestone ends with a green tree: build, tests, clippy, a `--screenshot` check a
|
||||
|
||||
## Honest status (2026-11-09, v0.1.0 early alpha)
|
||||
|
||||
Streaming WAV headers and PCM sample blocks now delegate to `craft_audio::wav_stream`;
|
||||
AniCraft retains mixing, frame-range limits, temporary-file cleanup and VFS writes.
|
||||
This integration depends on craft-libs #1962 and still requires hosted parity validation.
|
||||
|
||||
| Dimension | Status |
|
||||
|---|---|
|
||||
| Architecture | M0 skeleton in place: geom → doc → render → engine → cli → shell, layering enforced by `cargo test` (`src/architecture_tests.rs`: lower layers only, no UI crates below the shell, and the AGENTS.md map must match). |
|
||||
|
||||
Reference in new issue
Block a user