reader

package
v1.23.7 Latest Latest
Warning

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

Go to latest
Published: May 26, 2021 License: MIT Imports: 15 Imported by: 73

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 Aftertouch

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

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

func ControlChange

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

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)) func(r *Reader)

Copyright is called for the copyright message

func Cuepoint

func Cuepoint(cb func(p Position, text string)) func(r *Reader)

Cuepoint is called for the cuepoint message

func DeprecatedChannel

func DeprecatedChannel(cb func(p Position, channel uint8)) func(r *Reader)

Channel is called for the deprecated MIDI channel message

func DeprecatedPort

func DeprecatedPort(cb func(p Position, port uint8)) func(r *Reader)

Port is called for the deprecated MIDI port message

func Device

func Device(cb func(p Position, name string)) func(r *Reader)

Device is called for the device port message

func Duration

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

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

func Each

func Each(cb func(*Position, midi.Message)) func(r *Reader)

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

func EndOfTrack

func EndOfTrack(cb func(p Position)) func(r *Reader)

EndOfTrack is called for the end of a track message

func IgnoreMIDIClock

func IgnoreMIDIClock() func(*Reader)

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

func Instrument

func Instrument(cb func(p Position, name string)) func(r *Reader)

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)) func(r *Reader)

Key is called for the key signature (change) message

func Lyric

func Lyric(cb func(p Position, text string)) func(r *Reader)

Lyric is called for the lyric message

func MTC

func MTC(cb func(frame uint8)) func(r *Reader)

MTC is called for a MIDI timing code message

func Marker

func Marker(cb func(p Position, text string)) func(r *Reader)

Marker is called for the marker message

func NoLogger

func NoLogger() func(*Reader)

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

func NoteOff

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

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)) func(r *Reader)

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)) func(r *Reader)

Decrement is called, when the decrement of a NRPN arrives

func NrpnIncrement

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

Increment is called, when the increment of a NRPN arrives

func NrpnLSB

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

LSB is called, when the LSB of a NRPN arrives

func NrpnMSB

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

MSB is called, when the MSB of a NRPN arrives

func NrpnReset

func NrpnReset(cb func(p *Position, channel uint8)) func(r *Reader)

Reset is called, when the reset or null NRPN arrives

func Pitchbend

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

Pitchbend is called for pitch bend messages

func PolyAftertouch

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

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

func Program

func Program(cb func(p Position, text string)) func(r *Reader)

Program is called for the program name message

func ProgramChange

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

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

func RTActivesense

func RTActivesense(cb func()) func(r *Reader)

Activesense is called for a active sense message

func RTClock

func RTClock(cb func()) func(r *Reader)

Clock is called for a clock message

func RTContinue

func RTContinue(cb func()) func(r *Reader)

Continue is called for a continue message

func RTReset

func RTReset(cb func()) func(r *Reader)

Reset is called for a reset message

func RTStart

func RTStart(cb func()) func(r *Reader)

Start is called for a start message

func RTStop

func RTStop(cb func()) func(r *Reader)

Stop is called for a stop message

func RTTick

func RTTick(cb func()) func(r *Reader)

Tick is called for a tick message

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(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 ReadSMFHeader added in v1.20.2

func ReadSMFHeader(in io.Reader, options ...smfreader.Option) (smf.Header, error)

ReadSMFHeader reads just the header of a SMF

func Resolution

func Resolution(r *Reader) uint32

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

func RpnDecrement

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

Decrement is called, when the decrement of a RPN arrives

func RpnIncrement

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

Increment is called, when the increment of a RPN arrives

func RpnLSB

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

LSB is called, when the MSB of a RPN arrives

func RpnMSB

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

MSB is called, when the MSB of a RPN arrives

func RpnReset

func RpnReset(cb func(p *Position, channel uint8)) func(r *Reader)

Reset is called, when the reset or null RPN arrives

func SMFHeader

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

func SMPTE

func SMPTE(cb func(p Position, hour, minute, second, frame, fractionalFrame byte)) func(r *Reader)

SMPTE is called for the smpte offset message

func SPP

func SPP(cb func(pos uint16)) func(r *Reader)

SPP is called for a song position pointer message

func SequenceNo

func SequenceNo(cb func(p Position, number uint16)) func(r *Reader)

SequenceNo is called for the sequence number message

func SequencerData

func SequencerData(cb func(p Position, data []byte)) func(r *Reader)

SequencerData is called for the sequencer specific message

func SetLogger

func SetLogger(l Logger) func(*Reader)

SetLogger allows to set a custom logger for the Reader

func SongSelect

func SongSelect(cb func(num uint8)) func(r *Reader)

SongSelect is called for a song select message

func SysEx

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

Complete is called for a complete system exclusive message

func SysExContinue

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

Continue is called for a continuing system exclusive message

func SysExEnd

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

End is called for an ending system exclusive message

func SysExEscape

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

Escape is called for an escaping system exclusive message

func SysExStart

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

Start is called for a starting system exclusive message

func TempoBPM

func TempoBPM(cb func(p Position, bpm float64)) func(r *Reader)

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

func Text

func Text(cb func(p Position, text string)) func(r *Reader)

Text is called for the text message

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.

func TimeSig

func TimeSig(cb func(p Position, num, denom uint8)) func(r *Reader)

TimeSigis called for the time signature (change) message

func TrackSequenceName

func TrackSequenceName(cb func(p Position, name string)) func(r *Reader)

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()) func(r *Reader)

Tune is called for a tune request message

func Undefined

func Undefined(cb func(p Position, typ byte, data []byte)) func(r *Reader)

Undefined is called for the undefined meta message

func Unknown

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

Unknown is called for unknown messages

Types

type Logger

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

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

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(callbacksAndOptions ...func(r *Reader)) *Reader

New returns a new Reader

func (*Reader) Callback added in v1.16.5

func (r *Reader) Callback(cb func(*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