Browse Source

Use SDL's LockAudio() & UnlockAudio() instead of customized mutex

louyihua 8 years ago
parent
commit
c0f0d0230a
7 changed files with 22 additions and 49 deletions
  1. 11 23
      audio.c
  2. 1 2
      mp3play.c
  3. 1 2
      oggplay.c
  4. 5 7
      players.h
  5. 1 3
      rixplay.cpp
  6. 3 4
      sound.c
  7. 0 8
      win32/sdlpal.vcxproj

+ 11 - 23
audio.c

@@ -41,7 +41,6 @@ typedef struct tagAUDIODEVICE
 {
    SDL_AudioSpec             spec;		/* Actual-used sound specification */
    SDL_AudioCVT              cvt;		/* Audio format conversion parameter */
-   SDL_mutex                *mtx;		/* Mutex for preventing using destroyed objects */
    AUDIOPLAYER              *pMusPlayer;
    AUDIOPLAYER              *pCDPlayer;
 #if PAL_HAS_SDLCD
@@ -125,8 +124,6 @@ AUDIO_FillBuffer(
    memset(stream, 0, len);
 #endif
 
-   SDL_mutexP(gAudioDevice.mtx);
-
    gAudioDevice.cvt.buf = stream;
    gAudioDevice.cvt.len = len;
 
@@ -175,8 +172,6 @@ AUDIO_FillBuffer(
    // Convert audio from native byte-order to actual byte-order
    //
    SDL_ConvertAudio(&gAudioDevice.cvt);
-
-   SDL_mutexV(gAudioDevice.mtx);
 }
 
 INT
@@ -243,13 +238,12 @@ AUDIO_OpenDevice(
 
    SDL_BuildAudioCVT(&gAudioDevice.cvt, AUDIO_S16SYS, spec.channels, spec.freq, spec.format, spec.channels, spec.freq);
 
-   gAudioDevice.mtx = SDL_CreateMutex();
    gAudioDevice.fOpened = TRUE;
 
    //
    // Initialize the sound subsystem.
    //
-   gAudioDevice.pSoundPlayer = SOUND_Init(gAudioDevice.mtx);
+   gAudioDevice.pSoundPlayer = SOUND_Init();
 
    //
    // Initialize the music subsystem.
@@ -257,21 +251,21 @@ AUDIO_OpenDevice(
    switch (gConfig.eMusicType)
    {
    case MUSIC_RIX:
-	   if (!(gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "mus.mkf"), gAudioDevice.mtx)))
+	   if (!(gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "mus.mkf"))))
 	   {
-		   gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "MUS.MKF"), gAudioDevice.mtx);
+		   gAudioDevice.pMusPlayer = RIX_Init(va("%s%s", gConfig.pszGamePath, "MUS.MKF"));
 	   }
 	   break;
    case MUSIC_MP3:
 #if PAL_HAS_MP3
-	   gAudioDevice.pMusPlayer = MP3_Init(gAudioDevice.mtx);
+	   gAudioDevice.pMusPlayer = MP3_Init();
 #else
 	   gAudioDevice.pMusPlayer = NULL;
 #endif
 	   break;
    case MUSIC_OGG:
 #if PAL_HAS_OGG
-	   gAudioDevice.pMusPlayer = OGG_Init(gAudioDevice.mtx);
+	   gAudioDevice.pMusPlayer = OGG_Init();
 #else
 	   gAudioDevice.pMusPlayer = NULL;
 #endif
@@ -314,14 +308,14 @@ AUDIO_OpenDevice(
    }
    case MUSIC_MP3:
 #if PAL_HAS_MP3
-	   gAudioDevice.pCDPlayer = MP3_Init(gAudioDevice.mtx);
+	   gAudioDevice.pCDPlayer = MP3_Init();
 #else
 	   gAudioDevice.pCDPlayer = NULL;
 #endif
 	   break;
    case MUSIC_OGG:
 #if PAL_HAS_OGG
