Trimming VGMs

From vgmrips

This tutorial was witten by (Jazz) Jackalope, building on work from DukeNukem.

Once you have your VGMs logged, you must also trim them. Why? VGM Players can't automatically tell where the song's supposed to loop, unlike other game music formats. You'll have to find the start and end of the music, as well as where the loops begin. This is, therefore, the most important part of the process - but also possibly the most difficult.

I forgot to mention it in a prior step, but if the pack you're working on already existed on VGMRips before, you probably want to check its forum thread for any replies. Helpful members sometimes make posts there describing issues with the packs or giving trivia on the soundtrack. If anyone's noted an improper loop, you'll find out there too.

Splitting a Single Soundtrack-VGM Recording into Many VGMs

As I promised, I'll cover the VGM Splitters for those who recorded all tracks into a single large VGM. (This info will eventually move to Logging VGMs where I should have put it before. I just... didn't want to confuse readers with a bunch of tricky concepts right away.)

vgm_sptd splits on delays; that is, long Waits. You can specify the number of samples the silence takes; the default is 32768 samples, or about 0.75 seconds. (One second is 44100 samples in a VGM.) Leave 3-5 seconds between songs and you should be fine. If you're getting more VGMs than there are songs, try increasing the samples to a much larger number, like 65535.

While this program makes splitting incredibly easy, it doesn't work for all chips/games. For instance, some send otherwise useless commands to the music hardware, which doesn't count as silence to the delay splitter. Don't worry if this happens, because you can use the other splitter.

vgm_spts splits on sample numbers that you give it. In this case, you'll have to use a WAV file of the entire recording, or use vgm2txt with strategic time slices, to find the samples to use. That's pretty similar to finding trim points already.

This splitter starts at zero, and each number you enter tells it where to split next. For the last song in the file, you must give -1 as the final number. This indicates the end of file, where 0 would just make the program close without collecting the rest.

You can also specify all the numbers at once on the command line, such as like so:

vgm_spts Stream.vgm 44100 176400 0

As you've noticed, the command ended with 0 and not -1. This will make only two files, stopping at 176400 samples. To make three files out of the whole source VGM, just use -1 in place of 0.

When you split by samples, you may find the VGMs have no percussion. In those cases, you can use the last resort command line switch, -state, to force drum info to remain.


Both these tools automatically trim silence up to the start of other commands in the split VGMs. Normally, this is good. If some songs begin with silence as part of the song, you'll have to check that it didn't get cut off. If so, you'll have to add it back while using vgm_trim.

Now, for full disclosure: the above tools use the same trimming technology as vgm_trim. (Which also has the state switch.) That means technically, you can just use vgm_trim on the one massive file. In practice this is way too inconvenient and you definitely want to use these splitters.

What are we trying to find?

The starting trim sample and ending trim sample are needed for all VGMs. Songs that loop have a loop sample as well. For songs without an intro, the start sample and loop sample may be the same number.

Let's look at some visual examples to help make this concrete.

Type 1 - No Loop (Loop Sample = 0)

These are songs that play just once and then stop in silence: that is, there is no Loop Sample. Jingle, Game Over and Intro songs are usually of this type. Here's an example from Aladdin, "Ne Naw Tune":

♪ listen to this ♪

As you can see, the song is complete on its own. There's a leading silence at the beginning of the VGM (black area), and we'll cut this off by placing the Start Sample (yellow line) closer to where the sound actually begins. On the other hand, the music finishes without extra silence (see note below), and hence the End Sample (red line) is at the very end.

Sometimes you'll have to cut extra silence or sound after the song finishes as well. In other cases, the ending of the file is also the exact end of the log. Why's this? To make silence, some games send Wait commands to the sound chip when all the registers are null (that is, nothing else is happening). Some songs don't send anything at all once they've finished playing, so the VGM won't have anything else logged after the song is finished either.

That sounds convenient, but not so fast: some such logs may sound cut off at the end. In those cases, you can add silence for the sound to finish by giving vgm_trim an End Sample beyond the actual end of the VGM.

Note: Sometimes these songs give a strange error about notes playing past the end-of-file... and even adding silence doesn't fix this. You can mention the issue in IRC or when you're posting on the forums, but you can probably safely ignore the error; we don't know why it happens, but it doesn't affect playback.

Type 2 - Entire Song is Loops (Loop Sample = Start Sample)

The second type of music you'll find is when the entire song loops. That means it doesn't have any intro, and then the song just starts over once it finishes playing. Here's an example, also from Aladdin, "Storyline":

♪ listen to this ♪

As you can see (and hear), the song drops to a silence and begins all over again. The Start Sample (yellow) is just at the start of the song (removing a leading silence at the beginning), and the End Sample (red) is at the exact place where the song starts again. Trimming like this will remove the redundant data after the red marker and the unwanted silence at the beginning.

