Midi Meta-Data Update using PHP (PhpTabs)

-xXx- Dist1nc7ive stories, Technology

So, I downloaded a few “producer packs” from Ghosthack. I use these to help kick-start ideas when I’m producing a podcast recording.

The blank canvas is the most difficult place for me. These packs can really help. You can use them together, melody, arpeggio, bass line and chords, or you can just use pieces and parts. I’m more of a pieces and parts guy. I like midi over audio loops because you can change the instrument, edit them, double-time, half-time, speed up, slow-down, break them down and build them up.

The Problem

My main problem is that I’d drag a bass line or arpeggio into a track to see how it sounds, and instead of displaying the filename in the region, it would say only “Midi Out”.

BUT… I’d audition maybe 10-12 before I found one I wanted to explore further, but then I couldn’t remember which one I had dragged into the track. Was it “Ghosthack Bassline D#3.mid” or “Ghosthack Bassline E 4.mid”? You’d think I could just look at the last one I dragged in, but the way that works in Logic doesn’t mean the one you last dragged is currently highlighted. Of course it isn’t!

For instance, in the screenshot above, #1 is the one that is highlighted by a mouse-click, but I dragged #2 into the track (I couldn’t get screenshot of the actual dragging, but believe me, I tried!)

Poking/Prodding

So, I poked and prodded for a bit, using a hex editor, vim, “get info” in the Mac Finder. I would have manually changed them all to list the file name, but I couldn’t figure out how.

Get Info doesn’t say “Midi Out” anywhere.
vim showed the text I want, but if I changed it there, it wouldn’t work.
hex editors seemed to work, IIRC, but not at the scale I needed. I didn’t want to have to do that much editing.
PhpTabs

Then, I found PhpTabs. However, I had some issues with it writing the new midi data correctly. I did end up with a hack to get some unique-ness into the region name. It’s not perfect, but it helps a lot.

One of the things I realized is that I needed to rename the file using the same amount of characters in “Midi Out” (…8). When I tried to add the entire file name, it would save the file with no data (blank). If I grabbed the first 8 characters of the file name (substr($otitle, 0, 8)) then it would be the same every time (Ghosthac) and not at all helpful.

So, I changed it to grab the last 8 characters, which are generally unique. It ends up looking funny (‘ine F# 6’) but at least I can find the chords, arp or melody intended to go with it, so I can audition them together.

funny region name… but unique
The script that worked
require_once 'vendor/autoload.php';

use PhpTabs\IOFactory;

$filename = $argv[1];

$contents = file_get_contents($filename);
$otitle   = str_replace('.mid', '', $filename);
$title    = substr($otitle, -8);
$title    = trim($title);

if (strlen($title) < 8) {
    $title = str_pad($title, 8, '_');
}

$contents = str_replace('MIDI Out', $title, $contents);
file_put_contents($filename, $contents);
Questions and Conclusions

If you can spot a way to add the entire file name as what is shown in the Region Name when dragged in, let me know and I'll do it. If you are trying to do the same and are having problems, reach out to me. Maybe we can work on it together. The easiest way to get me is to DM me on Instagram or Twitter (@dist1nc7ive on both).