using System.Text.Json; using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Articulations; using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Composition; using GAJ3T.MusicGen.RiffGrooveBuilder.Generation.Profiles; using static GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Support.SyntheticPipelineProfiles; namespace GAJ3T.MusicGen.RiffGrooveBuilder.IntegrationTests.Articulations; public sealed class ArticulationPipelineTests { [Fact] public void ThreeRolePipelineHasOneExactSharedLeadInAndSyntheticCombinedControls() { var parsed = DescriptionParser.Parse(Example); Assert.True(parsed.Success); var compositionResult = CompositionPlanner.Plan(parsed.Description!); Assert.True(compositionResult.Success); var composition = compositionResult.Composition!; Assert.Empty(CompositionValidator.Validate(composition)); var profiles = Profiles(); Assert.All(profiles, p => Assert.Empty(InstrumentProfileValidator.Validate(p, true))); var before = JsonSerializer.Serialize(composition); var result = ArticulationPlanner.Plan(composition, profiles, true); Assert.True(result.Success, string.Join("; ", result.Diagnostics)); Assert.Empty(result.Diagnostics); var plan = result.Plan!; Assert.Equal(240, plan.LeadInTicks); Assert.Equal(15_360, plan.MusicalExtentTicks); Assert.Equal(15_600, plan.MusicalEndTick); Assert.Equal(15_600, plan.EndTick); Assert.Equal(175, plan.Tracks.Sum(t => t.Events.Count)); Assert.Equal(new[] { TrackRole.Lead, TrackRole.Bass, TrackRole.Drums }, plan.Tracks.Select(t => t.Role)); var lead = plan.Tracks[0]; var bass = plan.Tracks[1]; var drums = plan.Tracks[2]; Assert.Equal(new PlannedNoteOn(0, 0, 11, 91, true), Assert.Single(lead.Events.OfType(), e => e.IsKeyswitch)); Assert.Contains(new PlannedNoteOff(30, 0, 11, true), lead.Events); Assert.Equal(new PlannedNoteOn(120, 1, 12, 92, true), Assert.Single(bass.Events.OfType(), e => e.IsKeyswitch)); Assert.Contains(new PlannedNoteOff(140, 1, 12, true), bass.Events); Assert.Equal(new PlannedControlChange[] { new(0, 9, 21, 0, true), new(240, 9, 21, 100, false), new(15_600, 9, 21, 0, true) }, drums.Events.OfType()); Assert.Equal(Enumerable.Range(0, 32).Select(i => new PlannedNoteOn(240 + i * 480, 0, 64, 110, false)), lead.Events.OfType().Where(e => !e.IsKeyswitch)); Assert.Equal(Enumerable.Range(0, 4).Select(i => new PlannedNoteOn(240 + i * 3840, 1, 40, 96, false)), bass.Events.OfType().Where(e => !e.IsKeyswitch)); foreach (var (voice, pitch) in new[] { (DrumVoice.Kick, 50), (DrumVoice.Snare, 51), (DrumVoice.ClosedHiHat, 52) }) { var hits = composition.Tracks.Single(t => t.Role == TrackRole.Drums).Hits.Where(h => h.Voice == voice); Assert.Equal(hits.Select(h => new PlannedNoteOn(h.Start.ToTicks() + 240, 9, pitch, h.Velocity, false)), drums.Events.OfType().Where(e => e.Pitch == pitch)); Assert.Equal(hits.Select(h => new PlannedNoteOff((h.Start + h.Duration).ToTicks() + 240, 9, pitch, false)), drums.Events.OfType().Where(e => e.Pitch == pitch)); } Assert.All(plan.Tracks, t => { Assert.Equal(t.Events.OrderBy(e => e.Tick).ThenBy(e => e.Priority), t.Events); var active = new HashSet(); foreach (var e in t.Events) { Assert.True(e.Tick >= 0); if (e is PlannedNoteOn on) Assert.True(active.Add(on.Pitch)); if (e is PlannedNoteOff off) Assert.True(active.Remove(off.Pitch)); } Assert.Empty(active); }); Assert.Equal(before, JsonSerializer.Serialize(composition)); Assert.Equal(JsonSerializer.Serialize(plan), JsonSerializer.Serialize(ArticulationPlanner.Plan(composition, Profiles().Reverse(), true).Plan)); var repeated = ArticulationPlanner.Plan(composition, Profiles().Reverse(), true).Plan!; for (var i = 0; i < plan.Tracks.Count; i++) Assert.Equal(plan.Tracks[i].Events, repeated.Tracks[i].Events); Assert.Equal(96, composition.Tracks[0].Notes[0].Velocity); } [Theory] [InlineData("bass", TrackRole.Bass, 120)] [InlineData("palm-muted lead", TrackRole.Lead, 240)] [InlineData("kick on beat 1", TrackRole.Drums, 0)] public void SelectedSubsetsUseOnlyTheirOwnRequiredAdvance(string text, TrackRole role, long expected) { var composition = CompositionPlanner.Plan(text).Composition!; var result = ArticulationPlanner.Plan(composition, Profiles().Where(p => p.Role == role), true); Assert.True(result.Success, string.Join("; ", result.Diagnostics)); Assert.Equal(expected, result.Plan!.LeadInTicks); Assert.Equal(role, Assert.Single(result.Plan.Tracks).Role); Assert.Equal(expected, result.Plan.Tracks[0].Events.OfType().First(e => !e.IsKeyswitch).Tick); } [Fact] public void PartialDocumentedCatalogCannotPlanAllTargetsOrSupplyRoleDefaults() { var catalog = InstrumentProfileCatalog.LoadEmbedded(); Assert.Equal(5, catalog.Profiles.Count); Assert.All(catalog.Profiles, p => Assert.Equal(MappingVerification.Documented, p.Evidence.Status)); var composition = CompositionPlanner.Plan(Example).Composition!; foreach (var role in Enum.GetValues()) { Assert.False(catalog.Select(role).Success); Assert.False(catalog.Select(role, "synthetic-" + role).Success); } var result = ArticulationPlanner.Plan(composition, catalog.Profiles.Where(p => p.InstrumentId is "ample-ame" or "ample-abj")); Assert.False(result.Success); Assert.Null(result.Plan); Assert.Equal("Drums", Assert.Single(result.Diagnostics).Field); Assert.Equal("missing_or_duplicate_profile", result.Diagnostics[0].Code); } [Theory] [InlineData("missing", "missing_or_duplicate_profile")] [InlineData("unverified", "unverified_profile")] [InlineData("unsupported", "unsupported_articulation")] [InlineData("unmapped", "unmapped_drum_voice")] public void PipelineFailuresDoNotExposePartialEventPlans(string scenario, string code) { var composition = CompositionPlanner.Plan(Example).Composition!; var profiles = Profiles(); if (scenario == "missing") profiles = profiles.Take(2).ToArray(); if (scenario == "unsupported") profiles[0] = Pitched(TrackRole.Lead, 0, 240, false); if (scenario == "unmapped") { var d = profiles[2]; profiles[2] = new(d.Id, d.InstrumentId, d.Role, d.ProductName, d.ProductVersion, d.MappingVersion, d.Evidence, d.MinimumPitch, d.MaximumPitch, d.Channel, null, d.InitialControllers, null, d.FinalControllers, d.Rules.Where(r => r.Voice != DrumVoice.Snare).ToArray()); } var result = ArticulationPlanner.Plan(composition, profiles, allowSynthetic: scenario != "unverified"); Assert.False(result.Success); Assert.Null(result.Plan); Assert.Contains(result.Diagnostics, d => d.Code == code); } }