Product
Introducing Ruby Support in Socket
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
github.com/gomidi/mid
Porcelain library for reading and writing MIDI and SMF (Standard MIDI File)
Based on https://github.com/gomidi/midi.
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
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 portTo 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 portFor 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.
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"
)
// callback for note on messages
func noteOn(p *mid.Position, channel, key, vel uint8) {
fmt.Printf("NoteOn (ch %v: key %v vel: %v)\n", channel, key, vel)
}
// callback for note off messages
func noteOff(p *mid.Position, channel, key, vel uint8) {
fmt.Printf("NoteOff (ch %v: key %v)\n", channel, key)
}
func main() {
fmt.Println()
// to disable logging, pass mid.NoLogger() as option
rd := mid.NewReader()
// set the functions for the messages you are interested in
rd.Msg.Channel.NoteOn = noteOn
rd.Msg.Channel.NoteOff = noteOff
// to allow reading and writing concurrently in this example
// we need a pipe
piperd, pipewr := io.Pipe()
go func() {
wr := mid.NewWriter(pipewr)
wr.SetChannel(11) // sets the channel for the next messages
wr.NoteOn(120, 50)
time.Sleep(time.Second) // let the note ring for 1 sec
wr.NoteOff(120)
pipewr.Close() // finishes the writing
}()
for {
if rd.Read(piperd) == io.EOF {
piperd.Close() // finishes the reading
break
}
}
// Output:
// channel.NoteOn channel 11 key 120 velocity 50
// NoteOn (ch 11: key 120 vel: 50)
// channel.NoteOff channel 11 key 120
// NoteOff (ch 11: key 120)
}
API mostly stable and complete
It is recommended to use Go 1.11 with module support ($GO111MODULE=on
).
go get -d github.com/gomidi/mid/...
MIT (see LICENSE file)
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket is launching Ruby support for all users. Enhance your Rails projects with AI-powered security scans for vulnerabilities and supply chain threats. Now in Beta!
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.