We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2847

Summary: Loop points (start/end) for music
Product: SDL_mixer Reporter: Vitaly Novichkov <admin>
Component: miscAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: enhancement    
Priority: P2    
Version: unspecified   
Hardware: All   
OS: All   
Attachments: Add support for Vorbis comments based loop tags in OGG files
Demo file which contains looped fragment in middle

Description Vitaly Novichkov 2015-01-19 07:55:59 UTC
Hello!
I have a suggestion for custom loop points: unlike "from start to end" of music file, I have suggestion to make able to loop music from, for example, "from sample 3513 to sample 94125".

This thing will much useful. I wish to set up loop points as function which will accept integer values with numbers of audio file samples which will be start and end points of loop. When track starts, it start playback from begin and looping between defined points. To make definition of loop points I have, for example, by INI file which locating with music file.
Comment 1 Vitaly Novichkov 2016-04-08 11:03:40 UTC
Okay, I Already implemented that myself.
So, every OGG file can have vorbis tags: LOOPSTART, LOOPEND or LOOPLENGTH. There are contains PCM sample positions. Those points are remembering into special values. If loop is configured to infinite loop, every sample fetch check for loop end escaping. If loop end detected, crop unneeded sample data after loop end, and jump to loopStart.

Security notes: to avoid infinite loops and other similar bugs, before init loop points check that there are must be:
- Loop end must be always after loop start
- If loop start less or equal to loop end, mark them as invalid loop points and think like is no loop points.

My code of OGG processing is here: https://github.com/Wohlhabend-Networks/PGE-Project/blob/master/_Libs/SDL2_mixer_modified/music_ogg.c#L70

Note: you also will see custom resampler implementation which I made because buggy resampler implementation at SDL side.
Comment 2 Sam Lantinga 2017-10-18 05:32:27 UTC
This seems really useful. Your link gives a 404 though. Can you attach a patch based on the latest (highly modified) code in Mercurial?
http://hg.libsdl.org/SDL_mixer

Thanks!
Comment 3 Vitaly Novichkov 2017-10-18 05:48:28 UTC
Hello!

Sorry, I wrote this long time ago and I also did a lot of modifications, even I have moved my SDL Mixer fork into own repository away from the repository of my game engine project. The file I wanted to show:
https://github.com/WohlSoft/SDL-Mixer-X/blob/master/src/codecs/music_ogg.c

Yes, I'll create a patch for you based on top of your official repo and I'll attach it to here.

P.S. Before you rewrote music.c I did that by myself in my form where I have implemented similar to "Mix_MusicInterface" thing and I have fixed some of the bugs you have been fixed recently: https://github.com/WohlSoft/SDL-Mixer-X
I'll need to synchronize changes with you since our sources are becoming very different now
Comment 4 Sam Lantinga 2017-10-18 07:06:00 UTC
Thanks! Your fork looks great! :)
Comment 5 Vitaly Novichkov 2017-10-18 16:52:01 UTC
Created attachment 3008 [details]
Add support for Vorbis comments based loop tags in OGG files

LOOPSTART - loop start PCM sample position
LOOPEND - loop end PCM sample position
LOOPLENGTH - loop length PCM sample position (alternative to LOOPEND, added for compatibility with RPG Maker targeted OGG files)
Comment 6 Vitaly Novichkov 2017-10-18 16:58:40 UTC
Created attachment 3009 [details]
Demo file which contains looped fragment in middle

Try to play this short fragment in a regular player and then play same through SDL Mixer. A short fragment in middle must be looped forever.

Note: in my fork, I have the ability to pass loops limit:
https://github.com/WohlSoft/SDL-Mixer-X/blob/master/src/audio_codec.h#L89
https://github.com/WohlSoft/SDL-Mixer-X/blob/master/src/codecs/music_ogg.c#L175
which is not implemented here. Because of implementation specifics, I will need to modify all other codecs modules to add one small function into this interface.

I highly recommend you to have a function similar to this:
https://github.com/WohlSoft/SDL-Mixer-X/blob/master/src/audio_codec.h#L112
to initialize all interface structure fields with default (dummy or NULL) values instead of doing that in each codec module. That will give the ability to add more functions into the same interface without touching of other modules ;-)
Comment 7 Sam Lantinga 2017-10-18 23:07:14 UTC
Added, thanks!
https://hg.libsdl.org/SDL_mixer/rev/bd5fa0d4f34a

I'll leave this bug open for a bit in case you want to refine it.
Comment 8 Sam Lantinga 2017-10-21 19:26:40 UTC
I just looked over the code and it looks good, thanks!
Comment 9 Vitaly Novichkov 2017-10-21 19:35:08 UTC
The one con of this: when you ran music playing with disabled loop (limited count of loops or playing once), the loop will work infinite. To fix this I must to expand your Music Interface class (I gonna to optimize it a bit, if you don't object). I'll create another discussion for merge of our forks