Browse Source

Fix the fade out & fade in bug when using a larger audio buffer. Make rix extra init parameters a compilation-enabled feature.

louyihua 8 years ago
parent
commit
4460a720fd
7 changed files with 70 additions and 15 deletions
  1. 9 4
      adplug/rix.cpp
  2. 2 0
      adplug/rix.h
  3. 4 0
      global.c
  4. 2 0
      global.h
  5. 1 1
      oggplay.c
  6. 46 5
      rixplay.cpp
  7. 6 5
      sdlpal.vcxproj

+ 9 - 4
adplug/rix.cpp

@@ -86,6 +86,7 @@ CrixPlayer::~CrixPlayer()
   if (extra_vals) delete[] extra_vals;
 }
 
+#if USE_RIX_EXTRA_INIT
 void CrixPlayer::set_extra_init(uint32_t* regs, uint8_t* datas, int n)
 {
 	extra_length = n;
@@ -105,6 +106,7 @@ void CrixPlayer::set_extra_init(uint32_t* regs, uint8_t* datas, int n)
 		extra_vals = NULL;
 	}
 }
+#endif
 
 bool CrixPlayer::load(const std::string &filename, const CFileProvider &fp)
 {
@@ -234,15 +236,18 @@ inline void CrixPlayer::data_initial()
       ad_a0b0l_reg_(7,0x1F,0);
 
 	  // This is required for correct attack effect, by louyihua
+#if USE_RIX_EXTRA_INIT
 	  if (extra_regs && extra_vals && extra_length > 0)
 	  {
 		  for (uint32_t i = 0; i < extra_length; i++)
 			  opl->write(extra_regs[i], extra_vals[i]);
 	  }
-	  //opl->write(0xa8, 87);
-	  //opl->write(0xb8, 9);
-	  //opl->write(0xa7, 3);
-	  //opl->write(0xb7, 10);
+#else
+	  opl->write(0xa8, 87);
+	  opl->write(0xb8, 9);
+	  opl->write(0xa7, 3);
+	  opl->write(0xb7, 15/*10*/);	// Changed from 10 (original value) to 15 for better quality
+#endif
   }
   bd_modify = 0;
   ad_bd_reg();

+ 2 - 0
adplug/rix.h

@@ -48,7 +48,9 @@ class CrixPlayer: public CPlayer
   std::string gettype()
     { return std::string("Softstar RIX OPL Music Format"); };
 
+#if USE_RIX_EXTRA_INIT
   void set_extra_init(uint32_t* regs, uint8_t* datas, int n);
+#endif
 
  protected:	
   typedef struct {

+ 4 - 0
global.c

@@ -94,9 +94,11 @@ PAL_InitGlobals(
       }
    }
 
+#if USE_RIX_EXTRA_INIT
    gpGlobals->pExtraFMRegs = NULL;
    gpGlobals->pExtraFMVals = NULL;
    gpGlobals->dwExtraLength = 0;
+#endif
 
    if (fp = UTIL_OpenFile("sdlpal.cfg"))
    {
@@ -195,6 +197,7 @@ PAL_InitGlobals(
 					   else if (iVolume < 0)
 						   iVolume = 0;
 				   }
+#if USE_RIX_EXTRA_INIT
 				   else if (SDL_strcasecmp(p, "RIXEXTRAINIT") == 0)
 				   {
 					   int n = 1;
@@ -235,6 +238,7 @@ PAL_InitGlobals(
 						   }
 					   }
 				   }
