mirror of
https://github.com/juanjiant-bit/JIANT.git
synced 2026-12-06 04:44:33 +01:00
- lo/hi is the range POS can reach (wv_span, mod.c): the knob plus and minus the LFO's and the envelope's depths and |amt| of each route to POS (the part's slots, the macros', the punch-in key's, LIFE TIMBRE); all of 0..127 with CLAY or a route to POS itself. A steady LFO draws nothing after its first blocks. hi <= lo: one frame at the knob. - Drawn as BYTE draws it: one frame each block while a new key is drawn; the voices read the stored frames at their POS meanwhile (wv_dread, BYTE's wt_get). One grid per part. - na 6 when B is a user table and heard: FM's rule, documented as a deviation. - EDITOR_PROTOCOL.md: the contract paragraph restored, with a note that 'T' sits between 'C' and 'Y' and that clients walk the tags; the Played (WAVE) text rewritten. - The span reads its globals by symbol (hal/fm1_cc.h FM1_ADDR), so the pinned audio layout is not moved; target budgets for wv_user, wv_draw, wv_dread, wave_render. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
590 lines
31 KiB
C
590 lines
31 KiB
C
/* SPDX-License-Identifier: GPL-3.0-only
|
|
* Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */
|
|
/* (WAVE, editor protocol 1.7) the user wavetables (firmware/src/editor_wt.c) through the real editor path
|
|
* (editor_test.c is the base: hostsim, RAM flash; 0xD0000..0xD7FFF a NOR of its own):
|
|
* INFO 'C' 43 02 01 07, then 'T' 54 02 06 20, then 'Y'; WT_LIST on a blank flash (format 1)
|
|
* an upload (BEGIN, DATA in pieces, END) listed and read back (WT_GET) byte for byte
|
|
* played: BYTE's dynamic grid (wt-core.js userGrid) drawn in the audio's block: the key (g0, gs, na) for the POS the
|
|
* voices sweep, its frames as BYTE's wt_user_frame (the spec's code and LUT formula, here again), LINEAR and MULAW,
|
|
* the gains, A and B; drawn again only when the key changes; an LFO on POS and a knob turned: never silent, no
|
|
* click, the grid always the one for its key (and the cost: a redraw, a block, a voice)
|
|
* a CRC wrong (the record's, the header's own): rc 2, the slot as it was
|
|
* a power cut at every erase and program of an upload: after the boot the old table or the new one, never another;
|
|
* all six full (a slot rewritten in place): cut, that slot damaged (SINE), the others whole
|
|
* RENAME MOVE DELETE (each slot's gen + 1); used 0 / 1; an empty slot (rc 6); stale requests (rc 5); U7 / U8 of before
|
|
* and an empty slot: SINE (and shown SINE)
|
|
* Build: cc -O1 -w -Wno-implicit-function-declaration -Ibuild/gen -Ifirmware/src tests/wt_store_test.c -lm */
|
|
#define EDITOR_TEST_NO_MAIN 1
|
|
#include "editor_test.c"
|
|
#include <math.h>
|
|
static void excite_turn(track_t *t, const int16_t *vp, int32_t old) { (void)t; (void)vp; (void)old; }
|
|
|
|
static uint8_t *R = host_wire + 5; /* a reply's arguments */
|
|
static void put32(uint8_t *a, uint32_t v)
|
|
{
|
|
for (uint32_t i = 0; i < 5u; i++) a[i] = (uint8_t)((v >> (7u * i)) & 127u);
|
|
}
|
|
static uint32_t get32(const uint8_t *a)
|
|
{
|
|
return (uint32_t)a[0] | (uint32_t)a[1] << 7 | (uint32_t)a[2] << 14 | (uint32_t)a[3] << 21 | (uint32_t)a[4] << 28;
|
|
}
|
|
static void w32(uint8_t *p, uint32_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); }
|
|
static uint32_t crc(const uint8_t *p, uint32_t n) { return st_crc32(p, n); }
|
|
|
|
/* a .jwt v1 record: nf frames of bits (8 or 16), enc, a pattern from seed, the gains (b2) */
|
|
static uint32_t mk_jwt(uint8_t *r, uint32_t nf, uint32_t bits, uint32_t enc, uint32_t seed, const char *name)
|
|
{
|
|
uint32_t n = nf * 128u * bits / 8u, i;
|
|
memset(r, 0, 256);
|
|
memcpy(r, "JWT1", 4);
|
|
r[4] = 1; r[5] = (uint8_t)nf; r[6] = 7; r[7] = (uint8_t)bits; r[8] = 4; r[9] = (uint8_t)enc;
|
|
memset(r + 12, ' ', 12);
|
|
memcpy(r + 12, name, strlen(name) < 12u ? strlen(name) : 12u);
|
|
w32(r + 28, n);
|
|
for (i = 0; i < 132u; i++) r[32 + i] = (uint8_t)(i * 3u);
|
|
for (i = 0; i < 64u; i++) r[164 + i] = i < nf ? (uint8_t)(255u - (i * 37u + seed) % 120u) : 255u;
|
|
for (i = 0; i < n; i++) {
|
|
double ph = (double)(i % 128u) / 128.0 * 6.283185307179586, f = (double)(i / 128u) / (double)nf;
|
|
int v = (int)lrint(127.0 * sin(ph + f * (1.0 + seed)) * (0.6 + 0.4 * cos(ph * (2.0 + f * 3.0))));
|
|
r[256 + i] = (uint8_t)(int8_t)(v > 127 ? 127 : v < -128 ? -128 : v);
|
|
}
|
|
if (seed % 3u == 1u) r[256] = 0x80; /* (-128: MULAW's special case) */
|
|
w32(r + 24, crc(r + 32, 256u - 32u + n));
|
|
return 256u + n;
|
|
}
|
|
|
|
static uint32_t wt_req(uint32_t cmd, const uint8_t *a, uint32_t n) { return request(cmd, a, n); }
|
|
static uint32_t begin(uint32_t slot, uint32_t size, uint32_t c)
|
|
{
|
|
uint8_t a[11];
|
|
a[0] = (uint8_t)slot; put32(a + 1, size); put32(a + 6, c);
|
|
return wt_req(ED_WT_BEGIN, a, 11) ? R[1] : 99u;
|
|
}
|
|
static uint32_t data(uint32_t slot, uint32_t off, const uint8_t *p, uint32_t n)
|
|
{
|
|
static uint8_t a[400];
|
|
a[0] = (uint8_t)slot; put32(a + 1, off);
|
|
return wt_req(ED_WT_DATA, a, 6u + pack7(p, n, a + 6)) ? R[1] : 99u;
|
|
}
|
|
static uint32_t end(uint32_t slot, uint32_t abort, uint32_t *id)
|
|
{
|
|
uint8_t a[2] = {(uint8_t)slot, (uint8_t)abort};
|
|
uint32_t n = wt_req(ED_WT_END, a, abort ? 2u : 1u);
|
|
if (id) *id = n ? get32(R + 2) : 0u;
|
|
return n ? R[1] : 99u;
|
|
}
|
|
/* a whole upload: END's rc (or the first that failed, + 10 x the step) */
|
|
static uint32_t upload(uint32_t slot, const uint8_t *r, uint32_t size, uint32_t c, uint32_t *id)
|
|
{
|
|
uint32_t rc = begin(slot, size, c), o;
|
|
if (rc) return 10u + rc;
|
|
for (o = 0; o < size; o += 256u)
|
|
if ((rc = data(slot, o, r + o, size - o < 256u ? size - o : 256u)) != 0) return 20u + rc;
|
|
return end(slot, 0, id);
|
|
}
|
|
static uint32_t op(uint32_t o, uint32_t s, uint32_t t, const char *name)
|
|
{
|
|
uint8_t a[14];
|
|
uint32_t n = 2;
|
|
a[0] = (uint8_t)o; a[1] = (uint8_t)s;
|
|
if (o == 2u) a[n++] = (uint8_t)t;
|
|
if (o == 1u) { memset(a + 2, ' ', 12); memcpy(a + 2, name, strlen(name)); n = 14; }
|
|
return wt_req(ED_WT_OP, a, n) ? R[1] : 99u;
|
|
}
|
|
static uint32_t gen_of(uint32_t s) { return (uint32_t)R[5 + 25u * s + 6] | (uint32_t)R[5 + 25u * s + 7] << 7; }
|
|
static uint32_t list(void) { return wt_req(ED_WT_LIST, 0, 0) ? R[1] : 99u; }
|
|
static const uint8_t *slot_row(uint32_t s) { return R + 5 + 25u * s; }
|
|
/* the whole record of slot s read back with WT_GET (header, then the codes): 0 every piece rc 0 and equal to r */
|
|
static int get_all(uint32_t s, const uint8_t *r, uint32_t size)
|
|
{
|
|
static uint8_t got[4352];
|
|
uint8_t a[8];
|
|
uint32_t o, k;
|
|
for (o = 0; o < size; o += k) {
|
|
k = o < 256u ? 256u - o : size - o < 200u ? size - o : 200u;
|
|
a[0] = (uint8_t)s; put32(a + 1, o); a[6] = (uint8_t)(k & 127u); a[7] = (uint8_t)(k >> 7);
|
|
if (!wt_req(ED_WT_GET, a, 8) || R[1] || get32(R + 2) != o || ((uint32_t)R[7] | (uint32_t)R[8] << 7) != k ||
|
|
ed_unpack7(R + 9, host_wire_n - 15u, got + o, k) != k)
|
|
return 1;
|
|
}
|
|
return memcmp(got, r, size) != 0;
|
|
}
|
|
static void reboot(void) /* the RAM gone, the flash kept; the power-on check */
|
|
{
|
|
memset(&wt, 0, sizeof wt);
|
|
memset(&wvs, 0, sizeof wvs);
|
|
memset(wv, 0, sizeof wv);
|
|
host_wt_cut = -1;
|
|
wt_boot();
|
|
}
|
|
|
|
/* the spec's LUT (its formula) and wt_user_frame, then the line's int8 (eng_wave.c): the reference */
|
|
static int16_t ref_lut(int c, int enc)
|
|
{
|
|
if (!enc) return (int16_t)(c * 256);
|
|
if (c == -128) return -32768;
|
|
if (c < 0) return (int16_t)-ref_lut(-c, enc);
|
|
return (int16_t)lrint(32512.0 * (pow(8.0, c / 127.0) - 1.0) / 7.0);
|
|
}
|
|
/* BYTE's wt_user_frame at pq (Q8), as the spec has it, then the line's int8 */
|
|
static void ref_frame(const uint8_t *r, int32_t pq, int8_t *F)
|
|
{
|
|
const int8_t *codes = (const int8_t *)(r + 256);
|
|
const uint8_t *gain = r + 164;
|
|
int16_t lut[256], out[128];
|
|
uint32_t nf = r[5], k;
|
|
for (k = 0; k < 256u; k++) lut[k] = ref_lut((int)k - 128, r[9]);
|
|
uint32_t p = (uint32_t)(pq < 0 ? 0 : pq > (127 << 8) ? (127 << 8) : pq);
|
|
uint32_t s = p * (nf - 1u) * 16u / 127u, i = s >> 12, j = i + 1u < nf ? i + 1u : i;
|
|
int32_t fr = (int32_t)(s & 4095u);
|
|
const int8_t *a = codes + i * 128, *b = codes + j * 128;
|
|
for (k = 0; k < 128; k++) {
|
|
int32_t x = ((int32_t)lut[(uint8_t)(a[k] + 128)] * (int32_t)(gain[i] + 1u)) >> 8;
|
|
int32_t y = ((int32_t)lut[(uint8_t)(b[k] + 128)] * (int32_t)(gain[j] + 1u)) >> 8;
|
|
out[k] = (int16_t)(x + (((y - x) * fr) >> 12));
|
|
F[k] = (int8_t)clamp((out[k] + 128) >> 8, -128, 127);
|
|
}
|
|
}
|
|
/* BYTE's userGrid (the coordinator's text of wt-core.js): lo .. hi, base, nb -> g0, gs, na */
|
|
static void ref_key(int32_t lo, int32_t hi, int32_t base, int nb, int32_t *g0, int32_t *gs, int32_t *na)
|
|
{
|
|
if (hi <= lo) { *g0 = base; *gs = 0; *na = 1; }
|
|
else { *na = nb ? 6 : 8; *gs = (hi - lo) / (*na - 2) + 1; *g0 = base - (base - lo + *gs - 1) / *gs * *gs; }
|
|
}
|
|
/* half h of part ti: its key as the reference's, and its na frames BYTE's at g0 + k gs */
|
|
static int grid_is(uint32_t ti, uint32_t h, const uint8_t *r, int32_t g0, int32_t gs, int32_t na)
|
|
{
|
|
static int8_t F[128];
|
|
const int8_t *L = (const int8_t *)comb_buf[ti] + h * 1024u;
|
|
int ok = wv[ti].have[h] == wv[ti].want[h] && (int16_t)wv[ti].gk[h][0] == g0 &&
|
|
(wv[ti].gk[h][1] & 0x7FFFu) == (uint32_t)gs && (wv[ti].gk[h][1] >> 15) == (uint32_t)(na == 6);
|
|
for (int32_t k = 0; ok && k < na; k++) {
|
|
ref_frame(r, g0 + k * gs, F);
|
|
ok &= !memcmp(L + k * 128, F, 128);
|
|
}
|
|
return ok;
|
|
}
|
|
static void wave_part(uint32_t ti, int32_t a, int32_t b)
|
|
{
|
|
set_engine_of(&trk[ti], ENGI_WAVE); apply_preset_to(&trk[ti], 0); trk[ti].engine = trk[ti].eng_req;
|
|
trk[ti].p[P_E0] = (int16_t)a; trk[ti].p[P_E4] = (int16_t)b;
|
|
}
|
|
static void blocks(uint32_t n) /* the audio's blocks and the main loop's passes, interleaved */
|
|
{
|
|
for (uint32_t i = 0; i < n; i++) {
|
|
for (uint32_t ti = 0; ti < NTRK; ti++)
|
|
if (trk[ti].engine == ENGI_WAVE) ENG_WAVE.block(&trk[ti]);
|
|
}
|
|
}
|
|
static const int8_t *half(uint32_t ti, uint32_t h) { return (const int8_t *)comb_buf[ti] + h * 1024u; }
|
|
|
|
static uint8_t A0[4352], A1[4352], A2[4352];
|
|
|
|
static int basics(void)
|
|
{
|
|
int bad = 0;
|
|
uint32_t n, i, id, sz0, sz1;
|
|
reset();
|
|
memset(host_wt, 0xFF, sizeof host_wt);
|
|
reboot();
|
|
n = request(ED_INFO, 0, 0);
|
|
for (i = 0; i + 9u < n && !(host_wire[i] == 0x43 && host_wire[i + 1] == 2 && host_wire[i + 4] == 0x54); i++)
|
|
;
|
|
bad += check("INFO: 'C' 43 02 01 07, then 'T' 54 02 06 20 (six slots, 32 frames), then 'Y'", i + 9u < n &&
|
|
host_wire[i + 2] == 1u && host_wire[i + 3] == 7u && host_wire[i + 5] == 2u && host_wire[i + 6] == 6u &&
|
|
host_wire[i + 7] == 32u && host_wire[i + 8] == 0x59);
|
|
n = request(ED_WT_LIST, 0, 0);
|
|
bad += check("WT_LIST, a blank flash: 1, rc 0, 6 slots, 6 sectors, 6 free, every slot empty",
|
|
n == 5u + 5u + 6u * 25u + 1u && R[0] == 1u && !R[1] && R[2] == 6u && R[3] == 6u && R[4] == 6u &&
|
|
!slot_row(0)[0] && !slot_row(5)[0]);
|
|
sz0 = mk_jwt(A0, 32, 8, 0, 0, "LIN SWEEP");
|
|
sz1 = mk_jwt(A1, 5, 8, 1, 1, "MULAW 5");
|
|
bad += check("an upload (32 frames, LINEAR) into U1: END rc 0, the id the CRC of bytes 32..end",
|
|
upload(0, A0, sz0, crc(A0, sz0), &id) == 0u && id == crc(A0 + 32, sz0 - 32u) && sz0 == 4352u);
|
|
list();
|
|
bad += check("WT_LIST: U1 a table (32 frames, 8 bits, LINEAR, its flags, gen 1, its id, its name), 5 free",
|
|
R[4] == 5u && slot_row(0)[0] == 1u && slot_row(0)[1] == 32u && slot_row(0)[2] == 8u && slot_row(0)[3] == 0u &&
|
|
slot_row(0)[4] == 4u && slot_row(0)[6] == 1u && !slot_row(0)[7] && get32(slot_row(0) + 8) == id &&
|
|
!memcmp(slot_row(0) + 13, "LIN SWEEP ", 12) && !slot_row(1)[0]);
|
|
bad += check("WT_GET reads it back byte for byte (the header, then the codes)", !get_all(0, A0, sz0));
|
|
bad += check("a MULAW table of 5 frames (with a -128 code) into U2", upload(1, A1, sz1, crc(A1, sz1), &id) == 0u);
|
|
{ /* the firmware's MULAW table is the spec's formula */
|
|
int ok = 1;
|
|
for (i = 0; i < 128u; i++) ok &= WT_MU[i] == ref_lut((int)i, 1);
|
|
bad += check("WT_MU[128] is round(32512 x (8^(c/127) - 1) / 7)", ok);
|
|
}
|
|
{ /* played: A U1, B U2 (MIX 64: heard). Nothing can move POS: BYTE's one frame, at the knob's POS, in one block */
|
|
int32_t g0, gs, na, lo, hi, k;
|
|
wave_part(2, 16, 17);
|
|
trk[2].p[P_E5] = 64;
|
|
trk[2].p[P_E1] = 40;
|
|
trk[2].p[P_LD_SHP] = trk[2].p[P_ED_SHP] = 0;
|
|
trk[2].p[P_M1SRC] = trk[2].p[P_M2SRC] = trk[2].p[P_M3SRC] = trk[2].p[P_M4SRC] = 0;
|
|
blocks(1);
|
|
bad += check("a part asking for U1 / U2, nothing moving POS: one frame each at the knob's POS 40, in its first block "
|
|
"(nothing held silent)", wv_ready(&wv[2]) && grid_is(2, 0, A0, 40 << 8, 0, 1) &&
|
|
grid_is(2, 1, A1, 40 << 8, 0, 1));
|
|
/* the LFO's depth to the shape 10: POS 30 .. 50 can be reached (wv_span, BYTE's wf_span), B heard: six frames */
|
|
trk[2].p[P_LD_SHP] = 10;
|
|
lo = 30 << 8, hi = 50 << 8;
|
|
ref_key(lo, hi, 40 << 8, 1, &g0, &gs, &na);
|
|
blocks(1);
|
|
bad += check("the LFO's depth 10 given: a new key, drawn a frame a block, the part playing meanwhile (the table read "
|
|
"from the flash)", wv_ready(&wv[2]) && wv[2].have[0] == 0x41u && wv[2].have[1] == 0x41u &&
|
|
(int16_t)wv[2].gk[0][0] == g0);
|
|
for (k = 1; k < na - 1; k++) blocks(1);
|
|
bad += check("not whole until its last frame", wv[2].have[0] != wv[2].want[0]);
|
|
blocks(1);
|
|
bad += check("six blocks: six frames (B heard: BYTE's na with B), gs apart from g0, A and B each BYTE's "
|
|
"wt_user_frame there (LINEAR, gains; MULAW mu 7, 5 frames, -128)",
|
|
na == 6 && grid_is(2, 0, A0, g0, gs, na) && grid_is(2, 1, A1, g0, gs, na));
|
|
printf("wt: (POS 30 .. 50 reachable, knob 40: g0 %d gs %d na %d)\n", g0, gs, na);
|
|
{
|
|
int32_t l2, h2;
|
|
trk[2].p[P_ED_SHP] = -5; trk[2].p[P_M2SRC] = 1; trk[2].p[P_M2DST] = 3; trk[2].p[P_M2AMT] = -20;
|
|
wv_span(&trk[2], 40 << 8, &l2, &h2);
|
|
bad += check("the span: the LFO's, the envelope's and a matrix slot's depths on SHP add, both ways (10 + 5 + 20)",
|
|
l2 == 5 << 8 && h2 == 75 << 8);
|
|
trk[2].p[P_M3SRC] = 1; trk[2].p[P_M3DST] = 13; trk[2].p[P_M3AMT] = 3;
|
|
wv_span(&trk[2], 40 << 8, &l2, &h2);
|
|
bad += check("a slot on POS (E2) itself: the whole 0 .. 127", l2 == 0 && h2 == 127 << 8);
|
|
trk[2].p[P_ED_SHP] = 0; trk[2].p[P_M2SRC] = trk[2].p[P_M3SRC] = 0;
|
|
}
|
|
{
|
|
uint16_t bk0 = wv[2].gk[1][0], bk1 = wv[2].gk[1][1];
|
|
trk[2].p[P_E5] = 0;
|
|
blocks(1 + 8); /* (MIX's ramp down in the first; eight frames) */
|
|
ref_key(lo, hi, 40 << 8, 0, &g0, &gs, &na);
|
|
bad += check("MIX 0 (B unheard): A's eight frames; B not drawn again (its grid as it was)", na == 8 &&
|
|
grid_is(2, 0, A0, g0, gs, na) && wv[2].gk[1][0] == bk0 && wv[2].gk[1][1] == bk1 && wv_ready(&wv[2]));
|
|
}
|
|
{
|
|
int8_t k0 = half(2, 0)[5];
|
|
((int8_t *)comb_buf[2])[5] = (int8_t)(k0 ^ 0x55); /* (a mark: is it drawn again?) */
|
|
blocks(20);
|
|
bad += check("the key kept (the same parameters, the LFO running): nothing drawn again",
|
|
half(2, 0)[5] == (int8_t)(k0 ^ 0x55));
|
|
((int8_t *)comb_buf[2])[5] = k0;
|
|
}
|
|
list();
|
|
bad += check("WT_LIST: U1 used (1), U2 used (1); U3 not (0)", slot_row(0)[5] == 1u && slot_row(1)[5] == 1u &&
|
|
slot_row(2)[5] == 0u);
|
|
wave_part(3, 16, 16);
|
|
list();
|
|
bad += check("U1 named by two parts, A and B: used still 1 (0 / 1)", slot_row(0)[5] == 1u);
|
|
wave_part(3, 0, 0);
|
|
}
|
|
return bad;
|
|
}
|
|
|
|
/* the tables played: a note on U1 (A), POS swung by the LFO, or the knob turned, a second: every block its grid the
|
|
* one of its key (BYTE's frames there), the part never held silent nor faded in again, no click; and the cost */
|
|
#define PLAY_NB (FS / CTL)
|
|
static double py[PLAY_NB * CTL];
|
|
static int32_t pb[CTL];
|
|
static int16_t knob_k = -1; /* >= 0: the knob swept over the second */
|
|
typedef struct { uint32_t draws, keyok, ready, xin, out, quiet; double d2; int32_t miss; } play_t;
|
|
static double pd2(const double *y, uint32_t n)
|
|
{
|
|
double m = 0;
|
|
for (uint32_t i = 2; i < n; i++) { double d = fabs(y[i] - 2 * y[i - 1] + y[i - 2]); m = d > m ? d : m; }
|
|
return m;
|
|
}
|
|
static void play_setup(uint32_t a, int32_t pos, int32_t lfo)
|
|
{
|
|
memset(wv, 0, sizeof wv);
|
|
host_tracks_init();
|
|
host_preset(&trk[0], ENGI_WAVE, 0);
|
|
trk[0].p[P_E0] = (int16_t)a; trk[0].p[P_E1] = (int16_t)pos; trk[0].p[P_E4] = 0; trk[0].p[P_E5] = 0;
|
|
trk[0].p[P_ATK] = 0; trk[0].p[P_SUS] = 127; trk[0].p[P_REL] = 10; trk[0].p[P_FCUT] = 127; trk[0].p[P_ED_FLT] = 0;
|
|
trk[0].p[P_LD_FLT] = trk[0].p[P_LD_PIT] = trk[0].p[P_LD_AMP] = 0;
|
|
trk[0].p[P_LD_SHP] = (int16_t)lfo; trk[0].p[P_LWAVE] = LW_SIN; trk[0].p[P_LRATE] = 60; trk[0].p[P_LFADE] = 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 (uint32_t i = 0; i < 8u; i++) trk[0].pe_old[i] = trk[0].p[P_E0 + i];
|
|
}
|
|
static play_t play(uint32_t a, int32_t pos, int32_t lfo, const uint8_t *r)
|
|
{
|
|
play_t q = {0, 1, 1, 0, 0, 0, 0, 0};
|
|
uint32_t i, k;
|
|
play_setup(a, pos, lfo);
|
|
for (i = 0; i < 8u; i++) { engine_block(&trk[0]); track_render(&trk[0], pb, CTL); }
|
|
trk_note_on(&trk[0], 52, 100);
|
|
for (i = 0; i < 4u; i++) { engine_block(&trk[0]); track_render(&trk[0], pb, CTL); }
|
|
for (i = 0; i < PLAY_NB; i++) {
|
|
int was = wv[0].have[0] != wv[0].want[0];
|
|
uint16_t k0 = wv[0].gk[0][0];
|
|
if (knob_k >= 0) trk[0].p[P_E1] = (int16_t)(127u * i / (PLAY_NB - 1u));
|
|
engine_block(&trk[0]);
|
|
q.draws += was || wv[0].have[0] != wv[0].want[0] || wv[0].gk[0][0] != k0; /* (a frame drawn in this block) */
|
|
track_render(&trk[0], pb, CTL);
|
|
q.xin |= trk[0].xin != 0;
|
|
q.ready &= wv_ready(&wv[0]);
|
|
if (r) {
|
|
uint32_t g = wv[0].gk[0][1], gs = g & 0x7FFFu, na = !gs ? 1u : g >> 15 ? 6u : 8u;
|
|
int32_t g0 = (int16_t)wv[0].gk[0][0], p1 = trk[0].v[0].s[0] - 1;
|
|
q.keyok &= wv[0].have[0] != wv[0].want[0] || grid_is(0, 0, r, g0, (int32_t)gs, (int32_t)na);
|
|
if (wv[0].have[0] == wv[0].want[0]) { /* (the POS past the grid: read at its edge; by how much, Q8) */
|
|
int32_t top = g0 + (int32_t)((na - 1u) * gs), m = p1 < g0 ? g0 - p1 : p1 > top ? p1 - top : 0;
|
|
q.out += m > 0;
|
|
q.miss = m > q.miss ? m : q.miss;
|
|
}
|
|
}
|
|
for (k = 0; k < CTL; k++) py[i * CTL + k] = pb[k] / 32768.0;
|
|
}
|
|
for (i = 0; i < 8u; i++) {
|
|
double e = 0;
|
|
for (k = 0; k < PLAY_NB * CTL / 8u; k++) e += py[i * (PLAY_NB * CTL / 8u) + k] * py[i * (PLAY_NB * CTL / 8u) + k];
|
|
q.quiet += sqrt(e / (PLAY_NB * CTL / 8u)) < 0.01;
|
|
}
|
|
q.d2 = pd2(py, PLAY_NB * CTL);
|
|
return q;
|
|
}
|
|
static double play_ns(uint32_t a, int32_t lfo, uint32_t nv, int turn)
|
|
{
|
|
uint32_t i, k;
|
|
uint64_t t0;
|
|
play_setup(a, 64, lfo);
|
|
for (i = 0; i < 8u; i++) { engine_block(&trk[0]); track_render(&trk[0], pb, CTL); }
|
|
for (k = 0; k < nv; k++) trk_note_on(&trk[0], (uint8_t)(36 + 5 * k), 100);
|
|
for (i = 0; i < 8u; i++) { engine_block(&trk[0]); track_render(&trk[0], pb, CTL); }
|
|
t0 = now_ns();
|
|
for (i = 0; i < PLAY_NB; i++) {
|
|
if (turn) trk[0].p[P_E1] = (int16_t)(i & 64u ? 127u - (i & 63u) : i & 63u); /* (a new key every block) */
|
|
engine_block(&trk[0]); track_render(&trk[0], pb, CTL);
|
|
}
|
|
return (double)(now_ns() - t0) / (PLAY_NB * CTL);
|
|
}
|
|
static int playing(void)
|
|
{
|
|
int bad = 0;
|
|
play_t s, l, kn;
|
|
double still = 0, x;
|
|
uint32_t p;
|
|
for (p = 0; p <= 127u; p += 9u) { /* (the clicks' scale: the table held still, POS 0 .. 127) */
|
|
s = play(16, (int32_t)p, 0, A0);
|
|
still = s.d2 > still ? s.d2 : still;
|
|
}
|
|
s = play(16, 64, 0, A0);
|
|
bad += check("U1 held still (POS 64): one frame drawn, once; never silent", s.keyok && s.ready && !s.xin && !s.quiet &&
|
|
s.draws <= 1u);
|
|
l = play(16, 64, 40, A0);
|
|
printf("wt: (an LFO on POS (depth 40), a second after the note's first blocks: %u of %u blocks drew a frame, %u read past the grid; the largest 2nd "
|
|
"difference %.4f, held still %.4f)\n", l.draws, PLAY_NB, l.out, l.d2, still);
|
|
bad += check("an LFO on POS (a second): its range from its depth, the key steady: eight frames drawn once (one a block), "
|
|
"never again; every grid BYTE's for its key, never silent nor faded in, no click, never past the grid",
|
|
l.keyok && l.ready && !l.xin && !l.quiet && l.draws <= 8u && l.d2 <= still * 1.1 + 1e-4 && !l.out);
|
|
knob_k = 0;
|
|
kn = play(16, 0, 20, A0); /* (an LFO's depth too: eight frames a key) */
|
|
knob_k = -1;
|
|
printf("wt: (the knob turned 0 .. 127 in a second: %u blocks drew a frame; 2nd difference %.4f)\n", kn.draws, kn.d2);
|
|
bad += check("POS turned 0 .. 127 (a second): each new key drawn a frame a block, the table read from the flash "
|
|
"meanwhile; never silent nor faded in, no click", kn.keyok && kn.ready && !kn.xin && !kn.quiet &&
|
|
kn.d2 <= still * 1.1 + 1e-4);
|
|
{ /* the cost on this host: a frame drawn, a block's samples */
|
|
static int8_t F[128];
|
|
uint64_t t0 = now_ns();
|
|
uint32_t n = 100000u, i;
|
|
for (i = 0; i < n; i++) wv_draw(F, 0, (int32_t)(i & 1023u) * 31);
|
|
x = (double)(now_ns() - t0) / n;
|
|
printf("wt: cost on this host: a frame drawn %.0f ns (%.1f ns a block's sample, CTL %u)\n", x, x / CTL, CTL);
|
|
{
|
|
double a1 = 1e9, a2 = 1e9, a3 = 1e9, a4 = 1e9, a5 = 1e9, a6 = 1e9, a7 = 1e9, a8 = 1e9, v;
|
|
for (i = 0; i < 5u; i++) {
|
|
v = play_ns(1, 0, 1, 0); a1 = v < a1 ? v : a1;
|
|
v = play_ns(16, 0, 1, 0); a2 = v < a2 ? v : a2;
|
|
v = play_ns(16, 40, 1, 0); a3 = v < a3 ? v : a3;
|
|
v = play_ns(16, 40, 1, 1); a4 = v < a4 ? v : a4;
|
|
v = play_ns(1, 0, 8, 0); a5 = v < a5 ? v : a5;
|
|
v = play_ns(16, 0, 8, 0); a6 = v < a6 ? v : a6;
|
|
v = play_ns(16, 40, 8, 0); a7 = v < a7 ? v : a7;
|
|
v = play_ns(16, 40, 8, 1); a8 = v < a8 ? v : a8;
|
|
}
|
|
printf("wt: cost on this host, ns a sample (the part): 1 voice: FM2 %.1f, U1 still %.1f, U1 + LFO on POS %.1f, "
|
|
"U1 + LFO, the knob turning (a new key each block: always drawing, read from the flash) %.1f; 8 voices: %.1f, %.1f, %.1f, %.1f\n", a1, a2, a3, a4, a5, a6, a7, a8);
|
|
}
|
|
}
|
|
return bad;
|
|
}
|
|
|
|
static int failures(void)
|
|
{
|
|
int bad = 0;
|
|
uint32_t sz, id, before;
|
|
uint8_t a[11];
|
|
sz = mk_jwt(A2, 8, 8, 0, 2, "BAD CRC");
|
|
list(); before = get32(slot_row(0) + 8);
|
|
bad += check("a CRC wrong (BEGIN's): END rc 2, U1 as it was", upload(0, A2, sz, crc(A2, sz) ^ 1u, &id) == 2u &&
|
|
(list(), slot_row(0)[0] == 1u && get32(slot_row(0) + 8) == before) && !get_all(0, A0, 4352));
|
|
A2[24] ^= 0x55; /* the header's own CRC wrong, the record's right */
|
|
bad += check("the header's CRC field wrong: rc 2, nothing changed", upload(0, A2, sz, crc(A2, sz), &id) == 2u &&
|
|
(list(), get32(slot_row(0) + 8) == before));
|
|
A2[24] ^= 0x55;
|
|
A2[6] = 6; /* log2len 6: refused */
|
|
w32(A2 + 24, crc(A2 + 32, sz - 32u));
|
|
bad += check("a header BYTE's reader refuses (log2len 6): rc 2", upload(2, A2, sz, crc(A2, sz), &id) == 2u &&
|
|
(list(), !slot_row(2)[0]));
|
|
A2[6] = 7;
|
|
w32(A2 + 24, crc(A2 + 32, sz - 32u));
|
|
bad += check("WT_DATA without WT_BEGIN: rc 5 (stale)", data(3, 0, A2, 256) == 5u);
|
|
bad += check("WT_BEGIN past one sector (33 frames of 8 bits) or not whole frames: rc 1",
|
|
begin(3, 256u + 33u * 128u, 0) == 1u && begin(3, 256u + 100u, 0) == 1u && begin(6, 384u, 0) == 1u);
|
|
begin(3, sz, crc(A2, sz));
|
|
bad += check("WT_DATA out of order: rc 1", data(3, 256, A2 + 256, 256) == 1u);
|
|
usb.resets++;
|
|
bad += check("a USB reset in between: rc 5", data(3, 0, A2, 256) == 5u);
|
|
begin(3, sz, crc(A2, sz));
|
|
data(3, 0, A2, 256);
|
|
bad += check("WT_END before the last byte: rc 1, the slot empty", end(3, 0, &id) == 1u && (list(), !slot_row(3)[0]));
|
|
begin(3, sz, crc(A2, sz));
|
|
bad += check("WT_END abort (1): rc 0, the slot empty", end(3, 1, &id) == 0u && (list(), !slot_row(3)[0]));
|
|
a[0] = 2; put32(a + 1, 0); a[6] = 10; a[7] = 0;
|
|
bad += check("WT_GET of an empty slot: rc 6", wt_req(ED_WT_GET, a, 8) && R[1] == 6u);
|
|
a[0] = 0; put32(a + 1, 250); a[6] = 10;
|
|
bad += check("WT_GET across the header's end: rc 1", wt_req(ED_WT_GET, a, 8) && R[1] == 1u);
|
|
song.playing = 1;
|
|
bad += check("WT_BEGIN while playing: the transport stopped, rc 0", begin(3, sz, crc(A2, sz)) == 0u && !song.playing);
|
|
end(3, 1, &id);
|
|
return bad;
|
|
}
|
|
|
|
/* a power cut at every erase and program of a table replacing U1: after the boot U1 is the old one or the new one */
|
|
static int power_cuts(void)
|
|
{
|
|
int bad = 0, ok = 1, saw_old = 0, saw_new = 0;
|
|
uint32_t sz = mk_jwt(A2, 20, 8, 1, 4, "NEW ONE"), oldid, newid = crc(A2 + 32, sz - 32u), ops, k, id;
|
|
list(); oldid = get32(slot_row(0) + 8);
|
|
host_wt_ops = 0;
|
|
memcpy(A1 + 0, host_wt, 0); /* (the flash before: kept below) */
|
|
{
|
|
static uint8_t keep[0x8000];
|
|
memcpy(keep, host_wt, sizeof keep);
|
|
if (upload(0, A2, sz, crc(A2, sz), &id)) ok = 0;
|
|
ops = host_wt_ops;
|
|
for (k = 0; k <= ops && ok; k++) {
|
|
memcpy(host_wt, keep, sizeof keep);
|
|
reboot();
|
|
host_wt_cut = (int32_t)k; /* k operations, then the power goes */
|
|
upload(0, A2, sz, crc(A2, sz), &id);
|
|
reboot();
|
|
list();
|
|
id = get32(slot_row(0) + 8);
|
|
ok &= slot_row(0)[0] == 1u && (id == oldid || id == newid) && slot_row(1)[0] == 1u;
|
|
ok &= id == oldid ? !get_all(0, A0, 4352) : !get_all(0, A2, sz);
|
|
saw_old |= id == oldid;
|
|
saw_new |= id == newid;
|
|
}
|
|
bad += check("a power cut at each of an upload's erases and programs (U1 replaced): after the boot the old table or the new, whole",
|
|
ok && saw_old && saw_new && ops >= 20u);
|
|
printf("wt: (%u flash operations an upload of 20 frames; the cut after each)\n", ops);
|
|
}
|
|
reboot();
|
|
{ /* a part playing U1 when it is replaced: drawn again from the new one (POS 50, still: one frame) */
|
|
wave_part(2, 16, 0);
|
|
trk[2].p[P_E5] = 0;
|
|
trk[2].p[P_E1] = 50;
|
|
blocks(1);
|
|
bad += check("a part playing U1 after a reboot: the table on flash (the new one), drawn", wv_ready(&wv[2]) &&
|
|
grid_is(2, 0, A2, 50 << 8, 0, 1));
|
|
sz = mk_jwt(A0, 32, 8, 0, 0, "LIN SWEEP");
|
|
upload(0, A0, sz, crc(A0, sz), &id);
|
|
blocks(1);
|
|
bad += check("U1 written again while it plays: drawn again from the new table (its key the same: the generation's)",
|
|
wv_ready(&wv[2]) && grid_is(2, 0, A0, 50 << 8, 0, 1));
|
|
}
|
|
return bad;
|
|
}
|
|
|
|
/* all six slots full: the slot written is rewritten in place; cut, it is damaged (plays SINE), the others whole */
|
|
static int in_place(void)
|
|
{
|
|
int bad = 0, ok = 1;
|
|
uint32_t s, sz, id;
|
|
static uint8_t keep[0x8000];
|
|
for (s = 2; s < 6u; s++) {
|
|
sz = mk_jwt(A2, 4u + s, 8, s & 1u, s, "FILL");
|
|
ok &= upload(s, A2, sz, crc(A2, sz), &id) == 0u;
|
|
}
|
|
list();
|
|
bad += check("six tables: no sector free", ok && R[4] == 0u && slot_row(5)[0] == 1u);
|
|
memcpy(keep, host_wt, sizeof keep);
|
|
sz = mk_jwt(A2, 12, 8, 0, 9, "IN PLACE");
|
|
bad += check("a seventh table into U3 (in place): rc 0, listed", upload(2, A2, sz, crc(A2, sz), &id) == 0u &&
|
|
(list(), get32(slot_row(2) + 8) == id && R[4] == 0u) && !get_all(2, A2, sz));
|
|
memcpy(host_wt, keep, sizeof keep);
|
|
reboot();
|
|
host_wt_cut = 6; /* the sector erased, a few pieces written: the power goes */
|
|
upload(2, A2, sz, crc(A2, sz), &id);
|
|
reboot();
|
|
list();
|
|
bad += check("cut while U3 was rewritten in place: U3 damaged (state 2), the other five whole",
|
|
slot_row(2)[0] == 2u && slot_row(0)[0] == 1u && slot_row(1)[0] == 1u && slot_row(3)[0] == 1u &&
|
|
slot_row(4)[0] == 1u && slot_row(5)[0] == 1u && !get_all(0, A0, 4352));
|
|
wave_part(3, 18, 0);
|
|
blocks(2);
|
|
bad += check("a damaged slot plays SINE", wv[3].want[0] == 1u && wv_ready(&wv[3]));
|
|
bad += check("written again: whole", upload(2, A2, sz, crc(A2, sz), &id) == 0u && (list(), slot_row(2)[0] == 1u));
|
|
return bad;
|
|
}
|
|
|
|
static int ops_and_fallback(void)
|
|
{
|
|
int bad = 0;
|
|
uint32_t id0, id1, gen0, gen1;
|
|
list(); id0 = get32(slot_row(0) + 8); id1 = get32(slot_row(1) + 8); gen0 = gen_of(0); gen1 = gen_of(1);
|
|
bad += check("RENAME U1: its name, the id kept, its gen + 1", op(1, 0, 0, "RENAMED") == 0u &&
|
|
(list(), !memcmp(slot_row(0) + 13, "RENAMED ", 12) && get32(slot_row(0) + 8) == id0 &&
|
|
gen_of(0) == gen0 + 1u));
|
|
gen0++;
|
|
bad += check("RENAME with a byte past 0x7E: rc 1", (memset(A2, 0x7F, 14), A2[0] = 1, A2[1] = 0,
|
|
wt_req(ED_WT_OP, A2, 14) && R[1] == 1u));
|
|
bad += check("MOVE U1, U2: swapped (ids, generations), each gen + 1", op(2, 0, 1, 0) == 0u &&
|
|
(list(), get32(slot_row(0) + 8) == id1 && get32(slot_row(1) + 8) == id0 && gen_of(0) == gen1 + 1u &&
|
|
gen_of(1) == gen0 + 1u));
|
|
reboot();
|
|
list();
|
|
bad += check("after a reboot the same", get32(slot_row(0) + 8) == id1 && get32(slot_row(1) + 8) == id0 &&
|
|
!memcmp(slot_row(1) + 13, "RENAMED ", 12));
|
|
list(); gen0 = gen_of(3);
|
|
bad += check("DELETE U4: empty, a sector free, its gen + 1 (listed, empty)", op(0, 3, 0, 0) == 0u &&
|
|
(list(), !slot_row(3)[0] && R[4] == 1u && gen_of(3) == gen0 + 1u));
|
|
bad += check("DELETE an empty slot: rc 6", op(0, 3, 0, 0) == 6u);
|
|
wave_part(4, 19, 0);
|
|
blocks(2);
|
|
bad += check("an empty slot (U4) plays SINE", wv[4].want[0] == 1u && wv_ready(&wv[4]));
|
|
wave_part(4, 22, 23); /* (U7, U8 of before) */
|
|
blocks(2);
|
|
bad += check("A 22, B 23 (U7 / U8 stored before: gone) play SINE", wv[4].want[0] == 1u && wv[4].want[1] == 1u &&
|
|
ENG_WAVE.edit[0].max == 21 && !strcmp(N_WAVEF[21], "U6"));
|
|
{
|
|
char val[16]; const char *unit = 0;
|
|
int ok;
|
|
param_format(&ENG_WAVE.edit[0], 22, val, &unit); ok = !strcmp(val, "SINE");
|
|
param_format(&ENG_WAVE.edit[4], 23, val, &unit); ok &= !strcmp(val, "SINE");
|
|
param_format(&ENG_WAVE.edit[0], 21, val, &unit); ok &= !strcmp(val, "U6");
|
|
param_format(&ENG_WAVE.edit[2], 9, val, &unit); ok &= !strcmp(val, "FM"); /* (another list: its last, as before) */
|
|
bad += check("a stored 22 / 23 shown as what it plays (SINE), not U6; U6 itself U6", ok);
|
|
}
|
|
{
|
|
uint32_t sz = mk_jwt(A2, 8, 16, 0, 3, "SIXTEEN"), id;
|
|
bad += check("a 16-bit table: stored, listed (16 bits), WAVE plays SINE", upload(3, A2, sz, crc(A2, sz), &id) == 0u &&
|
|
(list(), slot_row(3)[0] == 1u && slot_row(3)[2] == 16u) && !get_all(3, A2, sz) &&
|
|
(wave_part(4, 19, 0), blocks(2), wv[4].want[0] == 1u));
|
|
}
|
|
return bad;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
int bad = 0;
|
|
bad += basics();
|
|
bad += playing();
|
|
bad += failures();
|
|
bad += power_cuts();
|
|
bad += in_place();
|
|
bad += ops_and_fallback();
|
|
printf("wt: %s (%d failed)\n", bad ? "FAILED" : "all passed", bad);
|
|
return bad != 0;
|
|
}
|