Files
fmini-1/firmware/src/click.c
T
ClaudeandКирилл Сапфиров c8d571bd22 Felucca 1.6.1
MENU > LARGE keeps one size per knob (#251): each knob shows all its
values in the size its longest value fits, so a value no longer jumps
between sizes while it is turned. No new fonts.

Updates check the package's size on the device: the update entry and the
update loader take only a package whose firmware area is exactly the
FM-1's. Another device's package is refused before anything is erased
(the update screen shows FAILED -15; the loader answers -14).

Second core (beta, PR #247): MENU > SYSTEM > 2ND CORE, OFF by default.
ON (BETA) applies from the next power-on: the second core renders some
of the four parts of each block, bit for bit the one-core sound. A unit
whose second core misreads goes on with one core and sets it back to
OFF. FELUCCA_CPU2=0 builds none of it. Start, mailbox and guards from
X0X (with Melodee's dual-core findings), the split and hardening after
Jangada.

Felucca-Engines: ANALOG, LOFI, TRIO, WHEEL and NOISE, the engine
interface, the shared DSP helpers and their table generator move to
https://github.com/hugelton/Felucca-Engines under the MIT licence,
included as the git submodule firmware/engines. The firmware stays
GPL-3.0-only; clone with --recurse-submodules. The sound is unchanged
(the same golden renders); the engines' own tests run with Felucca's.

Co-authored-by: Кирилл Сапфиров <gospodindilly@gmail.com>
2026-10-17 19:04:28 -03:00

58 lines
3.4 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* The metronome's click (1.1, Ideas Discussion #131): MENU > AUDIO CLICK (OFF / REC / ON), CLICK LEVEL (LOW / MID / HIGH)
* and COUNT-IN (OFF / 1 BAR / 2 BARS). seq.c decides when a beat clicks (click_beat: the song tempo's quarter notes, 4/4,
* the bar's first beat accented; the count-in's beats whatever CLICK is); this file makes the sound. A short sine blip
* that decays away (no sample): 1760 Hz on the bar's first beat, 1320 Hz on the others, 6 dB quieter; ~6 ms
* decay constant, silent after ~35 ms. It is added after the master (audio.c audio_block, after uac_tap and USB LEVEL):
* never through the FX, the reverb or the limiter, never into a recording (the sequence or USB audio), only into the
* DAC (headphones / line out, the speaker), scaled by MASTER as the rest of what the DAC gets.
* The MENU's three settings are bits of a byte no engine uses (ui.c ui_rec_prefs), copied here every UI frame
* (ui_input.c); 0 in every older setting = OFF / MID / OFF, today's behaviour. */
enum { CLICK_OFF, CLICK_REC, CLICK_ON };
static volatile uint8_t click_mode; /* CLICK_*: when the beats click while the transport runs */
static volatile uint8_t click_lvl = 1; /* 0 LOW 1 MID 2 HIGH */
static volatile uint8_t cin_bars; /* COUNT-IN: 0 off, 1 or 2 bars */
static volatile uint8_t click_req; /* seq.c: 1 a beat, 2 the bar's first; the next block starts it */
/* the magic circle (s, c: a sine and its quadrature) at k = 2 sin(pi f / FS), Q15; env Q15, decaying 1/256 a sample */
#define CLICK_K_DOWN 8195 /* 1760 Hz */
#define CLICK_K_BEAT 6154 /* 1320 Hz */
#define CLICK_AMP 16384 /* the circle's amplitude */
static const uint16_t CLICK_GAIN[3] = {1024, 2048, 4096}; /* LOW MID HIGH, Q12: the downbeat's peak -18 / -12 / -6 dB
* of a full-scale mix (Q15) at MASTER's top */
static struct { int32_t s, c, k, env, g; } clk;
/* add the click to one block of DAC samples (stereo Q15, after the master): out of line, a loop only while it sounds */
static __attribute__((noinline)) void click_render(int32_t *out, uint32_t n)
{
uint32_t i;
int32_t s, c, k, env, g;
if (click_req) {
uint32_t down = click_req == 2u;
clk.s = 0; /* (from 0: no step at the start) */
clk.c = CLICK_AMP;
clk.k = down ? CLICK_K_DOWN : CLICK_K_BEAT;
clk.env = 32767;
clk.g = CLICK_GAIN[click_lvl % 3u] >> (down ? 0 : 1); /* Q12: the downbeat x1, a beat x0.5 (-6 dB) */
click_req = 0;
}
if (clk.env <= 0)
return;
s = clk.s; c = clk.c; k = clk.k; env = clk.env;
g = (int32_t)(((uint32_t)clk.g * song.master_q12) >> 12); /* (MASTER, as the rest of the DAC's signal) */
for (i = 0; i < n; i++) {
int32_t v, l, r;
s += (k * c) >> 15;
c -= (k * s) >> 15;
v = (((s * env) >> 15) * g) >> 12;
env -= (env >> 8) + 1;
if (env < 0)
env = 0;
l = out[2u * i] + v;
r = out[2u * i + 1u] + v;
out[2u * i] = l > 32767 ? 32767 : l < -32767 ? -32767 : l;
out[2u * i + 1u] = r > 32767 ? 32767 : r < -32767 ? -32767 : r;
}
clk.s = s; clk.c = c; clk.env = env;
}