PTC – MML audio scripting with NES-like audio in mind

So I’ve had a request about how to make NES audio with PTC long ago, and I meant to make this tutorial back then, but things got in the way and whatnot, so no more interruptions!

NOTE: This is not a tutorial for how to make MML-scripted music. This is just going over how you can produce sounds like the NES uses.

Ok, first things first. In order to get NES-like music, we need to know what the NES used for making the sounds it does. The NES contains an APU (audio processing unit) that has 5 channels to it: two pulse wave generators (or square wave), a triangle wave, a noise generator, and a delta modulation channel for playing DPCM samples (similar to WAV samples). For what PTC comes with directly, we have 4 total pulse wave generators, and we have 2 noise generators. There is no triangle wave, but thankfully we can create one. With all the things we can do, we are left with a problem. We do not have the means to mimic the delta modulation channel. So I can only focus on the main 4 channels. There are other “additional channels” used in games like Castlevania 3j, but I won’t go over those. I figure once you understand the basics with the 4 channels, you may figure out how to handle those other ones yourselves.

First off is the pulse wave generators. PTC has access to these via the instruments from #144 to #150. However, the difference between these instruments is their duty cycles, which is the ratio between the pulse duration and the period within the oscillator. For those that don’t know what this all means, it simply means the sound will be different with each instrument. The NES pulse wave generators come with 4 different duty cycles, which are 12.5%, 25%, 50% and 75%. These match up with 4 of the 7 instruments we have available in sequential order; 144, 145, 147, and 149.

Next is the triangle wave. There is no instrument that replicates this channel in PTC, but thankfully, both the manual on the PTC website and the help menu in the program itself shows how you can make one under the BGMPRG section. It’s real simple. The cycle starts in the middle, goes diagonally upwards, then downwards to the opposite side, then back up into the middle. It will look like triangles. Here is the string I used for making it with BGMPRG….

DATA “000810182028303840485058606870787F787068605850484038302820181008”
DATA “00F8F0E8E0D8D0C7C0B8B0A8A098908880889098A0A8B0B8C0C8D0D8E0E8F0F8”

The last is the noise channel. While we have an instrument that takes on noise, there are some problems. The noise instrument in PTC and the noise channel on the NES are different. The DS/i creates what is known as white noise for its channel, which one could say is a randomly generated waveform of various sizes and lengths. The NES “noise” is much like pulse wave generator, but with a pseudo random sequence at 16 different frequencies with a length of about 32767 steps. In most cases, the noise channel we have is good enough. However, the NES noise has 2 modes. One that is the entire length of 32767 steps, and the other that is a segment of 93 steps of the entire length. We can generate an instrument (like what we did with the triangle wave) to mimic this mode, but we run into yet another problem. This 93 step sequence can be anywhere in the entire length, based on when this mode was activated, so the actual sound can vary from one time of play to another. For this second mode (which I used for Quickman’s stage theme), I made it into an instrument like I did for the triangle wave. It won’t mimic the variation you hear from the actual music, but it’s close enough. This is the string used for BGMPRG….

DATA “D03030D030D03030D0D03030D030D03030D030D0D03030D0D030303030D0D0D0”
DATA “303030D03030D03030D030D030D0D0D030D0D030D03030D0D030D03030D0D0D0”

Ok, so we have some information of the instruments we can use, but what about making NES-like music. Well, that is not something I can teach you as I am pretty much a noob on the subject. So how did I recreate the Megaman 2 music? Well, that’s where I go from looking like a professional to being the average joe. I had the tools to read the audio files and put them into a representation that I could map easily into PTC. Anyone recall how the music for Shovel Knight was made? For those that don’t, there is a program called Famitracker that aids in making NES music in a tracker-arrangement that you simply add notes, octave, volume, pitch, etc, and then you can export it to the actual NSF format so they can be played on NSF players.

But how does that help? Well, Famitracker itself doesn’t allow importing from NSF files, but people who got the source for Famitracker and NotSoFatso (a player for NSF formats) put them together to make an importer, called NSF Importer (you can find it by Googling). It allows you to import based on track. When you import, you’ll find various information per column. For square channels, the order is <note, octave, volume, pitch, and duty cycle>. The triangle channel’s order is <note, octave, -blank-, and pitch>. The triangle does not have volume adjustments, but it mutes via rest notes (which look like — in the note column).

The noise channel is a bit different in representation. It has 16 frequencies (range from 0 to 15, with 10 to 15 being A to F), so it mixes note and octave together. Other info is volume and mode. I had to figure out myself the note/octave for each frequency, and while far from perfect, what I list below is what I came up with (octave first, then note). While I only listed 12 matches, the latter 4 all sound about the same due to our ability to hear them. They do, however, sounds like they get quieter as you go for a higher frequency, so taking the ‘B’ frequency and changing the volume when plotting into PTC could help mimic them.

0 – O2 C, 1 – O2 A, 2 – O3 G#, 3 – O4 C#, 4 – O4 G, 5 – O5 D, 6 – O5 B, 7 – O6 D, 8- O6 F, 9 – O6 G, A – O6 A, B – O6 B

Well, that’s about all I can share, as the rest needs to simply be tinkered with by your own will, because sometimes you need to try them out yourself to understand them. I will note that NSF Importer imports the data at a rate of 60hz, so in 1sec’s worth of time, it will import/play 60 rows (I haven’t tried PAL music that operates at 50hz, but I assume that’ll be 50 rows per second). Because the tempo is always set to 150 (be sure to set this in PTC) and it reads at 60hz, I calculated that the smallest length for a note is a length of 96, so base your note plotting by that. Please don’t ask how I calculated it, because that was over 1.5 years ago and I completely forgot how I figured it out. As I said when I began this explanation, this is not a tutorial for how to make PTC MML music, so determining  the length is not part of this, but I will say that when you double a length value (like from A1 to A2), the actual play length is split in half, and likewise, splitting a length in half increases the play length by double. So if a note played for 2 rows in NSF Importer (one having the note itself and one without), the length value in PTC would be 48.

. A few notes about plotting into PTC. The square waves in NSF Importer are actually an octave off, so be sure to adjust that. Because the triangle wave is mostly for bass sounds, it’ll sound rather low on the DSi/3DS, so be sure to set the volume to as high as you can for that channel. As said before, importing operates at 60hz (or 50hz), but what was not said is that NES is not restricted to this speed. Various notes may turn on and off quicker than a frame, so NSF Importer will miss them.

Hope this helps.

PTC Tutorial – Large Background Scrolling

One of the things I was asked when I first showed off my Megaman 2 PTC project on Youtube was about how I handled large backgrounds when Petit Computer can only handle 64×64 tile backgrounds. I hadn’t seen many other PTC programs that utilized large backgrounds, nor any tutorials specifically targeted towards that aspect with PTC, so why not make one myself? So, this tutorial will focus on that.

QR and example code can be located at the end of this tutorial.

Continue reading