NOW LOADING

MidiStandardFileReader


Reads a Standard MIDI File into a sequence you can play or draw
Namespace Windows.Devices.Midi2.Utilities.Files
Type Name MidiStandardFileReader
Type WinRT Runtime Class
IDL MidiStandardFileReader.idl

MidiStandardFileReader reads a Standard MIDI File in any of its three formats, including files wrapped in the RIFF format some files use. What you get back is a MidiSequence: music on a timeline, no longer tied to the file format it came in.

This is a static class. There’s nothing to create.

Reading is asynchronous because a file may come from anywhere, and because a large file takes a while to read. A file with hundreds of thousands of events isn’t unusual.

Static Methods

Method Description
ReadAsync(stream) Reads from an IRandomAccessStream with the default limits
ReadAsync(stream, options) Reads from an IRandomAccessStream with these MidiFileReadOptions
ReadFromFileAsync(file) Reads from a StorageFile with the default limits
ReadFromFileAsync(file, options) Reads from a StorageFile with these MidiFileReadOptions

All four return a MidiFileReadResult. Check Succeeded before using Sequence.

Remarks

A file that stops making sense partway through is read up to that point and reported as truncated, instead of being rejected, because a file with a damaged end is usually still worth hearing. Set FailOnUnreadableData on the options if you’d rather refuse it completely.

Example

auto result{ co_await MidiStandardFileReader::ReadFromFileAsync(file) };

if (result.Succeeded())
{
    auto sequence = result.Sequence();

    if (result.Truncated())
    {
        // still playable, but the file ended before it said it would
    }
}

Samples

These read a file, show its title, length, tempo, time signature, and tracks, and then play it. Pass the path of a MIDI file on the command line, or leave it off to play one that comes with Windows.

Didn't find what you were looking for?

Windows MIDI Services is an open source project with all source available on GitHub. We have a great community on Discord as well. Between GitHub and Discord, you should find the information you are looking for.