midireader

package
v0.8.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 11, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package midireader provides a reader for live/streaming/"over the wire" MIDI data.

There is also a more comfortable package that has it all:

github.com/gomidi/midi/mid

Usage

import (
	"github.com/gomidi/midi/midireader"
	"github.com/gomidi/midi/midimessage/channel"    // (Channel Messages)
	"github.com/gomidi/midi/midimessage/realtime"   // (System Realtime Messages)

	// you may also want to use these
	// github.com/gomidi/midi/midimessage/cc         (Control Change Messages)
	// github.com/gomidi/midi/midimessage/syscommon  (System Common Messages)
	// github.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 channel.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 channel.NoteOff:
		...
	}
}

// deal with err
Example
package main

import (
	"bytes"
	"fmt"

	"github.com/gomidi/midi/midimessage/channel"
	"github.com/gomidi/midi/midimessage/realtime"
	"github.com/gomidi/midi/midireader"
	"github.com/gomidi/midi/midiwriter"

	"github.com/gomidi/midi"
)

func main() {
	var bf bytes.Buffer

	wr := midiwriter.New(&bf)
	wr.Write(channel.Ch2.NoteOn(65, 90))
	wr.Write(realtime.Reset)
	wr.Write(channel.Ch2.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
		}

		switch v := m.(type) {
		case channel.NoteOn:
			fmt.Printf("NoteOn at channel %v: key: %v velocity: %v\n", v.Channel(), v.Key(), v.Velocity())
		case channel.NoteOff:
			fmt.Printf("NoteOff at channel %v: key: %v\n", v.Channel(), v.Key())
		}

	}

}
Output:

NoteOn at channel 2: key: 65 velocity: 90
Realtime: Reset
NoteOff at channel 2: key: 65

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(src io.Reader, rthandler func(realtime.Message), options ...Option) midi.Reader

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 former are returned as NoteOffVelocity messages and keep the given velocity, the later 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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL