[Tux4kids-commits] r207 - in tuxmath/trunk: data/sounds src

tholy-guest at alioth.debian.org tholy-guest at alioth.debian.org
Tue Aug 21 15:29:44 UTC 2007


Author: tholy-guest
Date: 2007-08-21 15:29:44 +0000 (Tue, 21 Aug 2007)
New Revision: 207

Modified:
   tuxmath/trunk/data/sounds/cheer.wav
   tuxmath/trunk/src/game.c
   tuxmath/trunk/src/pause.c
   tuxmath/trunk/src/setup.c
   tuxmath/trunk/src/titlescreen.c
Log:
A fairly sizable cleanup of the audio in tuxmath:
1. We were not closing SDL_mixer on exit. This might explain some sudden failures (observed in both Linux & MacOSX) of the audio after running tuxmath many times? We'll see.
2. The logic governing the availability of sound & sound hardware was fragile. The most important fix was to assume that the hardware is _not_ available unless we make it all the way through initialization successfully.  This fixes cases where the global config file says "use_sound = 1" but the user's config file says "use_sound = 0"; the problem was the sound system was not initializing, but the old UseSound() and UsingSound() would return true because the audio HW was by default assumed to be available.  This caused segfaults when one returned to the main menu after playing 1 game.
3. I changed the sampling rate down to 22050, in accordance with the docs on Mix_OpenAudio. This fixed some audio skipping on my system (even though it's not old or slow...). One weird thing is that it slows down playback of the "cheer" sound, despite the fact that I also resampled this sound to 22050Hz. Any ideas?
4. Finally, a likely-never-to-be-triggered bug with regards to timer wraparounds was made more robust.


Modified: tuxmath/trunk/data/sounds/cheer.wav
===================================================================
(Binary files differ)

Modified: tuxmath/trunk/src/game.c
===================================================================
--- tuxmath/trunk/src/game.c	2007-08-21 11:17:32 UTC (rev 206)
+++ tuxmath/trunk/src/game.c	2007-08-21 15:29:44 UTC (rev 207)
@@ -248,7 +248,13 @@
     now_time = SDL_GetTicks();
     if (now_time < last_time + FPS)
     {
-      SDL_Delay(last_time + FPS - now_time);
+      //Prevent any possibility of a time wrap-around
+      // (this is a very unlikely problem unless there is an SDL bug
+      //  or you leave tuxmath running for 49 days...)
+      now_time = (last_time+FPS) - now_time;  // this holds the delay
+      if (now_time > FPS)
+	now_time = FPS;
+      SDL_Delay(now_time);
     }
   }
   while(GAME_IN_PROGRESS == game_status);

Modified: tuxmath/trunk/src/pause.c
===================================================================
--- tuxmath/trunk/src/pause.c	2007-08-21 11:17:32 UTC (rev 206)
+++ tuxmath/trunk/src/pause.c	2007-08-21 15:29:44 UTC (rev 207)
@@ -23,7 +23,7 @@
 // For tuxtype-related stuff:
 #include "titlescreen.h"
 
-// For Opts_UseSound()
+// For Opts_UsingSound()
 #include "options.h"
 
 
