Skip to content

Commit

Permalink
Validating the MidiMessage helper parameters a bit better
Browse files Browse the repository at this point in the history
  • Loading branch information
markheath committed Sep 7, 2015
1 parent d8d8511 commit 725e269
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions NAudio/Midi/MidiMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,48 @@ public MidiMessage(int rawData)
/// <summary>
/// Creates a Note On message
/// </summary>
/// <param name="note">Note number</param>
/// <param name="volume">Volume</param>
/// <param name="channel">MIDI channel</param>
/// <param name="note">Note number (0 to 127)</param>
/// <param name="volume">Volume (0 to 127)</param>
/// <param name="channel">MIDI channel (1 to 16)</param>
/// <returns>A new MidiMessage object</returns>
public static MidiMessage StartNote(int note, int volume, int channel)
{
ValidateNoteParameters(note, volume, channel);
return new MidiMessage((int)MidiCommandCode.NoteOn + channel - 1, note, volume);
}

private static void ValidateNoteParameters(int note, int volume, int channel)
{
ValidateChannel(channel);
if (note < 0 || note > 127)
{
throw new ArgumentOutOfRangeException("note", "Note number must be in the range 0-127");
}
if (volume < 0 || volume > 127)
{
throw new ArgumentOutOfRangeException("volume", "Velocity must be in the range 0-127");
}
}

private static void ValidateChannel(int channel)
{
if ((channel < 1) || (channel > 16))
{
throw new ArgumentOutOfRangeException("channel", channel,
String.Format("Channel must be 1-16 (Got {0})", channel));
}
}

/// <summary>
/// Creates a Note Off message
/// </summary>
/// <param name="note">Note number</param>
/// <param name="volume">Volume</param>
/// <param name="volume">Volume </param>
/// <param name="channel">MIDI channel (1-16)</param>
/// <returns>A new MidiMessage object</returns>
public static MidiMessage StopNote(int note, int volume, int channel)
{
ValidateNoteParameters(note, volume, channel);
return new MidiMessage((int)MidiCommandCode.NoteOff + channel - 1, note, volume);
}

Expand All @@ -61,6 +85,7 @@ public static MidiMessage StopNote(int note, int volume, int channel)
/// <returns>A new MidiMessageObject</returns>
public static MidiMessage ChangePatch(int patch, int channel)
{
ValidateChannel(channel);
return new MidiMessage((int)MidiCommandCode.PatchChange + channel - 1, patch, 0);
}

Expand All @@ -73,6 +98,7 @@ public static MidiMessage ChangePatch(int patch, int channel)
/// <returns>A new MidiMessageObject</returns>
public static MidiMessage ChangeControl(int controller, int value, int channel)
{
ValidateChannel(channel);
return new MidiMessage((int)MidiCommandCode.ControlChange + channel - 1, controller, value);
}

Expand Down

0 comments on commit 725e269

Please sign in to comment.