Files
JIANT/tests/ctl64_timing_test.c
Claude 3c6854b0f5 V10.8.40: the tempo exact and every DIV on one grid, a track's place from the transport's, RESYNC, MOOD on the drums
(a user: "the sequencer creeps ahead of the project's tempo"; the owner: "after a pattern change, an edit, EUCLID, a
variation, a track's start no longer lines up with the others'; I stop and play again")

- the tempo (fx.c beat_samples, seq_time): a beat a multiple of 24 samples, so every division of it is whole and tracks
  on different DIVs, the click and the clock out keep one grid (were FS * 60 / BPM cut, and cut again a division: at
  133 BPM a 1/16 track 8 ms a minute ahead of the tempo, a 1/32 one 20 ms, so 12 ms a minute apart); mix_block leaves
  a sample of the sequencer's time out when what that cut adds up to one: FS * 60 samples are BPM beats exactly
  (tests/drift_test.c: 0 samples after ten minutes, eight tempos, every DIV, SWING, FIT, the click). The DELAY's time
  as it was (the DAC's samples: beat_real)
- a track's place (seq.c seq_anchor): at every step it enters, the step and the samples into it from the transport's
  1/16s since its origin (PLAY, a song's section in, RESYNC) over its DIV. A track left alone is there already; one
  whose LEN or DIV changed under it (EUCLID's STEPS, MUTATE, a variation of another length, the knobs, SWING) is back
  with the others at its next step (tests/resync_test.c: 14 actions x 18 moments and EUCLID's knobs, none off; before:
  11 of the 14 left a track off). LIVE's playheads, a JUMP, FIT's own clock and an odd LEN with SWING as they run
- RESYNC: SEQ held + PLAY while it plays: every track from its first step at the next bar, in time (the banner, the
  [SEQ] layer's footer)
- GEN's MOOD on the drums in every GENRE mode (the owner: "CALM changes nothing audible, everything still has drums"):
  CALM the kit's levels down (the kick first; at -50 a low, short, dark closed hat alone), DANCE drums in every piece,
  a kick on the bar's first and third beats from +25 and on every beat from +40, the kit up and driven; the energy
  +-60 (was 40); MID byte for byte V10.8.39's (gen_entry_test). tests/gen_mood.c: the study's table
- usb_host.c uh_hub_status in the image (the Bluetooth segment's room)
- tests: the clocks that counted the DAC's samples count the sequencer's (rep_sync), the references that hold a
  sequence's timing follow (micro's 0.9.0 render, expr's MIDI OUT checksum, perform's 1/16, 14 golden renders: every
  key kept); layout re-recorded (REPIN=keep)

Image 583316 B of 584336 (1020 B free), RAM 97008 of 98304, RAM code 32504 of 34816, pool spare 8480 B, BT segment
73700 of 73728 B. audio.c as V23's.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
2026-11-24 22:29:50 +00:00

138 lines
6.4 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */
/* (0.9.9 ctl64) where the sequencer's notes land in the rendered audio, against where the sequencer puts them when it
* runs a sample at a time (seq_tick(t, 1): micro_test's reference): a MICRO-shifted pattern (steps off the grid by
* 64ths, as a live recording leaves them) on an ANALOG track with an instant attack, rendered through mix_block in
* blocks of CTL samples. Each note's onset (the master's first sample above the floor after a rest) minus the
* sequencer's sample for it. With blocks of 32 (before) a note started at its block's first sample: 0 .. 31 samples
* early or late; now (any CTL) at its own sample: within 2 (the ramp's first sample rounds to 0; the sequencer's
* sample-at-a-time run is itself a sample behind). Also: the same with the
* track MONO (each note cuts the one before: the voice restarted in place at the sample), and a DRUM kit's hits.
* build/host/ctl64_timing_test [OUT.wav] (OUT.wav: the POLY render, for listening / plotting) */
#define UI_TEST_NO_MAIN 1
#include "ui_test.c"
static int bad;
static void ck(const char *what, int ok)
{
printf("ctl64: %-100s %s\n", what, ok ? "ok" : "FAIL");
bad += !ok;
}
#define NN 6u
static const int8_t MIC[NN] = {0, 13, -9, 27, -30, 5}; /* each step's MICRO (64ths) */
static void pattern(uint32_t engine, uint32_t mode)
{
track_t *t = &trk[0];
uint32_t i;
ui_power_on();
song.master_q12 = 4096;
for (i = 1; i < NTRK; i++) trk[i].p[P_MUTE] = 1;
set_engine_of(t, engine);
t->engine = t->eng_req;
apply_preset_to(t, 0);
t->p[P_MUTE] = 0; t->p[P_SLEN] = 2 * NN; t->p[P_SDIV] = 2; t->p[P_SSWING] = 0; song.g[G_SWING] = 0;
t->p[P_VOICE] = (int16_t)mode; t->p[P_SGATE] = 40; t->p[P_ATK] = 0; t->p[P_REL] = 10;
t->p[P_CHOR] = t->p[P_DLY] = t->p[P_REV] = 0; t->p[P_DIST] = 0;
for (i = 0; i < NSTEP; i++) step_clear(&t->step[i]);
for (i = 0; i < NN; i++) { /* a note, then a rest (the onset after silence) */
step_t *s = &t->step[2 * i];
s->time = ST_NOTE; s->vel = 100;
if (engine == ENGI_DRUM) s->hit = (uint8_t)(1u << DV_HATC); /* (the closed hat: its tail is short) */
else { s->n = 1; s->note[0] = (uint8_t)(48 + 3 * i); }
ST_MICRO_SET(s, MIC[i]);
}
#ifdef TIMING_FIT
t->seq_fit = 128u | 8u; /* 12 steps inside 8 reference steps */
#endif
song.g[G_BPM] = 128;
for (i = 0; i < 200u; i++) { int32_t o[2 * CTL]; mix_block(o, CTL); } /* (the sound load settled) */
}
/* the sequencer alone, a sample at a time: the sample each of the NN notes starts on */
static void reference(uint32_t engine, uint32_t mode, int32_t *at)
{
track_t *t = &trk[0];
uint32_t now, k, v0, n = 0;
pattern(engine, mode);
seq_start();
for (k = 0; k < NN; k++) at[k] = -1;
for (now = 0; now < 2u * NN * 5168u + 2000u && n < NN; now++) { /* (a step at 128 BPM 1/16: 5168 samples) */
v0 = vage;
seq_tick(t, 1);
if (vage != v0) at[n++] = (int32_t)now;
}
seq_stop();
}
/* rendered in blocks: each note's onset in the track's own render (events_block, then track_render as mix_part does:
* the dry part, exactly 0 while nothing sounds; the master's leveler and DC blocker would never settle to 0) */
static void rendered(uint32_t engine, uint32_t mode, int32_t *on, const char *wav)
{
track_t *t = &trk[0];
static int32_t o[CTL];
uint32_t b, i, n = 0, zeros = 1000;
FILE *f = wav ? fopen(wav, "wb") : 0;
uint32_t frames = 2u * NN * 5168u + 4000u;
pattern(engine, mode);
transport_req = 1; ui_input();
for (i = 0; i < NN; i++) on[i] = -1;
if (f) wav_hdr(f, (frames / CTL) * CTL);
for (b = 0; b < frames / CTL; b++) {
events_block(CTL);
track_render(t, o, CTL);
for (i = 0; i < CTL; i++) {
int32_t x = o[i], a = x < 0 ? -x : x;
if (f) wav_put(f, x >> 2, x >> 2);
if (a >= 4) { /* (a kit's tail leaves a few LSBs) */
if (zeros >= 200u && n < NN) on[n++] = (int32_t)(b * CTL + i); /* (the first sample after a silence) */
zeros = 0;
} else
zeros++;
}
}
if (f) fclose(f);
transport_req = 2; ui_input();
}
static int32_t worst(const int32_t *ref, const int32_t *on, int32_t *mn, int32_t *mx)
{
uint32_t k;
int32_t w = 0;
*mn = 1 << 30; *mx = -(1 << 30);
for (k = 0; k < NN; k++) {
int32_t d = on[k] - ref[k], a = d < 0 ? -d : d;
if (ref[k] < 0 || on[k] < 0) return 1 << 20;
if (a > w) w = a;
if (d < *mn) *mn = d;
if (d > *mx) *mx = d;
}
return w;
}
int main(int argc, char **argv)
{
static const struct { uint32_t engine, mode; const char *name; int32_t tol; } C[3] = {
{ENGI_ANALOG, V_POLY, "ANALOG POLY", 2}, {ENGI_ANALOG, V_MONO, "ANALOG MONO (each note cuts the last)", 3}, /* (V10.8.40: 3: with the 1/16 of 5508 samples a note cut at another point of the one before: its first sample over the floor one later; POLY's the same event, 2) */
{ENGI_DRUM, V_POLY, "DRUM kit hits", 2}};
int32_t ref[NN], on[NN], mn, mx, w;
uint32_t c, k;
char what[200];
for (c = 0; c < 3u; c++) {
reference(C[c].engine, C[c].mode, ref);
rendered(C[c].engine, C[c].mode, on, c == 0u && argc > 1 ? argv[1] : 0);
for (k = 0; k < NN; k++) /* the grid of a run in blocks: the transport's position is 0 at
* the end of the block PLAY lands in (seq_start), so it sits
* CTL - 1 samples after the sample-at-a-time run's */
if (ref[k] >= 0 && k) ref[k] += (int32_t)CTL - 1; /* (not the first: PLAY's step starts the block) */
w = worst(ref, on, &mn, &mx);
printf("ctl64: %s: sequencer at", C[c].name);
for (k = 0; k < NN; k++) printf(" %d", ref[k]);
printf("; rendered at");
for (k = 0; k < NN; k++) printf(" %d", on[k]);
printf(" (onset - sequencer: %d .. %d samples)\n", mn, mx);
snprintf(what, sizeof what, "%s: every note's onset within %d sample(s) of the sequencer's (worst %d; blocks of %u)",
C[c].name, C[c].tol, w, (unsigned)CTL);
ck(what, w <= C[c].tol);
}
printf(bad ? "ctl64 timing: %d FAILED\n" : "ctl64 timing: all passed\n", bad);
return bad != 0;
}