How To: Looping music files

From Moondust Wiki
Revision as of 13:23, 21 November 2022 by Yingchun Soul (talk | contribs) (→‎Introduce)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This article explains how to make looping music files in various formats. The "Looping" music means the music file which has two parts: the intro theme which plays once at the start, and the loop theme which continues forever.


In games using the SDL Mixer X, the modified fork of SDL2 mixer library, is possible to play looping formats:

  • OGG, FLAC, and OPUS - loop start and loop end markers as Vorbis comments tags which are has PCM samples position. You can play any possible audio data as-is. The disadvantage is a big file size compare to trackers, chiptunes, and MIDI.
  • Tracker musics such as MOD, IT, S3M, XM, etc. - loop implemented as combination of "jump to pattern" and "row break" parameters in a tracks. You have able to play music and effects with any possible PCM samples and patterns with music notes. The disadvantage is some set of limits at every tracker format which requires some knowledge to make good music.
  • MIDI - as LoopStart and LoopEnd track markers which are used by ADLMIDI (now libADLMIDI which is a part of SDL Mixer X), OPNMIDI and FluidSynth. MIDI is a universal digital music format that would be played on any synthesizer with different sounding and quality. SDL Mixer X has the libADLMIDI - an emulated OPL3-based (YMF262) synthesizer with additional features such a support of the loop tags; and the libOPNMIDI - an emulated OPN2-based (YM2612) synthesizer with similar functionality to ADLMIDI. It's also possible to use any custom FM banks in various formats to change the result sounding. FM Synthesis can generate beautiful electronic waves which impossible to repeat via a wavetable synthesizer even with special effects. Similar effects are possible to repeat with real musical instruments. The disadvantage of FM synthesis is the impossibility to generate realistic sound effects or musical instruments.
  • Chiptunes (Game Music formats such as NSF, SPC, VGM, etc) has the loops support internally. If you making a looping file of one of those formats, don't forget to make the proper loop!

How to make looping OGG, OPUS, or FLAC on Audacity example

