1 MIDI Connections
midi-open
midi-close
2 MIDI Messages
note-on
note-off
aftertouch
program-change
3 Example
Version: 5.0.1.2

CoreMIDI

 (require (planet evhan/coremidi))

This module provides a minimal interface to Apple’s CoreMIDI library.

It provides for the creation & destruction of MIDI connections and for the following MIDI messages: Note On, Note Off, Program Change, and Aftertouch.

1 MIDI Connections

In order to send MIDI messages anywhere you must first create a MIDI connection:

(midi-open [destination-n    
  client-name    
  port-name])  midi-connection?
  destination-n : integer? = 0
  client-name : string? = "rkt client"
  port-name : string? = "rkt outport"

Opens a midi-connection to the destination-nth available MIDI destination (as provided by the CoreMIDI system), or the first available destination if no destination-n is given. client-name and port-name can optionally be the identifiers assigned to the new midi-connection’s internal components within the CoreMIDI system. For most purposes you can ignore these names.

midi-connection handles will remain open until closed:

(midi-close midi-destination)  void?
  midi-destination : midi-destination?

Destroys a midi-connection (previously created with midi-open).

2 MIDI Messages

The following MIDI messaging procedures all take a midi-connection as a first argument. This will be the destination to which the message is sent.

(note-on midi channel note [velocity])  void?
  midi : midi-connection?
  channel : integer?
  note : integer?
  velocity : integer? = 64

Sends a MIDI Note On signal to the given midi-connection on the given channel. note is the MIDI note number and velocity is optionally the note’s velocity. Both are integers from 0-127.

(note-off midi channel note [velocity])  void?
  midi : midi-connection?
  channel : integer?
  note : integer?
  velocity : integer? = 64

Entirely mimics note-on’s functionality, except that it sends a MIDI Note Off message instead of a Note On.

(aftertouch midi channel note touch)  void?
  midi : midi-connection?
  channel : integer?
  note : integer?
  touch : integer?

Sends a MIDI Aftertouch signal to the given midi-connection on the given channel. note is the note value and touch is the weight of the aftertouch. Both are integers from 0-127.

(program-change midi channel preset)  void?
  midi : midi-connection?
  channel : integer?
  preset : integer?

Sends a MIDI Program Change signal to the given midi-connection on the given channel. preset is the program number from 0-127.

3 Example

Here’s a simple example making use of most of the functions defined above. It simply plays a C major scale at 60 bpm.

  (define (play-each notes)
    (define midi (midi-open))
    (program-change midi 1 0)
    (for/list ((note notes))
      (note-on midi 1 note 120)
      (sleep 1)
      (note-off midi 1 note))
    (midi-close midi) (void))
  
  (play-each '(60 62 64 65 67 69 71 72))

Please let me know of any bugs you encounter.