Add curved staged envelopes
This commit is contained in:
parent
d7bdb8995c
commit
ae152fb490
12 changed files with 242 additions and 38 deletions
|
|
@ -51,11 +51,21 @@ public sealed record Dx7RateLevelEnvelopeApproximation(
|
|||
float GateSeconds,
|
||||
string Notes)
|
||||
{
|
||||
public string ToScriptSpec() =>
|
||||
$"env=rl rates={F(Envelope.Rate1Seconds)},{F(Envelope.Rate2Seconds)},{F(Envelope.Rate3Seconds)},{F(Envelope.Rate4Seconds)} " +
|
||||
$"levels={F(Envelope.Level1)},{F(Envelope.Level2)},{F(Envelope.Level3)},{F(Envelope.Level4)} gate={F(GateSeconds)}";
|
||||
public string ToScriptSpec()
|
||||
{
|
||||
var curves = Envelope.Curve1 == RateLevelCurve.Linear &&
|
||||
Envelope.Curve2 == RateLevelCurve.Linear &&
|
||||
Envelope.Curve3 == RateLevelCurve.Linear &&
|
||||
Envelope.Curve4 == RateLevelCurve.Linear
|
||||
? ""
|
||||
: $" curves={Curve(Envelope.Curve1)},{Curve(Envelope.Curve2)},{Curve(Envelope.Curve3)},{Curve(Envelope.Curve4)}";
|
||||
return $"env=rl rates={F(Envelope.Rate1Seconds)},{F(Envelope.Rate2Seconds)},{F(Envelope.Rate3Seconds)},{F(Envelope.Rate4Seconds)} " +
|
||||
$"levels={F(Envelope.Level1)},{F(Envelope.Level2)},{F(Envelope.Level3)},{F(Envelope.Level4)}{curves} gate={F(GateSeconds)}";
|
||||
}
|
||||
|
||||
private static string F(float value) => value.ToString("0.######", System.Globalization.CultureInfo.InvariantCulture);
|
||||
|
||||
private static string Curve(RateLevelCurve curve) => curve == RateLevelCurve.Exponential ? "exp" : "lin";
|
||||
}
|
||||
|
||||
public sealed record Dx7EnvelopeTracePoint(float TimeSeconds, float Gain, int Stage);
|
||||
|
|
@ -289,6 +299,41 @@ public static class Dx7SysEx
|
|||
return points;
|
||||
}
|
||||
|
||||
public static IReadOnlyList<Dx7EnvelopeTracePoint> TraceInterpolatedEnvelope(
|
||||
Dx7Envelope envelope,
|
||||
float gateSeconds,
|
||||
float durationSeconds,
|
||||
int outputLevel = 99,
|
||||
int sampleRate = 44100,
|
||||
int blockSize = 64)
|
||||
{
|
||||
var state = Dx7EnvelopeState.Start(envelope, outputLevel, sampleRate);
|
||||
var sampleCount = Math.Max(1, (int)MathF.Round(Math.Max(1f / sampleRate, durationSeconds) * sampleRate));
|
||||
var gateSample = Math.Clamp((int)MathF.Round(Math.Max(0, gateSeconds) * sampleRate), 0, sampleCount);
|
||||
var points = new List<Dx7EnvelopeTracePoint>(sampleCount);
|
||||
var previousGain = 0f;
|
||||
|
||||
for (var blockStart = 0; blockStart < sampleCount; blockStart += Math.Max(1, blockSize))
|
||||
{
|
||||
if (blockStart >= gateSample)
|
||||
{
|
||||
state.KeyDown(envelope, down: false);
|
||||
}
|
||||
state.Advance(envelope);
|
||||
var currentGain = state.Gain;
|
||||
var count = Math.Min(blockSize, sampleCount - blockStart);
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var t = (i + 1) / (float)blockSize;
|
||||
var gain = previousGain + (currentGain - previousGain) * t;
|
||||
points.Add(new Dx7EnvelopeTracePoint((blockStart + i) / (float)sampleRate, gain, state.Stage));
|
||||
}
|
||||
previousGain = currentGain;
|
||||
}
|
||||
|
||||
return points;
|
||||
}
|
||||
|
||||
public static Dx7VoiceBank ParseBank(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var data = PackedBankData(bytes);
|
||||
|
|
|
|||
|
|
@ -58,8 +58,10 @@ public static class FaustEmitter
|
|||
source.AppendLine("release_start(a,d,g) = max(g, a + d);");
|
||||
source.AppendLine("oneshot_adsr(a,d,s,r,g) = select2(age < a, select2(age < a + d, select2(age < release_start(a,d,g), select2(age < release_start(a,d,g) + r, 0.0, s * (1.0 - (age - release_start(a,d,g)) / max(0.0001, r))), s), 1.0 - (1.0 - s) * ((age - a) / max(0.0001, d))), age / max(0.0001, a));");
|
||||
source.AppendLine("seg(t,t0,d,a,b) = a + (b - a) * clip01((t - t0) / max(0.0001, d));");
|
||||
source.AppendLine("seg_exp(t,t0,d,a,b) = exp(log(max(0.00001, a)) + (log(max(0.00001, b)) - log(max(0.00001, a))) * clip01((t - t0) / max(0.0001, d)));");
|
||||
source.AppendLine("seg_curve(c,t,t0,d,a,b) = select2(c < 0.5, seg_exp(t,t0,d,a,b), seg(t,t0,d,a,b));");
|
||||
source.AppendLine("rl_release_start(r1,r2,r3,g) = max(g, r1 + r2 + r3);");
|
||||
source.AppendLine("rl4_env(r1,l1,r2,l2,r3,l3,r4,l4,g) = select2(age < r1, select2(age < r1 + r2, select2(age < r1 + r2 + r3, select2(age < rl_release_start(r1,r2,r3,g), select2(age < rl_release_start(r1,r2,r3,g) + r4, l4, seg(age, rl_release_start(r1,r2,r3,g), r4, l3, l4)), l3), seg(age, r1 + r2, r3, l2, l3)), seg(age, r1, r2, l1, l2)), seg(age, 0, r1, 0, l1));");
|
||||
source.AppendLine("rl4_env(r1,l1,c1,r2,l2,c2,r3,l3,c3,r4,l4,c4,g) = select2(age < r1, select2(age < r1 + r2, select2(age < r1 + r2 + r3, select2(age < rl_release_start(r1,r2,r3,g), select2(age < rl_release_start(r1,r2,r3,g) + r4, l4, seg_curve(c4, age, rl_release_start(r1,r2,r3,g), r4, l3, l4)), l3), seg_curve(c3, age, r1 + r2, r3, l2, l3)), seg_curve(c2, age, r1, r2, l1, l2)), seg_curve(c1, age, 0, r1, 0, l1));");
|
||||
source.AppendLine("lfo_sin(hz, phase) = sin(2.0 * ma.PI * (age * hz + phase));");
|
||||
source.AppendLine("lfo_tri(hz, phase) = 1.0 - 4.0 * abs((age * hz + phase - floor(age * hz + phase)) - 0.5);");
|
||||
source.AppendLine("lfo_sq(hz, phase) = select2((age * hz + phase - floor(age * hz + phase)) < 0.5, -1.0, 1.0);");
|
||||
|
|
@ -283,7 +285,10 @@ public static class FaustEmitter
|
|||
}
|
||||
|
||||
private static string RateLevelEnvelopeExpression(RateLevelEnvelope envelope, string gate) =>
|
||||
$"rl4_env({F(envelope.Rate1Seconds)}, {F(envelope.Level1)}, {F(envelope.Rate2Seconds)}, {F(envelope.Level2)}, {F(envelope.Rate3Seconds)}, {F(envelope.Level3)}, {F(envelope.Rate4Seconds)}, {F(envelope.Level4)}, {gate})";
|
||||
$"rl4_env({F(envelope.Rate1Seconds)}, {F(envelope.Level1)}, {Curve(envelope.Curve1)}, {F(envelope.Rate2Seconds)}, {F(envelope.Level2)}, {Curve(envelope.Curve2)}, {F(envelope.Rate3Seconds)}, {F(envelope.Level3)}, {Curve(envelope.Curve3)}, {F(envelope.Rate4Seconds)}, {F(envelope.Level4)}, {Curve(envelope.Curve4)}, {gate})";
|
||||
|
||||
private static string Curve(RateLevelCurve curve) =>
|
||||
curve == RateLevelCurve.Exponential ? "1" : "0";
|
||||
|
||||
private static string OscillatorExpression(SynthPatch patch, Voice voice, int voiceIndex, string frequency, string duty, string fmIndex, ParameterMap parameters)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -40,6 +40,12 @@ public sealed record Envelope(
|
|||
public float DurationSeconds => AttackSeconds + DecaySeconds + ReleaseSeconds;
|
||||
}
|
||||
|
||||
public enum RateLevelCurve
|
||||
{
|
||||
Linear,
|
||||
Exponential
|
||||
}
|
||||
|
||||
public sealed record RateLevelEnvelope(
|
||||
float Rate1Seconds,
|
||||
float Level1,
|
||||
|
|
@ -48,7 +54,11 @@ public sealed record RateLevelEnvelope(
|
|||
float Rate3Seconds,
|
||||
float Level3,
|
||||
float Rate4Seconds,
|
||||
float Level4);
|
||||
float Level4,
|
||||
RateLevelCurve Curve1 = RateLevelCurve.Linear,
|
||||
RateLevelCurve Curve2 = RateLevelCurve.Linear,
|
||||
RateLevelCurve Curve3 = RateLevelCurve.Linear,
|
||||
RateLevelCurve Curve4 = RateLevelCurve.Linear);
|
||||
|
||||
public enum NoteSource
|
||||
{
|
||||
|
|
|
|||
|
|
@ -635,11 +635,20 @@ public static class PatchScript
|
|||
|
||||
private ParsedEnvelope RateLevelParsedEnvelope(IReadOnlyList<float> rates, IReadOnlyList<float> levels, IReadOnlyDictionary<string, string> fields, int line, string fieldPath)
|
||||
{
|
||||
var curves = TryGetAny(fields, ["curves", "curve"], out var curveText)
|
||||
? ParseCurveList(curveText, line)
|
||||
: [RateLevelCurve.Linear, RateLevelCurve.Linear, RateLevelCurve.Linear, RateLevelCurve.Linear];
|
||||
if (curves.Count != 4)
|
||||
{
|
||||
throw new PatchScriptException(line, "rate/level envelope needs four curves");
|
||||
}
|
||||
|
||||
var envelope = new RateLevelEnvelope(
|
||||
Math.Max(0, rates[0]), Math.Clamp(levels[0], 0, 1.5f),
|
||||
Math.Max(0, rates[1]), Math.Clamp(levels[1], 0, 1.5f),
|
||||
Math.Max(0, rates[2]), Math.Clamp(levels[2], 0, 1.5f),
|
||||
Math.Max(0, rates[3]), Math.Clamp(levels[3], 0, 1.5f));
|
||||
Math.Max(0, rates[0]), Math.Clamp(levels[0], 0, 4f),
|
||||
Math.Max(0, rates[1]), Math.Clamp(levels[1], 0, 4f),
|
||||
Math.Max(0, rates[2]), Math.Clamp(levels[2], 0, 4f),
|
||||
Math.Max(0, rates[3]), Math.Clamp(levels[3], 0, 4f),
|
||||
curves[0], curves[1], curves[2], curves[3]);
|
||||
var defaultGate = Math.Max(envelope.Rate1Seconds + envelope.Rate2Seconds + envelope.Rate3Seconds, 0.02f);
|
||||
return new ParsedEnvelope(
|
||||
new Envelope(envelope.Rate1Seconds, envelope.Rate2Seconds + envelope.Rate3Seconds, envelope.Level3, envelope.Rate4Seconds),
|
||||
|
|
@ -647,6 +656,22 @@ public static class PatchScript
|
|||
envelope);
|
||||
}
|
||||
|
||||
private static IReadOnlyList<RateLevelCurve> ParseCurveList(string value, int line)
|
||||
{
|
||||
var parts = value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
|
||||
if (parts.Length == 0)
|
||||
{
|
||||
throw new PatchScriptException(line, "curves list cannot be empty");
|
||||
}
|
||||
|
||||
return parts.Select(part => part.ToLowerInvariant() switch
|
||||
{
|
||||
"lin" or "linear" => RateLevelCurve.Linear,
|
||||
"exp" or "exponential" => RateLevelCurve.Exponential,
|
||||
_ => throw new PatchScriptException(line, $"unknown rate/level curve `{part}`")
|
||||
}).ToArray();
|
||||
}
|
||||
|
||||
private float GateSeconds(IReadOnlyDictionary<string, string> fields, int line, float defaultValue, string fieldPath) =>
|
||||
GetBoundFloat(fields, line, defaultValue, $"{fieldPath}/note/gate", "gate", "hold", "duration");
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue