Sign in

    Sakuraqwq1/moonaudio/src/audio does not have a README file

    AudioError

    pub(all) suberror AudioError {
    Truncated(String)
    Unsupported(String)
    Invalid(String)
    OutOfRange(String)
    }

    Errors raised by every moonaudio decoder, encoder and DSP helper.

    The four variants deliberately separate the reason a byte stream was rejected so callers can decide whether to skip a file, fall back to another decoder, or report the problem to the user.

    AudioError::kind

    fn AudioError::kind(self : AudioError) -> String

    Short machine friendly tag of an [AudioError], suitable for logs.

    AudioError::message

    fn AudioError::message(self : AudioError) -> String

    Human readable explanation of an [AudioError].

    AudioBuffer

    pub(all) struct AudioBuffer {
    sample_rate : Int
    channels : Int
    data : Array[Double]
    }

    A decoded audio signal: interleaved Double samples normalised to [-1.0, 1.0], plus the sample rate and channel count needed to interpret them.

    Interleaved layout keeps decoding, resampling and re-encoding simple: frame f channel c lives at data[f * channels + c]. Samples are stored as Double because every integer PCM width up to 32 bit converts losslessly into it, and DSP stages work in floating point anyway.

    AudioBuffer::append

    fn AudioBuffer::append(self : AudioBuffer, other : AudioBuffer) -> Unit raise AudioError

    Append every frame of another buffer that shares the same layout.

    AudioBuffer::apply_gain

    fn AudioBuffer::apply_gain(self : AudioBuffer, gain : Double) -> Unit

    Multiply every sample by gain.

    AudioBuffer::apply_gain_clamped

    fn AudioBuffer::apply_gain_clamped(self : AudioBuffer, gain : Double) -> Unit

    Multiply every sample by gain while keeping |sample| <= 1.0.

    AudioBuffer::copy

    fn AudioBuffer::copy(self : AudioBuffer) -> AudioBuffer

    Return a copy of the buffer.

    AudioBuffer::duration_seconds

    fn AudioBuffer::duration_seconds(self : AudioBuffer) -> Double

    Playback duration in seconds.

    AudioBuffer::frames

    fn AudioBuffer::frames(self : AudioBuffer) -> Int

    Number of frames (one sample per channel) in the buffer.

    AudioBuffer::from_channels

    fn AudioBuffer::from_channels(sample_rate : Int, channels : Array[Array[Double]]) -> AudioBuffer raise AudioError

    Build a buffer from per-channel arrays, converting to interleaved layout.

    AudioBuffer::is_empty

    fn AudioBuffer::is_empty(self : AudioBuffer) -> Bool

    true when the buffer carries no frames.

    AudioBuffer::new

    fn AudioBuffer::new(sample_rate : Int, channels : Int) -> AudioBuffer

    Create an empty buffer with sample_rate and channels.

    AudioBuffer::normalize

    fn AudioBuffer::normalize(self : AudioBuffer, target? : Double) -> Double

    Scale every sample so the peak equals target (in [0, 1]).

    Returns the applied linear gain; a silent buffer is left untouched.

    AudioBuffer::peak

    fn AudioBuffer::peak(self : AudioBuffer) -> Double

    Largest absolute sample value in the buffer.

    AudioBuffer::peak_dbfs

    fn AudioBuffer::peak_dbfs(self : AudioBuffer) -> Double

    Peak level in dBFS (-inf for silence).

    AudioBuffer::push_frame

    fn AudioBuffer::push_frame(self : AudioBuffer, samples : ArrayView[Double]) -> Unit

    Append one frame given as a slice of per-channel samples.

    The slice is padded with silence or truncated when its length differs from the channel count.

    AudioBuffer::remix

    fn AudioBuffer::remix(self : AudioBuffer, channels : Int) -> AudioBuffer raise AudioError

    Downmix or upmix to channels channels.

    Mono to stereo duplicates the channel, stereo to mono averages both channels, and other conversions average all source channels then replicate the result, which keeps analysis pipelines predictable.

    AudioBuffer::rms

    fn AudioBuffer::rms(self : AudioBuffer) -> Double

    Root mean square level of all samples.

    AudioBuffer::rms_dbfs

    fn AudioBuffer::rms_dbfs(self : AudioBuffer) -> Double

    RMS level in dBFS (-inf for silence).

    AudioBuffer::sample

    fn AudioBuffer::sample(self : AudioBuffer, frame : Int, channel : Int) -> Double

    Read one sample; out of range reads yield 0.0 so DSP stages stay total.

    AudioBuffer::set_sample

    fn AudioBuffer::set_sample(self : AudioBuffer, frame : Int, channel : Int, value : Double) -> Unit

    Overwrite one sample, ignoring writes outside the buffer.

    AudioBuffer::silence

    fn AudioBuffer::silence(sample_rate : Int, channels : Int, frames : Int) -> AudioBuffer raise AudioError

    Create a silent buffer that already contains frames frames.

    AudioBuffer::slice

    fn AudioBuffer::slice(self : AudioBuffer, start : Int, end : Int) -> AudioBuffer

    Return the frame range [start, end) as a new buffer.

    AudioBuffer::to_channels

    fn AudioBuffer::to_channels(self : AudioBuffer) -> Array[Array[Double]]

    Split the buffer into per-channel arrays.

    SampleFormat

    pub(all) enum SampleFormat {
    U8
    I16
    I24
    I32
    F32
    F64
    }

    Encoding of a single sample as stored in a container.

    moonaudio always decodes samples to Double in the closed interval [-1.0, 1.0], so this type only describes the source (or requested target) representation.

    SampleFormat::bits

    fn SampleFormat::bits(self : SampleFormat) -> Int

    Number of bits occupied by one sample of this format.

    SampleFormat::bytes

    fn SampleFormat::bytes(self : SampleFormat) -> Int

    Number of bytes occupied by one sample of this format.

    SampleFormat::is_float

    fn SampleFormat::is_float(self : SampleFormat) -> Bool

    true when the format stores IEEE floating point samples.

    SampleFormat::is_signed

    fn SampleFormat::is_signed(self : SampleFormat) -> Bool

    true when the format stores signed integers.

    SampleFormat::max_code

    fn SampleFormat::max_code(self : SampleFormat) -> Double

    Largest integer code of the format (1.0 for float formats).

    SampleFormat::parse

    fn SampleFormat::parse(name : String) -> SampleFormat raise AudioError

    Parse a format name such as "s16", "s24", "f32" or "u8".

    The accepted spellings follow ffmpeg style tooling, with a few aliases (pcm16, float32, int24, ...) so the CLI is forgiving.

    SampleFormat::same

    fn SampleFormat::same(self : SampleFormat, other : SampleFormat) -> Bool

    true when two sample formats are the same variant.

    SampleFormat::to_name

    fn SampleFormat::to_name(self : SampleFormat) -> String

    Canonical short name of a sample format, the inverse of [SampleFormat::parse].

    alaw_decode

    fn alaw_decode(byte : Int) -> Double

    G.711 A-law expansion, as used by WAVE_FORMAT_ALAW and Sun .au encoding 27.

    alaw_encode

    fn alaw_encode(sample : Double) -> Int

    G.711 A-law compression, the inverse of [alaw_decode].

    ascii_at

    fn ascii_at(data : Bytes, offset : Int, length : Int) -> String?

    Interpret length bytes starting at offset as ASCII, or None when the range leaves the buffer.

    ascii_is

    fn ascii_is(data : Bytes, offset : Int, length : Int, expected : String) -> Bool

    true when data[offset .. offset + length] equals expected.

    clamp_sample

    fn clamp_sample(value : Double) -> Double

    Clamp a normalised sample into [-1.0, 1.0], mapping NaN to 0.0.

    Every encoder in moonaudio funnels integer conversion through this function, so a DSP stage that overshoots can never wrap around to the opposite polarity.

    dbfs

    fn dbfs(amplitude : Double) -> Double

    Linear amplitude to dBFS, with 0.0 mapped to negative infinity.

    dequantize

    fn dequantize(code : Int, format : SampleFormat) -> Double

    Convert an integer code of format back to a normalised sample.

    double_to_f32_bits

    fn double_to_f32_bits(value : Double) -> Int64

    Encode a Double as an IEEE 754 single precision bit pattern.

    Values outside the single precision range saturate to infinity and NaN is canonicalised, which keeps encoder output deterministic.

    f32_bits_to_double

    fn f32_bits_to_double(bits : Int64) -> Double

    Decode an IEEE 754 single precision pattern into a Double.

    Implemented by hand so the library does not depend on a host float reinterpretation intrinsic; the operation is exact for every finite value.

    from_dbfs

    fn from_dbfs(decibels : Double) -> Double

    dBFS back to linear amplitude.

    invalid

    fn invalid(message : String) -> AudioError

    Construct an Invalid error with a message.

    lerp

    fn lerp(a : Double, b : Double, t : Double) -> Double

    Linear interpolation between a and b.

    mulaw_decode

    fn mulaw_decode(byte : Int) -> Double

    G.711 mu-law expansion, as used by WAVE_FORMAT_MULAW and Sun .au encoding 1.

    The table is generated arithmetically instead of stored, which keeps the decoder allocation free and exact.

    mulaw_encode

    fn mulaw_encode(sample : Double) -> Int

    G.711 mu-law compression, the inverse of [mulaw_decode].

    out_of_range

    fn out_of_range(message : String) -> AudioError

    Construct an OutOfRange error with a message.

    quantize

    fn quantize(value : Double, format : SampleFormat) -> Int

    Convert a normalised sample to the integer code used by format.

    Unsigned 8 bit uses offset binary (0 is -1.0, 255 is +1.0); signed formats use two's complement. Float formats return 0 and exist here only so callers can treat every format uniformly.

    read_ascii

    fn read_ascii(data : Bytes, offset : Int, length : Int) -> String raise AudioError

    Read length bytes as an ASCII string.

    read_f32be

    fn read_f32be(data : Bytes, offset : Int) -> Double raise AudioError

    Read a big endian IEEE 754 single precision sample.

    read_f32le

    fn read_f32le(data : Bytes, offset : Int) -> Double raise AudioError

    Read a little endian IEEE 754 single precision sample.

    read_f64be

    fn read_f64be(data : Bytes, offset : Int) -> Double raise AudioError

    Read a big endian IEEE 754 double precision sample.

    read_f64le

    fn read_f64le(data : Bytes, offset : Int) -> Double raise AudioError

    Read a little endian IEEE 754 double precision sample.

    read_i16be

    fn read_i16be(data : Bytes, offset : Int) -> Int raise AudioError

    Read a big endian signed 16 bit integer, sign extended to Int32.

    read_i16le

    fn read_i16le(data : Bytes, offset : Int) -> Int raise AudioError

    Read a little endian signed 16 bit integer, sign extended to Int32.

    read_i24be

    fn read_i24be(data : Bytes, offset : Int) -> Int raise AudioError

    Read a big endian signed 24 bit integer, sign extended to Int32.

    read_i24le

    fn read_i24le(data : Bytes, offset : Int) -> Int raise AudioError

    Read a little endian signed 24 bit integer, sign extended to Int32.

    read_i32be

    fn read_i32be(data : Bytes, offset : Int) -> Int raise AudioError

    Read a big endian signed 32 bit integer.

    read_i32le

    fn read_i32le(data : Bytes, offset : Int) -> Int raise AudioError

    Read a little endian signed 32 bit integer.

    read_u16be

    fn read_u16be(data : Bytes, offset : Int) -> Int raise AudioError

    Read a big endian unsigned 16 bit integer.

    read_u16le

    fn read_u16le(data : Bytes, offset : Int) -> Int raise AudioError

    Read a little endian unsigned 16 bit integer.

    read_u24be

    fn read_u24be(data : Bytes, offset : Int) -> Int raise AudioError

    Read a big endian unsigned 24 bit integer.

    read_u24le

    fn read_u24le(data : Bytes, offset : Int) -> Int raise AudioError

    Read a little endian unsigned 24 bit integer.

    read_u32be

    fn read_u32be(data : Bytes, offset : Int) -> Int64 raise AudioError

    Read a big endian unsigned 32 bit integer, widened to Int64.

    read_u32le

    fn read_u32le(data : Bytes, offset : Int) -> Int64 raise AudioError

    Read a little endian unsigned 32 bit integer, widened to Int64 so the value stays positive.

    read_u8

    fn read_u8(data : Bytes, offset : Int) -> Int raise AudioError

    Read one unsigned byte.

    require_bytes

    fn require_bytes(data : Bytes, offset : Int, length : Int, what : String) -> Unit raise AudioError

    Bounds check used by every byte level reader.

    Audio containers are read from untrusted files, so every multi-byte read is guarded: a malformed length must surface as a Truncated error instead of a trap.

    sign_extend

    fn sign_extend(raw : Int, bits : Int) -> Int

    Sign extend raw from bits bits to 32 bits.

    Used by containers that keep samples at their native width but report a different valid bit count (WAVE_FORMAT_EXTENSIBLE, 20 bit in 24 bit slots).

    truncated

    fn truncated(message : String) -> AudioError

    Construct a Truncated error with a message.

    u16le_bytes

    fn u16le_bytes(value : Int) -> Bytes

    Little endian 16 bit pair of bytes, handy for chunk headers.

    unsupported

    fn unsupported(message : String) -> AudioError

    Construct an Unsupported error with a message.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io