midi

package
v2.22.1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: GPL-3.0 Imports: 15 Imported by: 0

Documentation

Overview

Package midi renders Standard MIDI Files (.mid, .midi, .kar and RIFF .rmi) to stereo float audio. By default notes are played on an emulated OPL3 FM chip by libADLMIDI with an embedded General MIDI bank, so MIDI works with nothing installed; when a SoundFont is configured the song is rendered from its samples by TinySoundFont instead. A Decoder is a beep StreamSeekCloser.

Index

Constants

View Source
const (
	// DefaultSampleRate is the output rate used when Options.SampleRate is 0.
	DefaultSampleRate = 48000
	// DefaultMaxVoices is the SoundFont voice cap used when
	// Options.MaxVoices is 0.
	DefaultMaxVoices = 64
	// MaxFileBytes is the largest MIDI file Open reads.
	MaxFileBytes = 8 << 20
	// MaxSoundFontBytes is the largest SoundFont file that is loaded.
	MaxSoundFontBytes = 512 << 20
	// DefaultSoundFontMemory is the SoundFont memory budget used when
	// Options.SoundFontMemory is 0.
	DefaultSoundFontMemory = 2 << 30

	// RendererFM and RendererSoundFont are the values of Info.Renderer.
	RendererFM        = "fm"
	RendererSoundFont = "soundfont"
)

Variables

View Source
var (
	// ErrSoundFont is wrapped by the error logged when the configured
	// SoundFont cannot be used. Open does not return it: the song is
	// rendered with the FM synthesizer instead.
	ErrSoundFont = errors.New("midi: cannot load SoundFont")
	// ErrClosed is returned by a Decoder used after Close.
	ErrClosed = errors.New("midi: decoder closed")
	// ErrTooLarge is returned for a MIDI file above MaxFileBytes.
	ErrTooLarge = errors.New("midi: file too large")
)

Functions

func Supported

func Supported(ext string) bool

Supported reports whether Open handles files with extension ext, which includes the dot and is matched case-insensitively.

Types

type Decoder

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

Decoder streams one rendered song. It is not safe for concurrent use.

func Open

func Open(path string, opts Options) (*Decoder, error)

Open renders the MIDI file at path.

func OpenBytes

func OpenBytes(data []byte, opts Options) (*Decoder, error)

OpenBytes renders a MIDI file held in memory. data may be a Standard MIDI File or an RMI file wrapping one; FM rendering also takes the other formats libADLMIDI reads (XMI, MUS and friends). A SoundFont that cannot be loaded is logged and the song is rendered with FM instead.

func (*Decoder) Close

func (d *Decoder) Close() error

Close frees the synthesizer. It is safe to call more than once.

func (*Decoder) Err

func (d *Decoder) Err() error

Err returns the error that stopped the stream, if any.

func (*Decoder) Info

func (d *Decoder) Info() Info

Info describes the song and the renderer playing it.

func (*Decoder) Len

func (d *Decoder) Len() int

Len is the stream length in frames, including a release tail of about a second after the last event.

func (*Decoder) Position

func (d *Decoder) Position() int

Position is the current frame.

func (*Decoder) SampleRate

func (d *Decoder) SampleRate() int

SampleRate is the output rate in Hz.

func (*Decoder) Seek

func (d *Decoder) Seek(p int) error

Seek moves playback to frame p. Notes sounding before the seek are released; notes that started before p are not restarted, as in any MIDI sequencer.

func (*Decoder) Stream

func (d *Decoder) Stream(samples [][2]float64) (n int, ok bool)

Stream fills samples with rendered audio.

type Info

type Info struct {
	// Title comes from the karaoke title, track name or text meta events;
	// it is "" when the file has none.
	Title string
	// Renderer is RendererFM or RendererSoundFont for a Decoder, and ""
	// from ReadInfo, which does not pick a renderer.
	Renderer string
}

Info describes a song.

func ReadInfo

func ReadInfo(path string, maxBytes int64) (Info, error)

ReadInfo reads a MIDI file's metadata without rendering it. At most maxBytes of the file are read (MaxFileBytes when maxBytes <= 0); the title sits near the start, so a prefix is enough.

type Options

type Options struct {
	// SoundFontPath selects SoundFont rendering; "" uses FM synthesis.
	SoundFontPath string
	// SampleRate is the output rate in Hz; 0 means DefaultSampleRate.
	SampleRate int
	// MaxVoices caps simultaneous SoundFont voices; 0 means
	// DefaultMaxVoices. It does not apply to FM synthesis.
	MaxVoices int
	// SoundFontMemory caps the memory loading the SoundFont may take: the
	// file plus the samples it decodes to, which for SF3 can be many times
	// the file. A SoundFont over it renders with FM instead. 0 means
	// DefaultSoundFontMemory.
	SoundFontMemory int64
}

Options configures a Decoder.

Jump to

Keyboard shortcuts

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