/* SPDX-License-Identifier: GPL-3.0-only */
/* SUBCH-OM: FoMni's instrument (omni.c, unchanged) as a WebAssembly module for the browser.
 * Built by build.sh with clang for wasm32, freestanding: no libc, no Emscripten.
 *
 * The page's moves arrive as om_* commands, which the engine applies at the start of the next
 * om_render(), as on the FM-1. web_render(n) renders n stereo frames into a float buffer and
 * queues the MIDI the engine sends (its notes on channels 1-3 and 10, clock), stamped with the
 * frame count at the start of that render. */
#include "omni.h"

#define EX(name) __attribute__((export_name(#name)))
#define MAXN 1024
#define MQ 1024

static int32_t raw[2 * MAXN];
static float out[2 * MAXN];
static uint32_t midi_q[MQ][2];                  /* [frame, status | d1 << 8 | d2 << 16] */
static uint32_t midi_n;
static uint32_t frame;
static uint32_t gain = 2800;                    /* the MASTER pot, as FoMni's browser emulator */
static char text[16];

void om_midi_out(uint32_t st, uint32_t d1, uint32_t d2)
{
    if (st >= 0xF8u || midi_n >= MQ)            /* clock and start / stop: the page keeps time itself */
        return;
    midi_q[midi_n][0] = frame;
    midi_q[midi_n][1] = st | d1 << 8 | d2 << 16;
    midi_n++;
}

EX(init) void web_init(void) { om_init(); frame = 0; midi_n = 0; }
EX(set) void web_set(int p, int v) { om_set(p, v); }
EX(chord) void web_chord(int root, int type) { om_chord(root, type); }
EX(gate) void web_gate(int on) { om_gate(on); }
EX(strum) void web_strum(int s) { om_strum(s); }
EX(release) void web_release(int s) { om_release(s); }
EX(play) void web_play(int on) { om_play(on); }
EX(panic) void web_panic(void) { om_panic(); }
EX(master) void web_master(uint32_t q12) { gain = q12; }

/* render n frames (n <= MAXN) into out[] as L, R, L, R ... (-1..1) */
EX(render) float *web_render(uint32_t n)
{
    uint32_t i;
    if (n > MAXN)
        n = MAXN;
    om_render(raw, n, gain);
    for (i = 0; i < 2 * n; i++)
        out[i] = (float)raw[i] * (1.0f / 8388608.0f);
    frame += n;
    return out;
}
EX(frame) uint32_t web_frame(void) { return frame; }

/* the MIDI queued since the last midi_clear: midi_count pairs at midi_buf */
EX(midi_buf) uint32_t *web_midi_buf(void) { return &midi_q[0][0]; }
EX(midi_count) uint32_t web_midi_count(void) { return midi_n; }
EX(midi_clear) void web_midi_clear(void) { midi_n = 0; }

/* the state the screen shows */
EX(str_level) volatile float *web_str_level(void) { return om_str_level; }
EX(str_note) volatile uint8_t *web_str_note(void) { return om_str_note; }
EX(chord_level) float web_chord_level(void) { return om_chord_level; }
EX(bass_level) float web_bass_level(void) { return om_bass_level; }
EX(playing) int web_playing(void) { return om_playing; }
EX(step) int web_step(void) { return om_step; }
EX(steps) int web_steps(void) { return om_steps; }
EX(drum_hit) int web_drum_hit(void) { int h = om_drum_hit; om_drum_hit = 0; return h; }

/* the tables: parameters, chord sets, rhythms, names */
EX(nparams) int web_nparams(void) { return P_NPARAMS; }
EX(param_lo) int web_param_lo(int i) { return om_param_info(i)->lo; }
EX(param_hi) int web_param_hi(int i) { return om_param_info(i)->hi; }
EX(param_def) int web_param_def(int i) { return om_param_info(i)->def; }
static char *copy(const char *s)
{
    int k = 0;
    while (s && s[k] && k < 15) {
        text[k] = s[k];
        k++;
    }
    text[k] = 0;
    return text;
}
EX(param_name) char *web_param_name(int i) { return copy(om_param_info(i)->name); }
EX(param_text) char *web_param_text(int i, int v) { om_param_text(i, v, text); return text; }
EX(nprogs) int web_nprogs(void) { return OM_NPROGS; }
EX(prog_name) char *web_prog_name(int i) { return copy(om_prog(i)->name); }
EX(prog_root) int web_prog_root(int i, int pad) { return om_prog(i)->root[pad]; }
EX(prog_type) int web_prog_type(int i, int pad) { return om_prog(i)->type[pad]; }
EX(prog_keys) int web_prog_keys(int i) { return om_prog(i)->keys; }
EX(ntypes) int web_ntypes(void) { return CH_NTYPES; }
EX(type_name) char *web_type_name(int t) { return copy(OM_TYPE_NAME[(unsigned)t < CH_NTYPES ? t : 0]); }
EX(note_name) char *web_note_name(int n) { return copy(OM_NOTE_NAME[((n % 12) + 12) % 12]); }

/* the notes a chord gives the plate for KEYS (om_plate), into a 16-byte buffer */
static uint8_t plate[OM_NSTR];
EX(plate) uint8_t *web_plate(int keys, int root, int type, int transpose, int octave)
{
    om_plate(keys, root, type, transpose, octave, plate);
    return plate;
}

/* freestanding: what clang may call for struct copies and clears (bulk memory covers most) */
void *memset(void *d, int c, unsigned long n)
{
    unsigned char *p = d;
    while (n--)
        *(volatile unsigned char *)p++ = (unsigned char)c;
    return d;
}
void *memcpy(void *d, const void *s, unsigned long n)
{
    unsigned char *p = d;
    const unsigned char *q = s;
    while (n--)
        *(volatile unsigned char *)p++ = *q++;
    return d;
}
