audio

    Foundational audio DSP library: samples, buffers, oscillators, envelopes, biquad filters, ring buffers, windows and FFT. Pure, in-memory, all targets.

    audio
    dsp
    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    8 hours ago
    Downloads
    7

    No dependencies

    #maya0513/audio

    Foundational audio DSP library, written in pure MoonBit. In-memory only: no FFI, no file IO, no async, so every package builds and tests on all targets (wasm, wasm-gc, js, native).

    Internal sample representation is Double (Array[Double]), following the Sample / Frame / Signal separation of Rust's dasp and the API surface of the pure-MoonBit moonbit-dsp package.

    Status: not published. The source lives on GitHub (github.com/maya0513/audio.mbt); it is not on mooncakes.io yet.

    #Scope and non-goals

    This library is a set of foundational building blocks for audio signal processing over neutral in-memory data: unit samples, buffers of samples, and byte streams. Everything is a pure function over that data: no IO, no FFI, no async, and failures are explicit typed errors rather than panics or silent coercion.

    The boundary, in one list:

    • In scope: unit-sample conversion, in-memory buffers, signal generation (oscillators, noise), envelopes, filters, ring buffers, window functions, FFT, and an in-memory WAV codec.
    • The outside world is out of scope. Audio devices, realtime streaming and file IO never appear here; hosts move bytes and samples across the boundary.
    • Application semantics are out of scope. Tracks, clips, sequencing, mixing desks, effect chains and plug-in hosting are interpretations layered over these primitives by other libraries, not primitives themselves.
    • There is no spectral processing beyond the transform itself: no STFT, no phase vocoding, no spectral editing.
    • Loudness-standard normalisation (LUFS and friends) is an application-level convention and is out of scope; the only normalisation here is scaling to an explicit numeric target.
    • Music theory is out of scope. Pitch, scales and chords live in maya0513/music; this library only receives frequencies as Double hertz values. The two libraries never import each other; composition happens in libraries built on top of both.

    Areas admitted only in a restricted form:

    • Dynamics is limited to detector and gain-computation building blocks: no presets, no automatic makeup gain, no side-chain routing, and no packaged effect units.
    • Anything randomised (dither, coloured noise) takes an explicit seed and stays deterministic, like the existing noise generator; there is no global random source.
    • WAV chunk metadata beyond the audio data itself (smpl loop points) is exposed only as raw numeric metadata kept separate from AudioBuffer, with no playback semantics attached.
    • Resampling and FIR design are pure functions of their numeric parameters (rates, cutoffs, tap counts); no device or file concerns enter.

    #Packages

    PackageContents
    maya0513/audio/sampleUnit-sample conversions (Double <-> Int16/Int32/Float) with saturation, dB <-> linear gain, deterministic TPDF Ditherer. clamp_int16 is proven.
    maya0513/audio/bufferInterleaved AudioBuffer: planar conversion, gain, mix, clip, peak / RMS, normalise to an explicit peak target, fades, silence trim, crossfade, mid/side, reverse.
    maya0513/audio/oscillatorPhase-accumulator oscillator (sine / square / sawtooth / triangle) with polyBLEP on the discontinuous waveforms, TableOscillator over caller-supplied tables, and a deterministic LCG noise generator (white / pink / brown).
    maya0513/audio/envelopeADSR, AR and ASR envelopes as sample-stepped state machines, with retrigger-from-current-level.
    maya0513/audio/filterRBJ Audio EQ Cookbook biquads (the full family: low/high-pass, both band-pass forms, notch, all-pass, peaking, shelves), Direct Form I processing, Zavalishin trapezoidal SVF, one-pole low-pass and DC blocker.
    maya0513/audio/ringbufferFixed-capacity ring buffer. normalize_index is proven.
    maya0513/audio/windowRectangular / Hann / Hamming / Blackman / Blackman-Harris / Kaiser / FlatTop / Bartlett / Welch windows.
    maya0513/audio/fftRadix-2 FFT / IFFT over a Complex type, magnitude spectrum, real-input FFT, arbitrary-length DFT (Bluestein), DCT-II/III, Goertzel single-bin analysis. Floating-point DSP is covered by tolerance tests, not proofs.
    maya0513/audio/firWindowed-sinc FIR design (low-pass / high-pass), direct convolution and FFT-based convolution.
    maya0513/audio/resampleWindowed-sinc resampling between two sample rates given as parameters.
    maya0513/audio/dynamicsPeak / RMS level detector with attack/release smoothing, and a static soft-knee gain-computer curve. No presets, makeup gain or side-chaining.
    maya0513/audio/analysisNumeric analysis only: autocorrelation, zero-crossing rate, spectral centroid and flatness.
    maya0513/audio/wavIn-memory WAV codec on Bytes: PCM 8/16/24/32-bit and float32 encode / decode (chunk skipping, header validation, roundtrip-tested), plus smpl chunk raw numeric metadata. Unsupported formats are rejected explicitly. No file IO.

    #Example

    let osc = @oscillator.Oscillator::new(@oscillator.Waveform::Sine, frequency=440.0, sample_rate=48000)
    let samples = osc.render(4800) // 0.1 s of A4

    #Development

    The toolchain is pinned with mise (mise.toml): MoonBit 1.0.0-rc1 via vfox:maya0513/vfox-moonbit, plus Z3 for moon prove. All day-to-day commands are in the justfile:

    just check # moon check --deny-warn --target all just test # moon test on native, wasm, wasm-gc and js just coverage # must report no uncovered lines just prove # moon prove for the proof-enabled packages just bench # moon bench --target native just ci # everything

    Library packages keep 100% test coverage (moon coverage analyze reports no uncovered lines). Benchmarks are recorded in docs/benchmarks.md.