Sign in

    moonmel

    Lightweight, portable audio feature extraction engine for MoonBit (Mel Spectrogram, Log-Mel, MFCC)

    audio
    dsp
    mel
    mfcc
    spectrogram
    Download zip
    Version
    0.1.0
    License
    MIT
    Last updated
    55 minutes ago
    Downloads
    2

    No dependencies

    #Run from source

    Install MoonBit with moonc >= 0.10.14. Node.js 22.12+ or 24+ and pnpm 11 are needed only for the browser demo. Source installation does not depend on registry publication.

    git clone https://github.com/1zhouhaoran1/MoonMel.git cd MoonMel moon run examples/basic --target js

    Expected output for the included one-second 440 Hz signal:

    STFT: 513 x 63 Mel: 80 x 63 Log-Mel: 80 x 63 MFCC: 13 x 63

    On the project's Windows workstation, select the installed isolated compiler first:

    . ./tools/dev/activate.ps1

    #Browser demonstration

    pnpm --dir playground install --frozen-lockfile pnpm --dir playground run dev

    Open http://localhost:5173. predev and prebuild regenerate the browser bindings from current MoonBit sources, so compiler output is not maintained by hand. The page executes generated JavaScript in a Web Worker; WASM is a supported library build target. Audio stays on the device.

    ActionResult
    Select a synthetic tone, sweep, mixture or silenceSee an actual MoonBit spectrogram and waveform
    Upload an 8/16-bit PCM WAVDecode with MoonMel, downmix channels and preview up to ten seconds
    Change FFT/hop/Mel/MFCC parametersRecompute the currently selected audio
    Select Log-Mel, STFT or MFCCInspect values by pointer or left/right keys on the plot
    Play, capture a microphone, exportHear input, analyze a rolling one-second window, download JSON/CSV

    Microphone capture requires HTTPS or localhost and explicit browser permission. It uses AudioWorklet; it is a rolling-window demo, not the incremental streaming library API. The page caps uploaded files at 10 MB and preview analysis at 2,000 frames. Shorten audio or increase hop length when a limit is reached.

    #API

    let cfg = @moonmel.FeatureConfig::default()
    let result = @moonmel.analyze(samples, config=cfg).unwrap()
    println(result.mfcc.n_cols)

    analyze computes one shared STFT and filterbank, then derives Mel, Log-Mel and MFCC in order. Standalone stft, mel_spectrogram, log_mel_spectrogram, mfcc and the existing feature APIs remain available. WAV input uses @moonmel.decode_wav(bytes); then analyze audio.samples with a config whose sample_rate and f_max match the decoded rate.

    All feature matrices use [feature row][time frame]. The complex STFT uses [time frame][frequency bin]. Results use Result[T, MelError]; handle errors for user-controlled input rather than unwrapping. Public raw matrices and low-level kernels retain documented preconditions.

    #Numerical evidence and limitations

    The machine-readable report is produced by tools/reference/check_parity.py using librosa 1.0.0, NumPy 2.5.3 and SciPy 1.18.1. It compares eight deterministic cases, including short windows, arbitrary/odd FFTs and single-sample input. All Mel/Log-Mel/MFCC cells are compared; STFT checks up to 64 explicitly indexed bins in each frame. The comparison aligns periodic Hann, reflect padding, float64 Slaney filters and orthonormal DCT-II, with atol=rtol=1e-7. Observed maximum error across checked values is about 2.3e-12.

    This is not whole-library parity or a competitor speed ranking. In particular, librosa's default STFT padding is constant, whereas MoonMel uses reflect. HTK-style deltas, direct CQT, Chroma, HPSS, loudness and effects have their own documented conventions and are not claimed as drop-in equivalents. The Whisper configuration is a framing preset, not a complete model frontend.

    LibraryImplementation / fitDifference relevant to this project
    MoonMelPure MoonBit; compiled JS/WASM/NativeNative MoonBit integration, inspectable core pipeline; external coverage of advanced features is still limited
    librosaPython / NumPy / SciPy research ecosystemReference for this project's aligned core numerical checks
    MeydaJavaScript and Web Audio featuresEstablished browser feature integration
    Essentia.jsEssentia C++ through WASM with JS/TS APIBroader music algorithms and model integration

    #Checks and continuous integration

    node tools/quality/check-toolchain.mjs node tools/quality/check-architecture.mjs moon check --deny-warn moon test --deny-warn moon test --target js --deny-warn pnpm --dir playground run build pnpm --dir playground test

    Project CI runs check/build/test and the basic example for WASM, WASM-GC, JS and Native, plus a fresh playground build and binding integration tests. CI validates compiler minimum version. A configured workflow is not evidence that a remote run has passed; consult GitHub Actions for that state.

    For Windows native validation, run pwsh -File tools/quality/test-native.ps1; it uses an available MinGW GCC when no compiler override is set and restores its environment afterward. The local MSVC discovery points to missing headers, so it cannot be used as-is.

    Optional independent Python validation, using the prepared project environment:

    & A:\DevEnv\Envs\moonmel_py\Scripts\python.exe tools/reference/check_parity.py

    See Development for a portable uv setup, Acceptance for the nine-item acceptance guide, and Health for reviewed packages and remaining limits. Mooncakes publication is deferred at the owner's request.

    #License

    MIT, copyright 2026 zhr. Runtime algorithms are implemented in MoonBit; NumPy/SciPy/librosa are development reference dependencies. See third-party notices for fonts and reference sources.

    AnalysisResult

    One analysis result with shared intermediate matrices.

    AudioBuffer

    AudioBuffer stores single-channel 64-bit float PCM audio samples and metadata.

    BiquadCoeffs

    Re-export Biquad filter coefficients.

    BiquadFilter

    Re-export Biquad filter state.

    ChromaNorm

    Re-export Chroma normalization enum.

    Complex

    Complex represents a 2D complex number with Double precision.

    ContrastConfig

    Re-export Octave Spectral Contrast configuration.

    CqtConfig

    Re-export Constant-Q Transform config.

    FeatureConfig

    FeatureConfig stores parameters for STFT, Mel, and MFCC computation.

    FeatureMatrix

    FeatureMatrix stores 2D feature matrices [n_rows][n_cols]. Usually n_rows = feature dimension (n_mels or n_mfcc or n_freq_bins), and n_cols = number of time frames.

    Formant

    Re-export Formant types and configuration.

    FormantConfig

    Configuration parameters for formant extraction.

    FormantFrame

    Formant analysis result for a single audio frame.

    HpssConfig

    Re-export HPSS configuration.

    HpssResult

    Re-export HPSS separation result.

    LogScale

    Log compression representation.

    LoudnessResult

    Re-export ITU-R BS.1770-4 / EBU R128 Loudness result.

    LpcResult

    Re-export LPC solver result.

    MaskMode

    Re-export HPSS mask mode.

    MelError

    MelError represents potential failure states in audio feature extraction.

    MelNorm

    Normalization options for triangular mel filters.

    MelScale

    Mel scale formula choices.

    MultiChannelAudioBuffer

    MultiChannelAudioBuffer stores multi-channel 64-bit float PCM audio samples.

    OnsetConfig

    Re-export Onset configuration.

    OnsetMethod

    Re-export Onset method enum.

    PitchTrack

    Re-export Pitch tracking result.

    StreamingExtractor

    Re-export streaming extractor type alias.

    TempoConfig

    Re-export Tempo configuration.

    TempogramResult

    Re-export Tempogram analysis result.

    WindowType

    Window function choices for framing.

    YinConfig

    Re-export YIN pitch tracking configuration.

    apply_k_weighting

    fn apply_k_weighting(samples : Array[Double], sample_rate : Int) -> Array[Double]

    Re-export K-weighting filtering.

    autocorrelation

    fn autocorrelation(samples : Array[Double], max_lag : Int) -> Array[Double]

    Re-export biased autocorrelation.

    bluestein_fft

    Re-export Arbitrary-length DFT via Bluestein's Algorithm.

    bluestein_ifft

    Re-export Arbitrary-length IDFT via Bluestein's Algorithm.

    chroma_filterbank

    fn chroma_filterbank(sample_rate : Int, n_fft : Int, tuning? : Double, f_min? : Double) ->
    FeatureMatrix

    Re-export Chroma filterbank generation.

    chroma_from_spectrogram

    Re-export Chroma from spectrogram.

    compute_delta

    Re-export Delta / Delta-Delta dynamic feature calculation.

    compute_energy

    Re-export total frame sum-of-squares energy extraction.

    compute_rms

    Re-export Root Mean Square (RMS) frame energy extraction.

    cqt_frequencies

    Re-export CQT center frequencies calculation.

    create_window

    fn create_window(kind :
    WindowType
    , n : Int) -> Array[Double]

    Re-export Window generator.

    cumulative_mean_normalized_difference

    fn cumulative_mean_normalized_difference(d : Array[Double]) -> Array[Double]

    Re-export Cumulative Mean Normalized Difference Function (CMNDF).

    dct_ii

    fn dct_ii(input : Array[Double], n_out : Int, norm_ortho : Bool) -> Array[Double]

    Re-export DCT-II calculation.

    deemphasis

    fn deemphasis(samples : Array[Double], alpha? : Double) -> Array[Double]

    Re-export de-emphasis inverse filter.

    dft

    Re-export Universal DFT (Radix-2 FFT when power of 2, Bluestein's algorithm otherwise).

    difference_function

    fn difference_function(frame : Array[Double], tau_max : Int) -> Array[Double]

    Re-export YIN difference function.

    durand_kerner

    fn durand_kerner(coeffs : Array[Double], max_iters? : Int, tol? : Double) -> Array[
    Complex
    ]

    Re-export Durand-Kerner polynomial root solver.

    fft_padded

    Re-export Forward FFT with zero-padding for arbitrary length signals.

    formant_frequencies

    fn formant_frequencies(frames : Array[
    FormantFrame
    ], formant_idx : Int, fallback? : Double) -> Array[Double]

    Re-export formant frequencies track extractor.

    formant_track

    Re-export Formant tracking across entire audio signal.

    formants_frame

    Re-export Formant extraction on single audio frame.

    harmonic_energy_ratio

    fn harmonic_energy_ratio(result :
    HpssResult
    ) -> Array[Double]

    Re-export frame harmonic energy ratio.

    idft

    Re-export Universal IDFT (Radix-2 IFFT when power of 2, Bluestein's algorithm otherwise).

    istft

    Re-export Inverse STFT (ISTFT) time-domain audio reconstruction.

    levinson_durbin

    Re-export Levinson-Durbin recursion.

    log_mel_spectrogram

    Re-export Log-Mel Spectrogram extraction.

    loudness_meter

    Re-export ITU-R BS.1770-4 Loudness Meter for audio samples.

    lpc

    Re-export LPC analysis on audio samples.

    lpc_features

    fn lpc_features(samples : Array[Double], order : Int, frame_length : Int, hop_length : Int) -> Result[
    FeatureMatrix
    ,
    MelError
    ]

    Re-export frame-wise LPC feature extraction.

    lpc_residual

    fn lpc_residual(samples : Array[Double], lpc_coeffs : Array[Double]) -> Array[Double]

    Re-export LPC residual filtering.

    lpc_to_lpcc

    fn lpc_to_lpcc(lpc_coeffs : Array[Double], prediction_error : Double, n_lpcc : Int) -> Array[Double]

    Re-export LPC to LPCC conversion.

    lpcc_features

    fn lpcc_features(samples : Array[Double], order : Int, n_lpcc : Int, frame_length : Int, hop_length : Int) -> Result[
    FeatureMatrix
    ,
    MelError
    ]

    Re-export frame-wise LPCC feature extraction.

    median_filter_1d

    fn median_filter_1d(arr : Array[Double], kernel_size : Int) -> Array[Double]

    Re-export 1D median filter.

    median_filter_freq

    Re-export 2D median filter along frequency axis.

    median_filter_time

    Re-export 2D median filter along time axis.

    onset_detect_audio

    Re-export onset frame detection directly from audio samples.

    onset_strength

    Re-export onset strength envelope computation.

    onset_strength_audio

    Re-export onset strength envelope directly from audio samples.

    pad_reflect

    fn pad_reflect(samples : Array[Double], pad_width : Int) -> Array[Double]

    Re-export Reflect padding.

    peak_pick

    fn peak_pick(envelope : Array[Double], pre_max? : Int, post_max? : Int, pre_avg? : Int, post_avg? : Int, delta? : Double, wait? : Int) -> Array[Int]

    Re-export adaptive peak picking.

    percussive_energy_ratio

    fn percussive_energy_ratio(result :
    HpssResult
    ) -> Array[Double]

    Re-export frame percussive energy ratio.

    phase_vocoder

    Re-export Phase Vocoder time stretching on complex STFT frames.

    pitch_class_name

    fn pitch_class_name(idx : Int) -> String

    Re-export pitch class name mapping.

    pitch_shift

    fn pitch_shift(samples : Array[Double], sample_rate? : Int, n_steps? : Double, config? :
    FeatureConfig
    ) -> Result[Array[Double],
    MelError
    ]

    Re-export pitch shifting on audio samples without tempo change.

    pitch_yin

    Re-export YIN pitch tracking on audio samples.

    power_to_db

    Re-export power to decibel conversion.

    preemphasis

    fn preemphasis(samples : Array[Double], alpha? : Double) -> Array[Double]

    Re-export pre-emphasis high-pass filter.

    resample

    fn resample(samples : Array[Double], orig_sr : Int, target_sr : Int) -> Result[Array[Double],
    MelError
    ]

    Re-export audio resampler (polyphase by default).

    resample_linear

    fn resample_linear(samples : Array[Double], orig_sr : Int, target_sr : Int) -> Result[Array[Double],
    MelError
    ]

    Re-export linear interpolation audio resampler.

    resample_polyphase

    fn resample_polyphase(samples : Array[Double], orig_sr : Int, target_sr : Int, half_len? : Int) -> Result[Array[Double],
    MelError
    ]

    Re-export polyphase rational factor audio resampler.

    resample_sinc

    fn resample_sinc(samples : Array[Double], orig_sr : Int, target_sr : Int, filter_size? : Int) -> Result[Array[Double],
    MelError
    ]

    Re-export windowed-sinc bandlimited audio resampler.

    resample_to_length

    fn resample_to_length(samples : Array[Double], target_len : Int, filter_size? : Int) -> Result[Array[Double],
    MelError
    ]

    Re-export windowed-sinc resampling to target length.

    rfft_padded

    fn rfft_padded(frame : Array[Double], power? : Double) -> Result[Array[Double],
    MelError
    ]

    Re-export Real FFT spectrum with zero-padding for arbitrary length frames.

    spectral_contrast

    Re-export Octave Spectral Contrast from magnitude spectrogram.

    spectral_contrast_audio

    Re-export Octave Spectral Contrast directly from audio samples.

    spectral_flatness

    Re-export Spectral Flatness extraction.

    spectral_flux

    Re-export spectral flux computation.

    spectral_rolloff

    Re-export Spectral Rolloff extraction.

    super_flux

    fn super_flux(spec :
    FeatureMatrix
    , filter_size : Int) -> Array[Double]

    Re-export SuperFlux computation.

    tempogram_from_onset

    fn tempogram_from_onset(onset_env : Array[Double], sample_rate : Int, hop_length : Int, config? :
    TempoConfig
    ) ->
    TempogramResult

    Re-export tempogram from onset envelope.

    time_stretch

    fn time_stretch(samples : Array[Double], rate : Double, config? :
    FeatureConfig
    ) -> Result[Array[Double],
    MelError
    ]

    Re-export time stretching on audio samples without pitch shift.

    true_peak_db

    fn true_peak_db(samples : Array[Double], sample_rate : Int) -> Result[Double,
    MelError
    ]

    Re-export True Peak measurement in dBFS via 4x oversampling.

    yin_frame

    fn yin_frame(frame : Array[Double], sample_rate : Int, f_min? : Double, f_max? : Double, threshold? : Double) -> (Double, Double, Double)

    Re-export single-frame YIN pitch estimation.

    zero_crossing_rate

    Re-export Zero-Crossing Rate extraction.

    Source Files