How To: Looping music files

This article will explain 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
Since LunaLUA uses SDL Mixer X - a modified fork of SDL2 mixer library, is possible to play looping formats:
 * OGG and OPUS - 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) and by OPNMIDI. 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 (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. 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 or OPUS on Audacity example
You will need Audacity (I don't recommend you to use same OGG (or OPUS) 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 the best chance to don't purge quality on secondary compression).

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 begin are same or similar to escape appearance of clicks.

Hint: If you have multiple repeats of same audio piece inside your track - remove them and keep alone and then set selection to your loop zone.


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

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

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


 * When you finished work to set up tags, go to file -> export
 * Then choice 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 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 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 (PGE apps, LunaLua-SMBX and SMBX-38A, there are supported both LOOPEND and LOOPLENGTH endings) or in the RPG Maker (will work if you will use "LOOPLENGHT").


 * You also can watch the video!



Hint: PGE MusPlay utility since 1.4 can indicate the loop range on the seek bar which allows you to see it and better verify it in action.

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 (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 "loopStart" marker will appear in the necessary place of the track.
 * Correct the time value if needed (to choice 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 choice the accurate time placement of the marker event), and set the "loopEnd" name: Once you'll hit the "Ok", you will see that "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 PGE 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.