How To: Looping music files

From PGE Wiki
Jump to navigation Jump to search

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.


Since LunaLUA uses SDL Mixer X - a modified fork of SDL2 mixer library, is possible to play looping formats:

  • OGG, FLAC, 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, OPUS, or FLAC 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).

Tip: You also can use RPG Maker compatible BGM songs, they are also will have 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 best loop points positions)
LoopingOgg Audacity Step 02.png

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

LoopingOgg Audacity Step 03.png
  • ...and at begin are same or similar to escape appearance of clicks.
LoopingOgg Audacity Step 04.png

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"
LoopingOgg Audacity Step 05.png
Note: in Audacity older than 2.3 the "Metatags" menuitem is located in "File" menu.
  • 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.

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

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

LoopingOgg Audacity Step 06.png
  • When you finished work to set up tags, go to file -> export
LoopingOgg Audacity Step 07.png
  • 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).
LoopingOgg Audacity Step 08.png
  • When you made target file, please try to play just-saved music file via PGE (PGE Musplay tool, Editor, Engine).
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 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").

PGE MusPlay 1 4 looping song demo.png

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

Looping MIDI music example (ADLMIDI, bank #59)
(Download 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.

Note: Loop points are will work with libADLMIDI and libOPNMIDI only, Timidity, Fluidsynth 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 choice 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 "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 choice 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 "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 PGE MusPlay utility to verify that loop is correct.
Note: Since Sekaiju 5.7 you can play loop in format "loopStart/loopEnd" from the Sekaiju out the box!

(see the video demo)

Important note: You need to use "libADLMIDI" or "libOPNMIDI" synthesizers as others are don't supports loop points yet.
Important note: Because of imperfection, to be sure loop will work, let the seeker to pass the loop start point, and then you can seek to the loop end position to see the jump into loop start. Otherwise, the cursor will jump into music start.