top of page
Search

Add Easy Universal MIDI Input to Playable Unity Instruments

  • Writer: Cody Swanson
    Cody Swanson
  • Jun 25
  • 2 min read
In this post I'll outline the basics of setting up MIDI input inside a Unity project.
A playable synthesizer, inside the Unity game, Time in Place
A playable synthesizer, inside the Unity game, Time in Place

For whatever reason, it has been a goal of mine for a while to add midi input to my game, Time in Place. So far I've added playable instruments like a guitar, piano, drum machine, and synthesizer. Each instrument presents its own unique challenge when it comes to input, especially with support for both Keyboard + Mouse, and Gamepad.


But the idea of using a midi keyboard to play the keyboard related instruments always seemed like a great third option, and I finally got that working. As it turns out, thanks to Keijirō Takahashi's asset, Minis, this is very simple. Let's follow the basic logic for an A# note: MIDI A# pressed → OnMidiNoteOn → MidiNumberToName → PlayKey("A#3") → A# audio clip plays

Once you've installed the asset, add it to your instrument script: using Minis;

In your void Start() subscribe to MIDI events: InputSystem.onDeviceChange += OnDeviceChange;

foreach (var device in InputSystem.devices)

if (device is MidiDevice midi) RegisterMidiDevice(midi); Then, when a MIDI device is detected, we register it for when a MIDI note is played: void RegisterMidiDevice(MidiDevice midi)

{

midi.onWillNoteOn += OnMidiNoteOn;

midi.onWillNoteOff += OnMidiNoteOff;

} When the note is played, we get is MIDI number (for me, this is either MIDI # 46 or #58): void OnMidiNoteOn(MidiNoteControl note, float velocity)

{

string noteName = MidiNumberToName(note.noteNumber); // MIDI # to audio clip name

PlayKey(noteName); // Play the actual audio clip sample

} And then send that to be converted to a name:


string MidiNumberToName(int midiNumber)

{

string[] names = { "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B" };

int octave = (midiNumber / 12) - 1; // MIDI spec

string name = names[midiNumber % 12];

return $"{name}{octave}";

} Then we compare this note name to a clip in our audio clip dictionary (noteClips) and if found, we play it void PlayKey(string noteName)

{

if (noteClips.TryGetValue(noteName, out var clip))

{

audioSource.clip = clip;

audioSource.Play();

}

} And that's more or less what you do!

You'll have to decide how you want to do the note names, Dictionary, etc. I chose kind of a middle 1.5 or so octave range, so I have note names like A#3, B3, C3, C#3, etc. But it's up to you. Okay, I hope this was helpful or inspiring! Have a good one. -Cody





 
 
 

Recent Posts

See All
Blog test!

Hello! Is this thing on? Check one. Check one. Sibilance.

 
 
 

Comments


bottom of page