MusicTheory.Midi 3.0.0

dotnet add package MusicTheory.Midi --version 3.0.0
                    
NuGet\Install-Package MusicTheory.Midi -Version 3.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MusicTheory.Midi" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MusicTheory.Midi" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="MusicTheory.Midi" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MusicTheory.Midi --version 3.0.0
                    
#r "nuget: MusicTheory.Midi, 3.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MusicTheory.Midi@3.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MusicTheory.Midi&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=MusicTheory.Midi&version=3.0.0
                    
Install as a Cake Tool

MusicTheory.Midi

Standard MIDI files for MusicTheory: write measures, a tempo map and a key as a format 1 file, and read format 0 or 1 files back. No dependencies beyond MusicTheory.

using MusicTheory.Midi;

using var file = File.Create("tune.mid");
MidiWriter.Write(file, measures, new TempoMap(96), key: KeySignature.Parse("D"),
                 options: new MidiWriteOptions(TicksPerQuarter: 480, Channel: 0, Velocity: 90));

Track 0 holds the time signature, key signature and tempo meta events; track 1 holds the notes. Tied events become one note, and a position that does not fall on a whole tick throws an ArgumentException that names a TicksPerQuarter that works (a 7-tuplet at 480 fails, 3360 works).

MidiImport import = MidiReader.Read(File.OpenRead("tune.mid"));
// import.TimeSignature, import.Key, import.Tempo, import.Measures (ties restored across barlines)

The reader merges all tracks into one voice, snaps onsets and durations to a grid (1/64 of a whole note by default) and throws NotSupportedException for SMPTE division or notes that overlap with different durations.

Tested against music21

MusicTheory.Midi.UnitTests/Reference/ holds .mid files written by music21 (tools/reference/generate.py) and midi.csv, what music21 itself parses back from them: meter, key, and every note, chord (C4+E4+G4), rest and tie. MidiReferenceTests requires the reader to agree on all of it. A file cannot hold a trailing rest, so the fixtures never end on one.

Product Compatible and additional computed target framework versions.
.NET net11.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.0.0 60 11/21/2026