Band-limit amp modulation so a Square/Saw LFO no longer clicks (v0.23.1)
The amplifier is the only modulation destination with nothing downstream to
absorb a discontinuity. Pitch feeds a phase accumulator and cutoff feeds filter
coefficients, both of which integrate a step away; the amp multiplies the
output sample directly:
a = ampE * velocity * (1 + mAmp)
y = filtered * a * OUT_GAIN
A Saw, Square or S&H LFO steps instantaneously, so wiring one to Amp put a
full-scale jump straight on the output — a broadband click at every edge, and
at a musical LFO rate (8 Hz is a 1/8 note at 120 BPM) that is a stream of
clicks several times a second: the reported crackle. Sine and Triangle were
unaffected because they are continuous, which is why only the stepping
waveforms were reported.
mAmp now runs through a 2 ms one-pole, so each edge becomes a short ramp. At
8 Hz a half cycle is 62 ms, so 2 ms is 3% of it and a chop still reads as a
chop. The envelope itself is deliberately NOT smoothed — it is already
continuous, and rounding it there would soften every attack. A note starts at
its modulated level rather than ramping up to it, so triggering on a square
LFO's quiet half does not blip before ducking.
Measured by the new SynthAmpModTest, worst single-sample output jump during
sustain (C3 triangle, filter wide open, LFO -> Amp at full depth):
waveform before after
Square 0.804 0.018
S&H 0.705 0.017
Saw 0.590 0.011
(no mod) 0.003 0.003
Not addressed, and worth a separate look: the toolbox LFO *device* writes its
target parameter once per ~4 ms block, which steps every target for every
waveform. That is a different mechanism from the synth's own mod matrix and was
not what was reported here.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
208de84012
commit
3453f9f003
@@ -4,6 +4,7 @@
|
||||
package cc.tiredsysadmin.android.sizzle.audio
|
||||
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.exp
|
||||
import kotlin.math.sin
|
||||
import kotlin.math.tan
|
||||
|
||||
@@ -55,6 +56,18 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
private var shValue = 0f
|
||||
private var rng = 0x2545F491
|
||||
|
||||
// Smoothed amplifier modulation. The amp is the one destination with nothing
|
||||
// after it to absorb a discontinuity — pitch feeds a phase accumulator and
|
||||
// cutoff feeds filter coefficients, both of which integrate a step away, but
|
||||
// the amp multiplies the output sample directly. A Saw, Square or S&H
|
||||
// modulator steps instantaneously, so routing one to Amp put a full-scale jump
|
||||
// straight on the output: a click at every edge, which at a musical LFO rate
|
||||
// is heard as crackling. A one-pole spreads each edge over AMP_MOD_SMOOTH_SEC.
|
||||
private var ampMod = 0f
|
||||
private var ampModPrimed = false
|
||||
private val ampModCoeff =
|
||||
(1.0 - exp(-1.0 / (sampleRate * AMP_MOD_SMOOTH_SEC))).toFloat()
|
||||
|
||||
private val ampEnv = Adsr()
|
||||
private val filtEnv = Adsr()
|
||||
|
||||
@@ -77,6 +90,9 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
lfoInc = patch.lfoHz.toDouble() / sampleRate
|
||||
lfoPhase = 0.0 // retrigger the LFO per note for predictable modulation
|
||||
ctrl = 0 // force a cutoff recompute on the first sample
|
||||
// Start the note AT its modulated level rather than ramping up to it, so a
|
||||
// note triggered on a square LFO's quiet half does not blip before ducking.
|
||||
ampModPrimed = false
|
||||
// Precompute oscillator tuning so the per-sample loop skips Math.pow unless
|
||||
// pitch is actually being modulated (the common case).
|
||||
computeTuning()
|
||||
@@ -187,7 +203,11 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
val filtered = svf(mix)
|
||||
|
||||
// ---- amplifier ----
|
||||
var a = ampE * velocity * (1f + mAmp)
|
||||
// Smooth the modulation, not the envelope: the ADSR is already continuous,
|
||||
// and rounding it here would soften every attack.
|
||||
if (ampModPrimed) ampMod += (mAmp - ampMod) * ampModCoeff
|
||||
else { ampMod = mAmp; ampModPrimed = true }
|
||||
var a = ampE * velocity * (1f + ampMod)
|
||||
if (a < 0f) a = 0f
|
||||
val y = filtered * a * OUT_GAIN
|
||||
return if (y.isFinite()) y else 0f
|
||||
@@ -296,6 +316,10 @@ class SynthVoice(private val sampleRate: Int) {
|
||||
companion object {
|
||||
private const val OUT_GAIN = 0.5f
|
||||
private const val CTRL_INTERVAL = 16 // samples between cutoff recomputes
|
||||
/** One-pole time constant for amp modulation. Long enough to band-limit a
|
||||
* square edge, short enough that a chop still reads as a chop: at 8 Hz a
|
||||
* half-cycle is 62 ms, so 2 ms is 3% of it. */
|
||||
private const val AMP_MOD_SMOOTH_SEC = 0.002
|
||||
/** Equal-tempered MIDI note -> frequency in Hz (A4 = 69 = 440 Hz). */
|
||||
fun midiToHz(midi: Int): Double = 440.0 * Math.pow(2.0, (midi - 69) / 12.0)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user