If you want to be extra-sure of the pre-loop gap, you can make a looped WAV of your trimmed file and compare to the untrimmed log to make sure the silence isn't too long or too short. While that doesn't apply to loops with no gap, you can still compare trimmed and untrimmed that way.

If you meet trouble trimming this kind of loop, you can try to move the samples earlier a teeny bit, or treat it like it's the third type of loop and move them ahead until you get the right sound. In all cases, making sure the loop sounds right is way more important than trying to trim all possible excess information.

Type 3 - Songs with Intro and Loops (Loop Sample ≠Start Sample or 0)

The third type happens when the song has an intro part which plays only once. In this case, the Loop Sample is different from the Start Sample. Here's an example from Ghouls 'n Ghosts, the first boss (Shielder) battle song:

♪ listen to this ♪

This time we have three different trim points. The Start Sample (yellow) cuts a tiny bit of leading silence at the beginning. What follows is a small intro (colored in light yellow), which then introduces the part of the song that repeats (in lime green). This is the Loop Sample, marked here with a cyan line. The End Sample (red) is placed at the exact point the song loops later on, trimming the repeated part that comes afterwards (the second+ loop logged).

If trimming fails (hung notes or such), try moving the Loop Sample and End Sample further into the VGM. The goal is an accurate-sounding loop: having some extra to achieve that is perfectly fine. The main issue is those dreaded songs that won't seem to loop right except with a minutes-long intro...

How We May Find Them

There are at least four ways of finding the two/three numbers needed by vgm_trim.

  • Using vgmlpfnd and hoping for the best. Some soundtracks really are this easy, if you're lucky.
  • Finding Trim Points in WAV Files (info on creating them inside there). For another take on the same method, you can try the Manual looping tutorial; it's fairly fast if you can line the loop up with itself.
    • If you're having a lot of trouble with a Mega Drive game, using the "one WAV file per channel" command of BlastEm can help as well.
  • Finding Trim Points with vgm2txt. This tutorial gives four examples, and explains in example 2 how to make text files focusing only on the parts of the song you need.
  • It's possible to convert a GEMS-using Mega Drive game's tracks with gems2mid and try to find a loop via MIDI commands.

