mirror of
https://github.com/ruppert/fmini-1.git
synced 2026-12-06 12:54:23 +01:00
The audio ISR now plays the playback ring (usb_play_mix, audio.c): - After the USB input and USB LEVEL FIXED, before the click, the frames are added to the DAC's signal at MASTER's level (song.master_q12) and saturated at +-32767. They are not added to the USB input (uac_tap): the host would record its own output back. - Prebuffer: 128 frames before the first sound. The ring's fill is followed against TARGET 256 +- BAND 128: one frame is dropped above the band, one repeated below it (the host's clock is adaptive, so the device follows it; there is no feedback endpoint). A dry ring goes silent for the rest of the block and primes again. - The stream's reset (SET_INTERFACE, configuration, bus reset) sets a flag; the reader moves its own index. The audio ISR can nest TIMER5, so the writer never touches pb_r. - The playback format is 44.1 kHz only: the reader does not resample. Windows converts for applications that play at 48 kHz. tests/uac_out_test.c: the reader's blocks (prime, MASTER level, sums with saturation, drop and repeat, a dry ring, the flag, alt 0) and a 10 s run with the host 0.04 % slow: no dry ring, about 176 repeats. usb_desc_test.c: the playback format is 44.1 kHz only. Not tested on a real FM-1 yet. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QUF6pQ1NkTwmBC8SR8AK94
157 lines
6.5 KiB
C
157 lines
6.5 KiB
C
/* SPDX-License-Identifier: GPL-3.0-only
|
|
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
|
|
/* I2S output (ALNK0 -> external codec) and the
|
|
* audio ISR: per half buffer, blocks of CTL samples: mix_block (fx.c: events ->
|
|
* each synth part -> dist -> level / pan -> sends -> buses -> master) -> USB audio (uac_tap) -> the metronome's
|
|
* click (click.c, the DAC only) -> 24-bit stereo. */
|
|
/* registers: hal/fm1_audio.h */
|
|
#define HALF_WORDS (HALF_FRAMES * 2u)
|
|
#define DAC_TICKS 544u /* TIMER4 ticks per I2S frame (24 MHz / 44,117.6 Hz) */
|
|
#define CPU_AVG (4096u / HALF_FRAMES) /* halves the load meter averages: ~93 ms whatever the half */
|
|
#define OUT_SHIFT 7 /* Q15 -> 24-bit, -6 dBFS ceiling (M0f ran clean at -18 dBFS) */
|
|
|
|
static int32_t abuf[2u * HALF_WORDS] __attribute__((aligned(4)));
|
|
|
|
/* diagnostics, kept across resets and UBOOT entry: read with `fm1t memr` */
|
|
#define DBG_MAGIC 0x44424731u /* "DBG1" */
|
|
struct felucca_dbg {
|
|
uint32_t magic, halves, max_us, nested, in_audio, late, timer_irqs, ui_frames;
|
|
uint32_t last_us, cpu_q8, boots;
|
|
uint32_t stage, page, home; /* where the main loop is (breadcrumbs) */
|
|
uint32_t prev_stage, prev_page, prev_home, prev_rst, prev_frames; /* as found at boot */
|
|
} felucca_dbg __attribute__((section(".noinit")));
|
|
static volatile uint32_t audio_halves, audio_max_us;
|
|
static volatile uint32_t t5_nested_ticks; /* TIMER4 ticks TIMER5 spent nested in this ISR (main.c) */
|
|
static uint32_t audio_cpu_rem; /* keep the fractional IIR step: no low-load bias */
|
|
|
|
static void audio_block(int32_t *out, uint32_t n) /* mix (fx.c), then Q15 -> 24 bit */
|
|
{
|
|
uint32_t i;
|
|
mix_block(out, n);
|
|
#if FELUCCA_UAC
|
|
uac_tap(out, n); /* the USB audio input: the same master output */
|
|
#endif
|
|
if (fx_usb_fixed) /* USB LEVEL FIXED: USB took the full level, the DAC */
|
|
usb_fixed_dac(out, n); /* (speaker, headphones) gets MASTER's (fx.c) */
|
|
if (!scope_mix) /* the HOME scope (SCOPE OUT): the music, before the click */
|
|
for (i = 1; i < n; i += 2u) /* (MIX: fx.c mix_block took it) */
|
|
scope_buf[scope_w++ & (SCOPE_N - 1u)] = (int16_t)out[2u * i];
|
|
#if FELUCCA_UAC_OUT
|
|
usb_play_mix(out, n, (int32_t)song.master_q12); /* the USB playback (EP3), at MASTER; not in the USB input */
|
|
#endif
|
|
click_render(out, n); /* the metronome (click.c): the DAC only, not USB */
|
|
for (i = 0; i < n; i++) {
|
|
out[2u * i] *= 1 << OUT_SHIFT;
|
|
out[2u * i + 1u] *= 1 << OUT_SHIFT;
|
|
}
|
|
}
|
|
|
|
/* Overload: two halves in a row above 93 % (the render plus TIMER5 nested in it), fade one voice over the
|
|
* next block, and one more each half while it lasts. Keep each part's
|
|
* lowest POLY note and MONO / LEGATO / UNISON lead, as the shared budget does. */
|
|
static volatile uint8_t shed_req;
|
|
volatile uint32_t cpu_crit; /* overloads seen (shed_check): the FMINI pop-up "Warning: CPU High" (fmini_app.c) */
|
|
static uint8_t shed_over; /* bit k: the half k halves ago was over 93 % */
|
|
#define SHED_TICKS ((HALF_FRAMES * 1000000u / FS) * 93u / 100u * FM1_TICKS_PER_US) /* 93 % of a half */
|
|
static uint32_t shed_count;
|
|
|
|
/* end of a half: all = its ticks, TIMER5 nested in it included -> the render alone in us. The deadline sees
|
|
* both; shed after 2 overloaded halves in a row (out of line: keeps the ISR's render loops as they were) */
|
|
static __attribute__((noinline)) uint32_t shed_check(uint32_t all)
|
|
{
|
|
shed_over = (uint8_t)(shed_over << 1 | (all > SHED_TICKS));
|
|
if ((shed_over & 3u) == 3u) {
|
|
shed_req = 1;
|
|
cpu_crit++;
|
|
}
|
|
return (all - t5_nested_ticks) / FM1_TICKS_PER_US;
|
|
}
|
|
|
|
static void shed_voice(void)
|
|
{
|
|
uint32_t p, i;
|
|
voice_t *best = 0;
|
|
for (p = 0; p < NPART; p++)
|
|
for (i = 0; i < NVOICE; i++) {
|
|
voice_t *v = &trk[p].v[i];
|
|
if (v->active && !v->gate && v->stage != 4u && (!best || v->env < best->env))
|
|
best = v;
|
|
}
|
|
if (best) {
|
|
voice_kill(best);
|
|
shed_count++;
|
|
return;
|
|
}
|
|
for (p = 0; p < NPART; p++) {
|
|
const track_t *t = &trk[p];
|
|
uint32_t mode = trk_vmode(t), low = mode == V_POLY ? lowest_held(t) : 0u;
|
|
for (i = 0; i < NVOICE; i++) {
|
|
voice_t *v = &trk[p].v[i];
|
|
if (v->active && v->gate && v->stage != 4u &&
|
|
((mode == V_POLY && i != low) || (mode != V_POLY && i > 0u))) {
|
|
if (!best || v->age < best->age)
|
|
best = v;
|
|
}
|
|
}
|
|
}
|
|
if (best) {
|
|
voice_kill(best);
|
|
shed_count++;
|
|
}
|
|
}
|
|
|
|
void fm1_alnk0_irq(void) /* via isr_alnk0 (hal/fm1_isr.S) */
|
|
{
|
|
uint8_t p;
|
|
uint32_t t0;
|
|
felucca_dbg.in_audio = 1; /* first: TIMER5 nests from here on (main.c) */
|
|
p = fm1_audio_pending();
|
|
t0 = fm1_ticks();
|
|
t5_nested_ticks = 0;
|
|
fm1_audio_ack_aux(p);
|
|
if (p & FM1_AUDIO_HALF) {
|
|
uint32_t half = fm1_audio_free_half(), b, us;
|
|
int32_t *o = &abuf[half * HALF_WORDS];
|
|
if (shed_req) {
|
|
shed_req = 0;
|
|
shed_voice();
|
|
}
|
|
#if FELUCCA_UAC
|
|
uac_render_start();
|
|
#endif
|
|
for (b = 0; b < HALF_FRAMES; b += CTL) {
|
|
#ifdef FM1_INPUT_LAT
|
|
kb_out_tick = t0 + (HALF_FRAMES + b) * DAC_TICKS; /* when this block plays (seq.c kb_lat) */
|
|
#endif
|
|
audio_block(o + 2u * b, CTL);
|
|
}
|
|
fm1_audio_ack_half();
|
|
audio_halves++;
|
|
us = shed_check(fm1_ticks() - t0);
|
|
if (us > audio_max_us)
|
|
audio_max_us = us;
|
|
{
|
|
uint32_t load = song.cpu_q8 * (CPU_AVG - 1u) + (us * 256u) / (HALF_FRAMES * 1000000u / FS) + audio_cpu_rem;
|
|
song.cpu_q8 = load / CPU_AVG;
|
|
audio_cpu_rem = load % CPU_AVG;
|
|
}
|
|
if (fm1_audio_free_half() != half)
|
|
felucca_dbg.late++; /* the DMA moved on while we rendered */
|
|
felucca_dbg.halves++;
|
|
felucca_dbg.last_us = us;
|
|
if (us > felucca_dbg.max_us)
|
|
felucca_dbg.max_us = us;
|
|
felucca_dbg.cpu_q8 = song.cpu_q8;
|
|
}
|
|
felucca_dbg.in_audio = 0;
|
|
}
|
|
extern void isr_alnk0(void);
|
|
|
|
static void audio_init(void) /* codec and ALNK0 bring-up (fm1_audio.h) */
|
|
{
|
|
uint32_t i;
|
|
for (i = 0; i < 2u * HALF_WORDS; i++)
|
|
abuf[i] = 0;
|
|
fm1_audio_init(abuf, HALF_WORDS, isr_alnk0, 3);
|
|
}
|