[OPNA] An Analysis of The P.M.D. Music Data Format (Mostly the "Rhythm" Section)

Greetings to all the new comers.

This article is based on Github repo: ValleyBell/MidiConvertes and Mistydemeo/Pmdmini. Special thanks to オップナー2608 from PC-9800 Series Central Discord Channel for helping me.

Meh... a detailed description of this little project will be covered in a future article (If I actually would do). Basically, I'm trying to make a portable music player / midi synth based around YM2608. I expect this project to take forever to complete...orz

Let's get our hands dirty quick!

I started my investigation on this legendary sound system by checking out all the PC software available out there while listening to my favourite PC-98 Touhou arrangement: 神へ捧げる魂 ~ Highly Responsive to Prayers ( a.k.a. Th01_04 ). Here is a list of useful tools (all of which can be found easily on the internet):

  • Locale Emulator: Resolves Shift-JIS display issues on non-Japanese OSes.
  • FMPMDE ( together with PMDWin.dll and WinFMP.dll ): The Real-time Music Interpreter. Use this program to listen, toggle the channels on and off, and see the effects of your modifications.
  • drum_samples.zip: You can find this file on hoot's website. It contains all the drum samples inside YM2608's internal sample memory in WAV format. Without this zip, FMPMDE won't generate any drum sound (The effect can be observed by toggling the "SSG+RHY" switch).
  • MC.exe: The MML->PMD Compiler.
  • PMDRC: The PMD->MML Decompiler.
  • PC-98 Audio Rips.rar: Contains all the PMD files extracted from PC-98 Touhou games. You can also extract these files by yourself. (Detailed instructions can be found here).
  • Any HEX editor, such as Sublime Text + HexViewer Plugin.

At first, I thought I would be quite happy working with decompiled PMD files. However, it's not long before I found out that PMDRC doesn't bother with the rhythm sections. A track without drum is unlistenable; I'm forced to get a full understanding of PMD's file structure to continue.

For this reason, I attempted to understand the binary content of Th01_04.M using a HEX viewer. Unfortunately, there aren't many sources out there for me to get enough information. User オップナー2608 offered me this link: 
https://raw.githubusercontent.com/ValleyBell/MidiConverters/master/pmd_SeqFormat.txt, which is a detailed description of the PMD data format. The author of this document got his resources by laboriously reading the source code of  PMDwin.dll, whose author got his information from somewhere else in the PC-98 universe back in the 00s. Hats off to them.

I compared the description it against the MML file decompiled using PMDRC, sure enough, they match perfectly.

The text file says:

Let's take a look at Th01_04.M. Here is the address table:

Keep in mind we're dealing with little endian (It's natural to think of, since PMD is highly optimized for the x86 architecture). A human-friendly version looks like this:

Another important discrepancy to notice is that all of the file's data is shifted one byte forward, which means you should automatically add 1 to each of the encoded addresses. 

Now, since I want to focus on the rhythm section, I'll omit everything else in between. That text document does a great job explaining the FM and SSG channels. 

Side note: ZUN never uses SSG3 and ADPCM in his games. These channels are nearly empty, with just a few bytes of parameter settings:

Now, let's take a look at the first section of the rhythm channel: the Rhythm Sequence Table, also known as channel "K" in the MML terminologies.

Now we're able to translate this according to that text document

The next section is an address lookup table, which contains the absolute address for each "rhythm patterns". The "patterns" are stored in the section next to it:

You notices that I've attached what "rhythm" it actually is in Japanese. At first, unfortunately, the only thing I can get from the text file is:

What the heck "some ID/mask" is actually? In order to find an answer, I had to read the source code of PMDwin.dll. Luckily enough, all the primary logic of that program is stuffed into a single file.

Phew... That's all I need to know for now.

The next 4 bytes are address markers for the Extra Data (Title, Composer, etc...).

Following this, comes the FM Instrument Definition Table. All the parameters of the FM synth chip can be tweaked by the programmer. I won't go into much detail for this section (maybe reserved for a future article?) If you're interested in it, please read the original YM2608 datasheet and Yamaha FM synth chip product line application manuals.

The Extra Data is also leaded by an address table:

Copy the ASCII data displayed in your HEX viewer, paste it into a new text file, then find a way to change its encoding to Shift-JIS. You get this:

Yep, here are the title, composer, arranger and comment data.

That's pretty much it. Hope it can be helpful.

2 thoughts on “[OPNA] An Analysis of The P.M.D. Music Data Format (Mostly the "Rhythm" Section)”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.