You will most likely use more than one of these ways based on the challenge or lack of challenge the VGM presents. (You can't only use vgmlpfnd on a song with an intro, for instance.) RN22's vgmlpfnd Method, to find loops combining vgmlpfnd and WAV searching, is a good example. Really, the best way to learn looping/trimming is to keep practicing and learn many different methods.

Once you have the right numbers, you'll use vgm_trim to trim the VGMs. And then, you'll have to listen to each trimmed song to make sure it sounds correct. You can skip ahead in the song to make this faster, though sometimes it makes playback sound weird.

vgmlpfnd

The "!" "perfect loop lines" are not always 100% reliable. The indicated loop may be too short (e.g., "A A A B A A A B" may detect only "A" instead of "A A A B") or it may contain more than one loop (e.g., "A B A B A B" may result in a loop of "A B A B" instead of just "A B"). As always, you must check after trimming that the loop really matches the song's loop.

If you don't get anything useful on your first go, you can try using a lower Minimum Number of Matching Commands. I try 300 and then 129 as needed. (Any lower, and you'll dip into commands that aren't useful for loop finding, like instrument initializations.)

If the game you're ripping should've been logged at 50hz, you might find that logs at the right speed will give you handy "!"s that the wrong speed doesn't give. However, this should ideally have been accounted for during logging already.

If you thought you'd be clever and use loop samples from different lines that seem right (Yo), I must sadly burst your bubble. Within the same millisecond, many commands can occur. This crude method can't tell or even hint to you when the surrounding commands actually match up. You may still need to drill into a text file and/or examine the waveform to confirm when the commands actually match.

vgm2txt

Even without making WAVs, you can use your VGM Player of choice to try to find the rough time areas of the trim samples. It's possible to skip ahead and back... just beware, in VGMPlay, that this can cause funkotronic things to happen to the notes, such as being cut off. In those cases, let the song play to the points you need or try another player.

In the best case, the Start Sample is simply one big Wait at the start of the commands. In others, finding it can be tricky or confusing. The TXT will show that repetitive commands are still running leading up to the start (or after the end in an unlooped song). Every now and then you might see some Latch/Data commands that look very important, but aren't really. Combine careful listening, or even a WAV, with your TXT to help you find the right Start Sample.

As mentioned, you can give time slices to vgm2txt to zero in on the parts you think you need. For trickier songs, though, this won't be convenient; you might end up having to create many small files as you jump around. If you have more than enough RAM to open a massive text file twice, contrary to my copy-paste advice in the vgm2txt tutorial, it should be easier to do this with a full text conversion.

  1. Open the file in 2 editors (either side-by-side, or directly on top of each other so you can switch between them using Alt+Tab)
  2. Go to what you think is the "loop start" in one editor
  3. Go to \"loop end\" in the other editor
  4. Look at the commands and try to find the spot from where on the commands are the same in both editors. You don't have to understand the commands; just try to find where they match

As you do this final step, you should typically ignore the Wait commands; they can vary a bit with every loop or log attempt. I say typically, because larger Waits of similar size can help you be sure you're on the right track, or even mark good trimming points. More recent versions of vgm2txt have an option to ignore tiny waits, reducing the haystack you need to check through; at the moment, though, you have to build the tool from source to get this feature.

For trickier loops, try looking for spots where all the channels are turned off at once.

Caveats of the Trim

As if it isn't bad enough, there's some particularly sticky parts...

Silence Trimming Hiccups

Trimming the extra at the beginning of a song too closely may cause problems, like a volume reduction or broken instruments. (I suspect this can even happen when you use the VGM Splitters.) Double-check your beginning trims against the untrimmed VGM to be sure.

Fixing it is easy: if there's issues, move the Start Sample back in time some. You can also make vgm2txt slice out the first three (or any) seconds of your VGM and find the starting commands that way, for more precision.

Note: Any amount of silence below 500 samples is not noticed by the human ear at all.

Even if you avoid this issue, there's...

Shearing Intentional Song Silence

You want to avoid that, of course. If you're lucky, this silence (at the beginning) is already accounted for by a separate Wait command (or such), meaning it's easy to find with vgm2txt or retain with the splitters. Otherwise, you'll have to adjust for it in your Start Sample, and maybe use a recording of that silence (by starting the song while another is playing) in WAV form to check your work. See Logging VGMs for a bit more on this.

If vgm_sptd is stymied by a song with silence inside it, do either of the following:

  1. Make a recording with a huge silence buffer, then give vgm_sptd a large-enough Delay Sample number
  2. Use vgm_spts as suggested on its page.

Your Eardrums Playing Tricks

If all else fails and you find you can no longer trust your ears, it's perfectly acceptable to ask for help or even outright give the work of trimming to someone else. Just try your best on each track.

Trimming with vgm_trim

vgm_trim is a command line tool. You can simply drag (from within the folder) and drop VGMs on it, and input the numbers needed as it prompts. But if you have all the trimming points written down, it's easier to make a trim.bat file with lines like:

vgm_tag "Spliffyfop.vgm" 2986 40440 626409

for each track. This also keeps a handy log of the trim points, just in case something goes wrong.

In order, the numbers are the Start Sample, Loop Beginning, and End Sample. None of these will be zero for a song that loops. For a song that doesn't loop, use 0 for the Loop Beginning. (Make sure your Start Sample doesn't cut off beginning silence that's part of the song!)

Once the files are trimmed, it's time to check if everything is sounding good. Open the file in VGMPlay or Winamp. Pay special attention at the start, loop and ending points.

Here's an excellent hint to help you with checking your trim and loops: configure VGMPlay's .ini/the VGM input plugin to loop 1 time and fade for 60000 milliseconds (that is, 60 seconds). Since the fade is not added to the song length, you can find it easier by just moving the trackbar to just before the end and let the song finish. (For VGMPlay, use the right arrow to seek forward if this doesn't funkify playback.) When the song reaches the end with these settings, it will loop with a fade of one minute; you'll have plenty of time to check if everything sounds right, and the trackbar position/playback percentage jumping to the start will let you know the exact moment where the song looped.

(For VGMPlay, you can specify config options (each one with -c) on the command line, so your normal playback settings can stay intact. To do as suggested above, use VGMPlay -c General.MaxLoops=1 -c General.FadeTime=60000 file.vgm.

(Alternatively, you can also put VGMPlay.ini files in your working folder that have the options you want set. If VGMPlay finds a file with that name with the VGMs, it'll use that insteadfor playback/WAV logging.)

If after all this everything sounds good, then congratulations!

If not, something went wrong and you'll have to change your edit points to try again. Sometimes songs sound a bit off or weird, or a note gets cut or hangs. Moving the edit points around by the same few samples (commands willing) will usually avoid this. Keep trying, there's always a valid place! If you're having trouble finding an edit point at the exact place the song loops, you may try it in a different place, in a position later in the song where the sound wave drops to a quick silence, or perhaps just before a drum hit, etc. Anything easier to deal with is fine.

Keep in mind that, as long as it sounds right and it doesn't have excessive information, the trimming is good.

Continue...

Once you think you've got the trimming correct, the next step is tagging (mostly complete, but watch for edits soon) or, if you've already tagged, optimizing. Either way, you'll probably want all songs trimmed and good before you proceed.