Add named layer DSL surface
This commit is contained in:
parent
8426ca50f4
commit
62ea9bee17
11 changed files with 188 additions and 27 deletions
|
|
@ -34,7 +34,9 @@ public static class PatchScript
|
|||
private readonly List<OperatorGraph> _operatorGraphs = [];
|
||||
private readonly List<PatchParameter> _parameters = [];
|
||||
private readonly List<ParameterBinding> _parameterBindings = [];
|
||||
private readonly List<PatchLayer> _layers = [];
|
||||
private readonly Dictionary<string, string> _defaults = new(StringComparer.OrdinalIgnoreCase);
|
||||
private readonly Dictionary<string, Dictionary<string, string>> _layerDefaults = new(StringComparer.OrdinalIgnoreCase);
|
||||
private PendingOperatorGraph? _pendingOperatorGraph;
|
||||
|
||||
public List<Voice> Voices { get; } = [];
|
||||
|
|
@ -51,6 +53,7 @@ public static class PatchScript
|
|||
return new SynthPatch
|
||||
{
|
||||
Voices = Voices,
|
||||
Layers = _layers,
|
||||
OperatorGraphs = _operatorGraphs,
|
||||
Controls = _controls,
|
||||
Parameters = _parameters,
|
||||
|
|
@ -90,6 +93,10 @@ public static class PatchScript
|
|||
var name = Required(fields, "name", line);
|
||||
_templates[name] = Without(fields, "name");
|
||||
break;
|
||||
case "layer":
|
||||
FlushPendingOperatorGraph();
|
||||
AddLayer(fields, line);
|
||||
break;
|
||||
case "voice":
|
||||
FlushPendingOperatorGraph();
|
||||
Voices.Add(ParseVoice(ExpandVoiceFields(fields, line), Voices.Count, line));
|
||||
|
|
@ -130,6 +137,14 @@ public static class PatchScript
|
|||
private Dictionary<string, string> ExpandVoiceFields(Dictionary<string, string> fields, int line)
|
||||
{
|
||||
var expanded = new Dictionary<string, string>(_defaults, StringComparer.OrdinalIgnoreCase);
|
||||
if (TryGetAny(fields, ["layer"], out var layerName))
|
||||
{
|
||||
if (!_layerDefaults.TryGetValue(layerName, out var layer))
|
||||
{
|
||||
throw new PatchScriptException(line, $"unknown layer `{layerName}`");
|
||||
}
|
||||
Merge(expanded, layer);
|
||||
}
|
||||
if (TryGetAny(fields, ["use", "u"], out var templateName))
|
||||
{
|
||||
foreach (var name in templateName.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
|
||||
|
|
@ -145,6 +160,43 @@ public static class PatchScript
|
|||
return expanded;
|
||||
}
|
||||
|
||||
private void AddLayer(IReadOnlyDictionary<string, string> fields, int line)
|
||||
{
|
||||
var name = Required(fields, "name", line);
|
||||
if (_layers.Any(layer => layer.Name.Equals(name, StringComparison.OrdinalIgnoreCase)))
|
||||
{
|
||||
throw new PatchScriptException(line, $"duplicate layer `{name}`");
|
||||
}
|
||||
|
||||
var layer = new PatchLayer(
|
||||
name,
|
||||
GetAny(fields, ["engine", "e"], ""),
|
||||
TryGetAny(fields, ["min_key", "key_min", "lo"], out var minKey) ? ParseInt(minKey, line) : null,
|
||||
TryGetAny(fields, ["max_key", "key_max", "hi"], out var maxKey) ? ParseInt(maxKey, line) : null,
|
||||
GetBoundFloat(fields, line, 1, $"/layers/{_layers.Count}/gain", "gain", "g"),
|
||||
GetAny(fields, ["send", "effect", "fx"], ""));
|
||||
if (layer is { MinKey: { } min, MaxKey: { } max } && min > max)
|
||||
{
|
||||
throw new PatchScriptException(line, "layer min_key must be less than or equal to max_key");
|
||||
}
|
||||
|
||||
_layers.Add(layer);
|
||||
_layerDefaults[name] = LayerVoiceFields(layer, Without(fields, "name", "engine", "e", "min_key", "key_min", "lo", "max_key", "key_max", "hi", "send", "effect", "fx"));
|
||||
}
|
||||
|
||||
private static Dictionary<string, string> LayerVoiceFields(PatchLayer layer, IReadOnlyDictionary<string, string> fields)
|
||||
{
|
||||
var result = new Dictionary<string, string>(fields, StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["layer"] = layer.Name
|
||||
};
|
||||
if (!result.ContainsKey("gain"))
|
||||
{
|
||||
result["gain"] = F(layer.Gain);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ApplyPatch(IReadOnlyDictionary<string, string> fields, int line)
|
||||
{
|
||||
if (TryGetAny(fields, ["gain", "g"], out var gain)) Gain = ParseBoundFloat(gain, line, Gain, "/patch/gain");
|
||||
|
|
@ -230,6 +282,9 @@ public static class PatchScript
|
|||
|
||||
return new Voice
|
||||
{
|
||||
Layer = TryGetAny(fields, ["layer"], out var layerName)
|
||||
? _layers.First(layer => layer.Name.Equals(layerName, StringComparison.OrdinalIgnoreCase))
|
||||
: null,
|
||||
Oscillator = new Oscillator(
|
||||
waveform,
|
||||
frequency,
|
||||
|
|
@ -845,6 +900,7 @@ public static class PatchScript
|
|||
"p" or "patch" or "instrument" => "patch",
|
||||
"d" or "default" or "defaults" => "defaults",
|
||||
"def" or "t" or "template" => "template",
|
||||
"layer" or "kit" => "layer",
|
||||
"v" or "voice" => "voice",
|
||||
"opgraph" or "ops" or "operators" => "opgraph",
|
||||
"operator" or "op" => "operator",
|
||||
|
|
@ -959,6 +1015,9 @@ public static class PatchScript
|
|||
? parsed
|
||||
: throw new PatchScriptException(line, $"bad number `{value}`");
|
||||
|
||||
private static string F(float value) =>
|
||||
value.ToString("0.######", CultureInfo.InvariantCulture);
|
||||
|
||||
private static float PunchGain(float value) => 1 + Math.Max(0, Math.Clamp(value, -1, 1));
|
||||
|
||||
private static int ParseInt(string value, int line) =>
|
||||
|
|
|
|||
Loading…
Reference in a new issue