86 lines
3.5 KiB
C#
86 lines
3.5 KiB
C#
using GAJ3T.MusicGen.RiffGrooveBuilder.Core.Articulations;
|
|
using GAJ3T.MusicGen.RiffGrooveBuilder.Generation.Midi;
|
|
using Melanchall.DryWetMidi.Core;
|
|
|
|
namespace GAJ3T.MusicGen.RiffGrooveBuilder.UnitTests.Midi;
|
|
|
|
public sealed class MidiEventBuilderTests
|
|
{
|
|
[Theory]
|
|
[InlineData(false, 0, 0, 1)]
|
|
[InlineData(true, 15, 127, 127)]
|
|
[InlineData(false, 9, 50, 96)]
|
|
public void NoteOnPreservesAllValues(bool keyswitch, int channel, int pitch, int velocity)
|
|
{
|
|
var source = new PlannedNoteOn(123, channel, pitch, velocity, keyswitch);
|
|
var actual = Assert.IsType<NoteOnEvent>(MidiEventBuilder.Build(source));
|
|
Assert.Equal(channel, (int)actual.Channel);
|
|
Assert.Equal(pitch, (int)actual.NoteNumber);
|
|
Assert.Equal(velocity, (int)actual.Velocity);
|
|
Assert.Equal(0, actual.DeltaTime);
|
|
Assert.Equal(123, source.Tick);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(false, 0, 0)]
|
|
[InlineData(true, 15, 127)]
|
|
public void NoteOffIsExplicitWithZeroReleaseVelocity(bool keyswitch, int channel, int pitch)
|
|
{
|
|
var actual = Assert.IsType<NoteOffEvent>(MidiEventBuilder.Build(new PlannedNoteOff(456, channel, pitch, keyswitch)));
|
|
Assert.Equal(channel, (int)actual.Channel);
|
|
Assert.Equal(pitch, (int)actual.NoteNumber);
|
|
Assert.Equal(0, (int)actual.Velocity);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData(0, 0, 0, false)]
|
|
[InlineData(15, 127, 127, true)]
|
|
[InlineData(9, 21, 100, false)]
|
|
public void ControlChangePreservesValues(int channel, int number, int value, bool reset)
|
|
{
|
|
var actual = Assert.IsType<ControlChangeEvent>(MidiEventBuilder.Build(new PlannedControlChange(42, channel, number, value, reset)));
|
|
Assert.Equal(channel, (int)actual.Channel);
|
|
Assert.Equal(number, (int)actual.ControlNumber);
|
|
Assert.Equal(value, (int)actual.ControlValue);
|
|
}
|
|
|
|
public static IEnumerable<object[]> InvalidEvents()
|
|
{
|
|
yield return [new PlannedNoteOn(-1, 0, 64, 96, false)];
|
|
foreach (var channel in new[] { -1, 16, 256 })
|
|
yield return [new PlannedNoteOn(0, channel, 64, 96, false)];
|
|
foreach (var pitch in new[] { -1, 128, 256 })
|
|
{
|
|
yield return [new PlannedNoteOn(0, 0, pitch, 96, false)];
|
|
yield return [new PlannedNoteOff(0, 0, pitch, true)];
|
|
}
|
|
foreach (var velocity in new[] { -1, 0, 128, 256 })
|
|
yield return [new PlannedNoteOn(0, 0, 64, velocity, true)];
|
|
foreach (var value in new[] { -1, 128, 256 })
|
|
{
|
|
yield return [new PlannedControlChange(0, 9, value, 0, false)];
|
|
yield return [new PlannedControlChange(0, 9, 21, value, true)];
|
|
}
|
|
}
|
|
|
|
[Theory]
|
|
[MemberData(nameof(InvalidEvents))]
|
|
public void InvalidRangesNeverWrapOrClamp(PlannedMidiEvent planned) =>
|
|
Assert.Throws<ArgumentOutOfRangeException>(() => MidiEventBuilder.Build(planned));
|
|
|
|
[Fact]
|
|
public void UnknownTypeNullAndCancellationFailExplicitly()
|
|
{
|
|
Assert.Throws<ArgumentNullException>(() => MidiEventBuilder.Build(null!));
|
|
var error = Assert.Throws<NotSupportedException>(() => MidiEventBuilder.Build(new UnknownEvent()));
|
|
Assert.Contains(nameof(UnknownEvent), error.Message);
|
|
Assert.Throws<OperationCanceledException>(() => MidiEventBuilder.Build(
|
|
new PlannedNoteOff(0, 0, 64, false), new CancellationToken(true)));
|
|
}
|
|
|
|
private sealed record UnknownEvent() : PlannedMidiEvent(0, 0)
|
|
{
|
|
public override int Priority => 2;
|
|
}
|
|
}
|