-	   gAudioDevice.pCDPlayer = OGG_Init(gAudioDevice.mtx);
+	   gAudioDevice.pCDPlayer = OGG_Init();
 #else
 	   gAudioDevice.pCDPlayer = NULL;
 #endif
@@ -357,8 +351,6 @@ AUDIO_CloseDevice(
 {
    SDL_CloseAudio();
 
-   SDL_mutexP(gAudioDevice.mtx);
-
    if (gAudioDevice.pSoundPlayer != NULL)
    {
       gAudioDevice.pSoundPlayer->Shutdown(gAudioDevice.pSoundPlayer);
@@ -394,10 +386,6 @@ AUDIO_CloseDevice(
 #if PAL_HAS_NATIVEMIDI
    if (gConfig.eMusicType == MUSIC_MIDI) MIDI_Play(0, FALSE);
 #endif
-
-   SDL_mutexV(gAudioDevice.mtx);
-   SDL_DestroyMutex(gAudioDevice.mtx);
-   gAudioDevice.mtx = NULL;
 }
 
 SDL_AudioSpec*
@@ -515,12 +503,12 @@ AUDIO_PlayMusic(
       return;
    }
 #endif
-   SDL_mutexP(gAudioDevice.mtx);
+   SDL_LockAudio();
    if (gAudioDevice.pMusPlayer)
    {
       gAudioDevice.pMusPlayer->Play(gAudioDevice.pMusPlayer, iNumRIX, fLoop, flFadeTime);
    }
-   SDL_mutexV(gAudioDevice.mtx);
+   SDL_UnlockAudio();
 }
 
 BOOL
@@ -562,7 +550,7 @@ AUDIO_PlayCDTrack(
       }
    }
 #endif
-   SDL_mutexP(gAudioDevice.mtx);
+   SDL_LockAudio();
    if (gAudioDevice.pCDPlayer)
    {
 	   if (iNumTrack != -1)
@@ -575,7 +563,7 @@ AUDIO_PlayCDTrack(
 		   ret = gAudioDevice.pCDPlayer->Play(gAudioDevice.pCDPlayer, -1, FALSE, 0);
 	   }
    }
-   SDL_mutexV(gAudioDevice.mtx);
+   SDL_UnlockAudio();
 
    return ret;
 }

+ 1 - 2
mp3play.c

