Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MIDI file plugin

This plugin lets OxiGuitar open and save Standard MIDI Files, the .mid files that sequencers, notation programs, MIDI guitars, karaoke collections and almost every music program read and write. It turns a MIDI file into a score you can read, play and edit, and turns a score into a MIDI file you can take anywhere.

It reads .mid, .midi, .kar (karaoke) and RIFF MIDI (.rmi) files, and writes .mid. It only handles files. The plugin that makes OxiGuitar sound is a different one, the MIDI sound plugin.

What’s in a MIDI file

A MIDI file is a recording of a performance, not a score. It doesn’t say “a quarter note E on the 4th beat”; it says “at 1.98 seconds, start the note with key 64, this loud, on channel 1”, and later “at 2.41 seconds, stop it”. Along with the notes it holds the tempo, time and key signatures, instrument numbers (General MIDI), markers and lyrics. Any program can play it back, which is why the format has lasted since the 1980s, but it has no idea of bars, note values, strings or frets.

So opening one means transcribing it, which is what a musician does by ear:

  • Quantising. Real players are never exactly on the beat. The plugin lays a grid over each beat (halves, quarters, eighths, sixteenths, triplets) and moves each note to the nearest line, picking for each beat the simplest grid that fits what was played. A slightly late sixteenth stays a sixteenth.
  • Note values. From where notes start and end on that grid, it writes the durations: plain, dotted, tied and triplets.
  • Strings and frets. A MIDI file only knows pitches. OxiGuitar works out a fingering for guitar and bass tracks as the file opens. A MIDI guitar that sends one channel per string opens as one track with the strings it really played.

The grid is a very good guess, not a certainty: check the result before you rely on it, especially on swung or rubato playing.

Saving

Saving a MIDI file goes the other way, and it is simpler: the plugin writes exactly what OxiGuitar would play, note by note. Repeats are played out, and bends, slides, vibrato and the whammy bar become pitch bend. What you hear in OxiGuitar is what the file holds. What a MIDI file can’t hold (the notation as written, the strings and frets, the fingering) is lost, and OxiGuitar says so before saving. Save a MIDI file to play the song elsewhere, not to keep the score.

How to use it

Check it is installed

Tools → Plugins… lists every plugin OxiGuitar found, with its version, and lets you switch any of them off:

Open and save

  • Open: File → Open… and pick a .mid, .midi, .kar or .rmi file, or double-click one if your system links the file type to OxiGuitar. OxiGuitar looks inside the file to decide which plugin reads it; the extension doesn’t matter.
  • Save: File → Save As… and pick MIDI (.mid) as the format. If the format can’t hold everything in your score, OxiGuitar says what would be lost before it saves, and you can keep the rest in the .oxg file beside it.
  • The import log: anything the MIDI file plugin couldn’t bring across is listed in the footer at the bottom of the window when the file opens.

What you can do with it

  • Open a MIDI file as a score you can read and edit. Notes are placed on a grid, so a note a little early or late still lands on its beat. Tempo, time and key signatures, markers and lyrics come along, karaoke lyrics too.
  • Open a recording from a MIDI guitar. A guitar sent one channel per string opens as one track, with the frets worked out.
  • Save a song as a MIDI file to use in a sequencer or to play anywhere. Repeats are played out, and bends, slides, vibrato and the whammy bar are written as pitch bend.

General MIDI guitars and basses open as tablature. Channel 10 opens as drums. OxiGuitar warns you before saving about what a MIDI file can’t hold, such as the notation as written and the strings and frets.

Architecture and strategy

The plugin is one file. Reading turns the times at which notes were played into notation by quantising them beat by beat. Writing doesn’t copy the score. It takes the timeline OxiGuitar plays and writes that, which is why what you hear is what the file holds.

Parsing and writing the file itself is done by the midly library, used as published. The strategy is to fix that library’s gaps in our own code, and to decide the file type from the first bytes: MThd for a MIDI file, RMID in the header for RIFF MIDI.

docs/design.md in the repository says what is read and written and what is left out.

