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 MusicalComposition = GAJ3T.MusicGen.RiffGrooveBuilder.Core.Composition.Composition; namespace GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Midi; public sealed class DocumentedProfileMidiTests : IDisposable { private readonly string directory = Path.Combine(AppContext.BaseDirectory, "documented-midi-" + Guid.NewGuid().ToString("N")); [Theory] [InlineData("ample-aglp", false)] [InlineData("ample-aglp", true)] [InlineData("ample-ame", false)] [InlineData("ample-ame", true)] [InlineData("ample-agm", false)] [InlineData("ample-agm", true)] [InlineData("ample-agt", false)] [InlineData("ample-agt", true)] [InlineData("ample-abj", false)] public void DescriptionThroughEachAdmittedProfileWritesAndReadsActualMidi(string id, bool palmMute) { var profile = Select(id); var composition = CompositionPlanner.Plan(profile.Role == TrackRole.Bass ? "bass" : palmMute ? "palm-muted lead" : "lead").Composition!; var (plan, track) = WriteRead(composition, profile); var switchOn = Assert.Single(track, e => e.Event is NoteOnEvent on && on.NoteNumber < profile.MinimumPitch); Assert.Equal(0, switchOn.Tick); Assert.Equal(palmMute ? 26 : 24, (int)((NoteOnEvent)switchOn.Event).NoteNumber); Assert.Equal(100, (int)((NoteOnEvent)switchOn.Event).Velocity); Assert.Equal(30, Assert.Single(track, e => e.Event is NoteOffEvent off && off.NoteNumber < profile.MinimumPitch).Tick); Assert.Equal(120, plan.LeadInTicks); Assert.Equal(composition.Tracks[0].Notes.Select(n => n.Start.ToTicks() + 120), track.Where(e => e.Event is NoteOnEvent on && on.NoteNumber >= profile.MinimumPitch).Select(e => e.Tick)); Assert.Equal(plan.MusicalEndTick, track.Last().Tick); } [Theory] [InlineData("ample-aglp", Articulation.Sustain)] [InlineData("ample-aglp", Articulation.PalmMute)] [InlineData("ample-ame", Articulation.Sustain)] [InlineData("ample-ame", Articulation.PalmMute)] [InlineData("ample-agm", Articulation.Sustain)] [InlineData("ample-agm", Articulation.PalmMute)] [InlineData("ample-agt", Articulation.Sustain)] [InlineData("ample-agt", Articulation.PalmMute)] public void UnknownStateBoundaryNotesRepeatsAndTransitionsNeverCombineSwitches( string id, Articulation first) { var profile = Select(id); var second = first == Articulation.Sustain ? Articulation.PalmMute : Articulation.Sustain; var firstRule = profile.Rules.Single(r => r.Articulation == first); var secondRule = profile.Rules.Single(r => r.Articulation == second); var composition = new MusicalComposition(120, new(4, 4), new(4, KeyMode.Minor), 1, [new(TrackRole.Lead, [ new(profile.MinimumPitch, new(0), new(1, 4), firstRule.Velocity.Minimum, first), new(profile.MaximumPitch, new(1), new(1, 4), firstRule.Velocity.Maximum, first), new(profile.MinimumPitch, new(2), new(1, 4), secondRule.Velocity.Minimum, second), new(profile.MaximumPitch, new(3), new(1, 4), firstRule.Velocity.Minimum, first) ])]); var (_, track) = WriteRead(composition, profile); var switches = track.Where(e => e.Event is NoteOnEvent on && on.NoteNumber < profile.MinimumPitch).ToArray(); Assert.Equal(new long[] { 0, 1920, 2880 }, switches.Select(e => e.Tick)); Assert.Equal(new[] { firstRule.Keyswitch!.Pitch, secondRule.Keyswitch!.Pitch, firstRule.Keyswitch.Pitch }, switches.Select(e => (int)((NoteOnEvent)e.Event).NoteNumber)); Assert.Equal(new long[] { 30, 1950, 2910 }, track.Where(e => e.Event is NoteOffEvent off && off.NoteNumber < profile.MinimumPitch).Select(e => e.Tick)); var musical = track.Where(e => e.Event is NoteOnEvent on && on.NoteNumber >= profile.MinimumPitch).ToArray(); Assert.Equal(new long[] { 120, 1080, 2040, 3000 }, musical.Select(e => e.Tick)); Assert.Equal(composition.Tracks[0].Notes.Select(n => n.Pitch), musical.Select(e => (int)((NoteOnEvent)e.Event).NoteNumber)); Assert.Equal(composition.Tracks[0].Notes.Select(n => n.Velocity), musical.Select(e => (int)((NoteOnEvent)e.Event).Velocity)); Assert.Equal(new long[] { 360, 1320, 2280, 3240 }, track.Where(e => e.Event is NoteOffEvent off && off.NoteNumber >= profile.MinimumPitch).Select(e => e.Tick)); } [Theory] [InlineData("ample-aglp", 127, "velocity_layer_conflict")] [InlineData("ample-ame", 63, "velocity_layer_conflict")] [InlineData("ample-ame", 127, "velocity_layer_conflict")] [InlineData("ample-agm", 127, "velocity_layer_conflict")] [InlineData("ample-agt", 127, "velocity_layer_conflict")] [InlineData("ample-abj", 126, "velocity_layer_conflict")] [InlineData("ample-abj", 127, "velocity_layer_conflict")] public void UnsupportedSustainLayersFailBeforeAnyFileIsCreated(string id, int velocity, string code) { var profile = Select(id); var model = new MusicalComposition(120, new(4, 4), new(4, KeyMode.Minor), 1, [new(profile.Role, [new(64, new(0), new(1), velocity, Articulation.Sustain)])]); var result = ArticulationPlanner.Plan(model, [profile]); Assert.False(result.Success); Assert.Null(result.Plan); Assert.Contains(result.Diagnostics, d => d.Code == code); Assert.False(Directory.Exists(directory)); } private static InstrumentProfile Select(string id) { var catalog = InstrumentProfileCatalog.LoadEmbedded(); var role = catalog.Profiles.Single(p => p.InstrumentId == id).Role; var selection = catalog.SelectInstrument(role, id); Assert.True(selection.Success, string.Join("; ", selection.Diagnostics)); var profile = selection.Profile!; Assert.Equal(MappingVerification.Documented, profile.Evidence.Status); Assert.Empty(InstrumentProfileValidator.Validate(profile)); return profile; } private (MidiEventPlan Plan, (long Tick, MidiEvent Event)[] Track) WriteRead( MusicalComposition composition, InstrumentProfile profile) { var before = JsonSerializer.Serialize(new { composition, profile }); var result = ArticulationPlanner.Plan(composition, [profile]); Assert.True(result.Success, string.Join("; ", result.Diagnostics)); var plan = result.Plan!; Directory.CreateDirectory(directory); var path = Path.Combine(directory, "documented.mid"); MidiFileWriter.WriteFile(path, composition, plan); Assert.True(new FileInfo(path).Length > 0); var file = MidiFile.Read(path, new ReadingSettings { EndOfTrackStoringPolicy = EndOfTrackStoringPolicy.Store }); Assert.Equal(MidiFileFormat.MultiTrack, file.OriginalFormat); Assert.Equal(960, Assert.IsType(file.TimeDivision).TicksPerQuarterNote); var chunks = file.Chunks.Cast().ToArray(); Assert.Equal(2, chunks.Length); Assert.Equal(profile.Role.ToString(), Assert.Single(chunks[1].Events.OfType()).Text); long tick = 0; var actual = chunks[1].Events.Select(e => (Tick: tick += e.DeltaTime, Event: e)).ToArray(); var channelEvents = actual.Where(e => e.Event is ChannelEvent).ToArray(); Assert.Equal(plan.Tracks[0].Events.Select(e => e.Tick), channelEvents.Select(e => e.Tick)); Assert.All(channelEvents, e => Assert.Equal(profile.Channel, (int)((ChannelEvent)e.Event).Channel)); for (var i = 0; i < channelEvents.Length; i++) { if (plan.Tracks[0].Events[i] is PlannedNoteOn expectedOn) { var on = Assert.IsType(channelEvents[i].Event); Assert.Equal(expectedOn.Pitch, (int)on.NoteNumber); Assert.Equal(expectedOn.Velocity, (int)on.Velocity); } else { var off = Assert.IsType(channelEvents[i].Event); Assert.Equal(Assert.IsType(plan.Tracks[0].Events[i]).Pitch, (int)off.NoteNumber); } } var active = new HashSet(); var heldSwitches = new HashSet(); foreach (var (_, e) in channelEvents) { if (e is NoteOnEvent on) { Assert.True(active.Add(on.NoteNumber)); if (on.NoteNumber < profile.MinimumPitch) { Assert.Empty(heldSwitches); Assert.True(heldSwitches.Add(on.NoteNumber)); } } if (e is NoteOffEvent off) { Assert.True(active.Remove(off.NoteNumber)); heldSwitches.Remove(off.NoteNumber); } } Assert.Empty(active); Assert.Empty(heldSwitches); Assert.IsType(actual.Last().Event); Assert.Equal(plan.EndTick, actual.Last().Tick); Assert.Equal(before, JsonSerializer.Serialize(new { composition, profile })); return (plan, actual); } [Fact] public void AbjSustainOnlyBoundaryDynamicsInitializeOnceOnSeparateBassChannel() { var profile = Select("ample-abj"); var composition = new MusicalComposition(120, new(4, 4), new(4, KeyMode.Minor), 1, [new(TrackRole.Bass, [ new(35, new(0), new(1, 4), 1, Articulation.Sustain), new(76, new(2), new(1, 4), 125, Articulation.Sustain) ])]); var (_, events) = WriteRead(composition, profile); Assert.Equal(24, (int)((NoteOnEvent)Assert.Single(events, e => e.Event is NoteOnEvent on && on.NoteNumber < 35).Event).NoteNumber); Assert.Equal(new[] { 1, 125 }, events.Where(e => e.Event is NoteOnEvent on && on.NoteNumber >= 35) .Select(e => (int)((NoteOnEvent)e.Event).Velocity)); Assert.All(events.Where(e => e.Event is ChannelEvent), e => Assert.Equal(1, (int)((ChannelEvent)e.Event).Channel)); } [Fact] public void AbjPalmMuteIsExplicitlyUnsupportedInsteadOfSubstituted() { var profile = Select("ample-abj"); var composition = CompositionPlanner.Plan("palm-muted bass").Composition!; var result = ArticulationPlanner.Plan(composition, [profile]); Assert.False(result.Success); Assert.Null(result.Plan); Assert.All(result.Diagnostics, d => Assert.Equal("unsupported_articulation", d.Code)); Assert.False(Directory.Exists(directory)); } public void Dispose() { if (Directory.Exists(directory)) Directory.Delete(directory, recursive: true); } }