+#endif
 				   else if (SDL_strcasecmp(p, "CD") == 0)
 				   {
 					   char cd_type[32];

+ 2 - 0
global.h

@@ -611,9 +611,11 @@ typedef struct tagGLOBALVARS
    DWORD            dwFrameNum;
 
    /* Configurable options */
+#if USE_RIX_EXTRA_INIT
    uint32_t        *pExtraFMRegs;
    uint8_t         *pExtraFMVals;
    uint32_t         dwExtraLength;
+#endif
    CODEPAGE         iCodePage;
    DWORD            dwWordLength;
    DWORD            dwExtraMagicDescLines;

+ 1 - 1
oggplay.c

@@ -257,10 +257,10 @@ OGG_FillBuffer(
 	)
 {
 	LPOGGPLAYER player = (LPOGGPLAYER)object;
-	double volume = (double)gpGlobals->iVolume / (SDL_MIX_MAXVOLUME * 3 / 4);
 
 	if (player->fReady) {
 		ogg_packet       op; /* one raw packet of data for decode */
+		double volume = (double)gpGlobals->iVolume / (SDL_MIX_MAXVOLUME * 3 / 4);
 		int total_bytes = 0, stage = player->iStage;
 
 		while (total_bytes < len) {

+ 46 - 5
rixplay.cpp

@@ -304,17 +304,56 @@ RIX_Play(
 	//
 	SOUND_PlayCDA(-1);
 
-	pRixPlayer->fNextLoop = fLoop;
-
-	if (iNumRIX == pRixPlayer->iCurrentMusic)
+	if (iNumRIX == pRixPlayer->iCurrentMusic && pRixPlayer->iNextMusic == -1)
 	{
+		/* Will play the same music without any pending play changes,
+		   just change the loop attribute */
+		pRixPlayer->fLoop = fLoop;
 		return TRUE;
 	}
 
+	if (pRixPlayer->FadeType != RIXPLAYER::NONE)
+	{
+		/* There is a previous running fading out or fading in.
+		Start time should be adjusted for a seamless effect. */
+		DWORD dwFadeLength = (DWORD)(flFadeTime * 1000) / 2, now = SDL_GetTicks();
+		if (pRixPlayer->FadeType == RIXPLAYER::FADE_OUT)
+		{
+			if (now >= pRixPlayer->dwStartFadeTime + pRixPlayer->dwFadeLength)
+			{
+				/* Fading out has been completed, so the next call of RIX_FillBuffer should just start fading in. */
+				pRixPlayer->dwStartFadeTime = now - dwFadeLength;
+			}
+			else
+			{
+				/* Fading out is in progress, so the next call of RIX_FillBuffer should start fading out with the same volume. */
+				pRixPlayer->dwStartFadeTime = now - (DWORD)ceil((double)dwFadeLength * (now - pRixPlayer->dwStartFadeTime) / pRixPlayer->dwFadeLength);
+			}
+		}
+		else
+		{
+			if (now >= pRixPlayer->dwStartFadeTime + pRixPlayer->dwFadeLength)
+			{
+				/* Fading in has been completed, so the next call of RIX_FillBuffer just need to start fading out. */
+				pRixPlayer->dwStartFadeTime = now;
+			}
+			else
+			{
+				/* Fading in is in progress, so the next call of RIX_FillBuffer should just start fading out with the same volume. */
+				pRixPlayer->dwStartFadeTime = now - (DWORD)ceil((double)dwFadeLength * (1.0 - (double)(now - pRixPlayer->dwStartFadeTime) / pRixPlayer->dwFadeLength));
+			}
+		}
+		pRixPlayer->dwFadeLength = dwFadeLength;
+	}
+	else
+	{
+		pRixPlayer->dwStartFadeTime = SDL_GetTicks();
+		pRixPlayer->dwFadeLength = (DWORD)(flFadeTime * 1000) / 2;
+	}
+
 	pRixPlayer->iNextMusic = iNumRIX;
-	pRixPlayer->dwStartFadeTime = SDL_GetTicks();
-	pRixPlayer->dwFadeLength = (DWORD)(flFadeTime * 1000) / 2;
 	pRixPlayer->FadeType = RIXPLAYER::FADE_OUT;
+	pRixPlayer->fNextLoop = fLoop;
 	pRixPlayer->fReady = TRUE;
 
 	return TRUE;
@@ -428,10 +467,12 @@ RIX_Init(
 		}
 	}
 
+#if USE_RIX_EXTRA_INIT
 	if (gpGlobals->pExtraFMRegs && gpGlobals->pExtraFMVals)
 	{
 		pRixPlayer->rix->set_extra_init(gpGlobals->pExtraFMRegs, gpGlobals->pExtraFMVals, gpGlobals->dwExtraLength);
 	}
+#endif
 
 	//
 	// Success.

+ 6 - 5
sdlpal.vcxproj

@@ -60,10 +60,6 @@
       <WarningLevel>Level3</WarningLevel>
       <SuppressStartupBanner>true</SuppressStartupBanner>
       <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
-      <InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
-      <IntrinsicFunctions>true</IntrinsicFunctions>
-      <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
-      <OmitFramePointers>true</OmitFramePointers>
     </ClCompile>
     <ResourceCompile>
       <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@@ -123,7 +119,12 @@
   <ItemGroup>
     <ClCompile Include="adplug\adlibemu.c" />
     <ClCompile Include="adplug\dbemuopl.cpp" />
-    <ClCompile Include="adplug\dbopl.cpp" />
+    <ClCompile Include="adplug\dbopl.cpp">
+      <InlineFunctionExpansion Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">AnySuitable</InlineFunctionExpansion>
+      <IntrinsicFunctions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</IntrinsicFunctions>
+      <FavorSizeOrSpeed Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Speed</FavorSizeOrSpeed>
+      <OmitFramePointers Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</OmitFramePointers>
+    </ClCompile>
     <ClCompile Include="adplug\demuopl.cpp" />
     <ClCompile Include="battle.c">
       <AdditionalIncludeDirectories Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>