reader

package
v1.16.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2020 License: MIT Imports: 15 Imported by: 68

Documentation

Overview

Package reader provides an easy abstraction for reading of cable MIDI and SMF (Standard MIDI File) data.

First create new Reader with callbacks. To read a SMF file use ReadSMFFile. To read cable MIDI, use the ListenTo method with an input port.

See the examples in the top level examples folder.

To connect with the MIDI ports of your computer use it with the adapter package for rtmidi (gitlab.com/gomidi/rtmididrv) or portmidi (gitlab.com/gomidi/portmididrv).

Index

Constants

View Source
const LiveResolution = smf.MetricTicks(1920)

LiveResolution is the resolution used for live over the wire reading with Reader.ReadFrom

Variables

This section is empty.

Functions

func Duration

func Duration(r *Reader, deltaticks uint32) time.Duration

Duration returns the duration for the given delta ticks, respecting the current tempo

func ReadAll

func ReadAll(r *Reader) error

ReadAll reads midi messages until an error happens

func ReadAllFrom

func ReadAllFrom(r *Reader, src io.Reader) (err error)

ReadAllFrom reads midi messages from src until an error happens (for "live" MIDI data "over the wire"). io.EOF is the expected error that is returned when reading should stop.

ReadAllFrom does not close the src.

The messages are dispatched to the corresponding attached functions of the Reader.

They must be attached before Reader.ReadAllFrom is called and they must not be unset or replaced until Read returns. For more infomation about dealing with the MIDI messages, see Reader.

func ReadFrom

func ReadFrom(r *Reader, rd midi.Reader)

func ReadSMF

func ReadSMF(r *Reader, src io.Reader, options ...smfreader.Option) error

ReadAllSMF reads midi messages from src (which is supposed to be the content of a standard midi file (SMF)) until an error or io.EOF happens.

ReadAllSMF does not close the src.

If the read content was a valid midi file, nil is returned.

The messages are dispatched to the corresponding attached functions of the Reader.

They must be attached before Reader.ReadSMF is called and they must not be unset or replaced until ReadSMF returns. For more infomation about dealing with the SMF midi messages, see Reader and SMFPosition.

func ReadSMFFile

func ReadSMFFile(r *Reader, file string, options ...smfreader.Option) error

ReadSMFFile open, reads and closes a complete SMF file. If the read content was a valid midi file, nil is returned.

The messages are dispatched to the corresponding attached functions of the handler.

They must be attached before Reader.ReadSMF is called and they must not be unset or replaced until ReadSMF returns. For more infomation about dealing with the SMF midi messages, see Reader and SMFPosition.

func ReadSMFFileHeader

func ReadSMFFileHeader(r *Reader, file string, options ...smfreader.Option) (smf.Header, error)

ReadSMFFileHeader reads just the header of a SMF file

func ReadSMFFrom

func ReadSMFFrom(r *Reader, rd smf.Reader) error

func Resolution

func Resolution(r *Reader) uint32

Resolution returns the ticks of a quarternote If it can't be determined, 0 is returned

func Ticks

func Ticks(r *Reader, d time.Duration) uint32

Ticks returns the ticks that correspond to a duration while respecting the current tempo If it can't be determined, 0 is returned

func TimeAt

func TimeAt(r *Reader, absTicks uint64) *time.Duration

TimeAt returns the time.Duration at the given absolute position counted from the beginning of the file, respecting all the tempo changes in between. If the time format is not of type smf.MetricTicks, nil is returned.

Types

type Callback

type Callback func(r *Reader)

func Aftertouch

func Aftertouch(cb func(p *Position, channel, pressure uint8)) Callback

Aftertouch is called for aftertouch messages (aka "channel pressure")

func ControlChange

func ControlChange(cb func(p *Position, channel, controller, value uint8)) Callback

Each is called for every control change message If RPN or NRPN callbacks are defined, the corresponding control change messages will not be passed to each and the corrsponding RPN/NRPN callback are called.

func Copyright(cb func(p Position, text string)) Callback

Copyright is called for the copyright message

func Cuepoint

func Cuepoint(cb func(p Position, text string)) Callback

Cuepoint is called for the cuepoint message

func DeprecatedChannel

func DeprecatedChannel(cb func(p Position, channel uint8)) Callback

Channel is called for the deprecated MIDI channel message

func DeprecatedPort

func DeprecatedPort(cb func(p Position, port uint8)) Callback

Port is called for the deprecated MIDI port message

func Device

func Device(cb func(p Position, name string)) Callback

Device is called for the device port message

func Each

