Monday, February 11, 2019

MIDI keyboard animation demo

I've been playing around with UE4's possibilities for triggering events using MIDI (a language for communication between electronic musical instruments). At first, I thought there might be a Blender plugin for this, but the Python script I found online doesn't seem to work correctly in the latest version of Blender. 
Instead, I'm using the "Procedural MIDI" plugin by Scott Bishel available on the Unreal Marketplace, and a piano model I purchased several months ago. The keyboard that came with that piano had the keys fused together, so I had to separate them first in Blender, then import them into UE4, each with unique names for each key. The Procedural MIDI plugin allows the data from the MIDI file to be accessed by Blueprints, which in this case changes the position of one of the 88 keys. Several hours later, and after coding an extremely laborious Blueprint, I have this video:


Here is the basic Blueprint to run the key movements:


The first half essentially opens the MIDI file, and plays it. The second half siphons off the MIDI data as it is played, and assigns the note data to a single variable. A function in the middle (see below) translates that number to a specific key on the keyboard, then whenever the MIDI file triggers a "note on" event, the key referenced by the Receive Note Name function is rotated by -3°; a subsequent "note off" event returns it to 0°. Here is that function (the "Are we there yet?" function), which goes through every note on the keyboard, and confirms if it is the one that should be moved or not:


And this is as much as could fit on the screen at once:


Believe it or not, this works because MIDI data is sequential, meaning no two notes actually play at the same time. The human ear perceives separate MIDI events as chords because it happens very fast!

No comments:

Post a Comment