using System.Security.Cryptography; using System.Text.Json; using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Articulations; using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Composition; using GAJ3T.MusicGen.RiffGrooveBuilder.Generation.Midi; using GAJ3T.MusicGen.RiffGrooveBuilder.Generation.Profiles; using Melanchall.DryWetMidi.Core; using Xunit.Abstractions; using MusicalComposition = GAJ3T.MusicGen.RiffGrooveBuilder.Core.Composition.Composition; using static GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Support.SyntheticPipelineProfiles; namespace GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Midi; [CollectionDefinition("MIDI working directory", DisableParallelization = true)] public sealed class MidiWorkingDirectoryCollection; [Collection("MIDI working directory")] public sealed class MidiGenerationPipelineTests : IDisposable { private readonly string directory = Path.Combine(AppContext.BaseDirectory, "midi-pipeline-" + Guid.NewGuid().ToString("N")); private readonly ITestOutputHelper output; public MidiGenerationPipelineTests(ITestOutputHelper output) { this.output = output; Directory.CreateDirectory(directory); } [Fact] public void FullExampleWritesActualTypeOneFileFromUnrelatedCwdAndReadsEveryEventBack() { var (composition, plan) = Plan(Example); var before = JsonSerializer.Serialize(new { composition, plan }); Assert.Equal(240, plan.LeadInTicks); Assert.Equal(15_360, plan.MusicalExtentTicks); Assert.Equal(15_600, plan.MusicalEndTick); Assert.Equal(15_600, plan.EndTick); var path = Path.Combine(directory, "full-example.mid"); var previous = Environment.CurrentDirectory; try { Environment.CurrentDirectory = Environment.SystemDirectory; MidiFileWriter.WriteFile(path, composition, plan); } finally { Environment.CurrentDirectory = previous; } var file = ReadAndAssert(path, plan); var tracks = file.Chunks.Cast().ToArray(); Assert.Equal(new[] { "Conductor", "Lead", "Bass", "Drums" }, tracks.Select(Name)); var conductor = Absolute(tracks[0]); Assert.Equal(500_000, Assert.IsType( Assert.Single(conductor, e => e.Event is SetTempoEvent).Event).MicrosecondsPerQuarterNote); var meter = Assert.Single(tracks[0].Events.OfType()); Assert.Equal(4, meter.Numerator); Assert.Equal(4, meter.Denominator); Assert.All(conductor.Where(e => e.Event is not EndOfTrackEvent), e => Assert.Equal(0, e.Tick)); Assert.Equal(32, tracks[1].Events.OfType().Count(e => e.NoteNumber == 64)); Assert.Equal(4, tracks[2].Events.OfType().Count(e => e.NoteNumber == 40)); Assert.Equal(48, tracks[3].Events.OfType().Count()); Assert.Equal(Enumerable.Range(0, 32).Select(i => (Tick: 240L + i * 480, Pitch: 64, Velocity: 110, Channel: 0)), Absolute(tracks[1]).Where(e => e.Event is NoteOnEvent on && on.NoteNumber == 64).Select(Note)); Assert.Equal(Enumerable.Range(0, 4).Select(i => (Tick: 240L + i * 3840, Pitch: 40, Velocity: 96, Channel: 1)), Absolute(tracks[2]).Where(e => e.Event is NoteOnEvent on && on.NoteNumber == 40).Select(Note)); Assert.Equal((0L, 11, 91, 0), Note(Assert.Single(Absolute(tracks[1]), e => e.Event is NoteOnEvent on && on.NoteNumber == 11))); Assert.Equal(30, Assert.Single(Absolute(tracks[1]), e => e.Event is NoteOffEvent off && off.NoteNumber == 11).Tick); Assert.Equal((120L, 12, 92, 1), Note(Assert.Single(Absolute(tracks[2]), e => e.Event is NoteOnEvent on && on.NoteNumber == 12))); Assert.Equal(140, Assert.Single(Absolute(tracks[2]), e => e.Event is NoteOffEvent off && off.NoteNumber == 12).Tick); Assert.Equal(new[] { (0L, 21, 0), (240L, 21, 100), (15_600L, 21, 0) }, Absolute(tracks[3]).Where(e => e.Event is ControlChangeEvent).Select(e => (e.Tick, (int)((ControlChangeEvent)e.Event).ControlNumber, (int)((ControlChangeEvent)e.Event).ControlValue))); foreach (var (voice, pitch) in new[] { (DrumVoice.Kick, 50), (DrumVoice.Snare, 51), (DrumVoice.ClosedHiHat, 52) }) { var expected = composition.Tracks.Single(t => t.Role == TrackRole.Drums).Hits.Where(h => h.Voice == voice); Assert.Equal(expected.Select(h => (h.Start.ToTicks() + 240, pitch, h.Velocity, 9)), Absolute(tracks[3]).Where(e => e.Event is NoteOnEvent on && on.NoteNumber == pitch).Select(Note)); } var repeated = Plan(Example, Profiles().Reverse()); var repeatedPath = Path.Combine(directory, "repeat.mid"); MidiFileWriter.WriteFile(repeatedPath, repeated.Composition, repeated.Plan); Assert.Equal(File.ReadAllBytes(path), File.ReadAllBytes(repeatedPath)); Assert.Equal(before, JsonSerializer.Serialize(new { composition, plan })); output.WriteLine($"Actual .mid write/read-back: bytes={new FileInfo(path).Length}, SHA256={Convert.ToHexString(SHA256.HashData(File.ReadAllBytes(path)))}"); output.WriteLine("Read-back: type=1, PPQ=960, tracks=4, channel events=175, musical notes=32/4/48, switch pairs=2, EOTs=4 at 15600."); } [Theory] [InlineData("bass", "Bass", 1, 4)] [InlineData("palm-muted lead", "Lead", 0, 32)] [InlineData("kick on beat 1", "Drums", 9, 4)] [InlineData("lead and bass", "Lead,Bass", 0, 36)] public void SelectedRolesHaveOnlyTheirNamedTracksAndConfiguredChannels(string text, string names, int firstChannel, int musicalNotes) { var (composition, plan) = Plan(text); var path = Path.Combine(directory, "subset.mid"); MidiFileWriter.WriteFile(path, composition, plan); var tracks = ReadAndAssert(path, plan).Chunks.Cast().Skip(1).ToArray(); Assert.Equal(names.Split(','), tracks.Select(Name)); Assert.Equal(firstChannel, (int)tracks[0].Events.OfType().First().Channel); Assert.Equal(musicalNotes, plan.Tracks.Sum(t => t.Events.OfType().Count(e => !e.IsKeyswitch))); } [Fact] public void NonFourFourDescriptionPreservesDenominatorBeatPositionsAndMetadata() { var (composition, plan) = Plan("3/8, 2 bars, 123 bpm: kick on beats 1 and 3, snare on beat 2, closed hi-hat eighth notes"); var path = Path.Combine(directory, "three-eight.mid"); MidiFileWriter.WriteFile(path, composition, plan); var tracks = ReadAndAssert(path, plan).Chunks.Cast().ToArray(); var meter = Assert.Single(tracks[0].Events.OfType()); Assert.Equal(3, meter.Numerator); Assert.Equal(8, meter.Denominator); Assert.Equal(487_805, Assert.Single(tracks[0].Events.OfType()).MicrosecondsPerQuarterNote); Assert.Equal(2880, plan.EndTick); Assert.Equal(new long[] { 0, 960, 1440, 2400 }, Absolute(tracks[1]).Where(e => e.Event is NoteOnEvent on && on.NoteNumber == 50).Select(e => e.Tick)); Assert.Equal(12, tracks[1].Events.OfType().Count()); } [Fact] public void LongRepeatedPatternUsesExactTicksWithoutAccumulatedDrift() { var (composition, plan) = Plan("1000 bars, 7/8: sixteenth-note lead"); var path = Path.Combine(directory, "long.mid"); MidiFileWriter.WriteFile(path, composition, plan); var tracks = ReadAndAssert(path, plan).Chunks.Cast().ToArray(); var notes = Absolute(tracks[1]).Where(e => e.Event is NoteOnEvent).ToArray(); Assert.Equal(14_000, notes.Length); Assert.Equal(240, plan.LeadInTicks); Assert.Equal(Enumerable.Range(0, 14_000).Select(i => 240L + 240L * i), notes.Select(e => e.Tick)); Assert.Equal(3_360_000, plan.MusicalExtentTicks); Assert.Equal(3_360_240, plan.EndTick); Assert.Equal(3_360_240, Absolute(tracks[1]).Last().Tick); } [Fact] public void RestsKeepFullDeclaredBarsEvenAfterLastNoteOff() { var (composition, plan) = Plan("4 bars: kick on beat 1"); var path = Path.Combine(directory, "rests.mid"); MidiFileWriter.WriteFile(path, composition, plan); var track = ReadAndAssert(path, plan).Chunks.Cast().Last(); Assert.Equal(12_480, Absolute(track).Last(e => e.Event is NoteOffEvent).Tick); Assert.Equal(15_360, Absolute(track).Last().Tick); Assert.Equal(2880, track.Events.OfType().Last().DeltaTime); Assert.Equal(0, track.Events.Last().DeltaTime); } [Theory] [InlineData(false, 4110)] [InlineData(true, 20000)] public void ResetPulseOrMomentaryReleaseTailSetsEveryTracksGlobalEnd(bool momentary, long expectedEnd) { var profiles = Profiles(); var lead = profiles[0]; var rules = momentary ? new[] { new ArticulationRule(Articulation.Sustain, null, null, StateBehavior.None, null, new(1, 127, null), []), new ArticulationRule(Articulation.PalmMute, null, null, StateBehavior.Momentary, new(11, 91, 240, 20_000), new(64, 127, 110), []) } : lead.Rules; profiles[0] = new(lead.Id, lead.InstrumentId, lead.Role, lead.ProductName, lead.ProductVersion, lead.MappingVersion, lead.Evidence, lead.MinimumPitch, lead.MaximumPitch, lead.Channel, lead.InitialArticulation, lead.InitialControllers, momentary ? null : Articulation.Sustain, lead.FinalControllers, rules); var (composition, plan) = Plan("1 bar: palm-muted sustained lead, bass, kick on beat 1", profiles); Assert.Equal(4080, plan.MusicalEndTick); Assert.Equal(expectedEnd, plan.EndTick); var path = Path.Combine(directory, "tail.mid"); MidiFileWriter.WriteFile(path, composition, plan); var tracks = ReadAndAssert(path, plan).Chunks.Cast().ToArray(); Assert.All(tracks, t => Assert.Equal(expectedEnd, Absolute(t).Last().Tick)); var lastOff = Absolute(tracks[1]).Last(e => e.Event is NoteOffEvent); Assert.Equal(expectedEnd, lastOff.Tick); Assert.Equal(momentary ? 11 : 10, (int)((NoteOffEvent)lastOff.Event).NoteNumber); } [Fact] public void RealWriterNoOverwritePreflightAndCancellationLeaveNoUndisclosedFiles() { var (composition, plan) = Plan(Example); var path = Path.Combine(directory, "existing.mid"); File.WriteAllText(path, "another owner"); var unrelated = Path.Combine(directory, ".other-owner.stage"); File.WriteAllText(unrelated, "keep"); Assert.Throws(() => MidiFileWriter.WriteFile(path, composition, plan)); Assert.Equal("another owner", File.ReadAllText(path)); var cancelled = Path.Combine(directory, "cancelled.mid"); Assert.Throws(() => MidiFileWriter.WriteFile(cancelled, composition, plan, new CancellationToken(true))); Assert.False(File.Exists(cancelled)); Assert.Throws(() => MidiFileWriter.WriteFile(Path.Combine(directory, "missing", "fail.mid"), composition, plan)); Assert.Throws(() => MidiFileWriter.WriteFile(Path.Combine(directory, "invalid.mid"), null!, plan)); Assert.Equal(new[] { ".other-owner.stage", "existing.mid" }, Directory.GetFiles(directory).Select(Path.GetFileName).Order(StringComparer.Ordinal)); Assert.Equal("keep", File.ReadAllText(unrelated)); Assert.DoesNotContain(InstrumentProfileCatalog.LoadEmbedded().Profiles, p => p.Evidence.Status is MappingVerification.Synthetic or MappingVerification.Unverified); } private static (MusicalComposition Composition, MidiEventPlan Plan) Plan(string text, IEnumerable? profiles = null) { var parsed = DescriptionParser.Parse(text); Assert.True(parsed.Success, string.Join("; ", parsed.Diagnostics)); var composition = CompositionPlanner.Plan(parsed.Description!); Assert.True(composition.Success, string.Join("; ", composition.Diagnostics)); Assert.Empty(CompositionValidator.Validate(composition.Composition)); var selected = (profiles ?? Profiles()).Where(p => composition.Composition!.Tracks.Any(t => t.Role == p.Role)).ToArray(); Assert.All(selected, p => Assert.Empty(InstrumentProfileValidator.Validate(p, allowSynthetic: true))); var planned = ArticulationPlanner.Plan(composition.Composition, selected, allowSynthetic: true); Assert.True(planned.Success, string.Join("; ", planned.Diagnostics)); return (composition.Composition!, planned.Plan!); } private static MidiFile ReadAndAssert(string path, MidiEventPlan plan) { Assert.True(new FileInfo(path).Length > 0); var file = MidiFile.Read(path, new ReadingSettings { EndOfTrackStoringPolicy = EndOfTrackStoringPolicy.Store, MissedEndOfTrackPolicy = MissedEndOfTrackPolicy.Abort }); Assert.Equal(MidiFileFormat.MultiTrack, file.OriginalFormat); Assert.Equal(960, Assert.IsType(file.TimeDivision).TicksPerQuarterNote); var tracks = file.Chunks.Cast().ToArray(); Assert.Equal(plan.Tracks.Count + 1, tracks.Length); foreach (var track in tracks) { Assert.Single(track.Events.OfType()); Assert.IsType(track.Events.Last()); Assert.Equal(plan.EndTick, Absolute(track).Last().Tick); } foreach (var planned in plan.Tracks) { var track = Assert.Single(tracks, t => Name(t) == planned.Role.ToString()); var actual = Absolute(track).Where(e => e.Event is ChannelEvent).ToArray(); Assert.Equal(planned.Events.Select(EventValue), actual.Select(EventValue)); var active = new HashSet<(int Channel, int Pitch)>(); foreach (var (_, e) in actual) { if (e is NoteOnEvent on) Assert.True(active.Add(((int)on.Channel, (int)on.NoteNumber))); if (e is NoteOffEvent off) Assert.True(active.Remove(((int)off.Channel, (int)off.NoteNumber))); } Assert.Empty(active); } return file; } private static string Name(TrackChunk track) => Assert.Single(track.Events.OfType()).Text; private static (long Tick, MidiEvent Event)[] Absolute(TrackChunk track) { long tick = 0; return track.Events.Select(e => (Tick: tick = checked(tick + e.DeltaTime), Event: e)).ToArray(); } private static (long Tick, int Pitch, int Velocity, int Channel) Note((long Tick, MidiEvent Event) e) { var on = Assert.IsType(e.Event); return (e.Tick, (int)on.NoteNumber, (int)on.Velocity, (int)on.Channel); } private static (long Tick, string Kind, int Channel, int Number, int Value) EventValue(PlannedMidiEvent e) => e switch { PlannedNoteOn on => (e.Tick, "on", e.Channel, on.Pitch, on.Velocity), PlannedNoteOff off => (e.Tick, "off", e.Channel, off.Pitch, 0), PlannedControlChange cc => (e.Tick, "cc", e.Channel, cc.Number, cc.Value), _ => throw new InvalidOperationException("Unexpected planned event.") }; private static (long Tick, string Kind, int Channel, int Number, int Value) EventValue((long Tick, MidiEvent Event) e) => e.Event switch { NoteOnEvent on => (e.Tick, "on", (int)on.Channel, (int)on.NoteNumber, (int)on.Velocity), NoteOffEvent off => (e.Tick, "off", (int)off.Channel, (int)off.NoteNumber, (int)off.Velocity), ControlChangeEvent cc => (e.Tick, "cc", (int)cc.Channel, (int)cc.ControlNumber, (int)cc.ControlValue), _ => throw new InvalidOperationException("Unexpected read-back event.") }; public void Dispose() => Directory.Delete(directory, recursive: true); }