Fixed bug 934 - Native MIDI on OS X does not loop
authorSam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2012 00:47:32 -0500
changeset 5299ca74f21d466
parent 528 bc0dfe8ae026
child 530 a74d68325094
Fixed bug 934 - Native MIDI on OS X does not loop
music.c
native_midi/native_midi.h
native_midi/native_midi_haiku.cpp
native_midi/native_midi_mac.c
native_midi/native_midi_macosx.c
native_midi/native_midi_win32.c
     1.1 --- a/music.c	Sat Dec 31 23:28:23 2011 -0500
     1.2 +++ b/music.c	Sun Jan 01 00:47:32 2012 -0500
     1.3 @@ -279,6 +279,12 @@
     1.4  #endif
     1.5  #ifdef MID_MUSIC
     1.6  			case MUS_MID:
     1.7 +#ifdef USE_NATIVE_MIDI
     1.8 +  				if ( native_midi_ok ) {
     1.9 +					/* Native midi is handled asynchronously */
    1.10 +					goto skip;
    1.11 +	  			}
    1.12 +#endif
    1.13  #ifdef USE_FLUIDSYNTH_MIDI
    1.14  				if ( fluidsynth_ok ) {
    1.15  					fluidsynth_playsome(music_playing->data.fluidsynthmidi, stream, len);
    1.16 @@ -494,6 +500,7 @@
    1.17  		music->type = MUS_MID;
    1.18  #ifdef USE_NATIVE_MIDI
    1.19    		if ( native_midi_ok ) {
    1.20 +/*printf("Native MIDI\n");*/
    1.21    			music->data.nativemidi = native_midi_loadsong(file);
    1.22  	  		if ( music->data.nativemidi == NULL ) {
    1.23  		  		Mix_SetError("%s", native_midi_error());
    1.24 @@ -504,6 +511,7 @@
    1.25  #endif
    1.26  #ifdef USE_FLUIDSYNTH_MIDI
    1.27  		if ( fluidsynth_ok ) {
    1.28 +/*printf("FluidSynth MIDI\n");*/
    1.29  			music->data.fluidsynthmidi = fluidsynth_loadsong(file);
    1.30  			if ( music->data.fluidsynthmidi == NULL ) {
    1.31  				music->error = 1;
    1.32 @@ -513,6 +521,7 @@
    1.33  #endif
    1.34  #ifdef USE_TIMIDITY_MIDI
    1.35  		if ( timidity_ok ) {
    1.36 +/*printf("Timidity MIDI\n");*/
    1.37  			music->data.midi = Timidity_LoadSong(file);
    1.38  			if ( music->data.midi == NULL ) {
    1.39  				Mix_SetError("%s", Timidity_Error());
    1.40 @@ -730,6 +739,18 @@
    1.41  {
    1.42  	int retval = 0;
    1.43  
    1.44 +	/* Try to seek instead of do a full halt and restart.
    1.45 +	   This fixes a bug with native MIDI on Mac OS X, where you
    1.46 +	   can't really stop and restart from the audio callback.
    1.47 +	*/
    1.48 +	if ( music == music_playing
    1.49 +	  /* MUS_MOD position isn't time, it's pattern order number (??) */
    1.50 +	     && music->type != MUS_MOD ) {
    1.51 +		if ( music_internal_position(position) == 0 ) {
    1.52 +			return 0;
    1.53 +		}
    1.54 +	}
    1.55 +
    1.56  	/* Note the music we're playing */
    1.57  	if ( music_playing ) {
    1.58  		music_internal_halt();
    1.59 @@ -900,6 +921,18 @@
    1.60  		MOD_jump_to_time(music_playing->data.module, position);
    1.61  		break;
    1.62  #endif
    1.63 +#ifdef MID_MUSIC
    1.64 +	    case MUS_MID:
    1.65 +#ifdef USE_NATIVE_MIDI
    1.66 +		if ( native_midi_ok ) {
    1.67 +			retval = native_midi_jump_to_time(music_playing->data.nativemidi, position);
    1.68 +			break;
    1.69 +		}
    1.70 +#endif
    1.71 +		/* TODO: Implement this for other music backends */
    1.72 +		retval = -1;
    1.73 +		break;
    1.74 +#endif
    1.75  #ifdef OGG_MUSIC
    1.76  	    case MUS_OGG:
    1.77  		OGG_jump_to_time(music_playing->data.ogg, position);
    1.78 @@ -1317,7 +1350,7 @@
    1.79  
    1.80  	SDL_LockAudio();
    1.81  	if ( music_playing ) {
    1.82 -		playing = music_internal_playing();
    1.83 +		playing = music_loops || music_internal_playing();
    1.84  	}
    1.85  	SDL_UnlockAudio();
    1.86  
     2.1 --- a/native_midi/native_midi.h	Sat Dec 31 23:28:23 2011 -0500
     2.2 +++ b/native_midi/native_midi.h	Sun Jan 01 00:47:32 2012 -0500
     2.3 @@ -31,6 +31,7 @@
     2.4  NativeMidiSong *native_midi_loadsong_RW(SDL_RWops *rw, int freerw);
     2.5  void native_midi_freesong(NativeMidiSong *song);
     2.6  void native_midi_start(NativeMidiSong *song);
     2.7 +int native_midi_jump_to_time(NativeMidiSong *song, double time);
     2.8  void native_midi_stop();
     2.9  int native_midi_active();
    2.10  void native_midi_setvolume(int volume);
     3.1 --- a/native_midi/native_midi_haiku.cpp	Sat Dec 31 23:28:23 2011 -0500
     3.2 +++ b/native_midi/native_midi_haiku.cpp	Sun Jan 01 00:47:32 2012 -0500
     3.3 @@ -258,6 +258,11 @@
     3.4    song->store->Start();
     3.5    currentSong = song;
     3.6  }
     3.7 +int native_midi_jump_to_time(NativeMidiSong *song, double time)
     3.8 +{
     3.9 +  /* Not yet implemented */
    3.10 +  return -1;
    3.11 +}
    3.12  void native_midi_stop()
    3.13  {
    3.14    if (currentSong == NULL) return;
     4.1 --- a/native_midi/native_midi_mac.c	Sat Dec 31 23:28:23 2011 -0500
     4.2 +++ b/native_midi/native_midi_mac.c	Sun Jan 01 00:47:32 2012 -0500
     4.3 @@ -254,6 +254,12 @@
     4.4  	SDL_PauseAudio(0);
     4.5  }
     4.6  
     4.7 +int native_midi_jump_to_time(NativeMidiSong *song, double time)
     4.8 +{
     4.9 +	/* Not yet implemented */
    4.10 +	return -1;
    4.11 +}
    4.12 +
    4.13  void native_midi_stop()
    4.14  {
    4.15  	if (gTunePlayer == NULL)
     5.1 --- a/native_midi/native_midi_macosx.c	Sat Dec 31 23:28:23 2011 -0500
     5.2 +++ b/native_midi/native_midi_macosx.c	Sun Jan 01 00:47:32 2012 -0500
     5.3 @@ -264,6 +264,9 @@
     5.4          MusicPlayerStop(currentsong->player);
     5.5  
     5.6      currentsong = song;
     5.7 +
     5.8 +    MusicPlayerPreroll(song->player);
     5.9 +    MusicPlayerSetTime(song->player, 0);
    5.10      MusicPlayerStart(song->player);
    5.11  
    5.12      GetSequenceAudioUnit(song->sequence, &song->audiounit);
    5.13 @@ -276,6 +279,13 @@
    5.14      SDL_PauseAudio(0);
    5.15  }
    5.16  
    5.17 +int native_midi_jump_to_time(NativeMidiSong *song, double time)
    5.18 +{
    5.19 +    if (MusicPlayerSetTime(song->player, time) != noErr)
    5.20 +        return -1;
    5.21 +    return 0;
    5.22 +}
    5.23 +
    5.24  void native_midi_stop()
    5.25  {
    5.26      if (currentsong) {
     6.1 --- a/native_midi/native_midi_win32.c	Sat Dec 31 23:28:23 2011 -0500
     6.2 +++ b/native_midi/native_midi_win32.c	Sun Jan 01 00:47:32 2012 -0500
     6.3 @@ -275,6 +275,12 @@
     6.4    }
     6.5  }
     6.6  
     6.7 +int native_midi_jump_to_time(NativeMidiSong *song, double time)
     6.8 +{
     6.9 +	/* Not yet implemented */
    6.10 +	return -1;
    6.11 +}
    6.12 +
    6.13  void native_midi_stop()
    6.14  {
    6.15    if (!hMidiStream)