Files
JIANT/tests/math_timelock_test.c
Claude d9a4bba9ca MATH TIME (prototype): KEY / LOCK / TEMPO on MATH's MORE page by RESTART; LOCK the default
The owner: "que las fórmulas no cambien su time con las notas ... por defecto debería
estar así". MATH's t is already two clocks (MOTION's): the note's low 8 bits (the wave,
its pitch) and the slow bits above (the rhythm, the phrase). TIME picks the slow clock's
pace:
- KEY   the note's (as before: a key up, the formula faster). Every project and user
        preset of before reads KEY (its slot held 0), so they sound as they did.
- LOCK  C4's whatever the key: every key the same rhythm, transposed. At C4 KEY and LOCK
        are the same, bit for bit (the MOTION 64 shortcut kept where the paces are one).
        A factory preset load (ui.c apply_preset_to) sets it: the new default.
- TEMPO the song's clock (MOTION's own SYNC, 128 .. 255: t >> 13 a beat at MOTION 64),
        BYTE and FLOAT. WAVE SYNC (V10.8.62) stays as it was.
RESTART (KNOB 1) unchanged, combinable with any.

Where: core.h P_MTIME, in DRUM's IMAGE / VOICE's LYRIC slot (DIGITAL's OP3 REL: 0 on
every MATH track before it; a user preset and a project store it as any slot). MORE's
KNOB 2 (params.c PAGES, track_desc). lofi_render decides the slow clock once a block and
hands the renders its step in m->inc (0: the song's clock), so lofi_byte_render and
lofi_float_render each lose their own MOTION / WAVE SYNC tests (-4 B each).

Flash: TIME +108 B (code +76: lofi_render +68, apply_preset_to +12, track_desc +4, renders -8;
the descriptor and its names ~32), room 56 B: N_CHIP N_RWAVE N_MTIME packed into core.h (-24),
PNE (were tables of pointers), N_DIV = N_DTIME's first ten names (-32; PEN; enum_order tells
them apart by max; ui.c rate_name by str_nth; tests by N_DIV_N / desc_nm). Image 584336 B
of 584336 (0 B free; was 52). BT 73692 (unchanged), RAM .data+.bss 98120 (unchanged).
audio.c untouched; tools/audio_layout.txt recorded again (FELUCCA_REPIN=keep); a plain
build passes.

CPU (tests/cpu_lines_test.py, mean / worst %, the busier core): 101..108 and 0c the same
or 1 lower; 0d core 0 67 -> 68 / 71 -> 71 (+0.2 % instructions), 0e 82 / 85 unchanged;
0d / 0e with TIME KEY forced: the same as LOCK.