You will need to use an Audacity program (it's free and open-source!).

Notice Note: Please make a backup of the original file until making any changes.

Notice Tip: You also can use RPG Maker compatible BGM songs, they are also will have a working loop. You also can read THIS TUTORIAL which will give you the same working result.

Tutorial with Audacity

  • Open your audio file in Audacity
  • On the bottom, you'll find the selection meter, toggle it into "samples" units.
LoopingOgg Audacity Step 01.png
  • Then, set the selection to your loop point. (You also can read Official tutorial how to control Audacity while selecting the best loop points positions)
LoopingOgg Audacity Step 02.png

When you will calibrate the selection on the looping area, you can hold the Shift key and press the Play button to play the content of the selection and loop it. There is an example of alignment at the end:

LoopingOgg Audacity Step 03.png
  • ...and the example of alignment at the beginning. Make sure that waves at both edges are matching to avoid possible clicks when a player passes the loop edge.
LoopingOgg Audacity Step 04.png
Notice Tip: If your track contains many repeating fragments, you may remove all of them and keep the first only, you will reduce the final size of the file.

  • Next step: open the "Edit -> edit metatags" menu
LoopingOgg Audacity Step 05.png
Notice Note: in Audacity older than 2.3 the "Metatags" menu item is located in the "File" menu.

  • Find in a list of tags the empty fields, double-click on a title, and set the "LOOPSTART" value. Then double click value and fill it with the number of selection begin.

Do the same with a "LOOPEND". however, use the loop end samples value. (the "LOOPLENGTH" will work too, but instead of loop end sample position, the length between beginning and end in samples. If you plan to use your songs also in RPG Maker, please use the "LOOPLENGTH" tag instead of the "LOOPEND").

Notice Important note: Be careful, you must keep at least two samples after the loop end of the loop would not work!
LoopingOgg Audacity Step 06.png
  • When you finished your tags setting up work, go to the "file -> export" menu:
LoopingOgg Audacity Step 07.png
  • Then choose the OGG format and select the quality level (smaller quality gives lesser file size, but in some cases, may damage the result, and higher quality gives the bigger file size and lesser chance to damage the sounding).
LoopingOgg Audacity Step 08.png
  • After you exported the song, please try to play it via Moondust (Moondust Musplay tool, Editor, Engine) to verify the result.
LoopingOgg Audacity Step 09.png

Don't forget that your looping OGG/FLAC/OPUS file will don't loop in the regular players because there are don't support loop tags. Looping OGG files can be played in the MixerX library (Moondust apps, TheXTech, SMBX2, and SMBX-38A, there are supported both LOOPEND and LOOPLENGTH endings) or in the RPG Maker (will work if you will use the "LOOPLENGTH").

PGE MusPlay 1 4 looping song demo.png
Notice Tip: Moondust MusPlay utility since 1.4 can indicate the loop range on the seek bar which allows you to see it and easily verify it in action.

How to make looping Tracker music on OpenMPT example

To make a proper loop in your tracker music, you should add into loop end position the "Pattern jump" entry with a number of an upper pattern where you wish to loop music. But if you wish to set a more accurate loop start (for example, in the middle of the pattern), you also need to add the "Row break" entry before "Pattern jump" which will cause a skip of a specified number of rows in the next pattern (currently, next pattern - is a target pattern where playing position was jumped).

Notice Note: Loop will not work in the official SDL Mixer implementation because it was disabled by Modplug's flag inside SDL Mixer's code.

How to make looping MIDI

Looping MIDI music example (ADLMIDI, bank #59)
(Download MIDI)

To make a working loop in the MIDI-files, use any MIDI editor except FL Studio (because it saving MIDI-files with incorrect tempo meta-event which will cause playing of that MIDI-file with tempo 100), and add markers/meta-labels/etc. with labels (markers) "loopStart" and "loopEnd" at specific positions on your MIDI track.

Notice Note: Loop points are will work with libADLMIDI, libOPNMIDI and FluidSynth only, Timidity, and Native MIDI are not supports loop points and your MIDI-file will be played completely.

Tutorial with Sekaiju

You'll need to get the Sekaiju MIDI Sequencer (it's free and open source. It's designed for Windows, but works fine from the Wine on Linux and macOS).

  • Open your MIDI file in Sekaiju
  • Seek the playing cursor to the loop start position. Then, open a context menu at top of the seek bar and choose "Insert marker..." menu item:
MIDILoop 1.png
  • Correct the time value if needed (to choose the accurate time placement of the marker event), and set the "loopStart" name:
MIDILoop 2.png

Once you'll hit the "Ok", you will see that the "loopStart" marker will appear in the necessary place of the track.

  • Seek the playing cursor to the loop end position. Then, open a context menu at top of the seek bar and choose "Insert marker..." menu item:
MIDILoop 3.png

Correct the time value if needed (to choose the accurate time placement of the marker event), and set the "loopEnd" name:

MIDILoop 4.png

Once you'll hit the "Ok", you will see that the "loopEnd" marker will appear in the necessary place of the track.

  • As you have put necessary MIDI events, you will have the working loop in your MIDI file:
MIDILoop 5.png
  • Save the MIDI file and try to play it through the Moondust MusPlay utility to verify that loop is correct.
Notice Note: Since Sekaiju 5.7 you can play loop in the format "loopStart/loopEnd" from the Sekaiju out the box!
(see the video demo)
Notice Important note: You need to use "libADLMIDI", "libOPNMIDI" and "FluidSynth" synthesizers as others are don't supports loop points yet.
Notice Important note: Because of imperfection, to be sure the loop will work, let the seeker pass the loop start point, and then you can seek to the loop end position to see them jump into loop start. Otherwise, the cursor will jump into the music started.