mid
Porcelain library for reading and writing MIDI and SMF (Standard MIDI File)
Based on https://github.com/gomidi/midi.

Notice - Moved
Development has moved to GitLab https://gitlab.com/gomidi. It is recommended to use the code there instead, as the repos at Gitlab contains newer fixes, and some import references contained in the github repo may be broken
Description
Package mid provides an easy abstraction for reading and writing of "live" MIDI
and SMF
(Standard MIDI File) data.
MIDI
data could be written the following ways:
NewWriter
is used to write "live" MIDI to an io.Writer
.
NewSMF
is used to write SMF MIDI to an io.Writer
.
NewSMFFile
is used to write a complete SMF file.
WriteTo
writes "live" MIDI to an connect.Out
, aka MIDI out port
To read, create a Reader
and attach callbacks to it.
Then MIDI data could be read the following ways:
Reader.Read
reads "live" MIDI from an io.Reader
.
Reader.ReadSMF
reads SMF MIDI from an io.Reader
.
Reader.ReadSMFFile
reads a complete SMF file.
Reader.ReadFrom
reads "live" MIDI from an connect.In
, aka MIDI in port
For a simple example with "live" MIDI and io.Reader
and io.Writer
see the example below.
To connect with the MIDI ports of your computer (via connect.In and connect.Out), use it with one of the
driver packages for rtmidi
and portaudio
at https://github.com/gomidi/connect.
There you can find a simple example how to do it.
Example
We use an io.Writer
to write to and io.Reader
to read from. They are connected by the same io.Pipe
.
package main
import (
"fmt"
"github.com/gomidi/mid"
"io"
"time"
)
func noteOn(p *mid.Position, channel, key, vel uint8) {
fmt.Printf("NoteOn (ch %v: key %v vel: %v)\n", channel, key, vel)
}
func noteOff(p *mid.Position, channel, key, vel uint8) {
fmt.Printf("NoteOff (ch %v: key %v)\n", channel, key)
}
func main() {
fmt.Println()
rd := mid.NewReader()
rd.Msg.Channel.NoteOn = noteOn
rd.Msg.Channel.NoteOff = noteOff
piperd, pipewr := io.Pipe()
go func() {
wr := mid.NewWriter(pipewr)
wr.SetChannel(11)
wr.NoteOn(120, 50)
time.Sleep(time.Second)
wr.NoteOff(120)
pipewr.Close()
}()
for {
if rd.Read(piperd) == io.EOF {
piperd.Close()
break
}
}
}
Status
API mostly stable and complete
- Go version: >= 1.10
- OS/architectures: everywhere Go runs (tested on Linux and Windows).
Installation
It is recommended to use Go 1.11 with module support ($GO111MODULE=on
).
go get -d github.com/gomidi/mid/...
License
MIT (see LICENSE file)