Current limitations

  • Bends and slides read back as plain notes. Pitch bend in a file is named in the import log and left out; turning a bend curve back into OxiGuitar’s bend marks is possible later work.
  • One instrument per track. A program change after a track’s first note is named in the log and left out.
  • The first song only of a format 2 file (several songs in one), and at most 4096 bars.
  • Files timed in frames (SMPTE) are read at 120 beats a minute.
  • Swing and rubato quantise to straight notes as well as the grid allows; there is no swing detection.
  • No .rmi writing: it is the same file in a wrapper few programs need, and a .mid opens everywhere .rmi does.

Left out on purpose

  • Writing the score’s notation. A MIDI file can’t hold it, so the writer doesn’t pretend: it writes what plays. Keep the score in a Guitar Pro, MusicXML or TuxGuitar file.
  • A MIDI parser of our own. midly reads and writes the bytes well; we use it as published and handle its gaps in our code.
  • Guessing from the file name. The file type is decided from its first bytes, never from the extension.

Where you can help

  • Bends from pitch bend: recognising bend, release and slide shapes in a pitch-bend curve (src/, see docs/design.md).
  • Swing: detecting a swung feel and writing straight eighths with a swing marking.
  • MIDI files from real players, especially MIDI guitars and karaoke files, to test with. Never attach files you aren’t allowed to share.

Every change needs its tests and a row on the Features page; the Testing page says how to run them.

Requirements and assumptions

Requirements

  • OxiGuitar 0.25 or later (the plugin asks for interface 3.0). See the OxiGuitar manual.
  • Windows, macOS or Linux.
  • To hear a song you also need a plugin that plays scores and an audio output plugin.

Assumptions

  • A MIDI file records when notes were played, not what their values are. The grid OxiGuitar chooses is a good guess, not a certainty, so check the result before you rely on it.
  • A MIDI file can’t hold everything a score can. Saving one is for playing the song elsewhere, not for keeping the score.

Installing

You need OxiGuitar first: its manual says how to get it. This page is about the MIDI file plugin.

From a release

Each release on the releases page has a zip for each system (Linux, Windows and macOS; Linux and Windows for x86_64 and ARM64), a .deb, an .rpm and a Flatpak for Linux, a setup program for Windows that installs for you alone (no administrator password), a .pkg for macOS that does the same, and a SHA256SUMS file to check the downloads against. Take the file that matches your system.

Copying the file yourself

A plugin is a single file ending in .oxp. Unzip the release for your system and put midi-file.oxp in your plugins folder:

SystemYour plugins folder
Linux~/.local/share/oxiguitar/plugins/
macOS~/Library/Application Support/OxiGuitar/plugins/
Windows%APPDATA%\OxiGuitar\plugins\

Start OxiGuitar again. Tools → Plugins… lists what it found. The same menu switches a plugin off, and deleting the file removes it.

On macOS, a plugin file you downloaded yourself may be refused as coming from an unverified developer. Clear the download mark and start OxiGuitar again:

