Browse Source

Eliminate MIDI calls when PAL_HAS_NATIVEMIDI is not defined

louyihua 8 years ago
parent
commit
124361ef8a
5 changed files with 23 additions and 7 deletions
  1. 11 2
      audio.c
  2. 2 0
      input.c
  3. 0 5
      midi.h
  4. 8 0
      sound.c
  5. 2 0
      util.c

+ 11 - 2
audio.c

@@ -391,7 +391,9 @@ AUDIO_CloseDevice(
 	  gAudioDevice.pSoundBuffer = NULL;
    }
 
+#if PAL_HAS_NATIVEMIDI
    if (gConfig.eMusicType == MUSIC_MIDI) MIDI_Play(0, FALSE);
+#endif
 
    SDL_mutexV(gAudioDevice.mtx);
    SDL_DestroyMutex(gAudioDevice.mtx);
@@ -489,6 +491,10 @@ AUDIO_PlaySound(
 
 --*/
 {
+   // Unlike musics that use the 'load as required' strategy, sound player
+   // load the entire sound file at once, which may cause about 0.5s or longer
+   // latency for large sound files. To prevent this latency affects audio playing,
+   // the mutex lock is obtained inside the SOUND_Play function rather than here.
    if (gAudioDevice.pSoundPlayer)
    {
       gAudioDevice.pSoundPlayer->Play(gAudioDevice.pSoundPlayer, abs(iSoundNum), FALSE, 0.0f);
@@ -502,12 +508,15 @@ AUDIO_PlayMusic(
    FLOAT     flFadeTime
 )
 {
-   SDL_mutexP(gAudioDevice.mtx);
+#if PAL_HAS_NATIVEMIDI
    if (gConfig.eMusicType == MUSIC_MIDI)
    {
       MIDI_Play(iNumRIX, fLoop);
+      return;
    }
-   else if (gAudioDevice.pMusPlayer)
+#endif
+   SDL_mutexP(gAudioDevice.mtx);
+   if (gAudioDevice.pMusPlayer)
    {
       gAudioDevice.pMusPlayer->Play(gAudioDevice.pMusPlayer, iNumRIX, fLoop, flFadeTime);
    }

+ 2 - 0
input.c

@@ -1131,7 +1131,9 @@ PAL_ProcessEvent(
 
 --*/
 {
+#if PAL_HAS_NATIVEMIDI
    MIDI_CheckLoop();
+#endif
    while (PAL_PollEvent(NULL));
 }
 

+ 0 - 5
midi.h

@@ -43,11 +43,6 @@ MIDI_CheckLoop(
    VOID
 );
 
-#else
-
-# define MIDI_Play(iNumRIX, fLoop)
-# define MIDI_CheckLoop()
-
 #endif
 
 #ifdef __cplusplus

+ 8 - 0
sound.c

@@ -799,6 +799,14 @@ SOUND_Play(
 	const void      *snddata;
 	int              len, i;
 
+	//
+	// Check for NULL pointer.
+	//
+	if (player == NULL)
+	{
+		return FALSE;
+	}
+
 	//
 	// Get the length of the sound file.
 	//

+ 2 - 0
util.c

@@ -289,7 +289,9 @@ UTIL_Delay(
       while (PAL_PollEvent(NULL));
    }
 
+#if PAL_HAS_NATIVEMIDI
    MIDI_CheckLoop();
+#endif
 }
 
 void