Documentation ¶
Overview ¶
Package midireader provides a reader for live/streaming/"over the wire" MIDI data.
Usage
import ( "gitlab.com/gomidi/midi/midireader" . "gitlab.com/gomidi/midi/midimessage/channel" // (Channel Messages) "gitlab.com/gomidi/midi/midimessage/realtime" // (System Realtime Messages) // you may also want to use these // gitlab.com/gomidi/midi/midimessage/cc (Control Change Messages) // gitlab.com/gomidi/midi/midimessage/syscommon (System Common Messages) // gitlab.com/gomidi/midi/midimessage/sysex (System Exclusive Messages) ) // given some MIDI input var input io.Reader // create a callback for realtime messages rthandler := func(m realtime.Message) { // deal with it if m == realtime.Start { ... } } rd := midireader.New(input), rthandler) // everything but realtime messages, since they are covered by rthandler var m midi.Message var err error for { m, err = rd.Read() // to interrupt, the input.Read method must return io.EOF or any other error if err != nil { break } // deal with them based on a type switch switch msg := m.(type) { case NoteOn: fmt.Printf( "NoteOn at channel %v: key %v velocity: %v\n", msg.Channel(), // MIDI channels 1-16 correspond to msg.Channel 0-15 msg.Key(), msg.Velocity(), ) case NoteOff: ... } } if err != io.EOF { // real error here }
Example ¶
var bf bytes.Buffer wr := midiwriter.New(&bf) wr.Write(Channel2.Pitchbend(5000)) wr.Write(Channel2.NoteOn(65, 90)) wr.Write(realtime.Reset) wr.Write(Channel2.NoteOff(65)) rthandler := func(m realtime.Message) { fmt.Printf("Realtime: %s\n", m) } rd := midireader.New(bytes.NewReader(bf.Bytes()), rthandler) var m midi.Message var err error for { m, err = rd.Read() // breaking at least with io.EOF if err != nil { break } // inspect fmt.Println(m) switch v := m.(type) { case NoteOn: fmt.Printf("NoteOn at channel %v: key: %v velocity: %v\n", v.Channel(), v.Key(), v.Velocity()) case NoteOff: fmt.Printf("NoteOff at channel %v: key: %v\n", v.Channel(), v.Key()) } } if err != io.EOF { panic("error: " + err.Error()) }
Output: channel.Pitchbend channel 2 value 5000 absValue 13192 channel.NoteOn channel 2 key 65 velocity 90 NoteOn at channel 2: key: 65 velocity: 90 Realtime: Reset channel.NoteOff channel 2 key 65 NoteOff at channel 2: key: 65
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func New ¶
New returns a new reader for reading MIDI messages. When calling Read, any intermediate System Realtime Message will be either ignored (if rthandler is nil) or passed to rthandler (if not) while other MIDI messages will be returned.
The Reader does no buffering and makes no attempt to close src. If src.Read returns an io.EOF, the reader stops reading and returns the error.
Types ¶
type Option ¶
type Option func(rd *reader)
Option is a configuration option for a reader
func NoteOffVelocity ¶
func NoteOffVelocity() Option
NoteOffVelocity is an option for the reader that lets it differentiate between "fake" noteoff messages (which are in fact noteon messages (typ 9) with velocity of 0) and "real" noteoff messages (typ 8) having their own velocity. The later are returned as NoteOffVelocity messages and keep the given velocity, the former are returned as NoteOff messages without velocity. That means in order to get all noteoff messages, there must be checks for NoteOff and NoteOffVelocity (if this option is set). If this option is not set, both kinds are returned as NoteOff (default).