0
mirror of https://github.com/torvalds/GuitarPedal.git synced 2026-08-18 13:13:35 +00:00
Files
Linus Torvalds 0c1b9c3db3 Split Software/ into the four things it actually was
'Software' was the directory everything that was not KiCad ended up in,
which stopped describing anything a while ago - Validation and the web
app are software too.  Worse, it put the shared parts inside the
firmware, where they read as the firmware's own.

They are not.  Effects/ has three consumers built from it: the firmware,
Validation's bench, and the web app's controls, all generated from the
same POT: comments by gen_effects.py.  Audio/ has two - the bench
compiles the same biquads, the same envelope followers and the same
single_sample(), which is the whole reason a measurement on a
workstation says anything about the pedal.  Neither belongs under
Firmware/, so neither is under it any more:

  Effects/    one file per effect
  Audio/      the DSP they are built from, and the audio loop
  Firmware/   the rest of what runs on the pedal, and the submodules
  WebMIDI/    the web app
  scripts/    what the build runs
  Validation/ unchanged
  Hardware/, Documentation/, Images/

CMakeLists.txt and the wrapper Makefile move to the top with them,
because the build now consumes four of those directories and generates
into a fifth.  board.local and build/ come along; MIDI_CC_MAP.md is
generated into Documentation/ rather than into the old Software/ root.

scripts/ goes with the build rather than staying under the firmware,
because six of the ten had nothing to do with the firmware: gen_effects.py
reads Effects/ and writes to three different places, pow2/log2/quarter_sine
generate Audio/'s tables, check-readme.py compares Effects/ against the
README, and server.py serves the web app.  Four of them are invoked from
Validation, which was reaching into Firmware/ for tooling - the same
burying this commit is undoing.  The four that really are about the
firmware are ELF checks the top-level build drives anyway, and a second
scripts directory would only be a second place to look.

C includes say "Audio/foo.h" and the generated map says
"Effects/bar.h", with the repository root on the include path for both
the firmware and the bench.  Spelling the directory out rather than
relying on a bare name is what keeps Audio/cycles.h shimmable: a quoted
include searches the including file's own directory first.

The submodules are renamed as well as moved.  git mv updates their paths
but leaves the section names, and 'Software/pico-sdk' surviving in
.gitmodules would be the word this commit removes, still load-bearing.
That meant the nested modules under pico-sdk too - six .git files
pointing into .git/modules/Software - which is why 'git submodule update
--init --recursive' is worth running once after pulling this.

Verified rather than assumed: a clean configure and build, make check
(failing only on the missing-eeprom case it already failed on),
check-effects, all four analysis pages reproducing every series and
drawing every chart, and a flash to the board that still measures a
routed reverb where it did before.

One latent bug fell out of it.  bench/coeff declared only quarter_sine.h
of the three generated math tables, and Audio/util.h includes pow2.h and
log2.h as well - so building that target with an empty gen/ could never
have worked.  'make bench' builds bench/bench first, which generates all
three, so it stayed hidden until this rebuilt everything from nothing.

Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2026-08-11 13:48:26 -07:00

119 lines
4.4 KiB
C

