Browse Source

Configurable volume

louyihua 8 years ago
parent
commit
d25dd1d449
9 changed files with 34 additions and 47 deletions
  1. 10 0
      global.c
  2. 1 0
      global.h
  3. 2 0
      mp3play.c
  4. 3 7
      oggplay.c
  5. 1 9
      rixplay.cpp
  6. 3 0
      sdlpal.cfg.example
  7. 4 1
      sdlpal.vcxproj
  8. 10 28
      sound.c
  9. 0 2
      sound.h

+ 10 - 0
global.c

@@ -80,6 +80,7 @@ PAL_InitGlobals(
    INT       iOPLSampleRate = 49716;	// Default for 49716 Hz
    INT       iResampleQuality = RESAMPLER_QUALITY_MAX;	// Default to maximum quality
    INT       iAudioBufferSize = 1024;	// Default for 1024 samples
+   INT       iVolume = 100;				// Default for 100%
    MUSICTYPE eMusicType = g_fUseMidi ? MUSIC_MIDI : MUSIC_RIX;
    MUSICTYPE eCDType = PAL_HAS_SDLCD ? MUSIC_SDLCD : MUSIC_OGG;
    OPLTYPE   eOPLType = OPL_DOSBOX;
@@ -186,6 +187,14 @@ PAL_InitGlobals(
 					   else if (iAudioBufferSize < 2)
 						   iAudioBufferSize = 2;
 				   }
+				   else if (SDL_strcasecmp(p, "VOLUME") == 0)
+				   {
+					   sscanf(ptr, "%d", &iVolume);
+					   if (iVolume > 100)
+						   iVolume = 100;
+					   else if (iVolume < 0)
+						   iVolume = 0;
+				   }
 				   else if (SDL_strcasecmp(p, "RIXEXTRAINIT") == 0)
 				   {
 					   int n = 1;
@@ -287,6 +296,7 @@ PAL_InitGlobals(
    gpGlobals->dwExtraMagicDescLines = dwExtraMagicDescLines;
    gpGlobals->dwExtraItemDescLines = dwExtraItemDescLines;
    gpGlobals->wAudioBufferSize = (WORD)iAudioBufferSize;
+   gpGlobals->iVolume = SDL_MIX_MAXVOLUME * iVolume / 100;
 
    //
    // Set decompress function

+ 1 - 0
global.h

@@ -623,6 +623,7 @@ typedef struct tagGLOBALVARS
    INT              iSampleRate;
    INT              iOPLSampleRate;
    INT              iResampleQuality;
+   INT              iVolume;
    MUSICTYPE        eMusicType;
    MUSICTYPE        eCDType;
    OPLTYPE          eOPLType;

+ 2 - 0
mp3play.c

@@ -58,6 +58,8 @@ MP3_FillBuffer(
 {
 	LPMP3PLAYER player = (LPMP3PLAYER)object;
 	if (player->pMP3) {
+		player->pMP3->volume = gpGlobals->iVolume * 3 / 4;
+
 		mad_getSamples(player->pMP3, stream, len);
 
 		if (!mad_isPlaying(player->pMP3) && player->fLoop)

+ 3 - 7
oggplay.c

@@ -66,9 +66,9 @@ typedef struct tagOGGPLAYER
 	BOOL             fUseResampler;
 } OGGPLAYER, *LPOGGPLAYER;
 
-static SDL_FORCE_INLINE ogg_int16_t OGG_GetSample(float pcm, int volume)
+static SDL_FORCE_INLINE ogg_int16_t OGG_GetSample(float pcm, double volume)
 {
-	int val = floor(pcm * 32767.f + .5f) * volume / SDL_MIX_MAXVOLUME;
+	int val = (int)(floor(pcm * 32767.f + .5f) * volume);
 	/* might as well guard against clipping */
 	if (val > 32767) {
 		val = 32767;
@@ -257,11 +257,7 @@ OGG_FillBuffer(
 	)
 {
 	LPOGGPLAYER player = (LPOGGPLAYER)object;
-#ifdef __SYMBIAN32__
-	int volume = g_iVolume / 2;
-#else
-	int volume = SDL_MIX_MAXVOLUME / 2;
-#endif
+	double volume = (double)gpGlobals->iVolume / (SDL_MIX_MAXVOLUME * 3 / 4);
 
 	if (player->fReady) {
 		ogg_packet       op; /* one raw packet of data for decode */

+ 1 - 9
rixplay.cpp

@@ -32,9 +32,6 @@
 #include "adplug/rix.h"
 
 extern "C" BOOL g_fNoMusic;
-#ifdef __SYMBIAN32__
-extern "C" INT  g_iVolume;
-#endif
 
 typedef struct tagRIXPLAYER :
 	public MUSICPLAYER
@@ -78,12 +75,7 @@ RIX_FillBuffer(
 
 --*/
 {
-	INT       i, l;
-#ifdef __SYMBIAN32__
-	INT       volume = g_iVolume / 2;
-#else
-	INT       volume = SDL_MIX_MAXVOLUME / 2;
-#endif
+	INT       i, l, volume = gpGlobals->iVolume * 3 / 4;
 	UINT      t = SDL_GetTicks();
 	LPRIXPLAYER pRixPlayer = (LPRIXPLAYER)object;
 

+ 3 - 0
sdlpal.cfg.example

@@ -79,3 +79,6 @@
 # AudioBufferSize: Indicates the size of audio buffer in samples. Valid values are in 
 #                  range 2 - 32768, but should be power of 2. Default value is 1024.
 #AudioBufferSize=1024
+
+# Volume: Indicates the global volume. Valid values are from 0 to 100, default is 100.
+#Volume=100

+ 4 - 1
sdlpal.vcxproj

@@ -60,7 +60,10 @@
       <WarningLevel>Level3</WarningLevel>
       <SuppressStartupBanner>true</SuppressStartupBanner>
       <DebugInformationFormat>ProgramDatabase</DebugInformationFormat>
-      <Optimization>Disabled</Optimization>
+      <InlineFunctionExpansion>AnySuitable</InlineFunctionExpansion>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <FavorSizeOrSpeed>Speed</FavorSizeOrSpeed>
+      <OmitFramePointers>true</OmitFramePointers>
     </ClCompile>
     <ResourceCompile>
       <PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>

+ 10 - 28
sound.c

@@ -40,10 +40,6 @@ static BOOL  gSndOpened = FALSE;
 BOOL         g_fNoSound = FALSE;
 BOOL         g_fNoMusic = FALSE;
 
-#ifdef __SYMBIAN32__
-INT          g_iVolume  = SDL_MIX_MAXVOLUME * 0.1;
-#endif
-
 #ifdef PAL_CLASSIC
 int          g_iCurrChannel = 0;
 #endif
@@ -500,11 +496,7 @@ SOUND_FillAudio(
       // Mix as much data as possible
       //
       len = (len > gSndPlayer.audio_len[i]) ? gSndPlayer.audio_len[i] : len;
-#ifdef __SYMBIAN32__
-      SDL_MixAudio(stream, gSndPlayer.pos[i], len, g_iVolume);
-#else
-      SDL_MixAudio(stream, gSndPlayer.pos[i], len, SDL_MIX_MAXVOLUME * 2 / 3);
-#endif
+      SDL_MixAudio(stream, gSndPlayer.pos[i], len, gpGlobals->iVolume);
       gSndPlayer.pos[i] += len;
       gSndPlayer.audio_len[i] -= len;
    }
@@ -798,11 +790,9 @@ SOUND_GetAudioSpec(
 	return &gSndPlayer.spec;
 }
 
-#ifdef __SYMBIAN32__
-
 VOID
 SOUND_AdjustVolume(
-   INT    iDirectory
+   INT    iDirection
 )
 /*++
   Purpose:
@@ -811,7 +801,7 @@ SOUND_AdjustVolume(
 
   Parameters:
 
-    [IN]  iDirectory - value, Increase (>0) or decrease (<=0) 3% volume.
+    [IN]  iDirection - value, Increase (>0) or decrease (<=0) 3% volume.
 
   Return value:
 
@@ -819,32 +809,24 @@ SOUND_AdjustVolume(
 
 --*/
 {
-   if (iDirectory > 0)
+   if (iDirection > 0)
    {
-      if (g_iVolume <= SDL_MIX_MAXVOLUME)
+      gpGlobals->iVolume += SDL_MIX_MAXVOLUME * 0.03;
+      if (gpGlobals->iVolume > SDL_MIX_MAXVOLUME)
       {
-         g_iVolume += SDL_MIX_MAXVOLUME * 0.03;
-      }
-      else
-      {
-         g_iVolume = SDL_MIX_MAXVOLUME;
+		  gpGlobals->iVolume = SDL_MIX_MAXVOLUME;
       }
    }
    else
    {
-      if (g_iVolume > 0)
-      {
-         g_iVolume -= SDL_MIX_MAXVOLUME * 0.03;
-      }
-      else
+      gpGlobals->iVolume -= SDL_MIX_MAXVOLUME * 0.03;
+      if (gpGlobals->iVolume < 0)
       {
-         g_iVolume = 0;
+		  gpGlobals->iVolume = 0;
       }
    }
 }
 
-#endif
-
 VOID
 SOUND_PlayChannel(
    INT    iSoundNum,

+ 0 - 2
sound.h

@@ -54,12 +54,10 @@ SOUND_GetAudioSpec(
    VOID
 );
 
-#ifdef __SYMBIAN32__
 VOID
 SOUND_AdjustVolume(
    INT    iDirectory
 );
-#endif
 
 VOID
 SOUND_PlayMUS(