Add DX7 SysEx reference parsing
This commit is contained in:
parent
9d902be606
commit
10c42080be
6 changed files with 555 additions and 10 deletions
325
src/AquariumSynth.Dsl/Dx7SysEx.cs
Normal file
325
src/AquariumSynth.Dsl/Dx7SysEx.cs
Normal file
|
|
@ -0,0 +1,325 @@
|
|||
using System.Security.Cryptography;
|
||||
|
||||
namespace AquariumSynth.Dsl;
|
||||
|
||||
public enum Dx7FrequencyMode
|
||||
{
|
||||
Ratio,
|
||||
Fixed
|
||||
}
|
||||
|
||||
public enum Dx7LfoWaveform
|
||||
{
|
||||
Triangle,
|
||||
SawDown,
|
||||
SawUp,
|
||||
Square,
|
||||
Sine,
|
||||
SampleHold
|
||||
}
|
||||
|
||||
public sealed record Dx7Envelope(
|
||||
int Rate1,
|
||||
int Rate2,
|
||||
int Rate3,
|
||||
int Rate4,
|
||||
int Level1,
|
||||
int Level2,
|
||||
int Level3,
|
||||
int Level4);
|
||||
|
||||
public sealed record Dx7Operator(
|
||||
int Number,
|
||||
Dx7Envelope Envelope,
|
||||
int BreakPoint,
|
||||
int LeftDepth,
|
||||
int RightDepth,
|
||||
int LeftCurve,
|
||||
int RightCurve,
|
||||
int RateScaling,
|
||||
int AmplitudeModulationSensitivity,
|
||||
int KeyVelocitySensitivity,
|
||||
int OutputLevel,
|
||||
Dx7FrequencyMode FrequencyMode,
|
||||
int FrequencyCoarse,
|
||||
int FrequencyFine,
|
||||
int Detune);
|
||||
|
||||
public sealed record Dx7PitchEnvelope(Dx7Envelope Envelope);
|
||||
|
||||
public sealed record Dx7Lfo(
|
||||
int Speed,
|
||||
int Delay,
|
||||
int PitchModulationDepth,
|
||||
int AmplitudeModulationDepth,
|
||||
bool KeySync,
|
||||
Dx7LfoWaveform Waveform,
|
||||
int PitchModulationSensitivity);
|
||||
|
||||
public sealed record Dx7Voice(
|
||||
string Name,
|
||||
IReadOnlyList<Dx7Operator> Operators,
|
||||
Dx7PitchEnvelope PitchEnvelope,
|
||||
int Algorithm,
|
||||
int Feedback,
|
||||
bool OscillatorSync,
|
||||
Dx7Lfo Lfo,
|
||||
int Transpose)
|
||||
{
|
||||
public ReferencePatch ToReferencePatch(string id, ReferenceSource source) =>
|
||||
new(
|
||||
id,
|
||||
"dx7",
|
||||
Name,
|
||||
source,
|
||||
Features(),
|
||||
Array.Empty<PatchParameter>());
|
||||
|
||||
public IReadOnlyList<ReferenceFeature> Features()
|
||||
{
|
||||
var enabledOperators = Operators.Count(op => op.OutputLevel > 0);
|
||||
var loudest = Operators.OrderByDescending(op => op.OutputLevel).First();
|
||||
return
|
||||
[
|
||||
new("operator_count", Operators.Count.ToString(), "DX7 voices use six operators."),
|
||||
new("enabled_operator_count", enabledOperators.ToString(), "Operators with non-zero output level."),
|
||||
new("algorithm", Algorithm.ToString(), "DX7 algorithm number, 1-32."),
|
||||
new("feedback", Feedback.ToString(), "Global DX7 feedback level, 0-7."),
|
||||
new("oscillator_sync", OscillatorSync ? "on" : "off"),
|
||||
new("lfo_waveform", Lfo.Waveform.ToString()),
|
||||
new("lfo_pitch_mod_depth", Lfo.PitchModulationDepth.ToString()),
|
||||
new("lfo_amp_mod_depth", Lfo.AmplitudeModulationDepth.ToString()),
|
||||
new("loudest_operator", loudest.Number.ToString(), $"Output level {loudest.OutputLevel}.")
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record Dx7VoiceBank(IReadOnlyList<Dx7Voice> Voices);
|
||||
|
||||
public static class Dx7SysEx
|
||||
{
|
||||
public const int VoiceEditBufferLength = 155;
|
||||
public const int PackedVoiceLength = 128;
|
||||
public const int PackedBankVoiceCount = 32;
|
||||
public const int PackedBankDataLength = PackedVoiceLength * PackedBankVoiceCount;
|
||||
|
||||
public static Dx7Voice ParseVoice(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var data = VoiceEditBuffer(bytes);
|
||||
return ParseVoiceEditBuffer(data);
|
||||
}
|
||||
|
||||
public static Dx7VoiceBank ParseBank(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var data = PackedBankData(bytes);
|
||||
var voices = new List<Dx7Voice>(PackedBankVoiceCount);
|
||||
for (var i = 0; i < PackedBankVoiceCount; i++)
|
||||
{
|
||||
var packed = data.AsSpan(i * PackedVoiceLength, PackedVoiceLength);
|
||||
voices.Add(ParseVoiceEditBuffer(UnpackVoice(packed)));
|
||||
}
|
||||
|
||||
return new Dx7VoiceBank(voices);
|
||||
}
|
||||
|
||||
public static ReferenceSource SourceForBytes(string uri, string license, ReadOnlySpan<byte> bytes, string notes = "") =>
|
||||
new("dx7-sysex", uri, license, Convert.ToHexString(SHA256.HashData(bytes)).ToLowerInvariant(), notes);
|
||||
|
||||
public static byte[] UnpackVoice(ReadOnlySpan<byte> packed)
|
||||
{
|
||||
if (packed.Length != PackedVoiceLength)
|
||||
{
|
||||
throw new ArgumentException($"packed DX7 voice must be {PackedVoiceLength} bytes", nameof(packed));
|
||||
}
|
||||
|
||||
var voice = new byte[VoiceEditBufferLength];
|
||||
for (var op = 0; op < 6; op++)
|
||||
{
|
||||
var packedOffset = op * 17;
|
||||
var editOffset = op * 21;
|
||||
for (var i = 0; i <= 10; i++) voice[editOffset + i] = SevenBit(packed[packedOffset + i]);
|
||||
|
||||
var curves = SevenBit(packed[packedOffset + 11]);
|
||||
voice[editOffset + 11] = (byte)(curves & 0x03);
|
||||
voice[editOffset + 12] = (byte)((curves >> 2) & 0x03);
|
||||
|
||||
var rateScalingAndDetune = SevenBit(packed[packedOffset + 12]);
|
||||
voice[editOffset + 13] = (byte)(rateScalingAndDetune & 0x07);
|
||||
voice[editOffset + 20] = (byte)((rateScalingAndDetune >> 3) & 0x0F);
|
||||
|
||||
var sensitivities = SevenBit(packed[packedOffset + 13]);
|
||||
voice[editOffset + 14] = (byte)(sensitivities & 0x03);
|
||||
voice[editOffset + 15] = (byte)((sensitivities >> 2) & 0x07);
|
||||
|
||||
voice[editOffset + 16] = SevenBit(packed[packedOffset + 14]);
|
||||
|
||||
var frequency = SevenBit(packed[packedOffset + 15]);
|
||||
voice[editOffset + 17] = (byte)(frequency & 0x01);
|
||||
voice[editOffset + 18] = (byte)((frequency >> 1) & 0x1F);
|
||||
voice[editOffset + 19] = SevenBit(packed[packedOffset + 16]);
|
||||
}
|
||||
|
||||
for (var i = 0; i < 8; i++) voice[126 + i] = SevenBit(packed[102 + i]);
|
||||
voice[134] = (byte)(SevenBit(packed[110]) & 0x1F);
|
||||
|
||||
var feedbackAndSync = SevenBit(packed[111]);
|
||||
voice[135] = (byte)(feedbackAndSync & 0x07);
|
||||
voice[136] = (byte)((feedbackAndSync >> 3) & 0x01);
|
||||
|
||||
voice[137] = SevenBit(packed[112]);
|
||||
voice[138] = SevenBit(packed[113]);
|
||||
voice[139] = SevenBit(packed[114]);
|
||||
voice[140] = SevenBit(packed[115]);
|
||||
|
||||
var lfo = SevenBit(packed[116]);
|
||||
voice[141] = (byte)(lfo & 0x01);
|
||||
voice[142] = (byte)((lfo >> 1) & 0x07);
|
||||
voice[143] = (byte)((lfo >> 4) & 0x07);
|
||||
voice[144] = SevenBit(packed[117]);
|
||||
|
||||
for (var i = 0; i < 10; i++) voice[145 + i] = SevenBit(packed[118 + i]);
|
||||
return voice;
|
||||
}
|
||||
|
||||
private static Dx7Voice ParseVoiceEditBuffer(ReadOnlySpan<byte> data)
|
||||
{
|
||||
if (data.Length != VoiceEditBufferLength)
|
||||
{
|
||||
throw new ArgumentException($"DX7 voice edit buffer must be {VoiceEditBufferLength} bytes", nameof(data));
|
||||
}
|
||||
|
||||
var operators = new List<Dx7Operator>(6);
|
||||
for (var op = 0; op < 6; op++)
|
||||
{
|
||||
var offset = op * 21;
|
||||
operators.Add(new Dx7Operator(
|
||||
6 - op,
|
||||
new Dx7Envelope(
|
||||
Value(data, offset + 0),
|
||||
Value(data, offset + 1),
|
||||
Value(data, offset + 2),
|
||||
Value(data, offset + 3),
|
||||
Value(data, offset + 4),
|
||||
Value(data, offset + 5),
|
||||
Value(data, offset + 6),
|
||||
Value(data, offset + 7)),
|
||||
Value(data, offset + 8),
|
||||
Value(data, offset + 9),
|
||||
Value(data, offset + 10),
|
||||
Value(data, offset + 11),
|
||||
Value(data, offset + 12),
|
||||
Value(data, offset + 13),
|
||||
Value(data, offset + 14),
|
||||
Value(data, offset + 15),
|
||||
Value(data, offset + 16),
|
||||
Value(data, offset + 17) == 0 ? Dx7FrequencyMode.Ratio : Dx7FrequencyMode.Fixed,
|
||||
Value(data, offset + 18),
|
||||
Value(data, offset + 19),
|
||||
Value(data, offset + 20)));
|
||||
}
|
||||
|
||||
var pitchEnvelope = new Dx7PitchEnvelope(new Dx7Envelope(
|
||||
Value(data, 126),
|
||||
Value(data, 127),
|
||||
Value(data, 128),
|
||||
Value(data, 129),
|
||||
Value(data, 130),
|
||||
Value(data, 131),
|
||||
Value(data, 132),
|
||||
Value(data, 133)));
|
||||
|
||||
return new Dx7Voice(
|
||||
VoiceName(data.Slice(145, 10)),
|
||||
operators,
|
||||
pitchEnvelope,
|
||||
Value(data, 134) + 1,
|
||||
Value(data, 135),
|
||||
Value(data, 136) != 0,
|
||||
new Dx7Lfo(
|
||||
Value(data, 137),
|
||||
Value(data, 138),
|
||||
Value(data, 139),
|
||||
Value(data, 140),
|
||||
Value(data, 141) != 0,
|
||||
ParseLfoWaveform(Value(data, 142)),
|
||||
Value(data, 143)),
|
||||
Value(data, 144));
|
||||
}
|
||||
|
||||
private static byte[] VoiceEditBuffer(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
if (bytes.Length == VoiceEditBufferLength) return bytes.ToArray();
|
||||
|
||||
if (bytes.Length == VoiceEditBufferLength + 8 &&
|
||||
bytes[0] == 0xF0 &&
|
||||
bytes[1] == 0x43 &&
|
||||
bytes[3] == 0x00 &&
|
||||
bytes[4] == 0x01 &&
|
||||
bytes[5] == 0x1B &&
|
||||
bytes[^1] == 0xF7)
|
||||
{
|
||||
VerifyChecksum(bytes.Slice(6, VoiceEditBufferLength), bytes[^2]);
|
||||
return bytes.Slice(6, VoiceEditBufferLength).ToArray();
|
||||
}
|
||||
|
||||
if (bytes.Length == PackedVoiceLength) return UnpackVoice(bytes);
|
||||
|
||||
throw new ArgumentException($"unsupported DX7 voice payload length {bytes.Length}", nameof(bytes));
|
||||
}
|
||||
|
||||
private static byte[] PackedBankData(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
if (bytes.Length == PackedBankDataLength) return bytes.ToArray();
|
||||
|
||||
if (bytes.Length == PackedBankDataLength + 8 &&
|
||||
bytes[0] == 0xF0 &&
|
||||
bytes[1] == 0x43 &&
|
||||
bytes[3] == 0x09 &&
|
||||
bytes[4] == 0x20 &&
|
||||
bytes[5] == 0x00 &&
|
||||
bytes[^1] == 0xF7)
|
||||
{
|
||||
VerifyChecksum(bytes.Slice(6, PackedBankDataLength), bytes[^2]);
|
||||
return bytes.Slice(6, PackedBankDataLength).ToArray();
|
||||
}
|
||||
|
||||
throw new ArgumentException($"unsupported DX7 bank payload length {bytes.Length}", nameof(bytes));
|
||||
}
|
||||
|
||||
private static void VerifyChecksum(ReadOnlySpan<byte> data, byte checksum)
|
||||
{
|
||||
var sum = 0;
|
||||
foreach (var value in data) sum = (sum + SevenBit(value)) & 0x7F;
|
||||
if (((sum + SevenBit(checksum)) & 0x7F) != 0)
|
||||
{
|
||||
throw new ArgumentException("DX7 SysEx checksum is invalid");
|
||||
}
|
||||
}
|
||||
|
||||
private static Dx7LfoWaveform ParseLfoWaveform(int value) => value switch
|
||||
{
|
||||
0 => Dx7LfoWaveform.Triangle,
|
||||
1 => Dx7LfoWaveform.SawDown,
|
||||
2 => Dx7LfoWaveform.SawUp,
|
||||
3 => Dx7LfoWaveform.Square,
|
||||
4 => Dx7LfoWaveform.Sine,
|
||||
5 => Dx7LfoWaveform.SampleHold,
|
||||
_ => Dx7LfoWaveform.Triangle
|
||||
};
|
||||
|
||||
private static string VoiceName(ReadOnlySpan<byte> bytes)
|
||||
{
|
||||
var chars = new char[bytes.Length];
|
||||
for (var i = 0; i < bytes.Length; i++)
|
||||
{
|
||||
var value = SevenBit(bytes[i]);
|
||||
chars[i] = value is >= 32 and <= 126 ? (char)value : ' ';
|
||||
}
|
||||
|
||||
return new string(chars).Trim();
|
||||
}
|
||||
|
||||
private static int Value(ReadOnlySpan<byte> data, int index) => SevenBit(data[index]);
|
||||
|
||||
private static byte SevenBit(byte value) => (byte)(value & 0x7F);
|
||||
}
|
||||
|
|
@ -10,3 +10,4 @@
|
|||
{"kind":"package_boundary","lesson":"Aquarium consumes AquariumSynth.Dsl through a pinned package, not a live project reference. Breaking changes in AquariumSynthCSharp are allowed during synth construction as long as a new package version is packed and Aquarium is updated deliberately when the boundary should move.","source":"user wanted Aquarium builds insulated from synth-agent work"}
|
||||
{"kind":"runtime_patch_parameters","lesson":"Aquarium patch scripts now have first-class `param` declarations and patch/reference model contracts. Faust emission exposes declared parameters as smoothed hslider controls, but target binding is intentionally still absent and warned about; do not claim parameters shape sound until binding is designed.","source":"parameter contract implementation on 2026-05-13"}
|
||||
{"kind":"runtime_patch_parameters","lesson":"Aquarium parameter binding uses Option B: numeric field values may reference declared parameters with `@/path`, producing exact field bindings such as `/voices/0/filter/lpf`. The field owns the meaning; the parameter owns the runtime value. Avoid target-routing parameter semantics unless a new authority is deliberately mapped.","source":"user rejected target binding as a landmine; field-site binding implemented on 2026-05-13"}
|
||||
{"kind":"dx7_reference_path","lesson":"DX7 reference elaboration starts with structural extraction, not translation. The repo now parses 155-byte voice edit buffers, 128-byte packed voices, and 4104-byte packed 32-voice SysEx banks into neutral DX7 records with checksum validation and ReferencePatch features. Next pressure point is explicit algorithm topology metadata for carriers, modulation edges, and feedback edge location.","source":"DX7 SysEx parser slice on 2026-05-13"}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@
|
|||
"patch graph model",
|
||||
"patch parameter contracts",
|
||||
"reference patch contracts",
|
||||
"DX7 SysEx structural parsing",
|
||||
"patch script parser",
|
||||
"SFXR atoms and parameter mapping",
|
||||
"analysis/comparison metrics",
|
||||
|
|
@ -43,6 +44,10 @@
|
|||
],
|
||||
"rule": "Add DSL abstractions only when reference rebuilds prove the missing invariant."
|
||||
},
|
||||
"dx7_reference_path": {
|
||||
"status": "DX7 SysEx parser extracts voice edit buffers, packed voices, packed 32-voice banks, six operators, envelopes, algorithm, feedback, oscillator sync, LFO, transpose, voice name, checksum validation, and ReferencePatch features.",
|
||||
"next": "Add algorithm topology metadata for algorithms 1-32 before translating DX7 voices into Aquarium DSL."
|
||||
},
|
||||
"runtime_parameters": {
|
||||
"rule": "Patch parameters are runtime controls, not compile-time constants.",
|
||||
"faust_surface": "Faust UI controls expose parameter zones that hosts can update with setParamValue/getParamValue-style APIs.",
|
||||
|
|
@ -51,7 +56,7 @@
|
|||
},
|
||||
"last_verified": {
|
||||
"command": "dotnet test AquariumSynthCSharp.slnx --no-restore",
|
||||
"result": "23 passed",
|
||||
"commit": "pending field-site parameter binding commit"
|
||||
"result": "28 passed",
|
||||
"commit": "pending DX7 SysEx reference parsing commit"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,17 @@ the synth library while Aquarium work continues elsewhere.
|
|||
|
||||
Completed this slice:
|
||||
|
||||
- Added `Dx7SysEx` records and parsing for DX7 voice edit buffers, packed
|
||||
128-byte voices, and packed 32-voice SysEx banks.
|
||||
- Extracted six operators, envelopes, algorithm, feedback, oscillator sync,
|
||||
pitch envelope, LFO, transpose, and voice name into neutral DX7 records.
|
||||
- Added `Dx7Voice.ToReferencePatch` and structural feature extraction so DX7
|
||||
voices can pressure the reference model before translation exists.
|
||||
- Checksum validation rejects bad wrapped SysEx payloads.
|
||||
- Verified with `dotnet test AquariumSynthCSharp.slnx --no-restore`: 28 passed.
|
||||
|
||||
Previous slice:
|
||||
|
||||
- Added explicit `PatchParameter` contracts: stable path, label, default, min,
|
||||
max, step, unit, automation-rate notes, and notes.
|
||||
- Added neutral `ReferencePatch`, `ReferenceSource`, and `ReferenceFeature`
|
||||
|
|
@ -24,9 +35,9 @@ Completed this slice:
|
|||
|
||||
Next likely slice:
|
||||
|
||||
- Implement a DX7 SysEx parser skeleton for single voices and cartridge payloads.
|
||||
- Extract operator topology, envelopes, ratios, feedback, levels, LFO, and pitch
|
||||
envelope into feature records before trying to translate.
|
||||
- Add explicit DX7 algorithm topology metadata: carrier operators, modulator
|
||||
edges, and feedback edge location for algorithms 1-32. Keep it as reference
|
||||
topology data, not Aquarium graph translation yet.
|
||||
- Rebuild one or two small DX7-style sounds in Aquarium DSL.
|
||||
- Keep tests focused on structure first, then add rendered audio comparison once
|
||||
the render path is explicit.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
project: AquariumSynthCSharp
|
||||
spine_version: 1
|
||||
updated: 2026-05-13T00:37:07+01:00
|
||||
updated: 2026-05-13T00:43:44+01:00
|
||||
|
||||
prime_directive: >
|
||||
Build coherent synth machinery. References exist to reveal missing structure,
|
||||
|
|
@ -17,6 +17,7 @@ current_mechanism:
|
|||
faust_emitter: src/AquariumSynth.Dsl/FaustExport.cs
|
||||
analysis: src/AquariumSynth.Dsl/AudioAnalysis.cs
|
||||
built_ins: src/AquariumSynth.Dsl/BuiltInScripts.cs
|
||||
dx7_references: src/AquariumSynth.Dsl/Dx7SysEx.cs
|
||||
tests: tests/AquariumSynth.Dsl.Tests/PatchScriptTests.cs
|
||||
doctrine: docs/audio-doctrine.md
|
||||
roadmap: docs/reference-synth-roadmap.md
|
||||
|
|
@ -40,9 +41,9 @@ reference_targets:
|
|||
priority: 1
|
||||
purpose: "Structured FM/operator graph pressure test."
|
||||
source: "https://github.com/asb2m10/dexed"
|
||||
status: "DX7 SysEx parser and structural feature extraction exist; topology edge maps are next."
|
||||
first_slice:
|
||||
- "Parse DX7 SysEx single voice and cartridge payloads."
|
||||
- "Extract six-operator topology, envelopes, ratios, feedback, levels, LFO, pitch envelope."
|
||||
- "Add algorithm topology metadata for carrier operators, modulator edges, and feedback edge location."
|
||||
- "Rebuild two small DX7-style references in Aquarium DSL."
|
||||
zynaddsubfx:
|
||||
priority: 2
|
||||
|
|
@ -77,8 +78,9 @@ reference_targets:
|
|||
|
||||
next_agent_start_here:
|
||||
recommended_first_task: >
|
||||
Add a DX7 SysEx parser skeleton with feature extraction tests. Do not start
|
||||
by importing a giant bank.
|
||||
Add DX7 algorithm topology metadata for algorithms 1-32, then rebuild one
|
||||
or two tiny DX7-style references in Aquarium DSL. Do not import a giant
|
||||
bank.
|
||||
must_read:
|
||||
- AGENTS.md
|
||||
- docs/audio-doctrine.md
|
||||
|
|
@ -90,6 +92,13 @@ next_agent_start_here:
|
|||
- "dotnet test AquariumSynthCSharp.slnx --no-restore"
|
||||
|
||||
completed_slices:
|
||||
- date: 2026-05-13
|
||||
summary: >
|
||||
Added DX7 SysEx records and parsing for 155-byte voice edit buffers,
|
||||
128-byte packed voices, and 4104-byte packed 32-voice banks. Extracted
|
||||
operators, envelopes, algorithm, feedback, oscillator sync, LFO,
|
||||
transpose, voice name, checksum validation, and ReferencePatch features.
|
||||
verification: "dotnet test AquariumSynthCSharp.slnx --no-restore: 28 passed"
|
||||
- date: 2026-05-13
|
||||
summary: >
|
||||
Added PatchParameter and ReferencePatch contracts, `param` script parsing,
|
||||
|
|
|
|||
194
tests/AquariumSynth.Dsl.Tests/Dx7SysExTests.cs
Normal file
194
tests/AquariumSynth.Dsl.Tests/Dx7SysExTests.cs
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
using AquariumSynth.Dsl;
|
||||
|
||||
namespace AquariumSynth.Dsl.Tests;
|
||||
|
||||
public sealed class Dx7SysExTests
|
||||
{
|
||||
[Fact]
|
||||
public void ParsesSingleVoiceEditBuffer()
|
||||
{
|
||||
var data = InitVoice("AQ BRIGHT", algorithm: 5, feedback: 3);
|
||||
|
||||
var voice = Dx7SysEx.ParseVoice(data);
|
||||
|
||||
Assert.Equal("AQ BRIGHT", voice.Name);
|
||||
Assert.Equal(6, voice.Operators.Count);
|
||||
Assert.Equal(5, voice.Algorithm);
|
||||
Assert.Equal(3, voice.Feedback);
|
||||
Assert.True(voice.OscillatorSync);
|
||||
Assert.Equal(Dx7LfoWaveform.Sine, voice.Lfo.Waveform);
|
||||
Assert.Equal(99, voice.Operators.Single(op => op.Number == 1).OutputLevel);
|
||||
Assert.Equal(1, voice.Operators.Single(op => op.Number == 1).FrequencyCoarse);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UnpacksPackedVoiceData()
|
||||
{
|
||||
var data = InitVoice("PACKED", algorithm: 32, feedback: 7);
|
||||
data[0] = 88;
|
||||
data[11] = 2;
|
||||
data[12] = 1;
|
||||
data[13] = 5;
|
||||
data[20] = 10;
|
||||
data[14] = 3;
|
||||
data[15] = 6;
|
||||
data[17] = 1;
|
||||
data[18] = 12;
|
||||
|
||||
var voice = Dx7SysEx.ParseVoice(PackVoice(data));
|
||||
var op6 = voice.Operators.Single(op => op.Number == 6);
|
||||
|
||||
Assert.Equal("PACKED", voice.Name);
|
||||
Assert.Equal(32, voice.Algorithm);
|
||||
Assert.Equal(7, voice.Feedback);
|
||||
Assert.Equal(88, op6.Envelope.Rate1);
|
||||
Assert.Equal(2, op6.LeftCurve);
|
||||
Assert.Equal(1, op6.RightCurve);
|
||||
Assert.Equal(5, op6.RateScaling);
|
||||
Assert.Equal(10, op6.Detune);
|
||||
Assert.Equal(3, op6.AmplitudeModulationSensitivity);
|
||||
Assert.Equal(6, op6.KeyVelocitySensitivity);
|
||||
Assert.Equal(Dx7FrequencyMode.Fixed, op6.FrequencyMode);
|
||||
Assert.Equal(12, op6.FrequencyCoarse);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ParsesPackedThirtyTwoVoiceSysExBank()
|
||||
{
|
||||
var bankData = new byte[Dx7SysEx.PackedBankDataLength];
|
||||
for (var i = 0; i < Dx7SysEx.PackedBankVoiceCount; i++)
|
||||
{
|
||||
var name = $"AQ{i + 1:00}";
|
||||
PackVoice(InitVoice(name, algorithm: i % 32 + 1, feedback: i % 8))
|
||||
.CopyTo(bankData.AsSpan(i * Dx7SysEx.PackedVoiceLength));
|
||||
}
|
||||
|
||||
var bank = Dx7SysEx.ParseBank(WrapSysEx(format: 9, data: bankData));
|
||||
|
||||
Assert.Equal(32, bank.Voices.Count);
|
||||
Assert.Equal("AQ01", bank.Voices[0].Name);
|
||||
Assert.Equal("AQ32", bank.Voices[31].Name);
|
||||
Assert.Equal(32, bank.Voices[31].Algorithm);
|
||||
Assert.Equal(7, bank.Voices[31].Feedback);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void BuildsReferencePatchWithStructuralFeatures()
|
||||
{
|
||||
var data = InitVoice("FEATURES", algorithm: 12, feedback: 4);
|
||||
var source = Dx7SysEx.SourceForBytes("memory://features.syx", "project-authored", data);
|
||||
|
||||
var reference = Dx7SysEx.ParseVoice(data).ToReferencePatch("dx7/features", source);
|
||||
|
||||
Assert.Equal("dx7", reference.Family);
|
||||
Assert.Equal("FEATURES", reference.Name);
|
||||
Assert.Equal("dx7-sysex", reference.Source.Kind);
|
||||
Assert.Equal(64, reference.Source.Hash.Length);
|
||||
Assert.Contains(reference.Features, feature => feature.Name == "operator_count" && feature.Value == "6");
|
||||
Assert.Contains(reference.Features, feature => feature.Name == "algorithm" && feature.Value == "12");
|
||||
Assert.Contains(reference.Features, feature => feature.Name == "feedback" && feature.Value == "4");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void RejectsBadSysExChecksum()
|
||||
{
|
||||
var wrapped = WrapSysEx(format: 0, data: InitVoice("BAD SUM", algorithm: 1, feedback: 0));
|
||||
wrapped[^2] ^= 0x01;
|
||||
|
||||
var exception = Assert.Throws<ArgumentException>(() => Dx7SysEx.ParseVoice(wrapped));
|
||||
|
||||
Assert.Contains("checksum", exception.Message);
|
||||
}
|
||||
|
||||
private static byte[] InitVoice(string name, int algorithm, int feedback)
|
||||
{
|
||||
var data = new byte[Dx7SysEx.VoiceEditBufferLength];
|
||||
for (var op = 0; op < 6; op++)
|
||||
{
|
||||
var offset = op * 21;
|
||||
data[offset + 0] = 99;
|
||||
data[offset + 1] = 99;
|
||||
data[offset + 2] = 99;
|
||||
data[offset + 3] = 99;
|
||||
data[offset + 4] = 99;
|
||||
data[offset + 5] = 99;
|
||||
data[offset + 6] = 99;
|
||||
data[offset + 7] = op == 5 ? (byte)0 : (byte)99;
|
||||
data[offset + 16] = op == 5 ? (byte)99 : (byte)0;
|
||||
data[offset + 18] = 1;
|
||||
data[offset + 20] = 7;
|
||||
}
|
||||
|
||||
data[126] = 99;
|
||||
data[127] = 99;
|
||||
data[128] = 99;
|
||||
data[129] = 99;
|
||||
data[130] = 50;
|
||||
data[131] = 50;
|
||||
data[132] = 50;
|
||||
data[133] = 50;
|
||||
data[134] = (byte)(algorithm - 1);
|
||||
data[135] = (byte)feedback;
|
||||
data[136] = 1;
|
||||
data[137] = 35;
|
||||
data[141] = 1;
|
||||
data[142] = 4;
|
||||
data[143] = 3;
|
||||
data[144] = 24;
|
||||
|
||||
var paddedName = name.PadRight(10)[..10];
|
||||
for (var i = 0; i < paddedName.Length; i++) data[145 + i] = (byte)paddedName[i];
|
||||
return data;
|
||||
}
|
||||
|
||||
private static byte[] PackVoice(byte[] edit)
|
||||
{
|
||||
var packed = new byte[Dx7SysEx.PackedVoiceLength];
|
||||
for (var op = 0; op < 6; op++)
|
||||
{
|
||||
var packedOffset = op * 17;
|
||||
var editOffset = op * 21;
|
||||
for (var i = 0; i <= 10; i++) packed[packedOffset + i] = edit[editOffset + i];
|
||||
packed[packedOffset + 11] = (byte)(edit[editOffset + 11] | (edit[editOffset + 12] << 2));
|
||||
packed[packedOffset + 12] = (byte)(edit[editOffset + 13] | (edit[editOffset + 20] << 3));
|
||||
packed[packedOffset + 13] = (byte)(edit[editOffset + 14] | (edit[editOffset + 15] << 2));
|
||||
packed[packedOffset + 14] = edit[editOffset + 16];
|
||||
packed[packedOffset + 15] = (byte)(edit[editOffset + 17] | (edit[editOffset + 18] << 1));
|
||||
packed[packedOffset + 16] = edit[editOffset + 19];
|
||||
}
|
||||
|
||||
for (var i = 0; i < 8; i++) packed[102 + i] = edit[126 + i];
|
||||
packed[110] = edit[134];
|
||||
packed[111] = (byte)(edit[135] | (edit[136] << 3));
|
||||
packed[112] = edit[137];
|
||||
packed[113] = edit[138];
|
||||
packed[114] = edit[139];
|
||||
packed[115] = edit[140];
|
||||
packed[116] = (byte)(edit[141] | (edit[142] << 1) | (edit[143] << 4));
|
||||
packed[117] = edit[144];
|
||||
for (var i = 0; i < 10; i++) packed[118 + i] = edit[145 + i];
|
||||
return packed;
|
||||
}
|
||||
|
||||
private static byte[] WrapSysEx(byte format, byte[] data)
|
||||
{
|
||||
var bytes = new byte[data.Length + 8];
|
||||
bytes[0] = 0xF0;
|
||||
bytes[1] = 0x43;
|
||||
bytes[2] = 0x00;
|
||||
bytes[3] = format;
|
||||
bytes[4] = (byte)((data.Length >> 7) & 0x7F);
|
||||
bytes[5] = (byte)(data.Length & 0x7F);
|
||||
data.CopyTo(bytes.AsSpan(6));
|
||||
bytes[^2] = Checksum(data);
|
||||
bytes[^1] = 0xF7;
|
||||
return bytes;
|
||||
}
|
||||
|
||||
private static byte Checksum(byte[] data)
|
||||
{
|
||||
var sum = 0;
|
||||
foreach (var value in data) sum = (sum + value) & 0x7F;
|
||||
return (byte)((128 - sum) & 0x7F);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue