10.18.2018: Special Edition - TXT2MIDI.py


Today's soundtrack is Rest, Repose: Rest, Repose, the self-titled album from this band that features one of my favourite YouTubers, "Djent Daddy" Jared Dines. The guy is genuinely funny and is an incredible guitarist, drummer, and vocalist.


This afternoon's songwriting project isn't strictly featuring the writing of any songs. Actually, it's probably more about coding than it is about songwriting. But I had an idea that I want to try out: I want to build a Python program that will automatically generate music based on text input. The way I see it, I know how to use Python to read and write text files. What happens if I open a MIDI file in a text editor? Will I be able to reverse-engineer it? There's only one way to find out...


My first step was to download Tuxguitar, a free MIDI editor. The program required a system restart to complete installation. Once that was done, I started up the program and wrote a basic Em pentatonic scale in quarter notes, then exported the file to MIDI. I tried opening the file with Notepad and found utter gibberish. A quick Google search revealed the reason: Notepad was trying to convert binary to text. I downloaded Frhed, a hex code editor, to try to get a better understanding of the information that was saved in the text file. It was better, but still unclear. I had an idea. I did a web search for the hex names of the MIDI notes and found a great resource with a side-by-side table. According to the table, F would show as 29. So I tried something new in Tuxguitar: instead of writing a midi scale, I wrote a series of repeating Fs, then exported again as a MIDI file. I opened the new file in Frhed, took a screenshot, and found the pattern!

I tried replacing the instances of 29 with other numbers, then tested it: different notes played. Great success! The next step was to translate the note names from hex to text, and find the equivalent text codes when cross-referencing the midi to hex table. I typed the values that I wanted into the left side of Frhed, then found their values in the right-hand table. Once I'd done that, I created a new table that gave three translations of the same value: note, hex, and text.


That's all I have time for tonight; I'll keep working on this soon!

Day Two

Today's soundtrack is Eminem: Kamikaze.


Now that I've identified the symbols that make different notes, I'm going to start working in Python. My plan is build a program that will open a text file, convert the letters into the text symbols, then save the file as [file].mid.


I spent about six hours plugging away at it. Lots of troubleshooting and tweaking. I finally have a working product. I'm really happy with how this has turned out. I ended up using HxD Hex Editor because it allows me to paste directly into the binary section. So the end result is this:


The program welcomes the user, then prompts the user to input text. The program reads the text and translates from letters into corresponding hex code that equals different MIDI notes. The program outputs the information, including the necessary header and footer code, and the user pasts the information into a hex reader, then saves it with a .mid extension. The user now has a midi file that can be listened to or modified.


#RestRepose #Eminem #TXT2MIDI #TXT2MIDIpy #Tuxguitar #Frhed #HxD #MIDI