Use lmaudio from LMScript

lmaudio is an offline audio host for LMScript. A typed lmcode block synthesizes, processes and measures sound in one native process and returns numbers, CSV tables and WAV files.

Purpose

Use lmaudio to generate test signals and instrument voices, sequence notes and drum patterns from text, apply filters, dynamics and reverbs, and measure pitch, loudness, spectra and reverberation time. Results are deterministic, so a script can assert a known answer before it writes a file.

When to use it

Install

Install the LMScript compiler from npm. The host itself needs no separate install step: the install declaration in the block downloads the release binary into the lmscript tool cache when lmaudio is not on PATH.

npm install -g @lmctl-ai/lmctl
lmscript --version

The latest release is v0.2.2. Its release page lists lmaudio-linux-x64, lmaudio-linux-arm64, lmaudio-macos-x64, lmaudio-macos-arm64 and lmaudio-windows-x64.exe with a SHA256SUMS file. The repository README states that the current release lane builds the two Linux binaries, against glibc 2.35 or newer with all third-party libraries linked statically, so treat Linux as the supported platform for new releases. Use run_lmcode with lmscript 0.2.454 or newer; these pages were checked with lmscript 0.2.482.

Commands

The normal command is lmscript on a script that contains an lmcode block and calls run_lmcode. The host flags below are the protocol the compiler uses, and they are useful for inspection.

lmscript hello.lms
lmscript --check hello.lms
lmaudio --version
lmaudio --lmcode-schema --json
lmaudio --lmcode-signatures
lmaudio --notices
Command or variableEffect
lmaudio --versionPrints the release identity, for example lmaudio 0.2.2.
lmaudio --noticesPrints the third-party software notices with the upstream license texts.
lmaudio --lmcode-schemaPrints the embedded schema as readable JSON.
lmaudio --lmcode-schema --jsonPrints the exact schema bytes the compiler binds a program to (schema version 2, name lmaudio).
lmaudio --lmcode-signaturesPrints every canonical signature the host registry provides, one per line, standard catalog included.
lmaudio --lmcode FILE [FILE ...]Runs compiled lmcode program files. This is what run_lmcode calls; the compiler owns the program files.
lmaudio --dry-run --lmcode FILERuns a program without writing any file. The flag comes before --lmcode. The result envelope carries dry true.
LMAUDIO_MAX_MEMORY_MBEnvironment variable: total memory budget in MiB, default 4096. A value that is not a positive whole number is a usage error on every command.

lmscript --check compiles every block without running it. It needs the host schema, so run the script once, or have lmaudio on PATH, before the first check on a new machine: with no cached schema the check stops with the message No cached host schema for "lmaudio".

lmaudio --version
lmaudio 0.2.2

Inputs and outputs

Inputs are the literals in the block, values spliced from LMScript with ${...}, and WAV files read with audio.load_wav (PCM16, PCM24, float32 and WAVE_FORMAT_EXTENSIBLE). Durations are seconds, frequencies are Hz, and the sample rate argument is optional with a default of 44100.

OutputHowNotes
Numbers to LMScriptlmcode_return_put(name, value) or lmcode_return_set(value)They arrive as r.result after r = run_lmcode(block).
Report textaudio.print(text)Written to stderr, so it appears in the terminal and stays out of the result.
WAVBuffer.save_wav(path) or save_wav(path, bits)16, 24 or 32 bits. 16 and 24 bit output clips samples outside -1 to 1 and warns on stderr; NaN and infinity are refused.
CSVBuffer.save_csv, Spectrum.save_csv, Spectrogram.save_csv, Biquad.save_response_csv, Buffer.pitch_track_csvHeader line starts with a # character, for example # freq_hz,magnitude,db,phase_rad.
Textaudio.save_text(path, text)Any text file, for example plot settings.

A failed run still returns a map: ok is false and phase, code, message, line and column describe the failure. run_lmcode does not stop the script on a host failure, so test r.ok.

Safety rules

Worked examples

Arpeggio with a loudness check

# hello.lms: an A major arpeggio on a plucked string, checked and saved
mkdir("audio");
a = _lmcode_ cli="lmaudio" install="github:lmctl-ai/lmaudio@latest";
let arp = audio.sequence("A3:0.5 C#4:0.5 E4:0.5 A4:1.5", 120.0, "karplus", 22050);
let out = arp.reverb(0.8, 6500.0, 0.25).normalize_lufs(-16.0);
audio.assert_close(out.lufs(), -16.0, 0.01);
audio.print("pitch of the first note: " + str(arp.slice(0.0, 0.25).pitch()) + " Hz");
out.save_wav("audio/hello.wav", 16);
lmcode_return_put("lufs", out.lufs());
lmcode_return_put("seconds", out.duration());
end _lmcode_
r = run_lmcode(a);
println(json_stringify(r));
assert(r.ok);
lmscript hello.lms
pitch of the first note: 219.959988525778 Hz
lmaudio: warning: 1 sample(s) outside [-1, 1] were clipped while writing audio/hello.wav (16-bit PCM); normalize() first or save with bits=32
{"ok":true,"result":{"lufs":-16.00000004683295,"seconds":1.5000907029478459}}

The warning is real output: reverb pushed one sample above full scale after loudness normalization, and 16 bit output clipped it.

Drum pattern from text

