|
| 1 | +/** |
| 2 | + * One knob over several, for the five effects that cannot honestly have one |
| 3 | + * real parameter nominated as the control that matters. |
| 4 | + * |
| 5 | + * A compressor has seven controls and an author wants one. The rest of the rack |
| 6 | + * opens on a single real parameter — a filter's frequency, a delay's mix — but |
| 7 | + * these five have no such parameter: threshold means nothing without ratio, |
| 8 | + * ratio means nothing without make-up gain, and picking any one of them as the |
| 9 | + * face of the module would be a knob that lies about what it sets. |
| 10 | + * |
| 11 | + * So they get a derived one, exactly as the carve already does: `carveProfile` |
| 12 | + * turns a single 0..1 into six numbers, and these do the same for the rest of |
| 13 | + * the rack. `EFFECT_COPY[id].primary` is `"strength"` for precisely these five, |
| 14 | + * which is what says a module wants this treatment. |
| 15 | + * |
| 16 | + * Continuous rather than the three-point tables the design proposed. A table |
| 17 | + * makes gentle/middle/strong three settings an author picks between, and the |
| 18 | + * thing being modelled is not three settings — it is one axis, and the carve's |
| 19 | + * knob has proved that reads. The proposal's figures survive as the anchors: |
| 20 | + * each curve passes through them at 0, 0.5 and 1. |
| 21 | + * |
| 22 | + * Every number below is expressed in the parameter's own registry unit and then |
| 23 | + * clamped by `normalizeAudioFxParams` on the way into the chain, so a profile |
| 24 | + * cannot ship a value the effect would refuse. |
| 25 | + */ |
| 26 | + |
| 27 | +import { normalizeAudioFxParams, type HfAudioFxParamValues } from "./audioFx.js"; |
| 28 | + |
| 29 | +/** 0..1, whatever arrives. NaN reads as the middle rather than as silence. */ |
| 30 | +function clamp01(strength: number): number { |
| 31 | + return Number.isFinite(strength) ? Math.min(1, Math.max(0, strength)) : 0.5; |
| 32 | +} |
| 33 | + |
| 34 | +/** Two decimal places, so a derived value reads as a setting and not as float noise. */ |
| 35 | +function to2(value: number): number { |
| 36 | + return Number(value.toFixed(2)); |
| 37 | +} |
| 38 | + |
| 39 | +export interface HfAudioFxProfile { |
| 40 | + /** What the one knob is called. Never the DSP name of anything it drives. */ |
| 41 | + label: string; |
| 42 | + /** What the two ends sound like — the question an author actually has. */ |
| 43 | + ends: { low: string; high: string }; |
| 44 | + /** The parameters it sets. Everything else keeps the effect's own default. */ |
| 45 | + derives: readonly string[]; |
| 46 | + /** The mechanism values at a given strength. */ |
| 47 | + at(strength: number): HfAudioFxParamValues; |
| 48 | +} |
| 49 | + |
| 50 | +export const HF_AUDIO_FX_PROFILES: Record<string, HfAudioFxProfile> = { |
| 51 | + compressor: { |
| 52 | + label: "Evenness", |
| 53 | + ends: { low: "Barely touched", high: "Very even, quite squashed" }, |
| 54 | + derives: ["threshold", "ratio", "attack", "release", "makeup"], |
| 55 | + at(strength) { |
| 56 | + const s = clamp01(strength); |
| 57 | + return { |
| 58 | + // Lower threshold and higher ratio together: more of the signal is |
| 59 | + // caught, and what is caught is held harder. Moving one without the |
| 60 | + // other is the pair of knobs this exists to stop an author meeting. |
| 61 | + threshold: to2(-12 - s * 18), |
| 62 | + ratio: to2(2 + s * 4), |
| 63 | + // Faster as it gets firmer, because a firm compressor that lets peaks |
| 64 | + // through is doing the audible half of its job and not the useful half. |
| 65 | + attack: to2(25 - s * 20), |
| 66 | + release: to2(300 - s * 210), |
| 67 | + // Compression makes things quieter; this is the level put back, and it |
| 68 | + // has to rise with the amount taken off or the knob reads as a volume |
| 69 | + // control that goes the wrong way. |
| 70 | + // |
| 71 | + // Solved from measurement rather than proposed, and it is not linear: |
| 72 | + // gain reduction accelerates as the threshold drops and the ratio rises |
| 73 | + // together, so a straight line is too loud in the middle and too quiet |
| 74 | + // at the top. The design's 1/3/7 dB left the track +0.9 dB at rest, |
| 75 | + // +1.3 dB at the middle and −2.5 dB at full. Numbers in |
| 76 | + // `~/audio-fx-profiles-ab/README.md`. |
| 77 | + makeup: to2(s * s * 9.5), |
| 78 | + }; |
| 79 | + }, |
| 80 | + }, |
| 81 | + |
| 82 | + gate: { |
| 83 | + label: "Tightness", |
| 84 | + ends: { low: "Only true silence", high: "Cuts quiet words too" }, |
| 85 | + derives: ["threshold", "range", "release"], |
| 86 | + at(strength) { |
| 87 | + const s = clamp01(strength); |
| 88 | + return { |
| 89 | + threshold: to2(-55 + s * 23), |
| 90 | + // How far the gaps are ducked, never to silence: a room that stops dead |
| 91 | + // between sentences sounds broken rather than clean. |
| 92 | + range: to2(-10 - s * 20), |
| 93 | + // Release has to come along, which the design did not have — and it |
| 94 | + // dominates: swept against a fixed threshold and range, the gaps move |
| 95 | + // 0.2 dB at 140 ms and 13.4 dB at 10 ms. Left at the effect's 100 ms |
| 96 | + // default the gate was very nearly inaudible whatever else it was told. |
| 97 | + // |
| 98 | + // Measured on narration, speech level is unmoved (−24.0 dB) at every |
| 99 | + // release down to 10 ms, so the usual reason to stay slow — clipping |
| 100 | + // word endings — does not bite on this material at these thresholds. |
| 101 | + release: to2(120 - s * 105), |
| 102 | + }; |
| 103 | + }, |
| 104 | + }, |
| 105 | + |
| 106 | + saturate: { |
| 107 | + label: "Warmth", |
| 108 | + ends: { low: "Just a sheen", high: "Openly distorted" }, |
| 109 | + derives: ["threshold", "output"], |
| 110 | + at(strength) { |
| 111 | + const s = clamp01(strength); |
| 112 | + return { |
| 113 | + // Drive: the lower the threshold, the more of the signal meets the |
| 114 | + // curve. |
| 115 | + threshold: to2(-3 - s * 15), |
| 116 | + // Up, not down — which reverses the design's figure, on the measurement |
| 117 | + // that motivated taking one. A soft clipper at -18 dB threshold IS a |
| 118 | + // limiter at -18 dB: peak fell to 0.089 from 0.496 and RMS almost |
| 119 | + // halved, so the proposed trim of −3 dB made "warmer" mean "much |
| 120 | + // quieter" and an author would have heard the level, not the warmth. |
| 121 | + // Accelerating for the same reason as the compressor's make-up. |
| 122 | + output: to2(s * s * 2.8), |
| 123 | + }; |
| 124 | + }, |
| 125 | + }, |
| 126 | + |
| 127 | + reverb: { |
| 128 | + label: "Space", |
| 129 | + ends: { low: "A small tight room", high: "A big open hall" }, |
| 130 | + derives: ["size", "wet", "dry"], |
| 131 | + at(strength) { |
| 132 | + const s = clamp01(strength); |
| 133 | + return { |
| 134 | + // Anchored at the design's three figures — 0.25 / 0.55 / 0.90 — which a |
| 135 | + // single linear run cannot hit, because 0.55 is not their midpoint. |
| 136 | + size: to2(s <= 0.5 ? 0.25 + s * 0.6 : 0.55 + (s - 0.5) * 0.7), |
| 137 | + wet: to2(0.15 + s * 0.3), |
| 138 | + // Dry comes down as wet goes up, but not by the same amount: the two |
| 139 | + // legs sum, and matching them exactly makes a big room quieter than a |
| 140 | + // small one instead of further away. |
| 141 | + dry: to2(0.92 - s * 0.2), |
| 142 | + }; |
| 143 | + }, |
| 144 | + }, |
| 145 | + |
| 146 | + bitcrush: { |
| 147 | + label: "Crush", |
| 148 | + ends: { low: "Slightly gritty", high: "Destroyed" }, |
| 149 | + derives: ["bits", "samples", "mix"], |
| 150 | + at(strength) { |
| 151 | + const s = clamp01(strength); |
| 152 | + return { |
| 153 | + // Fewer steps and longer holds. `bits` runs DOWN as the knob runs up, |
| 154 | + // which is why it cannot be the module's face on its own. |
| 155 | + bits: to2(14 - s * 8), |
| 156 | + samples: Math.max(1, Math.round(1 + s * 3)), |
| 157 | + mix: to2(0.25 + s * 0.75), |
| 158 | + }; |
| 159 | + }, |
| 160 | + }, |
| 161 | +}; |
| 162 | + |
| 163 | +export function getAudioFxProfile(type: string): HfAudioFxProfile | undefined { |
| 164 | + return HF_AUDIO_FX_PROFILES[type]; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * The parameters a profile sets at this strength, merged over what is there. |
| 169 | + * |
| 170 | + * Merged rather than replacing: a profile names only the parameters it derives, |
| 171 | + * and the rest — a compressor's knee, a saturation's curve type — are the |
| 172 | + * author's to set under Details and must survive the knob moving. |
| 173 | + */ |
| 174 | +export function applyAudioFxProfile( |
| 175 | + type: string, |
| 176 | + strength: number, |
| 177 | + params: HfAudioFxParamValues, |
| 178 | +): HfAudioFxParamValues { |
| 179 | + const profile = getAudioFxProfile(type); |
| 180 | + if (!profile) return params; |
| 181 | + return normalizeAudioFxParams(type, { ...params, ...profile.at(strength) }); |
| 182 | +} |
| 183 | + |
| 184 | +/** |
| 185 | + * The strength a set of parameters reads as, by inverting the profile's own |
| 186 | + * curve on its most characteristic parameter. |
| 187 | + * |
| 188 | + * A chain stores mechanism values, not the knob — the same contract |
| 189 | + * `normalizeCarveSettings` has, and for the same reason: the mechanism is what |
| 190 | + * renders, so it is what must be authoritative. Reading the knob back means one |
| 191 | + * parameter has to be nominated as the one that says most about intent. |
| 192 | + * |
| 193 | + * A hand-edited chain therefore lands the knob at the nearest strength that |
| 194 | + * would have produced its most telling value, which is the honest answer — the |
| 195 | + * alternative is a knob parked at a default while the effect is set to |
| 196 | + * something else entirely. |
| 197 | + */ |
| 198 | +export function audioFxProfileStrength(type: string, params: HfAudioFxParamValues): number { |
| 199 | + const profile = getAudioFxProfile(type); |
| 200 | + if (!profile) return 0.5; |
| 201 | + const key = profile.derives[0]; |
| 202 | + if (key === undefined) return 0.5; |
| 203 | + const value = params[key]; |
| 204 | + if (typeof value !== "number") return 0.5; |
| 205 | + const low = profile.at(0)[key]; |
| 206 | + const high = profile.at(1)[key]; |
| 207 | + if (typeof low !== "number" || typeof high !== "number" || low === high) return 0.5; |
| 208 | + return to2(Math.min(1, Math.max(0, (value - low) / (high - low)))); |
| 209 | +} |
0 commit comments