How To: Looping music files

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.

Introduce
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) and by OPNMIDI. 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!).

Tutorial with Audacity

 * Open your audio file in Audacity
 * On the bottom, you'll find the selection meter, toggle it into "samples" units.


 * Then set the selection to your loop point. (You also can read Official tutorial how to control Audacity while selecting best loop points positions)

When you will calibrate it you can hold shift and press play to loop selection. Be sure than waves at the end.


 * ...and at the beginning are same or similar to escape the appearance of clicks.


 * Next step: open "Edit -> edit metatags"

Do the same with a "LOOPEND", But with 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".).
 * Find in a list of tags the empty fields, double-click on a title, and set the "LOOPSTART" value. Then double click value and type number of selection begin.




 * When you finished work to set up tags, go to file -> export
 * Then choose the FLAC (optionally as backup) format and save, then repeat the previous step, but save into the OGG format ( a choice that quality which gives less size, but keeps the good quality. If target file got a poor quality, restore your backup and increase quality, then save it again).




 * When you made the target file, please try to play a just-saved music file via Moondust (Moondust Musplay tool, Editor, Engine).

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


 * You also can watch the video!



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).

How to make looping 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.

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:

Once you'll hit the "Ok", you will see that the "loopStart" marker will appear in the necessary place of the track.
 * Correct the time value if needed (to choose the accurate time placement of the marker event), and set the "loopStart" name:


 * 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:

Correct the time value if needed (to choose the accurate time placement of the marker event), and set the "loopEnd" name: 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:


 * Save the MIDI file and try to play it through the Moondust MusPlay utility to verify that loop is correct.

(see the video demo)


 * You also can watch the video!
 * Also, you can download the example MIDI file from this tutorial and see how it works.