How To Import MIDI Files In Unreal Engine 5

Table of Contents

Unreal Engine 5.4 and later can import MIDI files with the built-in Harmonix plugin. If you need your own data format, or you’re on an older version, you’ll write an importer in C++. This tutorial outlines what that takes.

Prerequisites

The skillsets you need to write your own MIDI importer. 

  1. Be Efficient in C++
  2. Be comfortable with UE5 C++ API.
  3. Understand Modules and Plugin Development for unreal engine.
  4. Understand UFactory
  5. Understand MIDI messages and the structure of a MIDI File.

Parse MIDI Files In C++

We don’t like to re-invent the wheel so we are going to straightaway recommend you find an existing C++ Library than can read/write MIDI files and makes it easy to work with. The MIDI Standard gets COMPLICATED. Reading/Writing MIDI files from scratch in C++ is not recommended.

 

So we used a 3rd party library to parse MIDI files for our MIDI Engine: Core plugin.

Once you have found your C++ Library, you need to read and understand what its functions do, so you’re going to have to learn how to use the library itself. This means you need to find a library with good documentation.

Convert MIDI File Data To Unreal Engine Data

Now that you can parse a MIDI file, you need to pack that information in C++ classes that unreal engine can understand. That means your classes will need to be UCLASS or USTRUCT. So basically, you read the data from the MIDI File, then store the data as variables in UCLASS.

For example, you could create 3 classes:

  • UMidiAsset
  • UMidiTrack
  • UMidiNote

The MidiNotes go inside MidiTracks, and MidiTracks are packed in a MidiAsset.

What ever structure you decide for your classes, make sure you understand it fully and it’s organised because you’re going to need to read this data and play it back.

Save MIDI Files as UAssets

Now that you have read the data and packed it into your special classes… you need to save that to disk. Unreal Engine does this automatically but you need to assist it by using the UFactory classes and other editor-facing classes in unreal engine.

Conclusion

Importing the file is only the first step. You’ll also need to play the notes back in time with your audio, and that’s where most of the work is.