func Each(cb func(*Position, midi.Message)) Callback

Each is called for every MIDI message in addition to the other callbacks.

func EndOfTrack

func EndOfTrack(cb func(p Position)) Callback

EndOfTrack is called for the end of a track message

func Instrument

func Instrument(cb func(p Position, name string)) Callback

Instrument is called for the instrument (name) message

func Key

func Key(cb func(p Position, key uint8, ismajor bool, num_accidentals uint8, accidentals_are_flat bool)) Callback

Key is called for the key signature (change) message

func Lyric

func Lyric(cb func(p Position, text string)) Callback

Lyric is called for the lyric message

func MTC

func MTC(cb func(frame uint8)) Callback

MTC is called for a MIDI timing code message

func Marker

func Marker(cb func(p Position, text string)) Callback

Marker is called for the marker message

func NoteOff

func NoteOff(cb func(p *Position, channel, key, velocity uint8)) Callback

NoteOff is called for noteoff messages (then the given velocity is passed) and for noteon messages of velocity 0 (then velocity is 0).

func NoteOn

func NoteOn(cb func(p *Position, channel, key, velocity uint8)) Callback

NoteOn is just called for noteon messages with a velocity > 0. Noteon messages with velocity == 0 will trigger NoteOff with a velocity of 0.

func NrpnDecrement

func NrpnDecrement(cb func(p *Position, channel, typ1, typ2 uint8)) Callback

Decrement is called, when the decrement of a NRPN arrives

func NrpnIncrement

func NrpnIncrement(cb func(p *Position, channel, typ1, typ2 uint8)) Callback

Increment is called, when the increment of a NRPN arrives

func NrpnLSB

func NrpnLSB(cb func(p *Position, channel uint8, typ1, typ2, msbVal uint8)) Callback

LSB is called, when the LSB of a NRPN arrives

func NrpnMSB

func NrpnMSB(cb func(p *Position, channel uint8, typ1, typ2, msbVal uint8)) Callback

MSB is called, when the MSB of a NRPN arrives

func NrpnReset

func NrpnReset(cb func(p *Position, channel uint8)) Callback

Reset is called, when the reset or null NRPN arrives

func Pitchbend

func Pitchbend(cb func(p *Position, channel uint8, value int16)) Callback

Pitchbend is called for pitch bend messages

func PolyAftertouch

func PolyAftertouch(cb func(p *Position, channel, key, pressure uint8)) Callback

PolyAftertouch is called for polyphonic aftertouch messages (aka "key pressure").

func Program

func Program(cb func(p Position, text string)) Callback

Program is called for the program name message

func ProgramChange

func ProgramChange(cb func(p *Position, channel, program uint8)) Callback

ProgramChange is called for program change messages. Program numbers start with 0.

func RTActivesense

func RTActivesense(cb func()) Callback

Activesense is called for a active sense message

func RTClock

func RTClock(cb func()) Callback

Clock is called for a clock message

func RTContinue

func RTContinue(cb func()) Callback

Continue is called for a continue message

func RTReset

func RTReset(cb func()) Callback

Reset is called for a reset message

func RTStart

func RTStart(cb func()) Callback

Start is called for a start message

func RTStop

func RTStop(cb func()) Callback

Stop is called for a stop message

func RTTick

func RTTick(cb func()) Callback

Tick is called for a tick message

func RpnDecrement

func RpnDecrement(cb func(p *Position, channel, typ1, typ2 uint8)) Callback

Decrement is called, when the decrement of a RPN arrives

func RpnIncrement

func RpnIncrement(cb func(p *Position, channel, typ1, typ2 uint8)) Callback

Increment is called, when the increment of a RPN arrives

func RpnLSB

func RpnLSB(cb func(p *Position, channel, typ1, typ2, lsbVal uint8)) Callback

LSB is called, when the MSB of a RPN arrives

func RpnMSB

func RpnMSB(cb func(p *Position, channel, typ1, typ2, msbVal uint8)) Callback

MSB is called, when the MSB of a RPN arrives

func RpnReset

func RpnReset(cb func(p *Position, channel uint8)) Callback

Reset is called, when the reset or null RPN arrives

func SMFHeader

func SMFHeader(cb func(smf.Header)) Callback

func SMPTE

func SMPTE(cb func(p Position, hour, minute, second, frame, fractionalFrame byte)) Callback

SMPTE is called for the smpte offset message

func SPP

func SPP(cb func(pos uint16)) Callback

SPP is called for a song position pointer message

func SequenceNo

func SequenceNo(cb func(p Position, number uint16)) Callback