@@ -138,7 +138,7 @@ MP3_Play(
 
 LPAUDIOPLAYER
 MP3_Init(
-	SDL_mutex *mutex
+	VOID
 )
 {
 	LPMP3PLAYER player;
@@ -147,7 +147,6 @@ MP3_Init(
 		player->FillBuffer = MP3_FillBuffer;
 		player->Play = MP3_Play;
 		player->Shutdown = MP3_Shutdown;
-		player->mutex = mutex;
 
 		player->pMP3 = NULL;
 		player->iMusic = -1;

+ 1 - 2
oggplay.c

@@ -472,7 +472,7 @@ OGG_Shutdown(
 
 LPAUDIOPLAYER
 OGG_Init(
-	SDL_mutex *mutex
+	VOID
 )
 {
 	LPOGGPLAYER player;
@@ -483,7 +483,6 @@ OGG_Init(
 		player->FillBuffer = OGG_FillBuffer;
 		player->Play = OGG_Play;
 		player->Shutdown = OGG_Shutdown;
-		player->mutex = mutex;
 
 		player->fp = NULL;
 		player->iMusic = -1;

+ 5 - 7
players.h

@@ -36,8 +36,7 @@ extern "C"
 #define AUDIOPLAYER_COMMONS \
 	VOID (*Shutdown)(VOID*); \
 	BOOL (*Play)(VOID*, INT, BOOL, FLOAT); \
-	VOID (*FillBuffer)(VOID*, LPBYTE, INT); \
-	SDL_mutex *mutex
+	VOID (*FillBuffer)(VOID*, LPBYTE, INT)
 
 	AUDIOPLAYER_COMMONS;
 } AUDIOPLAYER, *LPAUDIOPLAYER;
@@ -46,8 +45,7 @@ extern "C"
 
 LPAUDIOPLAYER
 RIX_Init(
-   LPCSTR     szFileName,
-   SDL_mutex *mutex
+   LPCSTR     szFileName
 );
 
 /* OGG */
@@ -55,7 +53,7 @@ RIX_Init(
 
 LPAUDIOPLAYER
 OGG_Init(
-	SDL_mutex *mutex
+	VOID
 );
 
 #endif
@@ -65,14 +63,14 @@ OGG_Init(
 
 LPAUDIOPLAYER
 MP3_Init(
-	SDL_mutex *mutex
+	VOID
 );
 
 #endif
 
 LPAUDIOPLAYER
 SOUND_Init(
-	SDL_mutex *mutex
+	VOID
 );
 
 #ifdef __cplusplus

+ 1 - 3
rixplay.cpp

@@ -375,8 +375,7 @@ RIX_Play(
 
 LPAUDIOPLAYER
 RIX_Init(
-	LPCSTR     szFileName,
-	SDL_mutex *mutex
+	LPCSTR     szFileName
 )
 /*++
   Purpose:
@@ -403,7 +402,6 @@ RIX_Init(
 		pRixPlayer->FillBuffer = RIX_FillBuffer;
 		pRixPlayer->Shutdown = RIX_Shutdown;
 		pRixPlayer->Play = RIX_Play;
-		pRixPlayer->mutex = mutex;
 	}
 
 	if (gConfig.fUseSurroundOPL)

+ 3 - 4
sound.c

@@ -848,7 +848,7 @@ SOUND_Play(
 		return FALSE;
 	}
 
-	SDL_mutexP(player->mutex);
+	SDL_LockAudio();
 
 	cursnd = &player->soundlist;
 	while (cursnd->next && cursnd->base)
@@ -878,7 +878,7 @@ SOUND_Play(
 	cursnd->ResampleMix = mixer;
 	player->cursounds++;
 
-	SDL_mutexV(player->mutex);
+	SDL_UnlockAudio();
 
 	return TRUE;
 }
@@ -973,7 +973,7 @@ SOUND_FillBuffer(
 
 LPAUDIOPLAYER
 SOUND_Init(
-	SDL_mutex *mutex
+	VOID
 )
 /*++
   Purpose:
@@ -1015,7 +1015,6 @@ SOUND_Init(
 			player->Play = SOUND_Play;
 			player->FillBuffer = SOUND_FillBuffer;
 			player->Shutdown = SOUND_Shutdown;
-			player->mutex = mutex;
 
 			player->LoadSound = func[i];
 			player->mkf = mkf;

+ 0 - 8
win32/sdlpal.vcxproj

@@ -102,8 +102,6 @@
       <StringPooling>true</StringPooling>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <WarningLevel>Level3</WarningLevel>
-      <SuppressStartupBanner>true</SuppressStartupBanner>
-      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
     </ClCompile>
     <ResourceCompile>
       <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@@ -133,8 +131,6 @@
       <StringPooling>true</StringPooling>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <WarningLevel>Level3</WarningLevel>
-      <SuppressStartupBanner>true</SuppressStartupBanner>
-      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
     </ClCompile>
     <ResourceCompile>
       <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@@ -165,8 +161,6 @@
       <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
       <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
       <WarningLevel>Level3</WarningLevel>
-      <SuppressStartupBanner>true</SuppressStartupBanner>
-      <DebugInformationFormat>EditAndContinue</DebugInformationFormat>
       <Optimization>Disabled</Optimization>
     </ClCompile>
     <ResourceCompile>
@@ -197,8 +191,6 @@
       <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
       <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
       <WarningLevel>Level3</WarningLevel>
-      <SuppressStartupBanner>true</SuppressStartupBanner>
-      <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
       <Optimization>Disabled</Optimization>
     </ClCompile>
     <ResourceCompile>