Add readable operator graph syntax

This commit is contained in:
Meta Vens 2026-05-13 11:47:39 +01:00
commit a3c4e64211
8 changed files with 268 additions and 37 deletions

View file

@ -685,9 +685,44 @@ public static class BuiltInScripts
name=dx7_algo8_core
freq=330
gain=0.18
carriers=1,3
ops=6:4:0.9:0:0.035:0.18,5:3:0.82:0:0.04:0.2,4:2:0.7:0.18:0.05:0.24,3:2:0.6:0:0.06:0.3,2:1:0.75:0:0.045:0.2,1:1:0.82:0:0.08:0.34
edges=6>5:1.1,5>3:0.9,4>3:0.75,2>1:0.85
operator name=op6
ratio=4
level=0.9
env=ad:0.035:0.18
operator name=op5
ratio=3
level=0.82
env=ad:0.04:0.2
operator name=op4
ratio=2
level=0.7
feedback=0.18
env=ad:0.05:0.24
operator name=op3
ratio=2
level=0.6
env=ad:0.06:0.3
operator name=op2
ratio=1
level=0.75
env=ad:0.045:0.2
operator name=op1
ratio=1
level=0.82
env=adsr:0.005:0.08:0.68:0.34
route from=op6 to=op5 index=1.1
route from=op5 to=op3 index=0.9
route from=op4 to=op3 index=0.75
route from=op2 to=op1 index=0.85
carrier name=op1
carrier name=op3
voice
freq=330

View file