SequenceNo is called for the sequence number message

func SequencerData

func SequencerData(cb func(p Position, data []byte)) Callback

SequencerData is called for the sequencer specific message

func SongSelect

func SongSelect(cb func(num uint8)) Callback

SongSelect is called for a song select message

func SysEx

func SysEx(cb func(p *Position, data []byte)) Callback

Complete is called for a complete system exclusive message

func SysExContinue

func SysExContinue(cb func(p *Position, data []byte)) Callback

Continue is called for a continuing system exclusive message

func SysExEnd

func SysExEnd(cb func(p *Position, data []byte)) Callback

End is called for an ending system exclusive message

func SysExEscape

func SysExEscape(cb func(p *Position, data []byte)) Callback

Escape is called for an escaping system exclusive message

func SysExStart

func SysExStart(cb func(p *Position, data []byte)) Callback

Start is called for a starting system exclusive message

func TempoBPM

func TempoBPM(cb func(p Position, bpm float64)) Callback

TempoBPM is called for the tempo (change) message, BPM is fractional

func Text

func Text(cb func(p Position, text string)) Callback

Text is called for the text message

func TimeSig

func TimeSig(cb func(p Position, num, denom uint8)) Callback

TimeSigis called for the time signature (change) message

func TrackSequenceName

func TrackSequenceName(cb func(p Position, name string)) Callback

TrackSequenceName is called for the sequence / track name message If in a format 0 track, or the first track in a format 1 file, the name of the sequence. Otherwise, the name of the track.

func Tune

func Tune(cb func()) Callback

Tune is called for a tune request message

func Undefined

func Undefined(cb func(p Position, typ byte, data []byte)) Callback

Undefined is called for the undefined meta message

func Unknown

func Unknown(cb func(p *Position, msg midi.Message)) Callback

Unknown is called for unknown messages

type Logger

type Logger interface {
	Printf(format string, vals ...interface{})
}

Logger is the inferface used by Reader for logging incoming messages.

type Option

type Option func(*Reader)

ReaderOption configures the reader

func IgnoreMIDIClock

func IgnoreMIDIClock() Option

IgnoreMIDIClock lets the reader not use MIDI clock to calculate the tempo

func NoLogger

func NoLogger() Option

NoLogger is an option to disable the defaut logging of a Reader

func Options

func Options(options ...midireader.Option) Option

func SetLogger

func SetLogger(l Logger) Option

SetLogger allows to set a custom logger for the Reader

type Position

type Position struct {

	// Track is the number of the track, starting with 0
	Track int16

	// DeltaTicks is number of ticks that passed since the previous message in the same track
	DeltaTicks uint32

	// AbsoluteTicks is the number of ticks that passed since the beginning of the track
	AbsoluteTicks uint64
}

Position is the position of the event inside a standard midi file (SMF) or since start listening on a connection.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader allows the reading of either "over the wire" MIDI data (via Read) or SMF MIDI data (via ReadSMF or ReadSMFFile).

Before any of the Read* methods are called, callbacks for the MIDI messages of interest need to be attached to the Reader. These callbacks are then invoked when the corresponding MIDI message arrives. They must not be changed while any of the Read* methods is running.

It is possible to share the same Reader for reading of the wire MIDI ("live") and SMF Midi data as long as not more than one Read* method is running at a point in time. However, only channel messages and system exclusive message may be used in both cases. To enable this, the corresponding callbacks receive a pointer to the Position of the MIDI message. This pointer is always nil for "live" MIDI data and never nil when reading from a SMF.

The SMF header callback and the meta message callbacks are only called, when reading data from an SMF. Therefore the Position is passed directly and can't be nil.

System common and realtime message callbacks will only be called when reading "live" MIDI, so they get no Position.

func New

func New(opt Option, callbacks ...Callback) *Reader

New returns a new Reader

func (*Reader) Delta

func (r *Reader) Delta() uint32

func (*Reader) Header

func (r *Reader) Header() smf.Header

func (*Reader) ListenTo

func (r *Reader) ListenTo(in midi.In) error

ListenTo connects the reader to the given MIDI in connection and listens for incoming messages.

func (*Reader) Read

func (r *Reader) Read() (m midi.Message, err error)

Read reads a midi.Message and dispatches it.

func (*Reader) ReadHeader

func (r *Reader) ReadHeader() error

func (*Reader) TempoBPM

func (r *Reader) TempoBPM() float64

BPM returns the current tempo in BPM (beats per minute)

func (*Reader) Track

func (r *Reader) Track() int16

Jump to

Keyboard shortcuts

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