xattr -d com.apple.quarantine ~/Library/Application\ Support/OxiGuitar/plugins/*.oxp

A plugin installed with the .pkg is not marked this way.

Linux package managers

The package is called oxiguitar-midi-file. It lives in a signed package repository on GitHub Pages that you add once.

Debian, Ubuntu and relatives:

sudo install -d -m 0755 /etc/apt/keyrings
sudo curl -fsSL https://mateusrdp.github.io/oxiguitar-packages/oxiguitar.gpg -o /etc/apt/keyrings/oxiguitar.gpg
sudo curl -fsSL https://mateusrdp.github.io/oxiguitar-packages/oxiguitar.sources -o /etc/apt/sources.list.d/oxiguitar.sources
sudo apt update
sudo apt install oxiguitar-midi-file

Fedora and relatives:

sudo curl -fsSL https://mateusrdp.github.io/oxiguitar-packages/oxiguitar.repo -o /etc/yum.repos.d/oxiguitar.repo
sudo dnf install oxiguitar-midi-file

Normal system updates keep the plugin current from then on.

OxiGuitar’s Flatpak is a sandbox that cannot see the system’s plugin folders, so it needs the plugin as a Flatpak extension. Flathub comes first because Flatpak takes its runtime from there:

flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
flatpak remote-add --if-not-exists oxiguitar https://mateusrdp.github.io/oxiguitar-packages/flatpak/oxiguitar.flatpakrepo
flatpak install oxiguitar io.github.mateusrdp.OxiGuitar.Plugin.midi-file

flatpak update keeps it current and flatpak remove takes it away.

Features and their tests

Everything the MIDI file plugin does, and the tests that check it. The test names are the ones cargo test prints; a stage: is a step of scripts/test-all.py. The script fails when a feature here names no test, or names a test that doesn’t exist, so a new feature gets its row and its tests in the same pull request (the Testing page says how to run them, on your own files too).

FeatureWhat it doesTests
Recognises MIDI files by their contentsStandard MIDI files (MThd) and RIFF MIDI files (RMID, read like the file inside); nothing else.contract::its_signatures_recognise_midi_files_only, corpus::midi_files_are_recognised_by_their_contents, read_write::a_riff_file_reads_like_the_file_inside, read_write::other_files_are_refused
Notes on a gridA note a little early or late lands on its beat: each beat-sized cell takes the grid (plain or triplet) that fits its notes best; lengths as plain, dotted and tuplet values.read_write::a_played_file_reads_on_the_grid, read::tests::grid_distances, time::tests::fills_lengths
Bars, tempo, keys and markersBars follow the time signatures, a short bar getting one of its own length; tempo changes, key signatures and markers on their bars; a SMPTE file at 120 bpm, a frame rate midly can’t read refused.read_write::a_played_file_reads_on_the_grid, read::tests::signatures_of_short_bars, read::tests::a_frame_rate_midly_cant_negate_is_refused, read_write::an_empty_file_reads_as_one_empty_bar
Tracks and instrumentsEach channel a track; guitars and basses open as tablature (a seven-string guitar or five-string bass when the notes go low), channel 10 as drums; velocities become dynamics.instruments::tests::kinds, read::tests::velocities_as_dynamics, read_write::a_played_file_reads_on_the_grid
A MIDI guitar’s channelsA guitar recorded with a channel for each string opens as one track; drums stay apart.read_write::a_guitar_with_a_channel_for_each_string_opens_as_one_track
Lyrics and karaokeLyrics on the beats they’re sung on; a karaoke file’s title, artist and words; texts in UTF-8 or Latin-1.read_write::a_karaoke_file_reads_its_title_and_words, text::tests::utf8_or_latin1
Writes MIDI filesFormat 1 at 960 ticks a quarter: the tempo map, a track a track, what was read written and read back the same.read_write::what_is_read_is_written_and_reads_back_the_same, write::tests::signatures_of_played_lengths
Played as OxiGuitar plays itRepeats played out; bends, slides, vibrato and the whammy bar as pitch bend curves.read_write::repeats_are_written_out, write::tests::curves
Names what it can’t writeThe notation as written, and the strings and frets, each kind of loss once, counted.log::tests::one_entry_per_kind_counted, read_write::what_is_read_is_written_and_reads_back_the_same
Your MIDI filesEvery MIDI file of the corpus reads, and from the second trip writes and reads back the same.corpus::every_corpus_midi_file_round_trips
Through the newest OxiGuitarEvery corpus file OxiGuitar opens (MIDI by this plugin, Guitar Pro by the released Guitar Pro plugins) is written as .mid and read back by this plugin, a second round trip changing nothing.stage:corpus through the core
A working plugin fileThe .oxp file keeps the plugin contract, with this repository’s SDK version and with the newest release of OxiGuitar.contract::the_plugin_file_keeps_the_contract, oxiguitar_plugin_checks::the_manifest_matches_the_plugin, stage:contract (latest core)

Testing

Every test of this repository runs with one command, on your own machine, and anyone can run it. When something fails, it tells you how to report it.

Run everything

python3 scripts/test-all.py --corpus /path/to/your/files

On Windows, type python instead of python3 if that’s what you have.

You need:

  • Rust, from rustup (the version in Cargo.toml’s rust-version or newer);
  • git, and Python 3.8 or newer (nothing to install with pip);
  • on Linux, the libraries a build needs: libasound2-dev, libpipewire-0.3-dev and libclang-dev on Debian and Ubuntu (alsa-lib-devel, pipewire-devel and clang-devel on Fedora);

It runs:

  1. every Rust test of the repository (cargo test over the whole workspace), with a progress bar;
  2. the plugin against the newest release of OxiGuitar: it fetches that release’s source with git (once per release, kept in a cache folder every OxiGuitar repository on your machine shares: ~/.cache/oxiguitar-test-all, ~/Library/Caches/oxiguitar-test-all on macOS, %LOCALAPPDATA%\oxiguitar-test-all on Windows, or OXIGUITAR_TEST_CACHE), builds its oxiguitar-cli and oxiguitar-plugin-test, builds this repository’s plugin file for your system and runs the plugin contract on it;
  3. every corpus file the plugin recognises, through that release’s oxiguitar-cli: recognised, read, written as .mid and read back, a second round trip changing nothing;
  4. a check of the Features page: every feature names a test, and every test it names exists.

It tests the system and processor it runs on, and only those (as Rust sees them: rustc -vV’s host). Tests for other systems are left out and the summary says so; they are not failures.

Your corpus

The corpus is any folder of files you have (searched recursively, hidden files left out): give it with --corpus DIR, or set OXIGUITAR_CORPUS=DIR. Each corpus test takes the files it understands and skips the rest, and only checks what must hold for every file (it opens, nothing panics, a round trip is stable), so your corpus doesn’t have to be ours to pass. Files are never uploaded, and never belong in the repository.

cargo test on its own uses OXIGUITAR_CORPUS too, or the repository’s ignored corpus/files/ folder, and quietly checks nothing when neither is there (that’s how CI runs). --no-corpus runs the script without one.

What you get

A line per stage while it runs, then the result. In target/test-all/<date>-<time>/:

  • summary.md: the result, your system, what failed and why, short enough to paste into a GitHub issue;
  • test-all.log: everything that happened, every command and its output, and your corpus’s file names, sizes and SHA-256 sums;
  • corpus-*.json: what each corpus test made of each file.

The script exits with 0 when everything passed, 1 when something failed, and 2 when it couldn’t start.

When something fails

  1. Update to the newest develop (git checkout develop && git pull) and run it again: it may be fixed already.
  2. Look for an issue about the same test at github.com/mateusrdp/oxiguitar-midi-file/issues.
  3. Otherwise open one: call it test-all: <the first failing test>, paste the whole summary.md, and attach test-all.log (drag it onto the text box; zip it first if it’s over 25 MB). Read the log first: it has your folder names and your corpus’s file names, so change anything you’d rather keep private.
  4. Don’t attach corpus files unless they’re yours to share. The summary lists each failing file’s SHA-256, which is often enough for us to find the same file. If a failing file is yours, attaching it helps a lot.
  5. You can also give summary.md and test-all.log to a language model along with this repository and ask it to find the cause.

Options

  • --release: an optimised test build: slower to build, faster corpus tests.
  • --ignore-rust-version: for a Rust older than rust-version (unsupported, but handy).
  • --core DIR: test against a checkout of OxiGuitar you already have, instead of its newest release; --core-version v0.21.5 picks a release.
  • --out DIR: where the results go.

Adding a feature

A new feature comes with its tests and a row on the Features page, in the same pull request; a new corpus test uses tests/support/corpus.rs. The script fails until the row names tests that exist.

Credits

Thank you to the people whose work this plugin is built on.

Code we use

ProjectAuthorsWhat it does hereLicence
OxiGuitar’s plugin SDK (oxiguitar-plugin, oxiguitar-model, oxiguitar-play, and oxiguitar-plugin-test for the tests)Mateus Rocha de Paula and the OxiGuitar contributorsThe plugin interface, the score model, and the timeline OxiGuitar plays, which the writer savesMIT OR Apache-2.0 for the interface and SDK; GPL-3.0-or-later for the model and player
midlynegamartin and contributorsReading and writing the bytes of a MIDI fileUnlicense
serde_jsonDavid Tolnay, Erick Tryzelaar and contributorsTests onlyMIT OR Apache-2.0
Rust and its standard libraryThe Rust ProjectEverything elseMIT OR Apache-2.0

The quantiser, the lyrics, the strings and everything between the bytes and the score are our own code.

Standards

StandardBy
Standard MIDI Files and General MIDIThe MIDI Manufacturers Association, now the MIDI Association
RIFF MIDI (.rmi)Microsoft and IBM’s RIFF container
The karaoke .kar conventionTune 1000

Tools

ToolUse
cargo-fuzz and libfuzzer-sysFuzzing the reader and the writer (fuzz/)
mdBookBuilds this manual
cargo-denyChecks the licences of everything we build with

Thanks

To negamartin for midly, and to everyone who has kept MIDI files working for four decades.