Files
JIANT/tests/audio_test.c
T
kurogedelic db70550344 Felucca 1.0.2
MENU, now a scrolling list: STYLE (FLAT / LINE, #57), ANIM (#46), KNOB ACCEL
(#52), FX LATCH (#40), BPM LOCK (#58), USB LEVEL (MASTER / FIXED), LEDS
OFF / DIM LO / DIM HI / INV; SPEAKER is SPEAKER EQ (FLAT / LOWCUT /
BASS+, #42). Palettes: GREY (the former MONO), a black-and-white MONO
and NIGHT (#50, #57). Tracks numbered 1-4 on cushions (#50); the motion icon
on the cards MOTION drives (#63). Divisions listed by length (#48).
Fixed: knob turns as a layer is let go no longer edit the page under it
(#39); FX DEPTH turns the right way (#40); BASS+ no longer cancels the
mids (#42); live sync keeps pending pushes on WATCH and no longer echoes
the editor's own loads as RELOAD (#64, #65). SAMPLE's PERC kit is
retired: its sounds load as the DRUM engine's kit.
2026-10-06 13:15:44 +09:00

201 lines
9.1 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* The real audio ISR with a simulated DMA and clock: overload must free work
* within one block without cutting another part's lead or bass. */
#define main hostsim_main
#include "hostsim.c"
#undef main
#define FM1_AUDIO_HALF 0x80u
#define FM1_TICKS_PER_US 1u
static uint32_t host_clock, host_us, host_half_reads, host_dma_advanced, host_acks, host_nest;
static uint8_t host_pending;
static volatile uint32_t t5_nested_ticks; /* audio.c's: TIMER5 nested in the render (main.c) */
static uint32_t fm1_ticks(void) { t5_nested_ticks = host_nest; host_clock += host_us; return host_clock; }
static uint8_t fm1_audio_pending(void) { return host_pending; }
static void fm1_audio_ack_aux(uint8_t p) { (void)p; }
static uint32_t fm1_audio_free_half(void) { return host_dma_advanced && host_half_reads++ > 0u; }
static void fm1_audio_ack_half(void) { host_acks++; }
static void fm1_audio_init(int32_t *b, uint32_t n, void (*isr)(void), uint32_t p)
{ (void)b; (void)n; (void)isr; (void)p; }
void isr_alnk0(void) {}
#define FELUCCA_UAC 1 /* audio.c taps USB audio: here into host_tap */
static int32_t host_tap[2 * 64];
static uint32_t host_tap_n;
static void uac_tap(const int32_t *out, uint32_t n) { memcpy(host_tap, out, 8u * n); host_tap_n = n; }
static void uac_render_start(void) {}
#include "../firmware/src/audio.c"
static int bad;
static void check(const char *what, int ok)
{
printf("audio: %-74s %s\n", what, ok ? "ok" : "FAIL");
bad += !ok;
}
static void fresh(void)
{
uint32_t p;
memset(trk, 0, sizeof trk);
memset(&song, 0, sizeof song);
memset(&chain, 0, sizeof chain);
host_tracks_init();
for (p = 0; p < NPART; p++) host_preset(&trk[p], 0, 0);
memset(&felucca_dbg, 0, sizeof felucca_dbg);
audio_cpu_rem = audio_halves = audio_max_us = 0;
shed_req = 0; shed_count = 0; shed_over = 0;
host_clock = host_us = host_half_reads = host_dma_advanced = host_acks = host_nest = 0;
host_pending = FM1_AUDIO_HALF;
transport_req = panic_req = 0;
}
static voice_t *held(track_t *t, uint32_t note)
{
uint32_t i;
for (i = 0; i < NVOICE; i++)
if (t->v[i].active && t->v[i].note == note) return &t->v[i];
return 0;
}
static void overload(void)
{
voice_t *lead, *bass, *extra;
uint32_t mode, i;
int32_t out[2 * CTL];
fresh();
trk[0].p[P_VOICE] = V_MONO;
trk_note_on(&trk[0], 40, 100);
trk[1].p[P_VOICE] = V_POLY;
trk_note_on(&trk[1], 48, 100);
trk_note_on(&trk[1], 60, 100);
lead = held(&trk[0], 40); bass = held(&trk[1], 48); extra = held(&trk[1], 60);
shed_voice();
check("overload keeps each part's MONO lead and lowest POLY note",
lead && bass && extra && lead->gate && bass->gate && extra->stage == 4u && shed_count == 1u);
mix_block(out, CTL);
check("overload frees its victim within one control block", extra && !extra->active && voices_busy() == 2u);
shed_voice();
check("a lead alone on each part stays protected", lead->gate && bass->gate && shed_count == 1u);
fresh();
host_preset(&trk[0], ENGI_DRUM, 0);
trk_note_on(&trk[0], 36, 100); trk_note_on(&trk[0], 38, 100);
lead = held(&trk[0], 36); extra = held(&trk[0], 38);
shed_voice(); mix_block(out, CTL);
check("overload frees a held one-shot drum without waiting for its decay",
lead && extra && lead->active && !extra->active && voices_busy() == 1u);
fresh();
trk[0].p[P_VOICE] = V_UNISON;
trk_note_on(&trk[0], 55, 100);
shed_voice(); mix_block(out, CTL);
check("UNISON loses an extra voice and retains its lead", trk[0].v[0].active && trk[0].v[0].gate && voices_busy() == NVOICE - 1u);
for (mode = V_MONO; mode <= V_LEGATO; mode++) {
fresh(); trk[0].p[P_VOICE] = V_POLY;
for (i = 0; i < NVOICE; i++) trk_note_on(&trk[0], 48u + 2u * i, 100);
trk[0].p[P_VOICE] = (int16_t)mode;
shed_voice(); mix_block(out, CTL);
check("overload can reclaim stale POLY voices after a MONO / LEGATO change",
shed_count == 1u && trk[0].v[0].gate && voices_busy() == NVOICE - 1u);
}
fresh();
trk[0].p[P_VOICE] = V_POLY;
trk_note_on(&trk[0], 60, 100); trk_note_on(&trk[0], 64, 100);
lead = held(&trk[0], 60); extra = held(&trk[0], 64);
trk_note_off(&trk[0], 60); trk_note_off(&trk[0], 64);
lead->env = 10000; extra->env = 10;
shed_voice();
check("released voices shed the quieter tail first", extra->stage == 4u && lead->stage != 4u);
}
static void dma(void)
{
uint32_t i, expected;
fresh();
host_pending = 0;
fm1_alnk0_irq();
check("an auxiliary interrupt does not render a DMA half", !host_acks && !audio_halves && !felucca_dbg.in_audio);
host_pending = FM1_AUDIO_HALF;
host_us = 5000;
fm1_alnk0_irq();
check("one slow half records elapsed load but does not shed yet", host_acks == 1u && !shed_req && felucca_dbg.last_us == 5000u && !felucca_dbg.late);
fm1_alnk0_irq();
check("a second slow half in a row requests shedding", host_acks == 2u && shed_req);
shed_req = 0;
host_half_reads = 0; host_dma_advanced = 1; host_us = 100;
fm1_alnk0_irq();
check("DMA advancing during a render is counted once", host_acks == 3u && felucca_dbg.late == 1u && !felucca_dbg.in_audio && !shed_req);
fresh();
expected = HALF_FRAMES * 1000000u / FS; /* the half's deadline in us */
host_us = expected * 90u / 100u; host_nest = expected * 30u / 100u; /* the render 60 %, TIMER5 nested 30 % */
fm1_alnk0_irq(); fm1_alnk0_irq();
check("TIMER5 nested in the render counts toward the deadline", shed_req && felucca_dbg.last_us == host_us - host_nest);
fresh();
host_us = 5000;
fm1_alnk0_irq(); host_us = 100; fm1_alnk0_irq(); host_us = 5000; fm1_alnk0_irq();
check("slow halves that are not consecutive do not shed", !shed_req);
fresh();
host_us = 580;
for (i = 0; i < 512u; i++) fm1_alnk0_irq();
expected = host_us * 256u / (HALF_FRAMES * 1000000u / FS);
check("steady CPU load converges without integer smoothing bias", song.cpu_q8 >= expected - 1u && song.cpu_q8 <= expected);
}
/* MENU > USB LEVEL (fx.c fx_usb_fixed, audio.c audio_block): MASTER (default) as before, USB = the DAC's signal and
* follows the knob; FIXED: USB at the full level whatever MASTER is (MASTER 0: USB still plays, the DAC is silent),
* the DAC exactly USB scaled by MASTER */
static double block_rms(const int32_t *b, uint32_t n)
{
double s = 0;
uint32_t i;
for (i = 0; i < 2u * n; i++) s += (double)b[i] * b[i];
return sqrt(s / (2.0 * n));
}
static double usb_run(uint32_t fixed, uint32_t master, int dac_is_tap_scaled_ok[1], double *dac_rms)
{
int32_t out[2 * 32];
uint32_t b, i;
double usb = 0, dac = 0;
fresh();
lim_env = LIM_T; dc_l = dc_r = dce_l = dce_r = 0; /* (the master's state as at power-on: runs compare) */
trk[0].p[P_VOICE] = V_POLY;
trk[0].p[P_DIST] = trk[0].p[P_CHOR] = trk[0].p[P_DLY] = trk[0].p[P_REV] = 0; /* (no tails between runs) */
trk_note_on(&trk[0], 48, 110); trk_note_on(&trk[0], 55, 110); trk_note_on(&trk[0], 64, 110);
fx_usb_fixed = (uint8_t)fixed;
song.master_q12 = master;
dac_is_tap_scaled_ok[0] = 1;
for (b = 0; b < 400u; b++) { /* ~0.3 s */
audio_block(out, 32);
for (i = 0; i < 64u; i++) {
int32_t want = fixed ? ((host_tap[i] * (int32_t)master) >> 12) << OUT_SHIFT : host_tap[i] << OUT_SHIFT;
if (out[i] != want) dac_is_tap_scaled_ok[0] = 0;
}
if (b >= 100u) { usb += block_rms(host_tap, 32); dac += block_rms(out, 32) / (1 << OUT_SHIFT); }
}
fx_usb_fixed = 0;
*dac_rms = dac / 300.0;
return usb / 300.0;
}
static void usb_level(void)
{
int ok_full, ok_quiet, ok_off, ok_fixed, ok_fixed0;
double d_full, d_quiet, d_off, d_fixed, d_fixed0;
double u_full = usb_run(0, 4096, &ok_full, &d_full), u_quiet = usb_run(0, 1024, &ok_quiet, &d_quiet);
double u_off = usb_run(0, 0, &ok_off, &d_off), u_fixed = usb_run(1, 1024, &ok_fixed, &d_fixed);
double u_fixed0 = usb_run(1, 0, &ok_fixed0, &d_fixed0);
printf("audio: USB / DAC rms: MASTER full %.0f / %.0f, 1/4 %.0f / %.0f, 0 %.0f / %.0f; FIXED 1/4 %.0f / %.0f, 0 %.0f / %.0f\n",
u_full, d_full, u_quiet, d_quiet, u_off, d_off, u_fixed, d_fixed, u_fixed0, d_fixed0);
check("USB LEVEL MASTER (default): USB is the DAC's signal and follows the knob (0: silent)",
ok_full && ok_quiet && ok_off && u_full > 1000.0 && u_quiet < u_full * 0.5 && u_off == 0.0);
check("USB LEVEL FIXED: USB at the full level whatever MASTER is (within 0.1 dB), MASTER 0 too",
fabs(20.0 * log10(u_fixed / u_full)) < 0.1 && fabs(20.0 * log10(u_fixed0 / u_full)) < 0.1);
check("USB LEVEL FIXED: the DAC gets exactly USB scaled by MASTER (MASTER 0: silent, 1/4: -12 dB)",
ok_fixed && ok_fixed0 && d_fixed0 == 0.0 && fabs(20.0 * log10(d_fixed / u_fixed) + 12.04) < 0.2);
}
int main(void)
{
setvbuf(stdout, NULL, _IONBF, 0);
usb_level();
overload(); dma();
printf(bad ? "audio: %d FAILED\n" : "audio: all passed\n", bad);
return bad != 0;
}