0
mirror of https://github.com/torvalds/GuitarPedal.git synced 2026-08-18 13:13:35 +00:00
Files
torvalds-GuitarPedal/Effects/tremolo.h
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

92 lines
3.6 KiB
C

// NAME: Tremolo [TREM]
// PRIORITY: 110
// MIX: STEREO // NORM pans, so it needs somewhere to pan *to*
// POT: "Rate" FREQUENCY(0.1 10.0) = 2.5 Hz
// POT: "Depth" LINEAR(0.0 1.0) = 0.5
// POT: "Mode" ENUM(NORM HARM) = NORM
// Tremolo: sinusoidal amplitude modulation, NORM and HARM modes.
//
// NORM: a rotation of the stereo signal, done as a complex multiply.
// Take the input as l + i*r and multiply by
//
// m = (1 - d) + d * (cos φ + i * sin φ)
//
// where φ is the LFO phase running freely round the circle and d is
// depth. At d = 0 the multiplier is 1 and nothing happens at all. At
// d = 1 it is a pure unit rotation, so |out| == |in| exactly - equal
// power by construction, for a mono input and a stereo one alike,
// with no mix law needed to arrange it. In between, m traces a circle
// offset from 1, so the magnitude varies as well as the angle and you
// get amplitude modulation and panning together, which is what a
// stereo tremolo sounds like.
//
// A mono input comes out in quadrature - l*cos on the left, l*sin on
// the right - both moving at the LFO rate, ninety degrees apart. Fold
// that back to mono and you get sqrt(2)*l*sin(φ + π/4), i.e. it
// degrades to a full-depth ordinary tremolo rather than cancelling to
// nothing, which is what an opposite-signs-per-channel version would
// have done.
//
// At full depth the rotation passes through φ = π, where the left
// channel is -l. Going all the way round means inverting on the way,
// which is inherent to rotating rather than a fault in it.
//
// HARM: two independent one-pole filters modelled on the Fender 6G4 harmonic
// vibrato circuit. Low branch is a 1-pole LP (R=220k, C=5nF, fc≈144.7 Hz);
// high branch is a 1-pole HP (R=1M, C=250pF, fc≈636.6 Hz, implemented as
// in - LP at that same corner). These are not complementary crossover halves,
// so lo+hi has a broad mid-frequency dip (~8 dB at 300 Hz). To avoid that
// level drop the LFO modulates the difference (lo - hi) added to in, not the
// branches directly: out = in + k*lfo*(lo - hi). Unity gain at zero depth,
// same anti-phase modulation character at non-zero depth.
static struct {
struct lfo_state lfo;
float depth; // NORM: how far the multiplier moves off 1
float k; // HARM: bipolar AM coefficient
int harmonic;
float lp1_a, lp1_z; // one-pole LP at 144.7 Hz (low branch)
float lp2_a, lp2_z; // one-pole LP at 636.6 Hz; HP = in - lp2 (high branch)
} trem;
static void trem_init(unsigned char pot[10])
{
set_lfo_freq(&trem.lfo, trem_rate_pot(pot));
float d = trem_depth_pot(pot);
trem.depth = d;
trem.k = d / (2.0f - d);
trem.harmonic = (pot[TREM_MODE] == 1);
trem.lp1_a = pow2(-9.06472028f * 144.7f / SAMPLES_PER_SEC);
trem.lp2_a = pow2(-9.06472028f * 636.6f / SAMPLES_PER_SEC);
}
static sample_t trem_step(sample_t in)
{
sample_t out;
if (trem.harmonic) {
// Still mono: the 6G4 circuit this models has one signal
// path, and the two branches are filters, not channels.
float lfo = lfo_step(&trem.lfo, lfo_sinewave);
float x = in.left;
float lo = (trem.lp1_z = x + trem.lp1_a * (trem.lp1_z - x));
float hi = x - (trem.lp2_z = x + trem.lp2_a * (trem.lp2_z - x));
out.left = out.right = x + trem.k * lfo * (lo - hi);
return out;
}
// The sawtooth is the raw phase, which is what we want here -
// fastsincos() takes its phase in cycles, so the two line up
// without any scaling in between.
struct sincos w = fastsincos(lfo_step(&trem.lfo, lfo_sawtooth));
float d = trem.depth;
float re = 1.0f + d * (w.cos - 1.0f);
float im = d * w.sin;
out.left = in.left * re - in.right * im;
out.right = in.left * im + in.right * re;
return out;
}