Tests: regress 219 golden renders, 0 changed (V23's intact); math_timelock_test rewritten
through lofi_render: WAVE SYNC as before, KEY faster up the octave, LOCK and TEMPO the
same slow time at C4 and C5 (BYTE, FLOAT), LOCK == KEY at C4, TEMPO follows the BPM, a
preset loads LOCK, a kit's IMAGE still 0; gen_test, fit_test, ui_test, app contract
(docs/NRPN.md, web/NRPN.md: 74 TIME on MATH) pass. The suite's other failures are the
same on 32099fee.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-30 19:06:21 +00:00

99 lines
5.6 KiB
C

// Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant). All rights reserved.
/* (V10.8.62, a user: "I love the MATH BYTEBEAT's sound and its kinda arp, but the bpm is not the same from a note to
* another") MATH's WAVE SYNC (the time locked): BYTE's slow bits (its rhythm, the arp's steps) from the tempo, the fast
* ones (the pitch) the note's. (math-pitch, the owner: "que las fórmulas no cambien su time con las notas") and MATH's
* TIME (core.h P_MTIME, MORE's KNOB 2): KEY the note's pace (as before), LOCK C4's whatever the key, TEMPO the song's.
* Two notes an octave apart at 120 BPM, through lofi_render (the voice's own set-up: TIME decided there):
* WAVE SYNC, TIME LOCK, TIME TEMPO (BYTE and FLOAT): the same slow time block by block, the same rhythm; two pitches
* TIME KEY: the octave runs the rhythm faster (as before); at C4 KEY and LOCK the same, bit for bit
* TEMPO follows the BPM (60 BPM: half the steps of 120); a MATH preset loads TIME LOCK, a store of before reads KEY
* Built and run by CI (voice-user-check.yml) */
#define UI_TEST_NO_MAIN 1
#include "ui_test.c"
#define TL_BLOCKS 6000u /* (~8.7 s: ~17 beats at 120) */
/* formula ALGO 4 (B02, BYTE) or 8 (F03, FLOAT), WAVE w, TIME tm, MOTION mo, at note n for TL_BLOCKS blocks, the bar's
* clock run by the test at bpm: each block's slow time (v.ph[1]) into ts[], the steps of the rhythm (the slow bits'
* changes) counted; the output's hash */
static uint32_t take(int16_t w, int16_t tm, int16_t mo, uint32_t n, uint32_t bpm, uint32_t *ts, uint32_t *steps)
{
static track_t t;
static voice_t v;
static int32_t o[CTL];
uint32_t k, i, h = 2166136261u, last = 0xFFFFFFFFu;
memset(&t, 0, sizeof t); memset(&v, 0, sizeof v);
t.p[P_E0] = 2; t.p[P_E1] = w; t.p[P_E2] = w == RW_FLOAT ? 8 : 4; t.p[P_E4] = mo; t.p[P_FCUT] = 127; t.p[P_FRES] = 50;
t.p[P_MTIME] = tm;
song.g[G_BPM] = (int16_t)bpm;
bar_n = 0; beat_n = 0; beat_pos = 0;
lofi_note_on(&t, &v);
*steps = 0;
for (k = 0; k < TL_BLOCKS; k++) {
vmod_t m;
memset(&m, 0, sizeof m);
m.amp0 = m.amp1 = 32767; m.shape = 64 << 8; m.pitch16 = (int32_t)n * 16;
memset(o, 0, sizeof o);
lofi_render(&t, &v, o, CTL, &m);
for (i = 0; i < CTL; i++) h = (h ^ (uint32_t)o[i]) * 16777619u;
ts[k] = v.ph[1];
if ((ts[k] >> 16) != last) { last = ts[k] >> 16; (*steps)++; }
beat_pos += CTL; /* (the bar's clock, as seq.c runs it) */
if (beat_pos >= beat_samples()) {
beat_pos -= beat_samples();
beat_n = (beat_n + 1u) & 3u;
if (!beat_n) bar_n++;
}
}
return h;
}
static uint32_t a[TL_BLOCKS], b[TL_BLOCKS];
/* C4 and C5 the same slow time, block by block; *ha / *hb their outputs' hashes, *sa / *sb their steps */
static uint32_t pair(int16_t w, int16_t tm, int16_t mo, uint32_t *ha, uint32_t *hb, uint32_t *sa, uint32_t *sb)
{
uint32_t k, same = 1;
*ha = take(w, tm, mo, 60, 120, a, sa);
*hb = take(w, tm, mo, 72, 120, b, sb);
for (k = 0; k < TL_BLOCKS; k++)
same &= a[k] == b[k];
return same;
}
int main(void)
{
uint32_t ha, hb, sa, sb, hk, hl, s60, s120;
int bad = 0;
bad += check("WAVE SYNC at 120 BPM: C4 and C5 the same slow time, block by block (the same rhythm)",
pair(RW_BLOCK, MT_KEY, 0, &ha, &hb, &sa, &sb) && sa == sb && sa > 4u);
bad += check("WAVE SYNC: the notes still two pitches (different output)", ha != hb);
printf("math-timelock: WAVE SYNC steps %u / %u\n", sa, sb);
pair(RW_BYTE, MT_KEY, 0, &hk, &hb, &sa, &sb);
bad += check("TIME KEY (BYTE): the octave runs the rhythm faster (as before)", sb > sa);
printf("math-timelock: KEY steps %u / %u\n", sa, sb);
bad += check("TIME LOCK (BYTE, MOTION 0): C4 and C5 the same slow time, two pitches",
pair(RW_BYTE, MT_LOCK, 0, &hl, &hb, &sa, &sb) && sa == sb && sa > 4u && hl != hb);
bad += check("TIME LOCK at C4 is KEY at C4, bit for bit", hl == hk);
bad += check("TIME LOCK (BYTE, MOTION 64: t as written, no shortcut): the same slow time",
pair(RW_BYTE, MT_LOCK, 64, &ha, &hb, &sa, &sb) && ha != hb);
bad += check("TIME LOCK (FLOAT): the same slow time, two pitches",
pair(RW_FLOAT, MT_LOCK, 64, &ha, &hb, &sa, &sb) && ha != hb);
take(RW_FLOAT, MT_KEY, 64, 60, 120, a, &sa);
take(RW_FLOAT, MT_KEY, 64, 72, 120, b, &sb);
bad += check("TIME KEY (FLOAT): the octave faster (as before)", sb > sa);
bad += check("TIME TEMPO (BYTE, FLOAT): the same slow time at C4 and C5",
pair(RW_BYTE, MT_TEMPO, 0, &ha, &hb, &sa, &sb) && ha != hb && pair(RW_FLOAT, MT_TEMPO, 0, &ha, &hb, &sa, &sb));
take(RW_BYTE, MT_TEMPO, 64, 60, 120, a, &s120);
take(RW_BYTE, MT_TEMPO, 64, 60, 60, a, &s60);
bad += check("TIME TEMPO follows the BPM (60 BPM about half the steps of 120)", s60 * 2u <= s120 + 2u && s60 * 2u + 2u >= s120);
printf("math-timelock: TEMPO steps at 120 / 60 BPM: %u / %u\n", s120, s60);
{
track_t *t = &trk[0];
set_engine_of(t, ENGI_LOFI);
bad += check("a MATH preset loads TIME LOCK, RESTART on", t->p[P_MTIME] == MT_LOCK && t->p[P_LRTRG] == 1);
bad += check("MORE's KNOB 2 on MATH: TIME (KEY LOCK TEMPO)", track_desc(t, P_MTIME)->max == 2 &&
str_eq(desc_nm(track_desc(t, P_MTIME), 0), "KEY") && str_eq(desc_nm(track_desc(t, P_MTIME), 2), "TEMPO"));
set_engine_of(t, ENGI_DRUM);
bad += check("a DRUM kit's IMAGE (the same slot) still 0 after a load", t->p[P_DXIMG] == 0);
}
printf(bad ? "math-timelock: %d FAILED\n" : "math-timelock: all ok\n", bad);
return bad != 0;
}