Add harmonic bank DSL surface

This commit is contained in:
Meta Vens 2026-05-14 20:36:06 +01:00
commit 9714913407
11 changed files with 187 additions and 23 deletions

View file

@ -793,10 +793,8 @@ public static class BuiltInScripts
layer name=body engine=add min_key=36 max_key=96 gain=0.16
layer name=shine engine=add min_key=48 max_key=108 gain=0.045
voice layer=body freq=220
voice layer=body freq=440 gain=0.075
voice layer=shine freq=660
voice layer=shine freq=880 gain=0.025
harmonics layer=body root=220 partials=1:0.16,2:0.075
harmonics layer=shine root=220 partials=3:0.045,4:0.025
""";
public const string ZynStylePadTexture = """

View file

@ -194,6 +194,13 @@ public sealed record PatchLayer(
float Gain = 1,
string EffectSend = "");
public sealed record HarmonicPartial(float Ratio, float Gain);
public sealed record HarmonicBank(
string LayerName,
float RootFrequencyHz,
IReadOnlyList<HarmonicPartial> Partials);
public sealed record ReferencePatch(
string Id,
string Family,
@ -227,6 +234,7 @@ public sealed record SynthPatch
{
public IReadOnlyList<Voice> Voices { get; init; } = Array.Empty<Voice>();
public IReadOnlyList<PatchLayer> Layers { get; init; } = Array.Empty<PatchLayer>();
public IReadOnlyList<HarmonicBank> HarmonicBanks { get; init; } = Array.Empty<HarmonicBank>();
public IReadOnlyList<OperatorGraph> OperatorGraphs { get; init; } = Array.Empty<OperatorGraph>();
public IReadOnlyList<ControlLane> Controls { get; init; } = Array.Empty<ControlLane>();
public IReadOnlyList<PatchParameter> Parameters { get; init; } = Array.Empty<PatchParameter>();

View file

@ -35,6 +35,7 @@ public static class PatchScript
private readonly List<PatchParameter> _parameters = [];
private readonly List<ParameterBinding> _parameterBindings = [];
private readonly List<PatchLayer> _layers = [];
private readonly List<HarmonicBank> _harmonicBanks = [];
private readonly Dictionary<string, string> _defaults = new(StringComparer.OrdinalIgnoreCase);
private readonly Dictionary<string, Dictionary<string, string>> _layerDefaults = new(StringComparer.OrdinalIgnoreCase);
private PendingOperatorGraph? _pendingOperatorGraph;
@ -54,6 +55,7 @@ public static class PatchScript
{
Voices = Voices,
Layers = _layers,
HarmonicBanks = _harmonicBanks,
OperatorGraphs = _operatorGraphs,
Controls = _controls,
Parameters = _parameters,
@ -97,6 +99,10 @@ public static class PatchScript
FlushPendingOperatorGraph();
AddLayer(fields, line);
break;
case "harmonics":
FlushPendingOperatorGraph();
AddHarmonicBank(fields, line);
break;
case "voice":
FlushPendingOperatorGraph();
Voices.Add(ParseVoice(ExpandVoiceFields(fields, line), Voices.Count, line));
@ -134,6 +140,54 @@ public static class PatchScript
}
}
private void AddHarmonicBank(IReadOnlyDictionary<string, string> fields, int line)
{
var layerName = Required(fields, "layer", line);
if (!_layerDefaults.ContainsKey(layerName))
{
throw new PatchScriptException(line, $"unknown layer `{layerName}`");
}
var rootFrequency = GetBoundFloat(fields, line, 440, $"/harmonics/{_harmonicBanks.Count}/root", "root", "base", "freq", "frequency");
if (rootFrequency <= 0)
{
throw new PatchScriptException(line, "harmonic root frequency must be greater than zero");
}
var partials = ParseHarmonicPartials(
GetAny(fields, ["partials", "bank", "tones", "drawbars"], ""),
line);
if (partials.Count == 0)
{
throw new PatchScriptException(line, "harmonics needs at least one partial");
}
_harmonicBanks.Add(new HarmonicBank(layerName, rootFrequency, partials));
var sharedFields = Without(fields,
"layer",
"root",
"base",
"freq",
"frequency",
"partials",
"bank",
"tones",
"drawbars",
"gain",
"g");
foreach (var partial in partials)
{
var voiceFields = new Dictionary<string, string>(sharedFields, StringComparer.OrdinalIgnoreCase)
{
["layer"] = layerName,
["freq"] = F(rootFrequency * partial.Ratio),
["gain"] = F(partial.Gain)
};
Voices.Add(ParseVoice(ExpandVoiceFields(voiceFields, line), Voices.Count, line));
}
}
private Dictionary<string, string> ExpandVoiceFields(Dictionary<string, string> fields, int line)
{
var expanded = new Dictionary<string, string>(_defaults, StringComparer.OrdinalIgnoreCase);
@ -674,6 +728,24 @@ public static class PatchScript
private static ParsedEnvelope AdEnvelope(float attackSeconds, float decaySeconds) =>
new(new Envelope(attackSeconds, decaySeconds, 0, 0), attackSeconds + decaySeconds);
private static IReadOnlyList<HarmonicPartial> ParseHarmonicPartials(string value, int line)
{
if (string.IsNullOrWhiteSpace(value)) return [];
return value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Select(part =>
{
var pieces = part.Split(':', 2);
if (pieces.Length != 2) throw new PatchScriptException(line, $"bad harmonic partial `{part}`");
var ratio = ParseFloat(pieces[0], line);
var gain = ParseFloat(pieces[1], line);
if (ratio <= 0) throw new PatchScriptException(line, "harmonic partial ratio must be greater than zero");
if (gain < 0) throw new PatchScriptException(line, "harmonic partial gain must be zero or greater");
return new HarmonicPartial(ratio, gain);
})
.ToArray();
}
private ParsedEnvelope ParseRateLevelEnvelope(IReadOnlyDictionary<string, string> fields, int line, string fieldPath)
{
var rates = ParseFloatList(Required(fields, "rates", line), line, "rates");
@ -901,6 +973,7 @@ public static class PatchScript
"d" or "default" or "defaults" => "defaults",
"def" or "t" or "template" => "template",
"layer" or "kit" => "layer",
"harmonics" or "partials" or "drawbars" => "harmonics",
"v" or "voice" => "voice",
"opgraph" or "ops" or "operators" => "opgraph",
"operator" or "op" => "operator",

View file

@ -21,17 +21,18 @@ public static class ReferenceRebuildCatalog
"zyn/project/additive-lead",
BuiltInScripts.ZynStyleAdditiveLead,
[
new("engine_add", "1", "Represented as explicit additive sine partial voices."),
new("engine_add", "1", "Represented as additive sine partials under named layer authority."),
new("named_layers", "body,shine", "Layer declarations group partial voices under reusable ADD engine intent."),
new("partial_voice_count", "4", "The additive shape is represented as four explicit Aquarium partial voices."),
new("additive_harmonic_bank", "2 banks / 4 partials", "Harmonic-bank syntax records authored ADD partial sets before lowering to ordinary voices."),
new("partial_voice_count", "4", "The additive shape lowers to four Aquarium partial voices."),
new("envelope_count", "2", "Amplitude and pitch contour pressure maps to ADSR plus vibrato in the current patch surface."),
new("lfo_count", "1", "The fixture's amplitude/frequency motion is represented as patch-level vibrato/macro motion.")
],
[
new("additive_harmonic_bank", "Zyn ADDsynth harmonic table", "Aquarium currently writes each partial as a voice; it lacks a terse harmonic-bank source."),
new("zyn_addsynth_oscillator_detail", "Zyn ADDsynth harmonic phase/bandwidth details", "Aquarium records harmonic ratios and gains, but not Zyn's full ADDsynth oscillator table semantics."),
new("zyn_free_envelope_exactness", "Zyn envelope parameter semantics", "The rebuild uses Aquarium ADSR/motion controls rather than Zyn's exact envelope timing model.")
],
"This is the easy Zyn pressure case: Aquarium can say the sound as layered partials, but the syntax is already showing the need for a compact additive harmonic-bank abstraction."),
"This is the easy Zyn pressure case: Aquarium can now say the sound as named harmonic banks while keeping the emitted voice graph plain enough to inspect."),
new(
"zyn/project-pad-texture/aquarium",
"zynaddsubfx",