/* SPDX-License-Identifier: GPL-3.0-only * Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */ /* WAVE (src/eng_wave.c, engine 9: JIANT BYTE's wavetables) on the host, on the real sources (ui_test.c's). * build/host/wave_test * 1. the tables: each of the 16 factory tables decoded (wv_step, the audio's own) is the generator's, frame for frame * (wave_tables.h WT_HASH_INIT: tools/gen_wave_tables.py's FNV of the eight frames it means); SINE is a sine; a part * playing A and B has them in its line. * 2. every value is heard: POS, MIX (with B), WARP (each of SYNC BEND MIRROR QUANT FM, and AMT in it), DETUNE, SUB, A and * B change the sound; B unheard at MIX 0 (no change at all); a note renders the same twice. * 3. no clicks: POS swept over the eight frames, MIX swept, a WARP's AMT swept, against the same note held still (the * largest second difference no larger); A changed under a held note (a dip and back, no step); a COMB TYPE asked * for on a sounding WAVE part (it plays BP: the line is the tables'). * 4. an empty user slot (U1..U6: none stored here; tests/wt_store_test.c stores them) and a U7 / U8 of before (22, 23) * play SINE, silently: A and B alike. * 5. the line shared: tables in, the part STRING for a while (its lines over WAVE's), WAVE again: the tables back, * byte for byte; the part asleep meanwhile: kept. * 6. bounded: every preset, 8 notes at velocity 127 (LEVEL 127): no voice sum past full scale, no mix sample clipped, * DC under 2 % (BYTE's tables as BYTE has them: some frames of FORMT SATUR NOISE carry 10 .. 20 % of DC, which the * master's DC blocker takes out, as BYTE's). * 7. PHYS's old data (engine 9 before WAVE): a user preset record of before (version < 4) plays as ANALOG as before; one * of today stays WAVE; a project with a WAVE track is written with phys 4 and opens; the same bytes as phys 3 (a * PHYS project of before) are refused as before; a project without WAVE keeps phys 3. * 8. cost: ns a sample and voice on this host, WAVE's heaviest setting against ANALOG's SAW LEAD (printed). */ #define UI_TEST_NO_MAIN 1 #include "ui_test.c" static int bad; static void ck(const char *what, int ok) { printf("wave: %-100s %s\n", what, ok ? "ok" : "FAIL"); bad += !ok; } static uint32_t wfnv(uint32_t h, const void *p, uint32_t n) { const uint8_t *b = (const uint8_t *)p; while (n--) h = (h ^ *b++) * 16777619u; return h; } #define NS (FS / 4u) /* a quarter second */ static int16_t base_e[8] = {1, 40, WW_OFF, 0, 4, 0, 0, 0}; /* FM2 at POS 40, B SAWSQ unheard */ static void setup(const int16_t *e) { uint32_t i; memset(trk, 0, sizeof trk); memset(wv, 0, sizeof wv); memset(tf_ic1, 0, sizeof tf_ic1), memset(tf_ic2, 0, sizeof tf_ic2), memset(tf_on, 0, sizeof tf_on); /* (the part's * filter from rest: each render as the first) */ memset(tf_ft, 0, sizeof tf_ft), memset(tf_xb, 0, sizeof tf_xb), memset(tf_fo, 0, sizeof tf_fo); memset(tf_cmb, 0, sizeof tf_cmb), memset(drift_clk, 0, sizeof drift_clk); host_tracks_init(); host_preset(&trk[0], ENGI_WAVE, 0); for (i = 0; i < 8u; i++) trk[0].p[P_E0 + i] = e[i]; trk[0].p[P_ATK] = 0; trk[0].p[P_SUS] = 127; trk[0].p[P_REL] = 10; trk[0].p[P_ED_FLT] = 0; trk[0].p[P_FCUT] = 127; trk[0].p[P_LD_FLT] = trk[0].p[P_LD_SHP] = trk[0].p[P_LD_PIT] = trk[0].p[P_LD_AMP] = 0; trk[0].p[P_M1SRC] = trk[0].p[P_M2SRC] = trk[0].p[P_M3SRC] = trk[0].p[P_M4SRC] = 0; trk[0].p[P_VOICE] = V_POLY; trk[0].p[P_CHOR] = trk[0].p[P_DLY] = trk[0].p[P_REV] = trk[0].p[P_DIST] = 0; for (i = 0; i < 8u; i++) trk[0].pe_old[i] = trk[0].p[P_E0 + i]; } /* one block of part 0 as the audio runs it (the engine's switch, then the part) */ static void block(int32_t *b) { engine_block(&trk[0]); track_render(&trk[0], b, CTL); } /* n samples of one note (the tables in first: a few blocks before the note) into y; per block, hook(k) may move a value */ static void render(const int16_t *e, uint32_t note, double *y, uint32_t n, void (*hook)(uint32_t k, uint32_t nb)) { static int32_t b[CTL]; uint32_t i, k; setup(e); for (i = 0; i < 8u; i++) block(b); trk_note_on(&trk[0], note, 100); for (i = 0; i < 4u; i++) /* (past the attack's first blocks) */ block(b); for (i = 0; i < n; i += CTL) { if (hook) hook(i / CTL, n / CTL); block(b); for (k = 0; k < CTL && i + k < n; k++) y[i + k] = b[k] / 32768.0; } } static double rms(const double *y, uint32_t n) { double s = 0; uint32_t i; for (i = 0; i < n; i++) s += y[i] * y[i]; return sqrt(s / (n ? n : 1)); } static double rdiff(const double *a, const double *b, uint32_t n) /* the difference's rms over a's */ { double s = 0; uint32_t i; for (i = 0; i < n; i++) s += (a[i] - b[i]) * (a[i] - b[i]); return sqrt(s / n) / (rms(a, n) + 1e-12); } static double d2max(const double *y, uint32_t n) /* the largest second difference (a click's mark) */ { double m = 0; uint32_t i; for (i = 2; i < n; i++) { double d = fabs(y[i] - 2 * y[i - 1] + y[i - 2]); m = d > m ? d : m; } return m; } /* ------------------------------------------------------------ 1. the tables --- */ static void test_tables(void) { static const uint32_t H[WT_NF] = WT_HASH_INIT; static int8_t F[WV_HALF]; static int32_t b[CTL]; uint32_t f, s, ok = 1, i; double c = 0, a = 0, q = 0; for (f = 0; f < WT_NF; f++) { memset(F, 0x55, sizeof F); for (s = 0; s < 16u && !wv_step(F, f, s); s++) ; ok &= s < 16u && wfnv(2166136261u, F, sizeof F) == H[f]; } ck("each of the 16 factory tables decoded is the generator's (tools/gen_wave_tables.py, BYTE main 94f8ac9)", ok); for (s = 0; !wv_step(F, 0, s); s++) ; for (i = 0; i < WV_FN; i++) { double r = sin(2 * M_PI * i / WV_FN); c += F[i] * r, a += (double)F[i] * F[i], q += r * r; } ck("SINE is a sine (correlation > 0.999, peak 120..127)", c / sqrt(a * q) > 0.999 && F[32] >= 120); { int16_t e[8] = {7, 0, 0, 0, 12, 0, 0, 0}; /* A FOLD, B FORMT */ static int8_t G[WV_HALF]; setup(e); for (i = 0; i < 8u; i++) block(b); for (s = 0; !wv_step(F, 7, s); s++) ; for (s = 0; !wv_step(G, 12, s); s++) ; ck("a part's line holds its A and B (FOLD, FORMT) as decoded, after a few blocks", wv_ready(&wv[0]) && !memcmp(comb_buf[0], F, WV_HALF) && !memcmp((int8_t *)comb_buf[0] + WV_HALF, G, WV_HALF)); } } /* ------------------------------------------------------------ 2. every value is heard --- */ static void test_values(void) { static double y0[NS], y1[NS]; static const char *const NM[8] = {"A", "POS", "WARP", "AMT", "B", "MIX", "DETUNE", "SUB"}; int16_t e[8]; uint32_t k, w; char what[160]; render(base_e, 45, y0, NS, 0); render(base_e, 45, y1, NS, 0); ck("a note renders the same twice", !memcmp(y0, y1, sizeof y0) && rms(y0, NS) > 0.02); { static const struct { uint32_t k; int16_t v, mix, warp; } CH[] = { {0, 9, 0, 0}, {1, 110, 0, 0}, {4, 13, 100, 0}, {5, 100, 0, 0}, {6, 60, 0, 0}, {7, 100, 0, 0}}; for (k = 0; k < NELEM(CH); k++) { double d; memcpy(e, base_e, sizeof e); e[CH[k].k] = CH[k].v; if (CH[k].mix) e[5] = CH[k].mix; render(e, 45, y1, NS, 0); if (CH[k].k == 4u) { /* (B: against the same MIX with the first B) */ int16_t e2[8]; memcpy(e2, base_e, sizeof e2); e2[5] = CH[k].mix; render(e2, 45, y0, NS, 0); } else render(base_e, 45, y0, NS, 0); d = rdiff(y0, y1, NS); snprintf(what, sizeof what, "%s heard (%s %d: %.0f %% of the sound changed)", NM[CH[k].k], NM[CH[k].k], CH[k].v, 100 * d); ck(what, d > 0.05 && rms(y1, NS) > 0.02); } } render(base_e, 45, y0, NS, 0); for (w = WW_SYNC; w < WW_N; w++) { double d, da; memcpy(e, base_e, sizeof e); e[2] = (int16_t)w; e[3] = 80; render(e, 45, y1, NS, 0); d = rdiff(y0, y1, NS); e[3] = 30; { static double y2[NS]; render(e, 45, y2, NS, 0); da = rdiff(y1, y2, NS); } snprintf(what, sizeof what, "WARP %s heard (AMT 80: %.0f %% changed; its AMT 30 against 80: %.0f %%)", N_WWARP[w], 100 * d, 100 * da); ck(what, d > 0.05 && da > 0.02); } memcpy(e, base_e, sizeof e); e[4] = 13; /* B NOISE, MIX 0 */ render(e, 45, y1, NS, 0); ck("B unheard at MIX 0 (WARP OFF): the same samples", !memcmp(y0, y1, sizeof y0)); } /* ------------------------------------------------------------ 3. no clicks --- */ static int16_t sweep_k; /* the value the hook sweeps */ static void sweep(uint32_t k, uint32_t nb) { trk[0].p[P_E0 + sweep_k] = (int16_t)(127u * k / (nb - 1u)); } static int16_t change_v; static void change(uint32_t k, uint32_t nb) { if (k == nb / 2u) trk[0].p[P_E0] = change_v; } static void comb_on(uint32_t k, uint32_t nb) { if (k == nb / 2u) trk[0].p[P_FTYPE] = FT_COMB; } static void test_clicks(void) { static double y[NS * 4], z[NS * 4]; static const struct { uint32_t k; int16_t a, warp, mix; const char *what; } SW[] = { {1, 1, WW_OFF, 0, "POS swept 0 .. 127 over FM2's eight frames"}, {1, 2, WW_OFF, 0, "POS swept over FMR's"}, {1, 4, WW_OFF, 0, "POS swept over SAWSQ's (two kept, the six between their blend)"}, {5, 1, WW_OFF, 0, "MIX swept A .. B"}, {3, 1, WW_SYNC, 0, "SYNC's AMT swept"}, {3, 1, WW_BEND, 0, "BEND's AMT swept"}, {3, 1, WW_FM, 50, "FM's AMT swept (B into A's phase, MIX 50)"}}; uint32_t k, j; char what[200]; for (k = 0; k < NELEM(SW); k++) { int16_t e[8] = {SW[k].a, 0, SW[k].warp, 0, 6, SW[k].mix, 0, 0}; double still = 0, sw; for (j = 0; j <= 8u; j++) { /* the note held still at nine places of the sweep */ e[SW[k].k] = (int16_t)(j * 127u / 8u); render(e, 40, z, NS, 0); still = d2max(z, NS) > still ? d2max(z, NS) : still; } sweep_k = (int16_t)SW[k].k; render(e, 40, y, NS * 4, sweep); sw = d2max(y, NS * 4); snprintf(what, sizeof what, "no click: %s in 1 s (largest 2nd difference %.4f, held still %.4f)", SW[k].what, sw, still); ck(what, sw <= still * 1.1 + 1e-4); } { int16_t e[8] = {1, 60, 0, 0, 4, 0, 0, 0}; double still, ch; render(e, 40, z, NS * 2, 0); still = d2max(z, NS * 2); change_v = 8; /* A FM2 -> ODD under the note */ render(e, 40, y, NS * 2, change); ch = d2max(y, NS * 2); snprintf(what, sizeof what, "no click: A changed under a held note (a dip and back: %.4f, held %.4f), it sounds after", ch, still); ck(what, ch <= still * 1.1 + 1e-4 && rms(y + NS * 2 - NS / 4, NS / 4) > 0.02); render(e, 40, y, NS * 2, comb_on); ch = d2max(y, NS * 2); snprintf(what, sizeof what, "a COMB TYPE asked for on a sounding WAVE part: BP, no click (%.4f), the tables kept", ch); ck(what, trk[0].p[P_FTYPE] == FT_BP && ch <= still * 1.1 + 1e-4 && wv_ready(&wv[0]) && rms(y + NS * 2 - NS / 4, NS / 4) > 0.005); } } /* ------------------------------------------------------------ 4. empty user slots --- */ static void test_fallback(void) { static double y0[NS], y1[NS]; int16_t e[8]; uint32_t u, ok = 1; memcpy(e, base_e, sizeof e); e[0] = 0; render(e, 52, y0, NS, 0); for (u = 0; u < WV_NU + 2u; u++) { e[0] = (int16_t)(WT_NF + u); render(e, 52, y1, NS, 0); ok &= !memcmp(y0, y1, sizeof y0); } ck("an empty user slot U1..U6, and 22 / 23 (U7 / U8 before 1.7: gone), as A play SINE, the same samples", ok && rms(y0, NS) > 0.02); memcpy(e, base_e, sizeof e); e[4] = 0, e[5] = 90; render(e, 52, y0, NS, 0); e[4] = WT_NF + 3; render(e, 52, y1, NS, 0); ck("an empty user slot as B plays SINE too (MIX 90)", !memcmp(y0, y1, sizeof y0)); ck("U1..U6 named (A's list: the 16 factory names, then U1..U6)", !strcmp(N_WAVEF[0], "SINE") && !strcmp(N_WAVEF[15], "SATUR") && !strcmp(N_WAVEF[16], "U1") && !strcmp(N_WAVEF[21], "U6") && ENG_WAVE.edit[0].max == 21 && WV_NU == 6u); } /* ------------------------------------------------------------ 5. the line shared --- */ static void test_shared(void) { static int32_t b[CTL]; static int8_t keep[2 * WV_HALF]; int16_t e[8] = {2, 30, 0, 0, 10, 50, 0, 0}; /* FMR, STEP */ uint32_t i, ok; setup(e); for (i = 0; i < 8u; i++) block(b); memcpy(keep, comb_buf[0], sizeof keep); host_preset_req(&trk[0], ENGI_STRING, 0); /* STRING: its lines over WAVE's */ for (i = 0; i < 4u; i++) block(b); for (i = 0; i < 4u; i++) trk_note_on(&trk[0], (uint8_t)(40 + 7 * i), 110); for (i = 0; i < 200u; i++) block(b); ok = trk[0].engine == ENGI_STRING && memcmp(keep, comb_buf[0], sizeof keep); for (i = 0; i < 4u; i++) trk_note_off(&trk[0], (uint8_t)(40 + 7 * i)); host_preset_req(&trk[0], ENGI_WAVE, 0); for (i = 0; i < 8u; i++) trk[0].p[P_E0 + i] = e[i]; for (i = 0; i < 400u && !(trk[0].engine == ENGI_WAVE && wv_ready(&wv[0])); i++) block(b); block(b); ck("STRING played in the part's line, then WAVE again: its tables back, byte for byte", ok && trk[0].engine == ENGI_WAVE && wv_ready(&wv[0]) && !memcmp(keep, comb_buf[0], sizeof keep)); trk[0].p[P_FTYPE] = FT_COMB; /* the COMB's own start (as if it ran: its line cleared) */ memset(comb_buf[0], 0, sizeof comb_buf[0]); block(b); ok = !wv_ready(&wv[0]) && trk[0].p[P_FTYPE] == FT_BP; for (i = 0; i < 8u; i++) block(b); ck("the line cleared under WAVE (the COMB's start): seen at the next block, decoded again", ok && wv_ready(&wv[0]) && !memcmp(keep, comb_buf[0], sizeof keep)); } /* ------------------------------------------------------------ 6. bounded --- */ static void test_bounded(void) { static int32_t o[2 * CTL], b[CTL]; uint32_t pi, i, k, j, ok = 1, okv = 1, okdc = 1; for (pi = 0; pi < ENG_WAVE.npresets; pi++) { int32_t peak = 0, vpk = 0; double sum = 0; uint32_t nb = FS / 2u / CTL; memset(trk, 0, sizeof trk); memset(wv, 0, sizeof wv); host_tracks_init(); host_preset(&trk[0], ENGI_WAVE, pi); trk[0].p[P_LEVEL] = 127; for (i = 0; i < 4u; i++) mix_block(o, CTL); for (k = 0; k < 8u; k++) trk_note_on(&trk[0], (uint8_t)(24u + k * 12u > 120u ? 120u : 24u + k * 12u), 127); for (i = 0; i < nb; i++) { mix_block(o, CTL); for (j = 0; j < 2u * CTL; j++) peak = abs(o[j]) > peak ? abs(o[j]) : peak; } memset(trk, 0, sizeof trk); memset(wv, 0, sizeof wv); host_tracks_init(); host_preset(&trk[0], ENGI_WAVE, pi); for (i = 0; i < 8u; i++) trk[0].pe_old[i] = trk[0].p[P_E0 + i]; for (i = 0; i < 4u; i++) block(b); for (k = 0; k < 8u; k++) trk_note_on(&trk[0], (uint8_t)(24u + k * 12u > 120u ? 120u : 24u + k * 12u), 127); for (i = 0; i < nb; i++) { block(b); for (j = 0; j < CTL; j++) { vpk = abs(b[j]) > vpk ? abs(b[j]) : vpk; sum += b[j]; } } ok &= peak < 32767; okv &= vpk < 8 * 2 * 32768; okdc &= fabs(sum / (nb * CTL)) < 0.02 * 32768 * 8; if (peak >= 32767 || vpk >= 8 * 2 * 32768 || fabs(sum / (nb * CTL)) >= 0.02 * 32768 * 8) printf("wave: %s: mix peak %d, voices %d, mean %.0f\n", ENG_WAVE.presets[pi].name, peak, vpk, sum / (nb * CTL)); } ck("every preset, 8 notes at velocity 127, LEVEL 127: no mix sample at full scale", ok); ck(" the voices' sum within 2 x full scale a voice; DC under 2 % (BYTE's own: FORMT SATUR NOISE frames carry up to 20 %)", okv && okdc); } /* ------------------------------------------------------------ 7. PHYS's old data --- */ static void test_old(void) { static up_rec_t r; static project_store_t st; static project_t q, back; uint32_t sum; memset(&r, 0, sizeof r); r.used = UP_USED, r.ver = 2u, r.engine = ENGI_PHYS, r.np = 8u; memcpy(r.name, "OLD PHYS", 8); up_migrate(&r); ck("a user preset of before (version 2) with engine 9: PHYS's, plays as ANALOG as before", eng_live(r.engine) == ENGI_ANALOG); memset(&r, 0, sizeof r); r.used = UP_USED, r.ver = UP_VER, r.engine = ENGI_WAVE, r.np = P_COUNT; memcpy(r.name, "MY WAVE", 7); up_migrate(&r); ck(" one of today (version 4) with engine 9: WAVE", r.engine == ENGI_WAVE && eng_live(r.engine) == ENGI_WAVE); ui_power_on(); host_preset(&trk[1], ENGI_WAVE, 3); project_capture(&q); ck("a project with a WAVE track is written with phys 4 and opens, the track WAVE", q.phys == 4u && proj_pack(&st, &q) && st.raw[64] == 4u && proj_import(&back, &st, sizeof st) && back.t[1].engine == ENGI_WAVE && back.t[1].p[P_E0] == ENG_WAVE.presets[3].e[0]); st.raw[64] = 3u; sum = proj_hash(st.raw, sizeof st.raw - 4u); memcpy(st.raw + sizeof st.raw - 4u, &sum, sizeof sum); ck(" the same bytes as phys 3 (engine 9 PHYS's, before WAVE): refused as before (OPEN IN STUDIO)", !proj_import(&back, &st, sizeof st) && proj_sl_found == 2); host_preset(&trk[1], ENGI_ANALOG, 0); project_capture(&q); ck("a project without WAVE keeps phys 3 (its bytes as before)", q.phys == 3u && proj_pack(&st, &q) && st.raw[64] == 3u); } /* ------------------------------------------------------------ 8. cost --- */ static double ns_voice(uint32_t e, const int16_t *ev, uint32_t pi) { static int32_t b[CTL]; uint32_t i, k, nb = FS / CTL, nv = 8; uint64_t t0; memset(trk, 0, sizeof trk); memset(wv, 0, sizeof wv); host_tracks_init(); host_preset(&trk[0], e, pi); if (ev) for (i = 0; i < 8u; i++) trk[0].p[P_E0 + i] = ev[i]; for (i = 0; i < 8u; i++) trk[0].pe_old[i] = trk[0].p[P_E0 + i]; trk[0].p[P_VOICE] = V_POLY; trk[0].p[P_SUS] = 127; for (i = 0; i < 8u; i++) block(b); for (k = 0; k < nv; k++) trk_note_on(&trk[0], (uint8_t)(36 + 5 * k), 100); for (i = 0; i < 8u; i++) block(b); t0 = now_ns(); for (i = 0; i < nb; i++) block(b); return (double)(now_ns() - t0) / (nb * CTL) / nv; } static void test_cost(void) { static const int16_t heavy[8] = {1, 64, WW_FM, 80, 4, 64, 60, 60}; /* FM warp, MIX, three voices, SUB */ static const int16_t plain[8] = {1, 64, WW_OFF, 0, 4, 0, 0, 0}; double a = 1e9, w = 1e9, h = 1e9, x; uint32_t r; for (r = 0; r < 5u; r++) { /* (the best of five: the host's noise out) */ x = ns_voice(ENGI_ANALOG, 0, 0); a = x < a ? x : a; x = ns_voice(ENGI_WAVE, plain, 0); w = x < w ? x : w; x = ns_voice(ENGI_WAVE, heavy, 0); h = x < h ? x : h; } printf("wave: cost, ns a sample and voice on this host: ANALOG SAW LEAD %.1f, WAVE one table %.1f (%.2f x), " "WAVE FM + MIX + DETUNE + SUB %.1f (%.2f x)\n", a, w, w / a, h, h / a); } int main(void) { test_tables(); test_values(); test_clicks(); test_fallback(); test_shared(); test_bounded(); test_old(); test_cost(); printf("wave: %s (%d failed)\n", bad ? "FAILED" : "all passed", bad); return bad != 0; }