Skip to content

Commit

Permalink
Check for out-of-range error in PatternBuilder.Note method by interval
Browse files Browse the repository at this point in the history
  • Loading branch information
melanchall committed Nov 21, 2024
1 parent d7b907f commit 6a4a1c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public sealed partial class PatternBuilderTests
#region Test methods

[Test]
[Description("Add two notes where first one takes default length and velocity and the second takes specified ones.")]
public void Note_MixedLengthAndVelocity()
{
var defaultNoteLength = MusicalTimeSpan.Quarter;
Expand All @@ -40,7 +39,6 @@ public void Note_MixedLengthAndVelocity()
}

[Test]
[Description("Add several notes with metric lengths.")]
public void Note_Multiple_MetricLengths()
{
var pattern = new PatternBuilder()
Expand All @@ -63,7 +61,6 @@ public void Note_Multiple_MetricLengths()
}

[Test]
[Description("Add several notes with metric lengths.")]
public void Note_Multiple_MetricLengths_TempoChanged()
{
var pattern = new PatternBuilder()
Expand All @@ -90,7 +87,6 @@ public void Note_Multiple_MetricLengths_TempoChanged()
}

[Test]
[Description("Add several notes by intervals.")]
public void Note_Multiple_Interval()
{
var defaultNoteLength = MusicalTimeSpan.Quarter;
Expand All @@ -113,6 +109,18 @@ public void Note_Multiple_Interval()
});
}

[Test]
public void Note_ByInterval_OutOfRange_Up() => Assert.Throws<ArgumentException>(() =>
new PatternBuilder()
.SetRootNote(DryWetMidi.MusicTheory.Note.Get((SevenBitNumber)30))
.Note(Interval.FromHalfSteps(100)));

[Test]
public void Note_ByInterval_OutOfRange_Down() => Assert.Throws<ArgumentException>(() =>
new PatternBuilder()
.SetRootNote(DryWetMidi.MusicTheory.Note.Get((SevenBitNumber)30))
.Note(-Interval.FromHalfSteps(100)));

[Test]
public void Note_ByString()
{
Expand Down
6 changes: 5 additions & 1 deletion DryWetMidi/Composing/PatternBuilder.Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ public PatternBuilder Note(Interval interval, ITimeSpan length = null, SevenBitN
{
ThrowIfArgument.IsNull(nameof(interval), interval);

var newNoteNumber = RootNote.NoteNumber + interval.HalfSteps;
if (newNoteNumber < SevenBitNumber.MinValue || newNoteNumber > SevenBitNumber.MaxValue)
throw new ArgumentException($"The result of transposition ({newNoteNumber}) of the current root note ({RootNote}) by the specified interval is out of valid note's number range.", nameof(newNoteNumber));

return Note(
RootNote.Transpose(interval),
MusicTheory.Note.Get((SevenBitNumber)newNoteNumber),
length ?? NoteLength,
velocity ?? Velocity);
}
Expand Down

0 comments on commit 6a4a1c1

Please sign in to comment.