mkdir("audio");
a = _lmcode_ cli="lmaudio" install="github:lmctl-ai/lmaudio@latest";
let sr = 22050;
let bpm = 124.0;
let kick = audio.drum("kick", 55.0, 0.5, 0.55, 0.45, 0.9, sr).normalize(0.95);
let hat = audio.drum("hihat", 3800.0, 0.06, 0.15, 0.7, 0.75, sr).normalize(0.95);
audio.assert_close(kick.slice(0.05, 0.45).pitch(30.0, 200.0), 55.0, 1.0);
let k = kick.pattern("X...X...X...X...", bpm, 4);
let h = hat.pattern("xo.oxo.oxo.oxo.o", bpm, 4);
let groove = k.mix(h, 0.25).normalize(0.9);
groove.save_wav("audio/groove.wav", 16);
lmcode_return_put("seconds", groove.duration());
lmcode_return_put("peak", groove.peak());
lmcode_return_put("kick_hz", kick.slice(0.05, 0.45).pitch(30.0, 200.0));
end _lmcode_
r = run_lmcode(a);
require(r.ok, "host failed");
println(json_stringify(r.result));
lmscript drums.lms
{"seconds":1.951609977324263,"peak":0.9000000357627869,"kick_hz":54.95497239392236}

Filter design, exact response and spectrum

mkdir("build");
a = _lmcode_ cli="lmaudio" install="github:lmctl-ai/lmaudio@latest";
let sr = 44100;
let lp = audio.biquad_design("lowpass", 800.0, 0.70710678, sr);
audio.assert_close(lp.db_at(800.0), -3.0103, 0.01);
lp.save_response_csv("build/lowpass.csv", 400, 20.0, 20000.0);
let saw = audio.saw(220.0, 1.0, 0.8, sr);
let soft = lp.apply(saw);
let spec = soft.spectrum("hann", 16384);
spec.save_csv("build/spectrum.csv", 5000.0);
lmcode_return_put("gain_db_at_800", lp.db_at(800.0));
lmcode_return_put("fundamental_hz", spec.peak_freq_in(150.0, 300.0));
lmcode_return_put("centroid_before", saw.centroid());
lmcode_return_put("centroid_after", soft.centroid());
end _lmcode_
r = run_lmcode(a);
require(r.ok, "host failed: " + r.get("message", ""));
println(json_stringify(r.result));
lmscript filter.lms
{"gain_db_at_800":-3.010299971215047,"fundamental_hz":219.95764983987954,"centroid_before":3862.1785215452837,"centroid_after":587.2414572204808}

The repository has 22 longer examples that also draw their figures with lmplot and lmdraw, for example drum_machine_pattern.lms and convolution_reverb_rt60.lms.

Errors and exit codes

Exit codeEnvelopeMeaning
0ok trueThe program ran and every audio.assert_close held.
2phase cli, code CLI_USAGEUnknown or misplaced arguments, or an invalid LMAUDIO_MAX_MEMORY_MB.
3phase load, for example code LOAD_BAD_BYTECODE or LOAD_UNBOUNDThe program file cannot be read, or it imports a function the host does not provide.
3phase run, code RUN_HOSTA host error with a source line and column: a failed audio.assert_close, a missing WAV file, an invalid argument, or the memory budget being exceeded.

Errors print one JSON envelope on stdout and one readable line on stderr. A failed assertion looks like this from LMScript:

a = _lmcode_ cli="lmaudio" install="github:lmctl-ai/lmaudio@latest";
let s = audio.sine(440.0, 0.5, 0.5, 22050);
audio.assert_close(s.pitch(), 100.0, 1.0);
end _lmcode_
r = run_lmcode(a);
println(json_stringify(r));
lmscript fail.lms
lmaudio: dsl line 3:7: assert_close failed: got 440.0002658, expected 100 (|diff| = 340.0002658 > 1)
{"ok":false,"phase":"run","code":"RUN_HOST","message":"dsl line 3:7: assert_close failed: got 440.0002658, expected 100 (|diff| = 340.0002658 > 1)","line":3,"column":7}
SymptomFix
Compile error: type Audio has no method NAMEThe compiler lists the valid methods in the message. Check the name against the reference.
No cached host schema for "lmaudio" during --checkRun the script once or put lmaudio on PATH, then check again.
cannot open WAV fileThe path is relative to the directory where lmscript runs.
LOAD_UNBOUND: import is not provided by the hostThe block calls a standard function that this release does not ship, such as the REST group.
invalid lmaudio arguments or memory budgetCheck flag order (--dry-run goes before --lmcode) and LMAUDIO_MAX_MEMORY_MB.
Warning that samples were clippedCall normalize first or save with 32 bits.

How to verify

  1. Check the host identity and surface: lmaudio --version prints the release, and lmaudio --lmcode-signatures prints 377 lines for v0.2.2.
  2. Compile without running: lmscript --check file.lms exits 0 when every block type-checks.
  3. Assert inside the block with audio.assert_close(actual, expected, tol) on a measured value such as pitch, lufs or rt60.
  4. Return the measured values with lmcode_return_put and assert r.ok in LMScript.
  5. Confirm that the expected WAV and CSV files exist and have a plausible size.
lmaudio --version
lmaudio --lmcode-signatures | wc -l
lmscript --check hello.lms
lmscript hello.lms
ls -l audio/hello.wav