voltergeist in C
This page is c/README.md from the repository.
voltergeist.h is the wire format of SPEC.md in plain C99, one header, for plugin hosts (VCV Rack, Max, Pure Data,
CLAP or VST through a wrapper, TouchDesigner) and microcontrollers (Pico, Daisy Seed, Teensy). The JavaScript in
src/ stays the reference; both pass the same vectors in vectors/.
- No allocation: the caller passes every buffer. No global state. No dependencies beyond
<stdint.h>,<stddef.h>and<string.h>; no<math.h>. - Numbers are read and written byte by byte, so it is correct on any CPU. It needs
floatas IEEE binary32 anddoubleas binary64 (every ARM, x86 and RISC-V target; not AVR, wheredoubleis 32 bits). - Decoding is zero copy: a decoded frame points into the buffer it was read from, and accessors read the values.
- Compiles as C++ too.
Include it
In exactly one .c (or .cpp) file:
#define VOLTERGEIST_IMPLEMENTATION
#include "voltergeist.h"
Everywhere else, #include "voltergeist.h" for the declarations. Define VG_API as static before the include to
keep every function private to one file.
Three sizes can be set before the implementation include: VG_CLOCK_KEEP (pongs the clock estimator keeps, default
120, as the reference), VG_EVENT_SEEN (event seqs remembered to drop repeats, default 256; the reference keeps 4096)
and VG_JSON_DEPTH (how deep a MAP's JSON may nest, default 32).
Decode a frame
vg_frame f;
int st = vg_decode(datagram, len, &f);
if (st < 0) { /* a reject: vg_status_name(st) says why, as the JS reader says it */ }
else if (st == VG_SKIP) { /* version 1, a type this reader does not know: ignore it */ }
else if (f.type == VG_SAMPLES) {
unsigned i, c;
for (i = 0; i < f.u.samples.count; i++) {
if (i < f.u.samples.redundant) continue; /* a repeat of the previous frame (or keep the first copy by seq) */
uint32_t seq = f.seq + i; /* wraps at 2^32 */
int32_t at = vg_offset(&f, i); /* with the I flag; else clock + i * 1e6 / rate */
for (c = 0; c < f.u.samples.channels; c++) {
double raw = vg_sample(&f, i, c); /* f32 as sent, i16 and i24 as raw integers */
double v = vg_to_unit(raw, f.u.samples.format, 0, 0, 0); /* or with the map's min and max */
/* ... */
}
}
} else if (f.type == VG_EVENTS) {
vg_event e;
vg_event_at(&f, 0, &e); /* e.offset, e.channel, e.kind, e.note, e.value */
} else if (f.type == VG_MIDI) {
vg_midi_msg m;
vg_midi_at(&f, 0, &m); /* m.offset, m.len, m.bytes: one whole MIDI message */
if (m.bytes[0] == 0xF0 && !owner_allows_sysex) { /* drop it: SysEx is off by default (SPEC.md 4.4) */ }
} else if (f.type == VG_MAP) {
vg_map_info_t m; /* f.u.map.json, f.u.map.len: checked, not parsed */
vg_map_channel_t ch;
vg_map_info(f.u.map.json, f.u.map.len, &m); /* m.kind, m.rate, m.format, m.channels */
vg_map_channel(f.u.map.json, f.u.map.len, 0, &ch); /* ch.name (raw JSON bytes), ch.min, ch.max, ch.voct,
ch.clamp_lo, ch.clamp_hi, ch.slew (with has_ flags) */
}
Encode SAMPLES
uint8_t out[1200];
float values[3 * 2] = { 0.5f, -1, 0.25f, 1.5f, 0, -0.125f }; /* 3 samples of 2 channels, sample major */
vg_head h = { 7, 1000, now_us, NULL }; /* stream, seq of the first sample, its clock, no extension */
vg_samples_in s = { 0 };
s.map = 1; s.channels = 2; s.format = VG_F32; s.count = 3; s.f32 = values;
size_t n = vg_encode_samples(out, sizeof out, &h, &s); /* 48 bytes, or 0 when refused */
For VG_I16 and VG_I24 pass raw integers in s.ints (vg_from_unit scales a value in the channel's unit to one).
s.offsets (k offsets, the first 0, never decreasing) sets the I flag, s.redundant the R flag. Every encoder returns
the length or 0, and with out NULL returns the length it needs. An encoder refuses anything a reader would reject:
a NaN or infinite clock or value, kind 0, more repeats than new samples, a MAP whose JSON breaks the key table, a
frame over VG_FRAME_MAX (65535 bytes, SPEC.md 1.2; vg_decode rejects a longer one as length).
The others: vg_encode_events, vg_encode_midi (an array of vg_midi_msg; each message is checked with
vg_midi_check, which a receiver can call too), vg_encode_map (id plus JSON bytes), vg_encode_hello, vg_encode_ping,
vg_encode_pong, vg_encode_clock, vg_encode_report, and vg_encode_raw for a type this library does not know.
Byte streams (TCP, serial)
static uint8_t frame_buf[2048]; /* the largest frame you accept; larger ones are skipped and counted */
vg_reader r;
vg_reader_init(&r, frame_buf, sizeof frame_buf);
/* for each chunk the transport hands over: */
while (len) {
const uint8_t *fr; size_t fl;
size_t used = vg_reader_feed(&r, data, len, &fr, &fl);
data += used; len -= used;
if (fr) { vg_frame f; vg_decode(fr, fl, &f); /* fr is valid until the next feed */ }
if (r.error) break; /* a length under 16: the connection cannot be resynchronised */
}
vg_stream_wrap(out, cap, frame, len) puts the u16 length in front of a frame for sending.
Clock and playout
vg_clock_est keeps the last 120 pongs and gives the median offset of the fastest third (SPEC.md 5.3):
vg_clock_pong(&e, &pong_frame, t3), then vg_clock_get(&e, &best). vg_playout plays samples out of a fixed buffer
the caller gives it (vg_slot per sample plus channels floats per sample), holding or interpolating over gaps;
vg_event_playout hands back due events in clock order. Both take the time on the sender's clock, as the reference
does: T = receiver_now - offset - delay. When a sample buffer is full the oldest sample is dropped and counted in
overflow; a full event buffer refuses the newcomer and counts it.
Safety limits
vg_limit is one channel's clamp and slew (SPEC.md 6.1), a small struct with no allocation. Fill it from the map's
channel, or from the receiver's own setting, which replaces the map's for that channel:
vg_limit lim;
vg_limit_init(&lim, ch.has_clamp, ch.clamp_lo, ch.clamp_hi, ch.has_slew ? ch.slew : 0);
/* each output tick, after playout, before the DAC, MIDI or OSC: */
out = vg_limit_apply(&lim, value, now_us); /* slew toward value, then clamp */
vg_limit_set(&lim, 1, 0, 6, 0); /* a new limit keeps the last output, so no step slips through */
Build and test
node c/gen-vectors.mjs # vectors/*.json and the JS clock and playout answers into c/vectors.h (idempotent)
cd c && make test # -std=c99 -Wall -Wextra -Wpedantic -Werror, then run
make asan # the same under AddressSanitizer and UBSan
make ubsan # UBSan alone, where ASan cannot run (the Raspberry Pi 4 kernel)
make cpp # compile the header as C++
sh build.sh # test and asan without make
The test decodes every valid vector and checks every field, encodes each from its fields and compares the bytes,
rejects every reject with the JS reader's reason, skips every skip, feeds the byte stream reader the whole vector set
split at every point, runs 500 random round trips and 40,000 mutated or cut vectors, and checks 39 MAP JSON cases, 26
UTF-8 cases, the clock estimator, both playouts and the safety limit against answers computed by the JS reference. 8300
checks pass in an arm64 container (2026-12-06; the rise from 4980 is mostly the byte stream test splitting the stream at
every point, which a 1024 byte SysEx vector lengthened). A MAP's JSON is taken from the vector's bytes, so a
decodeOnly vector (a key given twice) is checked as written. A MAP key given twice keeps its last value and a leading
BOM gets no special treatment (a MAP starting with one is not JSON, a HELLO name keeps it), as SPEC.md 4.3 and 5.1 say.
Where it differs from the JS reference
- A MAP's JSON may nest at most
VG_JSON_DEPTH(32) deep here; JS accepts any depth. - Numbers in a MAP are read without
strtod, so a value within a rounding step of the largest double may be judged finite where JS says infinite. - The event playout remembers the last 256 event seqs for repeats (JS 4096), and both playouts have a fixed capacity.
- The encoder refuses NaN and infinite numbers and out of order offsets, which the JS encoder writes without checking.
- There is no receiver object here, so the SysEx policy is the caller's: drop a message whose first byte is F0 unless
the owner allowed SysEx.
vg_midi_atwalks the records from the first, so reading every message of a frame is quadratic in the count (at most 255).