Pure-MoonBit FLAC lossless audio decoder: parses stream metadata and decodes frames to PCM with no native dependencies.
| File | Purpose |
|---|---|
| bitreader.mbt | A forward-only, big-endian bit cursor over Bytes. |
| crc.mbt | CRC-8 and CRC-16 (MSB-first), including the two FLAC polynomials. |
| error.mbt | The DecodeError type raised by every parsing entry point. |
| streaminfo.mbt | The 34-byte STREAMINFO block. |
| metadata.mbt | The fLaC marker and typed metadata-block chain. |
| frame.mbt | Frame-header parsing with CRC-8 verification. |
| residual.mbt | Partitioned Rice residual decoding. |
| subframe.mbt | CONSTANT / VERBATIM / FIXED / LPC subframe decoding. |
| decorrelate.mbt | Stereo channel decorrelation. |
| decode.mbt | Whole-frame and whole-stream decoding to interleaved PCM. |
| wav.mbt | RIFF/WAVE container output from decoded PCM. |
| md5.mbt | MD5 digest (RFC 1321) with hex rendering. |
let pcm : @flac.Pcm = @flac.decode(flac_bytes)
// pcm.sample_rate, pcm.channels, pcm.bits_per_sample, pcm.sampleslet wav_bytes : Bytes = pcm.to_wav()let ok : Bool = @flac.verify(flac_bytes)moon run cmd/mainpub(all) suberror DecodeError {
Truncated
InvalidBitCount
NotFlac
InvalidMetadata
InvalidFrameSync
InvalidFrameHeader
HeaderCrcMismatch
FrameCrcMismatch
InvalidResidual
InvalidSubframe
Unsupported(String)
}pub struct BitReader {
data : Bytes
pos : Int
}fn BitReader::read_frame_header(self : BitReader, info : StreamInfo) -> (FrameHeader, BitReader) raise DecodeErrorfn BitReader::read_residual(self : BitReader, block_size : Int, predictor_order : Int) -> (Array[Int], BitReader) raise DecodeErrorfn BitReader::read_subframe(self : BitReader, block_size : Int, bits_per_sample : Int) -> (Array[Int], BitReader) raise DecodeErrorpub struct FrameHeader {
blocking_strategy : Int
block_size : Int
sample_rate : Int
channels : Int
channel_assignment : Int
bits_per_sample : Int
frame_number : Int64
}fn FrameHeader::new(blocking_strategy : Int, block_size : Int, sample_rate : Int, channels : Int, channel_assignment : Int, bits_per_sample : Int, frame_number : Int64) -> FrameHeaderpub enum MetadataBlock {
StreamInfo(StreamInfo)
Padding(Int)
Application(Int, Bytes)
SeekTable(Array[SeekPoint])
VorbisComment(VorbisComment)
Picture(Picture)
Other(Int, Bytes)
}pub struct Picture {
picture_type : Int
mime_type : String
description : String
width : Int
height : Int
depth : Int
colors : Int
data : Bytes
}pub struct SeekPoint {
sample_number : Int64
offset : Int64
frame_samples : Int
}pub struct StreamInfo {
min_block_size : Int
max_block_size : Int
min_frame_size : Int
max_frame_size : Int
sample_rate : Int
channels : Int
bits_per_sample : Int
total_samples : Int64
md5 : Array[Byte]
}fn StreamInfo::new(min_block_size : Int, max_block_size : Int, min_frame_size : Int, max_frame_size : Int, sample_rate : Int, channels : Int, bits_per_sample : Int, total_samples : Int64, md5 : Array[Byte]) -> StreamInfofn crc16(data : Bytes, poly : Int, init : Int) -> Intfn crc16_flac(data : Bytes) -> Intfn crc16_range(data : Bytes, from : Int, to : Int, poly : Int, init : Int) -> Intfn crc8(data : Bytes, poly : Int, init : Int) -> Intfn crc8_flac(data : Bytes) -> Intfn crc8_range(data : Bytes, from : Int, to : Int, poly : Int, init : Int) -> Intfn decode_frame(data : Bytes, offset : Int, info : StreamInfo) -> (DecodedFrame, Int) raise DecodeErrorfn md5_hex(data : Bytes) -> StringInstall
Download zipPure-MoonBit FLAC lossless audio decoder: parses stream metadata and decodes frames to PCM with no native dependencies.