Industrial-grade procedural audio synthesizer, 8-bit retro sound FX generator and RIFF WAV encoder in pure MoonBit.
new Audio(dataUri).play();///|
let audio = @sfx.render_mml(
"T140 O4 C8 E8 G8 > C4",
Square(0.5),
ADSR::organ(),
44100,
)moon add zmjknn/moon_sfx{
"deps": {
"zmjknn/moon_sfx": "0.1.1"
}
}///|
/// In moon.pkg:
import {
"zmjknn/moon_sfx" @sfx,
}
///|
/// In main.mbt:
fn main {
// Generate a retro laser sound using seed 42
let wav_bytes = @sfx.play_laser_wav(42U)
println("Generated laser WAV: \{wav_bytes.length()} bytes")
}///|
fn make_synth_note() -> @sfx.AudioBuffer {
// Render a 440Hz Square wave for 0.5 seconds at 44.1kHz
let tone = @sfx.render_tone(
@sfx.Waveform::Square(0.5),
440.0,
0.5,
44100,
0.8,
)
// Apply percussive envelope
let adsr = @sfx.ADSR::percussive(0.4)
tone.apply_adsr(adsr, @sfx.Curve::Exponential)
// Add low-pass warmth and slight echo
tone.apply_filter(@sfx.FilterType::LowPass, 1200.0, 1.0)
tone.apply_echo(0.05, 0.4, 0.5)
tone
}///|
fn create_fanfare() -> String {
let score = @sfx.Score::victory_fanfare()
let audio = @sfx.render_score(
score,
140,
@sfx.Waveform::Square(0.5),
@sfx.ADSR::organ(),
44100,
)
audio.to_data_uri()
}moon run cmd/main==================================================================
moon_sfx: Procedural Retro Audio Synthesizer & Sound FX Engine
==================================================================
1. Generating Retro SFXR Sound FX Presets (44.1 kHz, 16-bit PCM):
------------------------------------------------------------------
- Laser Shoot: 25496 bytes (12726 samples, 288 ms)
- Jump Up: 26734 bytes (13345 samples, 302 ms)
- Coin Pickup: 25676 bytes (12816 samples, 290 ms)
- Explosion: 75184 bytes (37570 samples, 851 ms)
- Hit / Damage: 23414 bytes (11685 samples, 264 ms)
- Powerup: 53430 bytes (26693 samples, 605 ms)
- UI Blip Click: 8792 bytes (4374 samples, 99 ms)
- Repeating Burst: 35500 bytes (17728 samples, 401 ms)
2. Demonstrating Core Waveform Oscillators:
- Sine (440Hz): 8864 bytes
- Square PWM 50% (440Hz): 8864 bytes
- Sawtooth (440Hz): 8864 bytes
- Triangle (440Hz): 8864 bytes
- White Noise: 8864 bytes
3. Demonstrating DSP Digital Filters & Effects Pipeline:
- Base Sawtooth Tone: 8820 samples
- Applied Biquad Low-Pass Filter (800Hz, Q=1.4): OK
- Applied Cubic Overdrive Distortion (drive=3.0): OK
- Applied Retro Lo-Fi Bitcrusher (4-bit, 4x downsample): OK
- Applied Feedback Delay / Echo (delay=40ms, feedback=0.5): OK
4. Demonstrating Musical Score Sequencer:
- Victory Fanfare (BPM 140, Square Lead):
* Duration: 2464 ms
* Samples: 108675
* WAV Size: 217394 bytes
5. Interactive HTML5 Audio Playground Generation:
- Generated Single-File HTML5 Audio Studio: 1202294 characters
- Status: Ready for WebAssembly browser playback & zero-asset game audio!moon testTotal tests: 46, passed: 46, failed: 0.fn AudioBuffer::apply_echo(self : AudioBuffer, delay_seconds : Double, feedback : Double, wet_mix : Double) -> Unitfn AudioBuffer::apply_filter(self : AudioBuffer, filter_type : FilterType, cutoff_freq : Double, resonance_q : Double) -> Unitfn AudioBuffer::mix(self : AudioBuffer, other : AudioBuffer, offset_samples : Int, volume : Double) -> Unitpub struct BiquadFilter {
b0 : Double
b1 : Double
b2 : Double
a1 : Double
a2 : Double
x1 : Double
x2 : Double
y1 : Double
y2 : Double
}fn BiquadFilter::new(filter_type : FilterType, cutoff_freq : Double, resonance_q : Double, sample_rate : Int) -> BiquadFilterfn BiquadFilter::update_cutoff(self : BiquadFilter, filter_type : FilterType, cutoff_freq : Double, resonance_q : Double, sample_rate : Int) -> Unitfn Mixer::add_track(self : Mixer, buffer : AudioBuffer, offset_seconds : Double, volume : Double, pan : Double) -> Unitpub struct Rng {
state : UInt
}pub(all) struct SfxParams {
wave_type : Waveform
start_freq : Double
min_freq : Double
freq_slide : Double
freq_delta_slide : Double
square_duty : Double
duty_sweep : Double
vibrato_depth : Double
vibrato_speed : Double
attack_time : Double
sustain_time : Double
sustain_punch : Double
decay_time : Double
lpf_cutoff : Double
lpf_resonance : Double
lpf_sweep : Double
hpf_cutoff : Double
hpf_sweep : Double
phaser_offset : Double
phaser_sweep : Double
repeat_speed : Double
change_amount : Double
change_time : Double
master_volume : Double
} derive(Eq, Debug)pub struct Track {
buffer : AudioBuffer
offset_seconds : Double
volume : Double
pan : Double
mute : Bool
} derive(Eq)fn generate_html_playground() -> Stringfn play_blip_wav(seed : UInt) -> Bytesfn play_burst_wav(seed : UInt) -> Bytesfn play_coin_wav(seed : UInt) -> Bytesfn play_explosion_wav(seed : UInt) -> Bytesfn play_hit_wav(seed : UInt) -> Bytesfn play_jump_wav(seed : UInt) -> Bytesfn play_laser_wav(seed : UInt) -> Bytesfn play_powerup_wav(seed : UInt) -> Bytesfn render_fm_dynamic(carrier_freq : Double, mod_freq : Double, mod_index_start : Double, mod_index_end : Double, duration : Double, sample_rate : Int, adsr : ADSR, volume : Double) -> AudioBufferfn render_fm_tone(carrier_freq : Double, mod_freq : Double, mod_index : Double, duration : Double, sample_rate : Int, volume : Double) -> AudioBufferfn render_score(score : Score, bpm : Int, waveform : Waveform, adsr : ADSR, sample_rate : Int) -> AudioBufferfn render_tone(waveform : Waveform, frequency : Double, duration_sec : Double, sample_rate : Int, volume : Double) -> AudioBufferfn to_base64(bytes : Bytes) -> Stringfn write_u16_le(b : FixedArray[Byte], offset : Int, val : Int) -> Unitfn write_u32_le(b : FixedArray[Byte], offset : Int, val : Int) -> UnitInstall
Download zipIndustrial-grade procedural audio synthesizer, 8-bit retro sound FX generator and RIFF WAV encoder in pure MoonBit.