Skip to content

Commit

Permalink
Fixed looping behavior and Mix_Playing() result for looping channels
Browse files Browse the repository at this point in the history
Lee Salzman

If you passed in a negative loop count, it is supposed to behave as if it loops forever. However, it blindly decrements the loop counter regardless of whether or not the loop count is actually positive. So first fix is just to put a > 0 guard before the loop counter decrement.

The second fix is that Mix_Playing somehow checked only for looping > 0, so it ignored negative loop counts entirely, which just seems like a bug too. This has prevented me from using Mix_Playing internally in my engine if only because it never actually worked with looping. Mix_PlayingMusic oddly does it correctly and checks for non-zero.
  • Loading branch information
slouken committed Jul 9, 2013
1 parent befbaff commit ddd2310
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
12 changes: 8 additions & 4 deletions mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,13 +385,17 @@ static void mix_channels(void *udata, Uint8 *stream, int len)
if (mix_input != mix_channel[i].chunk->abuf)
SDL_free(mix_input);

--mix_channel[i].looping;
if (mix_channel[i].looping > 0) {
--mix_channel[i].looping;
}
mix_channel[i].samples = mix_channel[i].chunk->abuf + remaining;
mix_channel[i].playing = mix_channel[i].chunk->alen - remaining;
index += remaining;
}
if ( ! mix_channel[i].playing && mix_channel[i].looping ) {
--mix_channel[i].looping;
if (mix_channel[i].looping > 0) {
--mix_channel[i].looping;
}
mix_channel[i].samples = mix_channel[i].chunk->abuf;
mix_channel[i].playing = mix_channel[i].chunk->alen;
}
Expand Down Expand Up @@ -1111,14 +1115,14 @@ int Mix_Playing(int which)

for ( i=0; i<num_channels; ++i ) {
if ((mix_channel[i].playing > 0) ||
(mix_channel[i].looping > 0))
mix_channel[i].looping)
{
++status;
}
}
} else if ( which < num_channels ) {
if ( (mix_channel[which].playing > 0) ||
(mix_channel[which].looping > 0) )
mix_channel[which].looping )
{
++status;
}
Expand Down
4 changes: 3 additions & 1 deletion music.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ static int music_halt_or_loop (void)
if (music_loops)
{
Mix_Fading current_fade;
--music_loops;
if (music_loops > 0) {
--music_loops;
}
current_fade = music_playing->fading;
music_internal_play(music_playing, 0.0);
music_playing->fading = current_fade;
Expand Down
2 changes: 1 addition & 1 deletion native_midi/native_midi_haiku.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class MidiEventsStore : public BMidi
{
if (!ev) {
if (fLoops && fEvs) {
--fLoops;
if (fLoops > 0) --fLoops;
fPos = 0;
ev = fEvs;
} else
Expand Down
3 changes: 2 additions & 1 deletion native_midi/native_midi_win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ void CALLBACK MidiProc( HMIDIIN hMidi, UINT uMsg, DWORD_PTR dwInstance,
case MOM_POSITIONCB:
if ((currentsong->MusicLoaded) && (dwParam1 == (DWORD_PTR)&currentsong->MidiStreamHdr[currentsong->CurrentHdr])) {
if (currentsong->Loops) {
--currentsong->Loops;
if (currentsong->Loops > 0)
--currentsong->Loops;
currentsong->NewPos=0;
BlockOut(currentsong);
} else {
Expand Down

0 comments on commit ddd2310

Please sign in to comment.