@@ -34,7 +34,7 @@
 extern settings localsettings;
 
 void pause_load_media(void) {
-	if (Opts_UseSound()) 
+	if (Opts_UsingSound()) 
 		pause_sfx = LoadSound( "tock.wav" );
 
 	up = LoadImage("up.png", IMG_ALPHA);
@@ -54,7 +54,7 @@
 }
 
 void pause_unload_media(void) {
-	if (Opts_UseSound())
+	if (Opts_UsingSound())
 		Mix_FreeChunk(pause_sfx);
 	SDL_FreeSurface(up);
 	SDL_FreeSurface(down);
@@ -74,7 +74,7 @@
 	rectLeft.x = rectDown.x = 320 - (7*16) - rectLeft.w - 4;
 	rectRight.x = rectUp.x  = 320 + (7*16) + 4;
 
-	if (Opts_UseSound()) {
+	if (Opts_UsingSound()) {
 
 		SDL_BlitSurface(left, NULL, screen, &rectLeft);
 		SDL_BlitSurface(right, NULL, screen, &rectRight);
@@ -83,7 +83,7 @@
 		SDL_BlitSurface(up, NULL, screen, &rectUp);
 	}
 
-	if (Opts_UseSound()) {
+	if (Opts_UsingSound()) {
 
 		t = black_outline(_("Sound Effects Volume"), f1, &white);
 		s.y = 160;
@@ -196,7 +196,7 @@
 
 	/* --- stop all sounds, play pause noise --- */
 
-	if (Opts_UseSound()) {
+	if (Opts_UsingSound()) {
  		Mix_Pause(-1);
 		Mix_PlayChannel(-1, pause_sfx, 0);
 		sfx_volume = Mix_Volume(-1, -1);  // get sfx volume w/o changing it
@@ -211,7 +211,7 @@
 	darkenscreen(); 
 
 	pause_draw_info();
-	if (Opts_UseSound()) {
+	if (Opts_UsingSound()) {
 		draw_vols(sfx_volume, mus_volume);
 	}
 
@@ -230,7 +230,7 @@
 					exit(0);
 					break;
 				case SDL_KEYUP:
-					if (Opts_UseSound() && 
+					if (Opts_UsingSound() && 
 					   ((event.key.keysym.sym == SDLK_RIGHT) ||
 					    (event.key.keysym.sym == SDLK_LEFT))) 
 					    	tocks = 0;
@@ -242,7 +242,7 @@
 						paused = 0;
 						quit = 1;
 					}
-					if (Opts_UseSound()) { 
+					if (Opts_UsingSound()) { 
 						if (event.key.keysym.sym == SDLK_RIGHT) 
 							sfx_volume += 4;
 						if (event.key.keysym.sym == SDLK_LEFT) 
@@ -263,7 +263,7 @@
 
 					break;
 			}
-		if (Opts_UseSound() && mousePressed) {
+		if (Opts_UsingSound() && mousePressed) {
 			int x, y;
 
 			SDL_GetMouseState(&x, &y);
@@ -293,7 +293,7 @@
 			}
 		}
 
-		if (Opts_UseSound()) {
+		if (Opts_UsingSound()) {
 
 			if (sfx_volume > MIX_MAX_VOLUME)
 				sfx_volume = MIX_MAX_VOLUME;
@@ -333,7 +333,7 @@
 
 	SDL_ShowCursor(0);
 
-	if (Opts_UseSound()) {
+	if (Opts_UsingSound()) {
 		Mix_PlayChannel(-1, pause_sfx, 0);
 		Mix_Resume(-1);
 	}

Modified: tuxmath/trunk/src/setup.c
===================================================================
--- tuxmath/trunk/src/setup.c	2007-08-21 11:17:32 UTC (rev 206)
+++ tuxmath/trunk/src/setup.c	2007-08-21 15:29:44 UTC (rev 207)
@@ -400,6 +400,10 @@
 
 void initialize_SDL(void)
 {
+  // Audio parameters
+  int frequency,channels,n_timesopened;
+  Uint16 format;
+
   /* Init SDL Video: */
   screen = NULL;
 
@@ -429,6 +433,7 @@
 
   #ifndef NOSOUND
   /* Init SDL Audio: */
+  Opts_SetSoundHWAvailable(0);  // By default no sound HW
   if (Opts_UseSound())
   { 
     if (SDL_Init(SDL_INIT_AUDIO) < 0)
@@ -437,23 +442,26 @@
             "\nWarning: I could not initialize audio!\n"
             "The Simple DirectMedia error that occured was:\n"
             "%s\n\n", SDL_GetError());
-      Opts_SetSoundHWAvailable(0);
     }
-  }
-
- 
-  if (Opts_UseSound())
-  {
-    if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0)
-    {
-      fprintf(stderr,
-	      "\nWarning: I could not set up audio for 44100 Hz "
-	      "16-bit stereo.\n"
-	      "The Simple DirectMedia error that occured was:\n"
-	      "%s\n\n", SDL_GetError());
-      Opts_SetSoundHWAvailable(0);
+    else {
+      //if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 2048) < 0)
+      if (Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 2048) < 0)
+      {
+	fprintf(stderr,
+		"\nWarning: I could not set up audio for 44100 Hz "
+		"16-bit stereo.\n"
+		"The Simple DirectMedia error that occured was:\n"
+		"%s\n\n", SDL_GetError());
+      }
     }
+    n_timesopened = Mix_QuerySpec(&frequency,&format,&channels);
+    if (n_timesopened > 0)
+      Opts_SetSoundHWAvailable(1);
+    #ifdef TUXMATH_DEBUG
+    printf("Sound mixer: frequency = %d, format = %x, channels = %d, n_timesopened = %d\n",frequency,format,channels,n_timesopened);
+    #endif
   }
+  
   #endif
 
   SDL_VideoInfo *videoInfo;
@@ -592,6 +600,8 @@
 {
   /* Free all images and sounds used by SDL: */
   int i;
+  int frequency,channels,n_timesopened;
+  Uint16 format;
 
   TTF_CloseFont(default_font);
   TTF_Quit();
@@ -617,6 +627,15 @@
     musics[i] = NULL;
   }
 
+  // Close the audio mixer. We have to do this at least as many times
+  // as it was opened.
+  n_timesopened = Mix_QuerySpec(&frequency,&format,&channels);
+  while (n_timesopened) {
+    Mix_CloseAudio();
+    n_timesopened--;
+  }
+
+  // Finally, quit SDL
   SDL_Quit();
 
   /* frees the game_options struct: */

Modified: tuxmath/trunk/src/titlescreen.c
===================================================================
--- tuxmath/trunk/src/titlescreen.c	2007-08-21 11:17:32 UTC (rev 206)
+++ tuxmath/trunk/src/titlescreen.c	2007-08-21 15:29:44 UTC (rev 207)
@@ -200,7 +200,7 @@
   int old_key_menu = 5;
 
 
-  if (Opts_UseSound())
+  if (Opts_UsingSound())
   {
     Opts_SetMenuSound(1);
     Opts_SetMenuMusic(1);




More information about the Tux4kids-commits mailing list