Add Zyn formant motion workout

This commit is contained in:
Meta Vens 2026-05-17 20:34:52 +01:00
commit 788e72e177
9 changed files with 248 additions and 15 deletions

View file

@ -261,7 +261,7 @@ public static class FaustEmitter
{
source.AppendLine($"{name}_phased = {name}_filtered;");
}
source.AppendLine($"{name}_formants = {FormantExpression(name, voice)};");
source.AppendLine($"{name}_formants = {FormantExpression(name, voice, ownerPath, parameters)};");
source.AppendLine($"{name} = (({name}_phased * (1.0 - {formantMix}) + {name}_formants * {formantMix}) * {envelope}{tremolo} * max(0.0, 1.0 + patch_mod_gain + {gain}) * {parameters.Expression(OwnerField(ownerPath, "gain"), voice.Gain)});");
source.AppendLine();
}
@ -495,11 +495,35 @@ public static class FaustEmitter
return ordered;
}
private static string FormantExpression(string name, Voice voice)
private static string FormantExpression(string name, Voice voice, string ownerPath, ParameterMap parameters)
{
if (voice.Formants.Count == 0) return $"{name}_phased";
var gainSum = Math.Max(voice.Formants.Sum(formant => Math.Abs(formant.Gain)), 0.001f);
var parts = voice.Formants.Select(formant =>
if (voice.FormantFrames.Count > 0)
{
if (voice.FormantFrames.Count == 1)
{
return StaticFormantExpression(name, voice.FormantFrames[0].Formants);
}
var frameCount = voice.FormantFrames.Count;
var rate = parameters.Expression(OwnerField(ownerPath, "color/vowel_rate"), voice.FormantFrameRateHz);
var position = $"wrap01(age * {rate}) * {F(frameCount)}";
var frames = voice.FormantFrames.Select((frame, index) =>
{
var distance = $"min(abs(({position}) - {F(index)}), {F(frameCount)} - abs(({position}) - {F(index)}))";
var weight = $"max(0.0, 1.0 - {distance})";
return $"({StaticFormantExpression(name, frame.Formants)}) * ({weight})";
});
return string.Join(" + ", frames);
}
return StaticFormantExpression(name, voice.Formants);
}
private static string StaticFormantExpression(string name, IReadOnlyList<Formant> formants)
{
if (formants.Count == 0) return $"{name}_phased";
var gainSum = Math.Max(formants.Sum(formant => Math.Abs(formant.Gain)), 0.001f);
var parts = formants.Select(formant =>
{
var q = Math.Clamp(formant.FrequencyHz / Math.Max(formant.BandwidthHz, 10), 0.2f, 40);
return $"({name}_phased : fi.resonbp({F(formant.FrequencyHz)}, {F(q)}, 1.0)) * {F(formant.Gain)}";

View file

@ -136,6 +136,8 @@ public sealed record VoiceColor(
public sealed record Formant(float FrequencyHz, float BandwidthHz, float Gain);
public sealed record FormantFrame(IReadOnlyList<Formant> Formants);
public sealed record Modulator(
ModTarget Target,
ModWaveform Waveform = ModWaveform.Sine,
@ -334,6 +336,8 @@ public sealed record Voice
public FrequencyModulation Fm { get; init; } = new();
public VoiceColor Color { get; init; } = new();
public IReadOnlyList<Formant> Formants { get; init; } = Array.Empty<Formant>();
public IReadOnlyList<FormantFrame> FormantFrames { get; init; } = Array.Empty<FormantFrame>();
public float FormantFrameRateHz { get; init; } = 0.5f;
public IReadOnlyList<Modulator> Modulators { get; init; } = Array.Empty<Modulator>();
public float Gain { get; init; } = 0.2f;
}

View file

@ -397,6 +397,9 @@ public static class PatchScript
var formants = TryGetAny(fields, ["formants", "fs"], out var formantSpec)
? ParseFormants(formantSpec, line)
: [];
var formantFrames = TryGetAny(fields, ["vowels", "vowel_frames", "formant_frames"], out var vowelSpec)
? ParseFormantFrames(vowelSpec, line)
: [];
var modulators = TryGetAny(fields, ["mods", "m"], out var mods)
? ParseVoiceModulators(mods, line)
@ -470,6 +473,8 @@ public static class PatchScript
GetBoundFloat(fields, line, 0, OwnerField(ownerPath, "color/tremolo_hz"), "tremolo_hz", "th"),
GetBoundFloat(fields, line, 0, OwnerField(ownerPath, "color/formant_mix"), "formant_mix", "fmix")),
Formants = formants,
FormantFrames = formantFrames,
FormantFrameRateHz = GetBoundFloat(fields, line, 0.5f, OwnerField(ownerPath, "color/vowel_rate"), "vowel_hz", "vowel_rate", "vowels_hz"),
Modulators = modulators,
Gain = GetBoundFloat(fields, line, 0.2f, OwnerField(ownerPath, "gain"), "gain", "g") * gainScale
};
@ -1232,6 +1237,11 @@ public static class PatchScript
})
.ToList();
private static List<FormantFrame> ParseFormantFrames(string value, int line) =>
value.Split('|', StringSplitOptions.RemoveEmptyEntries)
.Select(frame => new FormantFrame(ParseFormants(frame, line)))
.ToList();
private static List<Modulator> ParseVoiceModulators(string value, int line) =>
value.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(part =>

View file

@ -109,6 +109,13 @@ public sealed record ZynPadRebuild(
IReadOnlyList<ReferenceFeature> MatchedFeatures,
IReadOnlyList<ReferenceFeature> MissingFeatures);
public sealed record ZynFormantMotionRebuild(
string InstrumentName,
int KitItemId,
string Script,
IReadOnlyList<ReferenceFeature> MatchedFeatures,
IReadOnlyList<ReferenceFeature> MissingFeatures);
public sealed record ZynPadTableRoot(float Sample0RootFrequencyHz, float? SelectedRootFrequencyHz = null);
public enum ZynEngine
@ -164,6 +171,92 @@ public static class ZynInstrumentReader
float playbackFrequencyHz = 261.6256f) =>
RebuildEnabledPadsAsAquaSynthScript(File.ReadAllBytes(path), tableRootFrequencyHzByKitItem, playbackFrequencyHz);
public static ZynFormantMotionRebuild RebuildFirstFormantMotionAsAquaSynthScript(
string path,
float playbackFrequencyHz = 110.0f) =>
RebuildFirstFormantMotionAsAquaSynthScript(File.ReadAllBytes(path), playbackFrequencyHz);
public static ZynFormantMotionRebuild RebuildFirstFormantMotionAsAquaSynthScript(
ReadOnlySpan<byte> bytes,
float playbackFrequencyHz = 110.0f)
{
if (playbackFrequencyHz <= 0) throw new ArgumentOutOfRangeException(nameof(playbackFrequencyHz));
var document = ParseDocument(bytes);
var instrument = document.Descendants("INSTRUMENT").FirstOrDefault()
?? throw new ArgumentException("Zyn instrument XML is missing INSTRUMENT");
var name = StringValue(instrument.Element("INFO"), "name", "unnamed");
var item = instrument
.Descendants("INSTRUMENT_KIT_ITEM")
.Where(item => BoolParam(item, "enabled"))
.FirstOrDefault(item => item.Descendants("FORMANT_FILTER").Any())
?? throw new ArgumentException("Zyn instrument XML has no enabled formant filter kit item");
var formantFilter = item
.Descendants("FORMANT_FILTER")
.FirstOrDefault(filter => filter.Ancestors("FILTER_PARAMETERS")
.Elements("FILTER_LFO")
.Any(lfo => IntParam(lfo, "intensity", 0) > 0))
?? item.Descendants("FORMANT_FILTER").First();
var filterLfo = formantFilter
.Ancestors("FILTER_PARAMETERS")
.Elements("FILTER_LFO")
.FirstOrDefault(lfo => IntParam(lfo, "intensity", 0) > 0);
var frameCount = Math.Clamp(IntParam(formantFilter, "sequence_size", 1), 1, 8);
var formantCount = Math.Clamp(IntParam(formantFilter, "num_formants", 3), 1, 6);
var sequence = formantFilter
.Elements("SEQUENCE_POS")
.OrderBy(element => IntAttribute(element, "id"))
.Select(element => IntParam(element, "vowel_id", 0))
.Take(frameCount)
.ToArray();
if (sequence.Length == 0)
{
sequence = Enumerable.Range(0, frameCount).ToArray();
}
var frames = sequence
.Select(vowelId => ParseZynVowelFrame(formantFilter, vowelId, formantCount))
.Where(frame => frame.Count > 0)
.ToList();
if (frames.Count == 0)
{
throw new ArgumentException("Zyn formant filter has no vowel frame formants");
}
var safeName = SafeIdentifier(name);
var vowelText = string.Join("|", frames.Select(frame => string.Join(",", frame.Select(formant => $"{F(formant.FrequencyHz)}:{F(formant.BandwidthHz)}:{F(formant.Gain)}"))));
var vowelRate = filterLfo is null ? 0.5f : Math.Clamp(RealParam(filterLfo, "freq", 0.5f), 0.01f, 20.0f);
var engines = EnabledEnginesForReader(item).ToArray();
var engine = engines.Contains(ZynEngine.AddSynth) ? "add" :
engines.Contains(ZynEngine.SubSynth) ? "sub" :
engines.Contains(ZynEngine.PadSynth) ? "pad" : "formant";
var script = new StringBuilder();
script.AppendLine("# Generated from a ZynAddSubFX formant-motion fixture for parity pressure.");
script.AppendLine("# Scope: first enabled kit item with a FORMANT_FILTER vowel sequence.");
script.AppendLine("patch gain=0.45 soft_clip=true");
script.AppendLine($"layer name={safeName} engine={engine} gain=0.25");
script.AppendLine($"voice layer={safeName} wave=saw freq={F(playbackFrequencyHz)} gain=0.18 gate=1.8 env=rl rates=0.04,0.18,0.9,0.3 levels=1,0.9,0.75,0 formant_mix=0.7 vowel_hz={F(vowelRate)} vowels={vowelText}");
return new ZynFormantMotionRebuild(
name,
IntAttribute(item, "id"),
script.ToString(),
[
new("formant_filter_count", item.Descendants("FORMANT_FILTER").Count().ToString(CultureInfo.InvariantCulture), "Read Zyn FORMANT_FILTER blocks from the enabled kit item."),
new("formant_sequence", string.Join(",", sequence), "Lowered the Zyn vowel sequence into AquaSynth vowel frames."),
new("formant_frames", frames.Count.ToString(CultureInfo.InvariantCulture), "Each frame owns one static formant bank; Faust blends between frames at control/audio expression time."),
new("formant_frame_rate_hz", F(vowelRate), filterLfo is null ? "No active FILTER_LFO was found; the workout uses a slow audible scan." : "Seeded from the active Zyn FILTER_LFO frequency.")
],
[
new("zyn_formant_position_driver", "filter frequency to vowel position", "Zyn derives vowel position from the formant filter cutoff path; AquaSynth currently states an explicit frame rate."),
new("zyn_formant_smoothing", "formant_slowness/vowel_clearness", "AquaSynth blends adjacent frames with a simple cyclic triangular window."),
new("zyn_add_voice_detail", "full ADD voice oscillator/FM stack", "This workout keeps the source deliberately plain so the formant-motion authority is the thing under test.")
]);
}
public static ZynPadRebuild RebuildFirstPadAsAquaSynthScript(
ReadOnlySpan<byte> bytes,
float tableRootFrequencyHz,
@ -473,6 +566,13 @@ public static class ZynInstrumentReader
private static bool IsEnabledPadItem(XElement item) =>
BoolParam(item, "enabled") && EngineEnabled(item, "pad_enabled", "PAD_SYNTH_PARAMETERS");
private static IEnumerable<ZynEngine> EnabledEnginesForReader(XElement item)
{
if (EngineEnabled(item, "add_enabled", "ADD_SYNTH_PARAMETERS")) yield return ZynEngine.AddSynth;
if (EngineEnabled(item, "sub_enabled", "SUB_SYNTH_PARAMETERS")) yield return ZynEngine.SubSynth;
if (EngineEnabled(item, "pad_enabled", "PAD_SYNTH_PARAMETERS")) yield return ZynEngine.PadSynth;
}
private static string StringValue(XElement? root, string name, string fallback) =>
root?.Elements("string")
.FirstOrDefault(element => AttributeValue(element, "name") == name)
@ -1433,6 +1533,43 @@ public static class ZynInstrumentReader
return int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsed) ? parsed : fallback;
}
private static float RealParam(XElement? root, string name, float fallback)
{
var value = root?.Elements("par_real")
.FirstOrDefault(element => AttributeValue(element, "name") == name)
?.Attribute("value")
?.Value;
return float.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var parsed) ? parsed : fallback;
}
private static IReadOnlyList<Formant> ParseZynVowelFrame(XElement formantFilter, int vowelId, int formantCount)
{
var center = IntParam(formantFilter, "center_freq", 64);
var octaves = IntParam(formantFilter, "octaves_freq", 64);
return formantFilter
.Elements("VOWEL")
.FirstOrDefault(vowel => IntAttribute(vowel, "id") == vowelId)
?.Elements("FORMANT")
.OrderBy(formant => IntAttribute(formant, "id"))
.Take(formantCount)
.Select(formant =>
{
var frequency = ZynFormantFrequencyHz(IntParam(formant, "freq", 64), center, octaves);
var q = Math.Clamp(MathF.Pow(25.0f, (IntParam(formant, "q", 64) - 32.0f) / 64.0f), 0.2f, 40.0f);
var gain = MathF.Pow(0.1f, (1.0f - IntParam(formant, "amp", 127) / 127.0f) * 4.0f);
return new Formant(frequency, Math.Max(10.0f, frequency / q), gain);
})
.ToArray() ?? [];
}
private static float ZynFormantFrequencyHz(int value, int center, int octaves)
{
var centerHz = 10000.0f * MathF.Pow(10.0f, -(1.0f - center / 127.0f) * 2.0f);
var octaveSpan = 0.25f + 10.0f * octaves / 127.0f;
var octaveFactor = MathF.Pow(2.0f, octaveSpan);
return centerHz / MathF.Sqrt(octaveFactor) * MathF.Pow(octaveFactor, Math.Clamp(value, 0, 127) / 127.0f);
}
private static float EnvelopeTime(XElement? envelope, string name, float fallback)
{
var value = IntParam(envelope, name, -1);