midiwriter

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: 3 Imported by: 1

Documentation

Overview

Package midiwriter provides a writer 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/midiwriter"
	"github.com/gomidi/midi/midimessage/channel"     // (Channel Messages)
	"time"

	// you may also want to use these
	// github.com/gomidi/midi/midimessage/realtime   (System Realtime Messages)
	// github.com/gomidi/midi/midimessage/cc         (ControlChange Messages)
	// github.com/gomidi/midi/midimessage/syscommon  (System Common Messages)
	// github.com/gomidi/midi/midimessage/sysex      (system exclusive messages)
)

// given some output
var output io.Writer

wr := midiwriter.New(output)

// simulates pressing down key 65 on MIDI channel 3 with velocity 90
// MIDI channels 1-16 correspond to channel.Ch0 - channel.Ch15.
wr.Write(channel.Ch2.NoteOn(65, 90))

// simulates keep pressing for 1 sec
time.Sleep(time.Second)

// simulates releasing key 65 on MIDI channel 3
wr.Write(channel.Ch2.NoteOff(65))
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"

	"time"

	"github.com/gomidi/midi"
)

func main() {
	var bf bytes.Buffer

	wr := midiwriter.New(&bf, midiwriter.NoRunningStatus())
	wr.Write(channel.Ch2.NoteOn(65, 90))
	wr.Write(realtime.Reset)
	time.Sleep(time.Second)
	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(dest io.Writer, opts ...Option) (wr midi.Writer)

New returns a new midi.Writer.

The Writer does no buffering and makes no attempt to close dest.

By default the writer uses running status for efficiency. You can disable that behaviour by passing the NoRunningStatus() option. If you don't know what running status is, keep the default.

Types

type Option

type Option func(*config)

Option is a configuration option for a writer

func NoRunningStatus

func NoRunningStatus() Option

NoRunningStatus is an option for the writer that prevents it from using the running status.

Jump to

Keyboard shortcuts

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