Skip to content

Commit

Permalink
Merge pull request naudio#750 from brianavid/patch-1
Browse files Browse the repository at this point in the history
Update MidiInAndOut.md to document Sysex message handling
  • Loading branch information
markheath authored Mar 7, 2021
2 parents 5d00a97 + acd31f2 commit fb35ce8
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion Docs/MidiInAndOut.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,34 @@ When you're done with sending MIDI events, simply `Dispose` the device.

```c#
midiOut.Dispose();
```
```

## Sending and Receiving Sysex message events

Sending a Sysex message can be done using MidiOut.SendBuffer(). It is not necessary to build and send an entire message as a single SendBuffer call as long as you ensure that the calls are not asynchronously interleaved.
```c#
private static void SendSysex(byte[] message)
{
midiOut.SendBuffer(new byte[] { 0xF0, 0x00, 0x21, 0x1D, 0x01, 0x01 });
midiOut.SendBuffer(message);
midiOut.SendBuffer(new byte[] { 0xF7 });
}
```

Receiving Sysex messages requires two actions in addition to the midiIn handling above: (1) Allocate a number of buffers each large enough to receive an expected Sysex message from the device. (2) Subscribe to the SysexMessageReceived EventHandler property:
```c#
midiIn = new MidiIn(selectedDeviceIndex);
midiIn.MessageReceived += midiIn_MessageReceived;
midiIn.ErrorReceived += midiIn_ErrorReceived;
midiIn.CreateSysexBuffers(BufferSize, NumberOfBuffers);
midiIn.SysexMessageReceived += midiIn_SysexMessageReceived;
midiIn.Start();
```

The second parameter to the SysexMessageReceived EventHandler is of type MidiInSysexMessageEventArgs, which has a SysexBytes byte array property:
```c#
static void midiIn_SysexMessageReceived(object sender, MidiInSysexMessageEventArgs e)
{
byte[] sysexMessage = e.SysexBytes;
....
```

0 comments on commit fb35ce8

Please sign in to comment.