// NAME: Preamp [PREAMP]
// PRIORITY: 20
// POT: "Level" LINEAR(-20.0 20.0) = 0.0 dB
// POT: "Sat" LINEAR(0.5 4.0) = 1.2 x
// POT: "Voice" ENUM(Tube JFET) = Tube
// Two-stage cascaded triode (Tube) or famous single-stage JFET preamp model.
// SATURATION drives both waveshapers; VOICE selects topology; LEVEL trims output.
// One-pole LPF: y[n] = x[n] + a*(y[n-1] - x[n]), 6 dB/oct.
// Pole: a = pow2(-2*pi*fc / (fs*ln2)); 9.06472 = 2*pi/ln2.
struct preamp_onepole {
float a, z;
};
static inline void preamp_onepole_set(struct preamp_onepole *f, float fc)
{
f->a = pow2(-9.06472028f * fc / SAMPLES_PER_SEC);
}
static inline float preamp_onepole_step(struct preamp_onepole *f, float x)
{
f->z = x + f->a * (f->z - x);
return f->z;
}
// Tube model: two-stage 12AX7 triode, class-A biased, interstage DC block.
// Fixed operating-point asymmetry for 60's vintage character (~0.15 grid bias).
#define PREAMP_TUBE_ASYMM 0.15f
#define PREAMP_TUBE_DC_R 0.995f // ~38 Hz HPF pole at 48 kHz
// tanhf(0.15f) and tanhf(0.045f) precomputed; saves two tanhf calls per sample.
#define PREAMP_TUBE_TANHB 0.14888f
#define PREAMP_TUBE_TANHA2 0.04496f
// Small-signal gain at default drive (1.2): stage1 * stage2 ≈ 0.885; norm targets ~1.05.
#define PREAMP_TUBE_NORM 1.186f
// JFET model: from schematic of a famous common-emitter 2N5457-like stage.
// Emitter-bypass shelf, asymmetric Class-A waveshaper, Miller-cap rolloff.
#define PREAMP_JFET_BIAS 0.12f // class-A operating offset
#define PREAMP_JFET_MAKEUP 1.6f // ~+4 dB output boost
#define PREAMP_JFET_SHELF_D 0.50f // shelf depth: ~6 dB LF cut below corner
#define PREAMP_JFET_SHELF 120.0f // Hz, emitter-bypass corner
#define PREAMP_JFET_MILLER 9000.0f // Hz, C_cb * R_c HF rolloff
#define PREAMP_JFET_DC_R 0.997f // ~71 Hz HPF pole at 48 kHz
// tanhf(0.12f) precomputed.
#define PREAMP_JFET_TANHB 0.11943f
// Small-signal gain at default drive (1.2): waveshaper * MAKEUP ≈ 1.89; norm targets ~1.05.
#define PREAMP_JFET_NORM 0.555f
static struct {
float level, drive;
int voice;
struct { float dc_x, dc_y; } tube;
struct {
struct preamp_onepole shelf, miller;
float dc_x, dc_y;
} jfet;
} preamp = {
.level = 1.0f,
.drive = 1.2f,
};
static inline void preamp_init(unsigned char pot[10])
{
preamp.level = db_to_level(preamp_level_pot(pot));
preamp.drive = preamp_sat_pot(pot);
preamp.voice = pot[PREAMP_VOICE];
// Shelf and miller corners are fixed; computed here because pow2() needs runtime tables.
preamp_onepole_set(&preamp.jfet.shelf, PREAMP_JFET_SHELF);
preamp_onepole_set(&preamp.jfet.miller, PREAMP_JFET_MILLER);
}
static inline float preamp_tube_step(float x, float drive)
{
// Stage 1: asymmetric bias pushes the operating point off-centre on
// the tanh curve, generating even-order harmonics (2nd-harmonic mechanism).
float s1 = tanhf(x * (drive * 0.7f) + PREAMP_TUBE_ASYMM) - PREAMP_TUBE_TANHB;
// Interstage DC block (~38 Hz HPF) strips stage-1 DC before stage 2
// so the bias offsets don't accumulate across the cascade.
float dc = s1 - preamp.tube.dc_x + PREAMP_TUBE_DC_R * preamp.tube.dc_y;
preamp.tube.dc_x = s1;
preamp.tube.dc_y = dc;
// Stage 2: re-saturates the cleaned signal at reduced asymmetry.
return tanhf(dc * (drive * 0.9f) + PREAMP_TUBE_ASYMM * 0.3f) - PREAMP_TUBE_TANHA2;
}
static inline float preamp_jfet_step(float x, float drive)
{
// Emitter-bypass low-shelf: subtracts a fraction of the LPF output to
// attenuate below 120 Hz by ~6 dB, matching the bypass capacitor rolloff.
float lf = preamp_onepole_step(&preamp.jfet.shelf, x);
float in = x - PREAMP_JFET_SHELF_D * lf;
// Asymmetric Class-A waveshaper; BIAS offsets the operating point so
// positive and negative swings saturate at different rates.
// Subtracting tanhf(BIAS) removes the static DC component.
float shaped = tanhf(in * drive + PREAMP_JFET_BIAS) - PREAMP_JFET_TANHB;
// DC block for the drive-dependent residual offset.
float dc = shaped - preamp.jfet.dc_x + PREAMP_JFET_DC_R * preamp.jfet.dc_y;
preamp.jfet.dc_x = shaped;
preamp.jfet.dc_y = dc;
// Miller-cap HF rolloff then characteristic output boost.
return preamp_onepole_step(&preamp.jfet.miller, dc) * PREAMP_JFET_MAKEUP;
}
static inline float preamp_step(float in)
{
float out = (preamp.voice == 0)
? preamp_tube_step(in, preamp.drive) * PREAMP_TUBE_NORM
: preamp_jfet_step(in, preamp.drive) * PREAMP_JFET_NORM;
return out * preamp.level;
}