Sakuraqwq1/moonflac-encoder/src/encode does not have a README file

    EncodeError

    pub suberror EncodeError {
    Empty
    BadChannels(Int)
    BadBits(Int)
    BadSampleRate(Int)
    NotWholeFrames(Int, Int)
    }

    Why an encode request was rejected.

    StreamEncoder

    pub struct StreamEncoder {
    sample_rate : Int
    channels : Int
    bits : Int
    frame_number : Int
    buffer : Array[Int64]
    filled : Int
    }

    Incremental encoder: feed PCM as it arrives, get FLAC frames back.

    The one-shot encode needs the whole signal because STREAMINFO carries the total sample count and the MD5 of the unencoded PCM. A stream does not have either up front, so this encoder writes both fields as zero, which RFC 9639 defines as "unknown". The reference flac tool does the same when its output is not seekable. A decoder still gets a valid stream, it just cannot use the digest to verify the decode.

    StreamEncoder::finish

    fn StreamEncoder::finish(self : StreamEncoder) -> Bytes

    Flush the final partial block. Call once, after the last [StreamEncoder::write]; further calls return nothing.

    StreamEncoder::header

    fn StreamEncoder::header(self : StreamEncoder) -> Bytes

    The fLaC marker and the STREAMINFO block, with the sample count and PCM digest left unknown. Emit this once, before the first frame.

    StreamEncoder::new

    fn StreamEncoder::new(sample_rate : Int, channels : Int, bits_per_sample : Int) -> StreamEncoder raise EncodeError

    Start a stream with the given parameters. Nothing is emitted until [StreamEncoder::header] or [StreamEncoder::write] is called.

    StreamEncoder::write

    fn StreamEncoder::write(self : StreamEncoder, samples : Array[Int64]) -> Bytes raise EncodeError

    Push interleaved PCM and receive whatever complete frames that produced.

    The result is usually empty for small inputs: samples are buffered until a full block is available, because frame size is what the format trades compression against latency for.

    encode

    fn encode(samples : Array[Int64], sample_rate : Int, channels : Int, bits_per_sample : Int) -> Bytes raise EncodeError

    Encode interleaved signed PCM as a native FLAC stream.

    samples holds channels interleaved values per frame, each already scaled to bits_per_sample two's complement range. The result is a complete .flac file: fLaC marker, a STREAMINFO block carrying the PCM digest, and fixed blocksize frames with per-frame CRC-8 and CRC-16.