Add native bandpass and notch filters

This commit is contained in:
Meta Vens 2026-05-16 10:32:28 +01:00
commit da459f80f3
11 changed files with 243 additions and 17 deletions

View file

@ -232,6 +232,8 @@ public static class FaustEmitter
: "";
var lpf = $"clip01({parameters.Expression(OwnerField(ownerPath, "filter/lpf"), voice.Filter.LowPass)} * (1.0 + {parameters.Expression(OwnerField(ownerPath, "filter/lpf_ramp"), voice.Filter.LowPassRamp)} * age * 1.8){lpfEnvelope} + patch_mod_lpf + {lpfMod})";
var hpf = $"clip01({parameters.Expression(OwnerField(ownerPath, "filter/hpf"), voice.Filter.HighPass)} * (1.0 + {parameters.Expression(OwnerField(ownerPath, "filter/hpf_ramp"), voice.Filter.HighPassRamp)} * age * 2.0){hpfEnvelope} + patch_mod_hpf + {hpfMod})";
var bpf = $"clip01({parameters.Expression(OwnerField(ownerPath, "filter/bpf"), voice.Filter.BandPass)})";
var notch = $"clip01({parameters.Expression(OwnerField(ownerPath, "filter/notch"), voice.Filter.Notch)})";
var highPassOrder = Math.Clamp(voice.Filter.HighPassOrder, 1, 12);
var hasExplicitQ = voice.Filter.LowPassQ > 0 || parameters.IsBound(OwnerField(ownerPath, "filter/lpf_q"));
var hasResonance = voice.Filter.LowPassResonance > 0 || parameters.IsBound(OwnerField(ownerPath, "filter/resonance"));
@ -243,13 +245,27 @@ public static class FaustEmitter
: hasResonance
? $"fi.resonlp(max(20.0, {lpf} * 18000.0), 0.7 + clip01({resonance}) * 18.0, 1.0)"
: $"fi.lowpass({lowPassOrder}, max(20.0, {lpf} * 18000.0))";
var bandpass = BandPassCascade(
$"max(20.0, {bpf} * 18000.0)",
$"max(0.1, {parameters.Expression(OwnerField(ownerPath, "filter/bpf_q"), voice.Filter.BandPassQ)})",
voice.Filter.BandPassOrder);
var notchFilter = NotchCascade(
$"max(20.0, {notch} * 18000.0)",
$"max(1.0, (max(20.0, {notch} * 18000.0)) / max(0.1, {parameters.Expression(OwnerField(ownerPath, "filter/notch_q"), voice.Filter.NotchQ)}))",
voice.Filter.NotchOrder);
var bandPassStage = voice.Filter.BandPass > 0 || parameters.IsBound(OwnerField(ownerPath, "filter/bpf"))
? $" : {bandpass}"
: "";
var notchStage = voice.Filter.Notch > 0 || parameters.IsBound(OwnerField(ownerPath, "filter/notch"))
? $" : {notchFilter}"
: "";
source.AppendLine($"{name}_freq = {frequency};");
source.AppendLine($"{name}_osc = {oscillator};");
source.AppendLine($"{name}_colored = ({name}_osc * (1.0 - {noiseMix}) + no.noise * {noiseMix});");
source.AppendLine($"{name}_driven = ma.tanh({name}_colored * (1.0 + {driveExpression} * 12.0)) / ma.tanh(1.0 + {driveExpression} * 12.0);");
source.AppendLine($"{name}_folded = {name}_driven * (1.0 - {foldExpression}) + fold({name}_driven * (1.0 + {foldExpression} * 3.5)) * {foldExpression};");
source.AppendLine($"{name}_filtered = {name}_folded : {lowpass} : fi.highpass({highPassOrder}, max(5.0, ({hpf}) * ({hpf}) * 7000.0));");
source.AppendLine($"{name}_filtered = {name}_folded : {lowpass} : fi.highpass({highPassOrder}, max(5.0, ({hpf}) * ({hpf}) * 7000.0)){bandPassStage}{notchStage};");
if (voice.Phaser.OffsetSeconds != 0 || voice.Phaser.RampSecondsPerSecond != 0 ||
parameters.IsBound(OwnerField(ownerPath, "phaser/offset")) ||
parameters.IsBound(OwnerField(ownerPath, "phaser/ramp")))
@ -272,6 +288,18 @@ public static class FaustEmitter
return string.Join(" : ", Enumerable.Repeat($"fi.resonlp({cutoff}, {q}, 1.0)", stages));
}
private static string BandPassCascade(string center, string q, int order)
{
var stages = Math.Max(1, (Math.Clamp(order, 1, 12) + 1) / 2);
return string.Join(" : ", Enumerable.Repeat($"fi.resonbp({center}, {q}, 1.0)", stages));
}
private static string NotchCascade(string center, string width, int order)
{
var stages = Math.Max(1, (Math.Clamp(order, 1, 12) + 1) / 2);
return string.Join(" : ", Enumerable.Repeat($"fi.notchw({width}, {center})", stages));
}
private static void EmitSpectralBank(
StringBuilder source,
SynthPatch patch,

View file

@ -111,6 +111,12 @@ public sealed record Filter(
float HighPass = 0,
float HighPassRamp = 0,
int HighPassOrder = 1,
float BandPass = 0,
float BandPassQ = 1,
int BandPassOrder = 1,
float Notch = 0,
float NotchQ = 1,
int NotchOrder = 1,
RateLevelEnvelope? LowPassEnvelope = null,
RateLevelEnvelope? HighPassEnvelope = null);

View file

@ -446,6 +446,12 @@ public static class PatchScript
GetBoundFloat(fields, line, 0, OwnerField(ownerPath, "filter/hpf"), "hpf", "h"),
GetBoundFloat(fields, line, 0, OwnerField(ownerPath, "filter/hpf_ramp"), "hpf_ramp", "hr"),
GetBoundInt(fields, line, 1, OwnerField(ownerPath, "filter/hpf_order"), "hpf_order", "hpfo"),
GetBoundFloat(fields, line, 0, OwnerField(ownerPath, "filter/bpf"), "bpf", "bp"),
GetBoundFloat(fields, line, 1, OwnerField(ownerPath, "filter/bpf_q"), "bpf_q", "bpq"),
GetBoundInt(fields, line, 1, OwnerField(ownerPath, "filter/bpf_order"), "bpf_order", "bpfo"),
GetBoundFloat(fields, line, 0, OwnerField(ownerPath, "filter/notch"), "notch", "nt"),
GetBoundFloat(fields, line, 1, OwnerField(ownerPath, "filter/notch_q"), "notch_q", "ntq"),
GetBoundInt(fields, line, 1, OwnerField(ownerPath, "filter/notch_order"), "notch_order", "nto"),
ParseFilterRateLevelEnvelope(fields, line, ownerPath, "lpf"),
ParseFilterRateLevelEnvelope(fields, line, ownerPath, "hpf")),
Phaser = new Phaser(

View file

@ -342,6 +342,12 @@ public static class ZynInstrumentReader
var lowPassOrder = ZynPadLowPassOrder(filter);
var highPass = ZynPadHighPass(filter, playbackFrequencyHz);
var highPassOrder = ZynPadHighPassOrder(filter);
var bandPass = ZynPadBandPass(filter, playbackFrequencyHz);
var bandPassQ = ZynPadFilterQ(filter);
var bandPassOrder = ZynPadBandPassOrder(filter);
var notch = ZynPadNotch(filter, playbackFrequencyHz);
var notchQ = ZynPadFilterQ(filter);
var notchOrder = ZynPadNotchOrder(filter);
var lowPassEnvelope = ZynPadLowPassEnvelope(filter, lowPass);
var highPassEnvelope = ZynPadHighPassEnvelope(filter, highPass);
var safeName = includeKitSuffix ? $"{safeBaseName}_{kitId}" : safeBaseName;
@ -377,6 +383,18 @@ public static class ZynInstrumentReader
matched.Add(new($"{featurePrefix}filter_hpf", F(highPass), "Mapped static Zyn PAD global high-pass frequency into AquaSynth layer filtering."));
matched.Add(new($"{featurePrefix}filter_hpf_order", highPassOrder.ToString(CultureInfo.InvariantCulture), "Mapped Zyn high-pass family and stage count into AquaSynth high-pass order."));
}
if (bandPass > 0)
{
matched.Add(new($"{featurePrefix}filter_bpf", F(bandPass), "Mapped static Zyn PAD global band-pass center into AquaSynth layer filtering."));
matched.Add(new($"{featurePrefix}filter_bpf_q", F(bandPassQ), "Mapped Zyn band-pass Q into AquaSynth filter bandwidth."));
matched.Add(new($"{featurePrefix}filter_bpf_order", bandPassOrder.ToString(CultureInfo.InvariantCulture), "Mapped Zyn band-pass family and stage count into AquaSynth band-pass order."));
}
if (notch > 0)
{
matched.Add(new($"{featurePrefix}filter_notch", F(notch), "Mapped static Zyn PAD global notch center into AquaSynth layer filtering."));
matched.Add(new($"{featurePrefix}filter_notch_q", F(notchQ), "Mapped Zyn notch Q into AquaSynth notch width."));
matched.Add(new($"{featurePrefix}filter_notch_order", notchOrder.ToString(CultureInfo.InvariantCulture), "Mapped Zyn notch family and stage count into AquaSynth notch order."));
}
if (lowPassEnvelope is not null)
{
matched.Add(new($"{featurePrefix}filter_lpf_envelope", "ratelevel", "Mapped Zyn PAD filter envelope octave offsets into AquaSynth low-pass cutoff motion."));
@ -398,7 +416,9 @@ public static class ZynInstrumentReader
: $" hpf_env=rl hpf_start={F(highPassEnvelope.StartLevel)} hpf_rates={F(highPassEnvelope.Rate1Seconds)},{F(highPassEnvelope.Rate2Seconds)},{F(highPassEnvelope.Rate3Seconds)},{F(highPassEnvelope.Rate4Seconds)} hpf_levels={F(highPassEnvelope.Level1)},{F(highPassEnvelope.Level2)},{F(highPassEnvelope.Level3)},{F(highPassEnvelope.Level4)} hpf_curves=lin,lin,lin,lin";
var filterQText = lowPass < 1 ? $" lpf_q={F(lowPassQ)}" : "";
var highPassText = highPass > 0 ? $" hpf={F(highPass)} hpf_order={highPassOrder}" : "";
script.AppendLine($"layer name={safeName} engine=pad gain={F(layerGain)} lpf={F(lowPass)}{filterQText} lpf_order={lowPassOrder}{highPassText}{filterEnvelopeText}{highPassEnvelopeText} env=rl rates={F(attack)},{F(decay)},1,{F(release)} levels=1,1,1,0 curves=lin,lin,lin,lin gate=1.5");
var bandPassText = bandPass > 0 ? $" bpf={F(bandPass)} bpf_q={F(bandPassQ)} bpf_order={bandPassOrder}" : "";
var notchText = notch > 0 ? $" notch={F(notch)} notch_q={F(notchQ)} notch_order={notchOrder}" : "";
script.AppendLine($"layer name={safeName} engine=pad gain={F(layerGain)} lpf={F(lowPass)}{filterQText} lpf_order={lowPassOrder}{highPassText}{bandPassText}{notchText}{filterEnvelopeText}{highPassEnvelopeText} env=rl rates={F(attack)},{F(decay)},1,{F(release)} levels=1,1,1,0 curves=lin,lin,lin,lin gate=1.5");
script.AppendLine($"spectrum layer={safeName} root={F(tableRootFrequencyHz)} freq={F(noteFrequencyHz)} spread=0 pad_mode={mode} pad_bandwidth={bandwidth} pad_bwscale={bandwidthScale} pad_profile={profile} pad_position={position} partials={partialText}");
}
@ -1119,6 +1139,36 @@ public static class ZynInstrumentReader
return Math.Clamp(MathF.Sqrt(cutoffHz / 7000.0f), 0f, 1f);
}
private static float ZynPadBandPass(XElement? filter, float playbackFrequencyHz)
{
if (filter is null || !TryZynPadFilterCutoff(filter, playbackFrequencyHz, out var cutoffHz, out var category, out var type))
{
return 0;
}
if (!IsZynPadBandPassFilter(category, type))
{
return 0;
}
return Math.Clamp(cutoffHz / 18000.0f, 20.0f / 18000.0f, 1f);
}
private static float ZynPadNotch(XElement? filter, float playbackFrequencyHz)
{
if (filter is null || !TryZynPadFilterCutoff(filter, playbackFrequencyHz, out var cutoffHz, out var category, out var type))
{
return 0;
}
if (!IsZynPadNotchFilter(category, type))
{
return 0;
}
return Math.Clamp(cutoffHz / 18000.0f, 20.0f / 18000.0f, 1f);
}
private static int ZynPadHighPassOrder(XElement? filter)
{
if (filter is null)
@ -1143,6 +1193,40 @@ public static class ZynInstrumentReader
return Math.Clamp(poles * (DescendantIntParam(filter, "stages", 0) + 1), 1, 12);
}
private static int ZynPadBandPassOrder(XElement? filter)
{
if (filter is null)
{
return 1;
}
var category = DescendantIntParam(filter, "category", 0);
var type = DescendantIntParam(filter, "type", 0);
if (!IsZynPadBandPassFilter(category, type))
{
return 1;
}
return Math.Clamp(2 * (DescendantIntParam(filter, "stages", 0) + 1), 1, 12);
}
private static int ZynPadNotchOrder(XElement? filter)
{
if (filter is null)
{
return 1;
}
var category = DescendantIntParam(filter, "category", 0);
var type = DescendantIntParam(filter, "type", 0);
if (!IsZynPadNotchFilter(category, type))
{
return 1;
}
return Math.Clamp(2 * (DescendantIntParam(filter, "stages", 0) + 1), 1, 12);
}
private static float ZynPadLowPassQ(XElement? filter)
{
if (filter is null)
@ -1161,6 +1245,17 @@ public static class ZynInstrumentReader
return MathF.Exp(q * q * MathF.Log(1000.0f)) - 0.9f;
}
private static float ZynPadFilterQ(XElement? filter)
{
if (filter is null)
{
return 1;
}
var q = DescendantIntParam(filter, "q", 64) / 127.0f;
return MathF.Exp(q * q * MathF.Log(1000.0f)) - 0.9f;
}
private static bool TryZynPadFilterCutoff(XElement filter, float playbackFrequencyHz, out float cutoffHz, out int category, out int type)
{
category = DescendantIntParam(filter, "category", 0);
@ -1185,6 +1280,14 @@ public static class ZynInstrumentReader
(category == 0 && type is 1 or 3) ||
(category == 2 && type == 1);
private static bool IsZynPadBandPassFilter(int category, int type) =>
(category == 0 && type == 4) ||
(category == 2 && type == 2);
private static bool IsZynPadNotchFilter(int category, int type) =>
(category == 0 && type == 5) ||
(category == 2 && type == 3);
private static RateLevelEnvelope? ZynPadLowPassEnvelope(XElement? filter, float baseLowPass)
{
var envelope = filter?.Element("FILTER_ENVELOPE");
@ -1382,8 +1485,12 @@ public static class ZynInstrumentSurvey
"cat=0 type=1",
"cat=0 type=2",
"cat=0 type=3",
"cat=0 type=4",
"cat=0 type=5",
"cat=2 type=0",
"cat=2 type=1"
"cat=2 type=1",
"cat=2 type=2",
"cat=2 type=3"
};
public static IReadOnlyList<ZynInstrumentSurveyItem> RankDirectory(string root, int take = 25)
@ -1688,7 +1795,7 @@ public static class ZynInstrumentSurvey
HandledPadGlobalFilters.Contains(filterKey) ? ZynCoverageStatus.Handled : ZynCoverageStatus.Unknown,
relativePath,
HandledPadGlobalFilters.Contains(filterKey)
? "LP/HP PAD filters map to AquaSynth cutoff, order, Q, and envelope fields."
? "PAD filter category/type maps to AquaSynth native filter fields."
: "Observed PAD filter category/type is not translated by the current PAD rebuild path.");
foreach (var filterLfo in filter.Elements("FILTER_LFO"))