Creating Music Loops
In Solarus, OGG music files can be looped by specifying values for
LOOPLENGTH in the file metadata. But how do you determine what these values should be so that the music loops seamlessly? This tutorial will show you how using the free audio editor Audacity.
- Creating Music Loops
The looping feature for OGG music files has been available starting with Solarus 1.5.
From the Solarus API Docs:
For OGG musics, Solarus detects the metadata tags LOOPSTART, LOOPEND and LOOPLENGTH. These tags are all optional. They should be set to a number of PCM samples. There will be an internal loop if LOOPSTART is specified. You can then use either LOOPEND or LOOPLENGTH to indicate from where you want the music to loop (by default: from the end of the file). If LOOPEND is specified, the music will loop from position LOOPEND to position LOOPSTART. Otherwise, if LOOPLENGTH is specified, the music will loop from position LOOPSTART + LOOPLENGTH to position LOOPSTART.
This tutorial was created using Audacity 2.3.2 for MacOS.
Finding the Loop Duration
The first thing you'll need is a music file that you want to loop. It should contain the intro and two full loops of the song.
Note: This tutorial will use a mono (single track) song as an example for the sake of simplicity, but the same steps could be applied to a stereo song that contains separate tracks for the left and right channels.
Launch Audacity and open your music file as shown in Figure 1.
The first thing you'll want to do is to look for a distinctive shape in the waveform that occurs during the loop (so you'll see it appear twice in the waveform display). In the example shown, one such distinctive pattern is marked with red arrows.
Place a marker approximately at this same location in both the first loop and the second loop (they don't have to be exact, there will be further adjustment later). Zoom in to where you want to place the marker and from the menu select:
Edit > Labels > Add Label at Selection
Figure 2 shows the placement of the first marker. You can give it a name if you like.
And a second marker is placed at approximately the same location in the second loop, as shown in Figure 3.
Zooming out to see the full waveform, both markers can be seen in Figure 4.
Next you'll want to select the portion of the song between the two markers. Do this by clicking in track 1 at the location of one of the markers and drag to the location of the other marker. Write down the number of samples in the selection as shown at the red circle in Figure 5.
The next step is to add a new blank track by selecting the following from the menu:
Tracks > Add New > Mono Track
Next you want to select the beginning of the new track. Do this by clicking somewhere in the new (blank) track and dragging as far as you can to the left as shown in Figure 6. If done correctly, then the start of the selection at the bottom of the window should read "000,000,000 samples".
Now insert an amount of silence in the second track equal to the duration of the loop. From the menu select:
Generate > Silence...
Change the dropdown to "samples" and enter the loop duration that you found earlier as shown in Figure 7. In this example the loop selection was 3,552,726 samples.
And track 2 with the silence now added is shown in Figure 8.
Now we want to duplicate track 1, inserting it into track 2 at the end of the silence. First of all, click somewhere in track 1 then from the menu select:
Select > Region > Track Start to End
This will select all of track 1. Track 2 should not be included in the selection. Now copy track 1 by selecting the following from the menu:
Edit > Copy
Next you want to select the very end of the silence in track 2 in order to insert the contents of track 1 there. Start by clicking in the blank region of track 2 and drag left until reaching the end of the silence. Make sure that when you release the mouse button you see a yellow vertical bar where the silence ends, indicating that your selection is at the correct location as shown in Figure 9. You can also verify you did it correctly by checking that the start of the selection (as shown at the bottom of the window) reads the number of samples that you found for the loop duration.
Now from the menu select:
Edit > Paste
Track 1 is now duplicated in track 2 with an initial delay equal to the loop duration as shown in Figure 10.
Notice that the second instance of the distinctive part of the waveform in track 1 that we found earlier lines up with the first instance of that distinctive part in track 2.
If the markers placed earlier weren't in the exact same spot in each loop, then track 1 and track 2 may not be aligned perfectly. If that is the case, then we'll make an adjustment to fix it. Zoom in to somewhere during the first loop in track 2 (which will be during the second loop of track 1).
Check to see if track 1 lines up with track 2. If they are lined up, then skip this next step. Figure 11 shows an example of track 1 not lining up with track 2 as indicated by the red arrow.
Find a distinctive part of the waveform and zoom in as much as you can such that the distinctive part is still visible in both tracks 1 and 2. Make a selection that starts from the distinctive part in track 1 and ends at the distinctive part of track 2 as shown in Figure 12.
Look at the length of the selection as shown at the bottom of the window (in this example it is 373 samples). Write this number down, as we are going to adjust the amount of silence at the beginning of track 2 by this amount. It is important to note whether track 1 is delayed relative to track 2 (in which case we need to add to the beginning silence in track 2) or whether track 2 is delayed relative to track 1 (in which case we need to subtract from the silence in track 2). In this example track 1 is delayed, so we will be adding 373 samples of silence to track 2.
To add samples (track 1 is delayed)
Do the following only if track 1 is delayed (see below if track 2 is delayed).
Zoom out and place the cursor in the silence of track 2 (make sure the selection length is 0 samples!). From the menu select:
Generate > Silence...
Change the dropdown to "samples" and enter the offset you found earlier (373 samples in this example) as shown in Figure 13.
Now write down the corrected value for the loop duration by adding the offset value to the loop duration found earlier:
LOOPLENGTH = 3,552,726 + 373 = 3,553,099 samples
Skip to Finding the Loop Start below
To subtract samples (track 2 is delayed)
Only if track 2 is delayed do the following instead.
Zoom out to find the silence of track 2 and zoom in again. Make a selection of the track 2 silence that is equal to the offset found earlier (373 samples in this example). You can confirm the length of the current selection at the bottom of the window.
Now press the backspace key to remove the current selection from track 2.
Now tracks 1 and 2 are in alignment
You should now be able to zoom in to where you were before and see that tracks 1 and 2 are now in alignment as shown in Figure 14.
Finding the Loop Start
Now we need to find where the song intro ends and the first loop begins. The easy way to do this is to play both tracks starting from the end of the silence in track 2. During the intro, track 2 will sound different from track 1. Once you no longer hear a difference between the two tracks, you've found the point where the loop begins.
Simply click in track 2 where the silence ends then click the play button at the top of the window. Once you've found the spot where the two tracks sound the same, you may want to place a marker as a reminder.
If the song intro sounds similar to the loop then it may be difficult to identify the point where the tracks first sound the same. In that case you can compare the waveforms of tracks 1 and 2 visually to find the point where they are no longer different. It may also be useful to mute track 1 or 2 so you can listen to just one track at a time. There is a mute button at the very top left of the pane for each track.
It isn't necessary to identify the exact moment where the intro ends. Moving the loop start farther to the right makes no difference in creating a seamless loop, but it will increase the file size slightly by adding more redundant samples to the intro section. However, delaying the loop start by a little bit can be useful if there is still some reverb present from the intro to ensure a more seamless transition.
Look for a good spot to place the loop start. You'll want to select somewhere slightly after the point where the intro ends (but not too far after in order to keep the file size small). Ideally you'll want a point that is nearly silent for a bit then abruptly changes to something distinctive so you'll know exactly where to put the marker. For this example, the spot chosen is shown by the red arrow in Figure 15.
One final adjustment may be needed because we want tracks 1 & 2 to be in perfect alignment at this spot. Zoom in on this region, ensuring that a visually distinctive segment is visible in both tracks 1 and 2. Then make a selection to determine the offset between tracks 1 and 2 as shown in Figure 16.
Like before you'll want to write down the length of the selection (45 samples in this example). This is the amount to add or subtract from the silence of track 1 depending on whether it is track 1 or 2 that is delayed.
But first add a marker here so you can easily find it again as shown in Figure 17. It doesn't matter what you call it as it is only temporary.
Since in this example track 2 is delayed, we will be removing 45 samples from the silence at the beginning of track 2. If in your case track 1 is delayed, then you will want to add samples to the silence instead (as described above).
While zoomed in to the silence of track 2, 45 samples are selected (as shown in Figure 18) and then removed by pressing the backspace key.
So once again you must readjust the LOOPLENGTH value that you wrote down to account for this latest offset:
LOOPLENGTH = 3,552,726 + 373 - 45 = 3,553,054 samples
Since this was the final adjustment, this is the value you'll want to enter later when saving the OGG file.
Now returning to the temporary marker at the loop start location, track 1 & 2 should now be in alignment as shown in Figure 19.
Now we want to choose the exact point to place the loop start marker. To aid in selecting a good spot, right-click on the vertical axis of each track and choose "Zoom In" a few times as shown in Figure 20. It will zoom in at the spot you click on the axis, so try to click near zero. When done, right-click and select "Zoom Reset" to return to normal zoom.
Look for a point (preferably near zero) that would result in a seamless transition for the loop. One such location is identified in Figure 21 by the marker labeled "LOOPEND".
The position of this LOOPEND marker is very important, as you'll need it later when saving the OGG file (be sure to write it down). In this example you can see that it occurs at 4,104,587 samples as indicated by the selection start value at the bottom of the window.
The way the loop transition will work is the music will play along track 1 until the LOOPEND marker is reached, at which point it jumps back to the start of the loop, which is where the LOOPEND marker appears in track 2. So for a seamless transition, you'll want to pick a point where it can change from track 1 to track 2 without an obvious discontinuity as shown in figure 22.
Now the full waveform is shown in Figure 23.
Track 2 is no longer needed at this point. You can remove it by clicking the X in the top left corner of the track 2 pane. The contents of track 1 beyond the LOOPEND marker also are not need, so you can select that part of the track and press the backspace key. In this example a little bit of track 1 beyond the marker was kept just to be safe, as is shown in Figure 24.
Saving the Music File
Now you just have to save the file while adding the appropriate metadata tags for where the loop occurs.
A note about music file formats
You'll need to export the file in the OGG format to be compatible with Solarus. However, OGG files are lossy, where each time you save an OGG file the quality will be reduced a bit in order to reduce file size. So opening an OGG file an re-saving it reduces the quality each time. MP3 & M4A are also examples of lossy audio formats.
If you're planning on making edits to the audio file later, you'll want to be sure to save a copy in a lossless format, such as WAV, AIF or FLAC. Only export to OGG at the very end once you are satisfied with the result.
You can also save the sound in the .aup format, which is the format used by audacity and is also lossless. This has the advantage of preserving any markers you've placed, but the file will not be usable in other audio programs.
Export OGG file
Once you're satisfied that everything is the way you want it, you're ready to export it as an OGG file. It is a good idea to save a copy of the file in one of the lossless formats listed above if you think you might want to edit it again later.
From the menu select:
File > Export > Export as OGG
A save dialog will pop up, allowing you to choose the location and name of the saved file. You'll also be able to select a quality level (from 0 to 10). If you're not sure what to pick, 6 is usually a good value. Higher numbers give a higher quality output but at the expense of a larger file size. Use higher values for more complicated audio. For audio that's just someone talking, a 3 will probably suffice.
Next you'll see a dialog pop up that lets you set the metadata tags. This is how you specify where the loop should occur. The dialog is shown in Figure 25.
The metadata tags we want to enter are custom, so enter the tags in the blank rows at the bottom. The Add button allows you to add more rows.
We found values for LOOPLENGTH and LOOPEND. However, the LOOPSTART tag must be specified in order for the music to loop. We can find its value with some simple arithmetic:
LOOPSTART = LOOPEND - LOOPLENGTH = 4,104,587 - 3,553,054 = 551,533 samples
Obviously the values to use for your music file will differ.
Figure 26 shows the metadata dialog with values entered for LOOPSTART and LOOPEND. It isn't necessary to enter LOOPLENGTH since the other values provide all of the needed info.
The last step is to put the exported OGG file in the "musics" folder of your Solarus project then open the project in the quest editor. Find the music file in the quest tree sidebar on the left, right-click it and select "Add to quest as music...". Then click the play selected music button in the toolbar at the top to test it to see if it loops correctly.