@ -94,6 +94,8 @@ public sealed record OperatorNode(
float Ratio = 1,
float Level = 1,
float Feedback = 0,
float AttackSeconds = 0,
float ReleaseSeconds = 0,
Envelope Envelope = null!)
{
public Envelope Envelope { get; init; } = Envelope ?? new();

View file

@ -19,7 +19,7 @@ public static class PatchScript
line = statement.Line;
}
if (compiler.Voices.Count == 0 && compiler.OperatorGraphs.Count == 0)
if (!compiler.HasOutput)
{
throw new PatchScriptException(line, "patch script produced no voices or operator graphs");
}
@ -35,24 +35,30 @@ public static class PatchScript
private readonly List<PatchParameter> _parameters = [];
private readonly List<ParameterBinding> _parameterBindings = [];
private readonly Dictionary<string, string> _defaults = new(StringComparer.OrdinalIgnoreCase);
private PendingOperatorGraph? _pendingOperatorGraph;
public List<Voice> Voices { get; } = [];
public List<OperatorGraph> OperatorGraphs => _operatorGraphs;
public bool HasOutput => Voices.Count > 0 || _operatorGraphs.Count > 0 || _pendingOperatorGraph is not null;
private Repeat? Repeat { get; set; }
private float Gain { get; set; } = 1;
private bool SoftClip { get; set; } = true;
public SynthPatch Build() => new()
public SynthPatch Build()
{
Voices = Voices,
OperatorGraphs = _operatorGraphs,
Controls = _controls,
Parameters = _parameters,
ParameterBindings = _parameterBindings,
Repeat = Repeat,
Gain = Gain,
SoftClip = SoftClip
};
FlushPendingOperatorGraph();
return new SynthPatch
{
Voices = Voices,
OperatorGraphs = _operatorGraphs,
Controls = _controls,
Parameters = _parameters,
ParameterBindings = _parameterBindings,
Repeat = Repeat,
Gain = Gain,
SoftClip = SoftClip
};
}
public void Apply(string statement, int line)
{
@ -62,6 +68,7 @@ public static class PatchScript
var fields = ParseFields(parts.Skip(1), line);
if (SfxrParams.Named(rawCommand) is { } namedParams)
{
FlushPendingOperatorGraph();
AddSfxrPatch(ApplySfxrFields(namedParams, fields, line));
return;
}
@ -69,31 +76,48 @@ public static class PatchScript
switch (command)
{
case "patch":
FlushPendingOperatorGraph();
ApplyPatch(fields, line);
break;
case "defaults":
FlushPendingOperatorGraph();
Merge(_defaults, fields);
break;
case "template":
FlushPendingOperatorGraph();
var name = Required(fields, "name", line);
_templates[name] = Without(fields, "name");
break;
case "voice":
FlushPendingOperatorGraph();
Voices.Add(ParseVoice(ExpandVoiceFields(fields, line), Voices.Count, line));
break;
case "opgraph":
AddOperatorGraph(fields, line);
StartOperatorGraph(fields, line);
break;
case "operator":
AddOperator(fields, line);
break;
case "route":
AddOperatorRoute(fields, line);
break;
case "carrier":
AddOperatorCarrier(fields, line);
break;
case "mod":
FlushPendingOperatorGraph();
AddModBus(fields, line);
break;
case "control":
FlushPendingOperatorGraph();
AddControlLane(fields, line);
break;
case "param":
FlushPendingOperatorGraph();
AddParameter(fields, line);
break;
case "sfxr":
FlushPendingOperatorGraph();
AddSfxrPatch(ParseSfxrCommand(fields, line));
break;
default:
@ -260,15 +284,102 @@ public static class PatchScript
? ParseOperatorIds(carrierSpec, line)
: operators.Select(op => op.Id).ToList();
var operatorIds = operators.Select(op => op.Id).ToHashSet();
foreach (var edge in edges)
AddValidatedOperatorGraph(line, new OperatorGraph(
GetAny(fields, ["name", "n"], $"opgraph{graphIndex}"),
GetBoundFloat(fields, line, 440, $"{graphPath}/freq", "freq", "frequency", "f"),
operators,
edges,
carriers,
GetBoundFloat(fields, line, 0.2f, $"{graphPath}/gain", "gain", "g")));
}
private void StartOperatorGraph(IReadOnlyDictionary<string, string> fields, int line)
{
FlushPendingOperatorGraph();
if (fields.ContainsKey("ops"))
{
AddOperatorGraph(fields, line);
return;
}
var graphIndex = _operatorGraphs.Count;
var graphPath = $"/opgraphs/{graphIndex}";
_pendingOperatorGraph = new PendingOperatorGraph(
line,
GetAny(fields, ["name", "n"], $"opgraph{graphIndex}"),
GetBoundFloat(fields, line, 440, $"{graphPath}/freq", "freq", "frequency", "f"),
GetBoundFloat(fields, line, 0.2f, $"{graphPath}/gain", "gain", "g"));
}
private void AddOperator(IReadOnlyDictionary<string, string> fields, int line)
{
var graph = RequiredPendingOperatorGraph(line);
var id = ParseOperatorId(Required(fields, "name", line), line);
var envelope = TryGetAny(fields, ["env", "envelope"], out var envSpec)
? ParseEnvelopeSpec(envSpec, line)
: new Envelope(
GetFloat(fields, line, 0, "attack", "a"),
GetFloat(fields, line, 0.1f, "sustain", "s"),
GetFloat(fields, line, 0.1f, "decay", "d"),
GetFloat(fields, line, 0, "punch", "pu"));
graph.Operators.Add(new OperatorNode(
id,
GetFloat(fields, line, 1, "ratio", "r"),
GetFloat(fields, line, 1, "level", "l"),
GetFloat(fields, line, 0, "feedback", "fb"),
0,
GetFloat(fields, line, 0, "release"),
envelope));
}
private void AddOperatorRoute(IReadOnlyDictionary<string, string> fields, int line)
{
var graph = RequiredPendingOperatorGraph(line);
var source = ParseOperatorId(Required(fields, "from", line), line);
var target = ParseOperatorId(Required(fields, "to", line), line);
graph.Edges.Add(new OperatorEdge(source, target, GetFloat(fields, line, 1, "index", "amount", "depth")));
}
private void AddOperatorCarrier(IReadOnlyDictionary<string, string> fields, int line)
{
var graph = RequiredPendingOperatorGraph(line);
graph.Carriers.Add(ParseOperatorId(Required(fields, "name", line), line));
}
private PendingOperatorGraph RequiredPendingOperatorGraph(int line) =>
_pendingOperatorGraph ?? throw new PatchScriptException(line, "operator graph command needs an active opgraph");
private void FlushPendingOperatorGraph()
{
if (_pendingOperatorGraph is null) return;
var graph = _pendingOperatorGraph;
_pendingOperatorGraph = null;
AddValidatedOperatorGraph(graph.Line, new OperatorGraph(
graph.Name,
graph.FrequencyHz,
graph.Operators,
graph.Edges,
graph.Carriers.Count > 0 ? graph.Carriers : graph.Operators.Select(op => op.Id).ToList(),
graph.Gain));
}
private void AddValidatedOperatorGraph(int line, OperatorGraph graph)
{
if (graph.Operators.Count == 0)
{
throw new PatchScriptException(line, "operator graph needs at least one operator");
}
var operatorIds = graph.Operators.Select(op => op.Id).ToHashSet();
foreach (var edge in graph.Edges)
{
if (!operatorIds.Contains(edge.SourceId) || !operatorIds.Contains(edge.TargetId))
{
throw new PatchScriptException(line, $"operator edge `{edge.SourceId}>{edge.TargetId}` references an unknown operator");
}
}
foreach (var carrier in carriers)
foreach (var carrier in graph.Carriers)
{
if (!operatorIds.Contains(carrier))
{
@ -276,13 +387,7 @@ public static class PatchScript
}
}
_operatorGraphs.Add(new OperatorGraph(
GetAny(fields, ["name", "n"], $"opgraph{graphIndex}"),
GetBoundFloat(fields, line, 440, $"{graphPath}/freq", "freq", "frequency", "f"),
operators,
edges,
carriers,
GetBoundFloat(fields, line, 0.2f, $"{graphPath}/gain", "gain", "g")));
_operatorGraphs.Add(graph);
}
private void AddParameter(IReadOnlyDictionary<string, string> fields, int line)
@ -381,6 +486,8 @@ public static class PatchScript
ParseFloat(pieces[1], line),
ParseFloat(pieces[2], line),
pieces.Length >= 4 ? ParseFloat(pieces[3], line) : 0,
0,
0,
pieces.Length >= 6 ? new Envelope(0, ParseFloat(pieces[4], line), ParseFloat(pieces[5], line)) : new Envelope());
})
.ToList();
@ -413,6 +520,32 @@ public static class PatchScript
.Select(part => ParseInt(part, line))
.ToList();
private static Envelope ParseEnvelopeSpec(string value, int line)
{
var pieces = value.Split(':');
return pieces[0].ToLowerInvariant() switch
{
"ad" when pieces.Length == 3 => new Envelope(
ParseFloat(pieces[1], line),
0,
ParseFloat(pieces[2], line)),
"adsr" when pieces.Length == 5 => new Envelope(
ParseFloat(pieces[1], line),
ParseFloat(pieces[2], line),
ParseFloat(pieces[4], line),
ParseFloat(pieces[3], line)),
_ => throw new PatchScriptException(line, $"bad envelope `{value}`")
};
}
private static int ParseOperatorId(string value, int line)
{
var normalized = value.StartsWith("op", StringComparison.OrdinalIgnoreCase)
? value[2..]
: value;
return ParseInt(normalized, line);
}
private void AddSfxrPatch(SfxrParams parameters)
{
var mapped = parameters.ToPatch();
@ -468,6 +601,17 @@ public static class PatchScript
if (TryGetAny(fields, ["arp_mod", "am"], out var arpMod)) parameters = parameters with { ArpMod = Math.Clamp(ParseFloat(arpMod, line), -1, 1) };
return parameters;
}
private sealed record PendingOperatorGraph(
int Line,
string Name,
float FrequencyHz,
float Gain)
{
public List<OperatorNode> Operators { get; } = [];
public List<OperatorEdge> Edges { get; } = [];
public List<int> Carriers { get; } = [];
}
}
private static readonly (string[] Keys, ModTarget Target)[] ModTargets =
@ -532,13 +676,16 @@ public static class PatchScript
return (ParseModTarget(pieces[0], line), ParseFloat(pieces[1], line));
});
private static string CanonicalCommand(string command) => command.ToLowerInvariant() switch
private static string CanonicalCommand(string command) => command.ToLowerInvariant() switch
{
"p" or "patch" => "patch",
"d" or "default" or "defaults" => "defaults",
"def" or "t" or "template" => "template",
"v" or "voice" => "voice",
"opgraph" or "ops" or "operators" => "opgraph",
"operator" or "op" => "operator",
"route" or "edge" => "route",
"carrier" or "out" => "carrier",
"mod" or "wob" or "wobble" or "bus" => "mod",
"lfo" or "control" => "control",
"param" or "parameter" => "param",
@ -556,6 +703,8 @@ public static class PatchScript
"a" => "attack",
"s" => "sustain",
"d" => "decay",
"from" or "src" => "from",
"to" or "dst" => "to",
_ => field
};