How To: Looping music files

In this article will be explained how to make looping music files in various formats. The "Looping" music means the music file which has two parts: intro theme which plays once on start, and loop theme which continues forever.

Introduce
Since LunaLUA uses SDL Mixer X - a modified fork of SDL2 mixer library, is possible to play looping formats:
 * OGG - loop start and loop end as vorbis comments tags which are has PCM samples position. You have able to play any possible audio data as-is. Con is a big file size in comparison to trackers and to 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 a music notes. Con is a some limits of each tracker format which requires a some knowage to make a good music.
 * MIDI - as LoopStart and LoopEnd track markers which are used by ADLMIDI (now libADLMIDI which a part of SDL Mixer X). MIDI is universal digital music format which would be playd on any synthesizers with different sounding and quality. Inside SDL Mixer X now is using the libADLMIDI - an emulated OPL3-based synthesizer with additional features such a support of the loop tags. Later will be implemented ability to use any custom FM banks in various formats to modify result sounding. FM Synthesis can generate beautiful electronic waves which impossible to repeat via wave-table synthesizer even with special effects. Similar effects are possible to repeat with real musical instruments. Con is an impossibility to generate realistic sound effect or musical instrument.
 * Game Music formats such as NSF, SPC, VGM, etc. are supports loops internally. If you making a looping file of one of those formats, don't forget to make a right loop!

How to make looping OGG on Audacity example
You would to have Audacity (I don't recommend you to use same ogg file, and if possible, open in Audacity the source file (highly recommended to have a flac, wav, or generate it again if you was rendered your previous version directly to MP3 or into OGG format). That will give best chance to don't purge quality on secondary compression).

When you opened your audio file, find in the bottom the selection meter, toggle it into "samples" units. Then set selection to your loop zone. When you will calibrate it you can hold shift and press play to loop selection. Be sure than waves at begib and at end are same or similar to escape appearence of clicks. If you have multiple repeats inside your track - remove them and keep alone and then set selection to your loop zone.

Next step: open "File -> edit metatags" Find in a list of tags the empty fields, double click to title and set "LOOPSTART". Then double click value and type number of selection begin. Do same with "LOOPEND", But with end samples value. (LOOPLENGTH will work too, but instead of loop end sample position, the length between begin and end in samples).

Important: Be careful, you must keep even two samples after loop end or loop will not work!

When you finished work to set up a tags, do export into the flac (optionally as backup) and then export into ogg format (choice that quality which gives less size, but keeps good quality. If target file got a poor quality, restore your backup and increase quality, then save it again). When you made target file, please try to play just-saved music file via PGE (PGE Musplay tool, Editor, Engine). Don't forget that your looping OGG file will not be loop in the regular players because there are not supports this feature. Looping OGG-files are will be played in the SDL2 Mixer X library (PGE apps and LunaLUA-SMBX) or in the RPG Maker (will work if you will use LOOPLENGHT).

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

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

How to make looping MIDI
To make 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 "LOOPStart" and "LOOPEnd" at specific positions on your MIDI track.

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