Output device
In DryWetMIDI an output MIDI device is represented by IOutputDevice interface. It allows to send events to a MIDI device. To understand what an output MIDI device is in DryWetMIDI, please read Overview article.
The library provides built-in implementation of IOutputDevice
: OutputDevice. To get an instance of OutputDevice
you can use either GetByName or GetByIndex static methods. To retrieve count of output MIDI devices presented in the system there is the GetDevicesCount method. You can get all output MIDI devices with GetAll method:
using System;
using Melanchall.DryWetMidi.Multimedia;
// ...
foreach (var outputDevice in OutputDevice.GetAll())
{
Console.WriteLine(outputDevice.Name);
}
Important
You can use OutputDevice
built-in implementation of IOutputDevice
only on the systems listed in the Supported OS article. Of course you can create your own implementation of IOutputDevice
as described in Custom output device section below.
After an instance of OutputDevice
is obtained, you can send MIDI events to device via SendEvent method. You cannot send meta events since such events can be inside a MIDI file only. If you pass an instance of meta event class, SendEvent
will do nothing. EventSent event will be fired for each event sent with SendEvent
(except meta events) holding the MIDI event sent. The value of DeltaTime property of MIDI events will be ignored, events will be sent to device immediately. To take delta-times into account, use Playback class.
If you need to interrupt all currently sounding notes, call the TurnAllNotesOff method which will send Note Off events on all channels for all note numbers (kind of "panic" button on MIDI devices).
Small example that shows sending MIDI data:
using System;
using Melanchall.DryWetMidi.Multimedia;
using Melanchall.DryWetMidi.Core;
// ...
using (var outputDevice = OutputDevice.GetByName("Some MIDI device"))
{
outputDevice.EventSent += OnEventSent;
outputDevice.SendEvent(new NoteOnEvent());
outputDevice.SendEvent(new NoteOffEvent());
}
// ...
private void OnEventSent(object sender, MidiEventSentEventArgs e)
{
var midiDevice = (MidiDevice)sender;
Console.WriteLine($"Event sent to '{midiDevice.Name}' at {DateTime.Now}: {e.Event}");
}
Important
You must always take care about disposing an OutputDevice
, so use it inside using
block or call Dispose
manually. Without it all resources taken by the device will live until GC collect them via finalizer of the OutputDevice
. It means that sometimes you will not be able to use different instances of the same device across multiple applications or different pieces of a program.
First call of SendEvent
method can take some time for allocating resources for device, so if you want to eliminate this operation on sending a MIDI event, you can call PrepareForEventsSending method before any MIDI event will be sent.
Custom output device
You can create your own output device implementation and use it in your app. For example, let's create super simple device that just outputs MIDI events to console:
private sealed class ConsoleOutputDevice : IOutputDevice
{
public event EventHandler<MidiEventSentEventArgs> EventSent;
public void PrepareForEventsSending()
{
}
public void SendEvent(MidiEvent midiEvent)
{
Console.WriteLine(midiEvent);
}
public void Dispose()
{
}
}
You can then use this device, for example, for debug in Playback.
Another one use case for custom output device is plugging some synth. So you create output device where SendEvent will redirect MIDI events to synth.