Pure MoonBit FLAC encoder: turn interleaved PCM into a native .flac stream with fixed and LPC prediction, Rice partitioned residuals and stereo decorrelation.
状态:0.2.0,核心功能可用。STREAMINFO + MD5、定长块帧与 CRC-8/CRC-16、CONSTANT / VERBATIM / FIXED(0–4) / LPC(1–12) 子帧、Rice 分区搜索、立体声去相关均已实现,并通过 libsndfile 与 Xcsa-ee/moonflac 两个独立解码器验证。流式编码与压缩率对齐见「路线图」。
| 能力 | 状态 |
|---|---|
| fLaC 标记 + STREAMINFO(含 PCM 的 MD5) | ✅ |
| 定长块帧(默认 4096),可变块长按尾块处理 | ✅ |
| 帧头 CRC-8、帧尾 CRC-16 | ✅ |
| CONSTANT / VERBATIM 子帧 | ✅ |
| FIXED 0–4 阶预测 + Rice 残差(参数搜索) | ✅ |
| 位深 4–32 bit 路径、1–8 声道(8/16/24 经参考解码器验证;32 bit libsndfile 不支持) | ✅ |
| 立体声去相关(left/side、right/side、mid/side,按位代价选优) | ✅ |
| LPC 子帧(Levinson-Durbin + 15-bit 系数量化 + 移位选择) | ✅ |
| Rice 分区搜索(partition order 1–8) | ✅ |
| 流式 / 增量编码 | ⬜ 计划中 |
// 交织的有符号 PCM,已经缩放到 bits_per_sample 的二进制补码范围
let samples : Array[Int64] = [0L, 100L, 200L, 300L]
let flac : Bytes = @moonflac_encoder.encode(samples, 44100, 1, 16)# 自检:合成 1 秒 440 Hz 正弦,编码后打印 base64
moon run cmd/main -- selftest
# 把 base64 的 WAV 编码成 base64 的 FLAC
moon run cmd/main -- encode "$(base64 -w0 input.wav)"
# 生成测试信号(采样率 声道 位深 帧数 类型)
moon run cmd/main -- gen 44100 2 16 4096 0
# MD5 自检,验证摘要实现
moon run cmd/main -- md5 abc| 配置 | FLAC | 原始 PCM | 压缩比 |
|---|---|---|---|
| 单声道 16-bit | 1,896 B | 8,192 B | 4.31× |
| 单声道 24-bit | 4,034 B | 12,288 B | 3.05× |
| 立体声 16-bit | 3,680 B | 16,384 B | 4.45× |
| 立体声 24-bit | 8,714 B | 24,576 B | 2.82× |
| 立体声 16-bit(两声道相同) | 1,899 B | 16,384 B | 8.61× |
python tools/verify.py matrix # 48 例跨解码器矩阵,逐样本比对 STREAMINFO 摘要
python tools/verify.py benchmark # 同一段 PCM 上与 libFLAC 的体积对比moon check --deny-warn
moon test --deny-warn
moon fmt --checkfn encode(samples : Array[Int64], sample_rate : Int, channels : Int, bits_per_sample : Int) -> Bytes raise EncodeErrorfn version() -> StringInstall
Download zipPure MoonBit FLAC encoder: turn interleaved PCM into a native .flac stream with fixed and LPC prediction, Rice partitioned residuals and stereo decorrelation.