Skip to content
Mathis edited this page Oct 29, 2021 · 7 revisions

Introduction

This page gives some simple code snippets to give you a sense of how the midi-dot-net API works. For more complete, working examples see the demo program in the source distribution.

Output

This snippet opens the first MIDI output device, sends a note On event, and then bends the pitch down. See Example02.cs for a more complete example.

C#:

OutputDevice outputDevice = OutputDevice.InstalledDevices[0];
outputDevice.Open();
outputDevice.SendNoteOn(Channel.Channel1, Pitch.C4, 80);  // Middle C, velocity 80
outputDevice.SendPitchBend(Channel.Channel1, 7000);  // 8192 is centered, so 7000 is bent down

VB.NET:

Dim outputDevice As OutputDevice = OutputDevice.InstalledDevices(0)
outputDevice.Open
outputDevice.SendNoteOn(Channel.Channel1, Pitch.C4, 80)  'Mddle C, velocity 80
outputDevice.SendPitchBend(Channel.Channel1, 7000)  '8192 is centered, so 7000 is bent down

Input

This snippet opens the first MIDI input device, registers a NoteOn event listener, and then prints the names of notes played until a key on the computer keyboard is pressed.

C#:

public static void NoteOn(NoteOnMessage msg) {
    Console.WriteLine(msg.Pitch.NotePreferringSharps());
}
...
InputDevice inputDevice = InputDevice.InstalledDevices[0];
inputDevice.Open();
inputDevice.NoteOn += new InputDevice.NoteOnHandler(NoteOn);
inputDevice.StartReceiving(null);  // Note events will be received in another thread
Console.ReadKey();  // This thread waits for a keypress

VB.NET:

Public Shared Sub NoteOn(ByVal msg As NoteOnMessage)
    Console.WriteLine(msg.Pitch.NotePreferringSharps)
End Sub
...
Private inputDevice As InputDevice = InputDevice.InstalledDevices(0)
inputDevice.Open
AddHandler inputDevice.NoteOn, AddressOf NoteOn
inputDevice.StartReceiving(Nothing)  'Note events will be received in another thread
Console.ReadKey  'This thread waits for a keypress

Input, Output, and Scheduling

This class implements a simple arpeggiator. It listens to an input device, and for each note played, it plays the major third one beat later, and the perfect fifth two beats later. The effect is an arpeggiated major triad chord. Time-delayed behavior like this is easy to implement with Clock's scheduler. (For a full example, see Example04.cs).

C#:

class Arpeggiator {
    public Arpeggiator(InputDevice inputDevice, OutputDevice outputDevice, Clock clock) {
        this.inputDevice = inputDevice;
        this.outputDevice = outputDevice;
        this.clock = clock;
        
        if (inputDevice != null) {
            inputDevice.NoteOn += new InputDevice.NoteOnHandler(this.NoteOn);
            inputDevice.NoteOff += new InputDevice.NoteOffHandler(this.NoteOff);
        }
    }
    
    public void NoteOn(NoteOnMessage msg) {
        clock.Schedule(new NoteOnMessage(outputDevice, msg.Channel, msg.Pitch + 4,  msg.Velocity, msg.BeatTime + 1));
        clock.Schedule(new NoteOnMessage(outputDevice, msg.Channel, msg.Pitch + 7, msg.Velocity, msg.BeatTime + 2));
    }

    public void NoteOff(NoteOffMessage msg) {
        clock.Schedule(new NoteOffMessage(outputDevice, msg.Channel, msg.Pitch + 4, msg.Velocity, msg.BeatTime + 1));
        clock.Schedule(new NoteOffMessage(outputDevice, msg.Channel, msg.Pitch + 7, msg.Velocity, msg.BeatTime + 2));
    }

    private InputDevice inputDevice;
    private OutputDevice outputDevice;
    private Clock clock;
}

VB.NET:

Class Arpeggiator
    
    Public Sub New(ByVal inputDevice As InputDevice, ByVal outputDevice As OutputDevice, ByVal clock As Clock)
        MyBase.New
        Me.inputDevice = inputDevice
        Me.outputDevice = outputDevice
        Me.clock = clock
        If (Not (inputDevice) Is Nothing) Then
            inputDevice.NoteOn = (inputDevice.NoteOn + New InputDevice.NoteOnHandler(Me.NoteOn))
            inputDevice.NoteOff = (inputDevice.NoteOff + New InputDevice.NoteOffHandler(Me.NoteOff))
        End If        
    End Sub
    
    Public Sub NoteOn(ByVal msg As NoteOnMessage)
        clock.Schedule(New NoteOnMessage(outputDevice, msg.Channel, (msg.Pitch + 4), msg.Velocity, (msg.BeatTime + 1)))
        clock.Schedule(New NoteOnMessage(outputDevice, msg.Channel, (msg.Pitch + 7), msg.Velocity, (msg.BeatTime + 2)))
    End Sub
    
    Public Sub NoteOff(ByVal msg As NoteOffMessage)
        clock.Schedule(New NoteOffMessage(outputDevice, msg.Channel, (msg.Pitch + 4), msg.Velocity, (msg.BeatTime + 1)))
        clock.Schedule(New NoteOffMessage(outputDevice, msg.Channel, (msg.Pitch + 7), msg.Velocity, (msg.BeatTime + 2)))
    End Sub
    
    Private inputDevice As InputDevice
    Private outputDevice As OutputDevice
    Private clock As Clock
End Class
Clone this wiki locally