playwave.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 17 Feb 2011 13:33:16 -0800
changeset 503 0c77794aaef2
parent 488 94b190330355
child 518 8bc9b5fd2aae
permissions -rw-r--r--
Fixed bug 1021 (misplaced HAVE_SIGNAL_H condition in playmus.c and playwave.c)

Ozkan Sezer 2010-10-08 01:45:34 PDT

The HAVE_SIGNAL_H dependent signal.h includes in playmus.c and playwave.c are
misplaced, because SDL_config.h is not included at that point and this causes
signal.h to be never included. Moving it after the SDL.h and SDL_mixer.h
includes fixes the issue, like in the following.
     1 /*
     2     PLAYWAVE:  A test application for the SDL mixer library.
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 #include <stdlib.h>
    26 #include <stdio.h>
    27 #include <string.h>
    28 
    29 #ifdef unix
    30 #include <unistd.h>
    31 #endif
    32 
    33 #include "SDL.h"
    34 #include "SDL_mixer.h"
    35 
    36 #ifdef HAVE_SIGNAL_H
    37 #include <signal.h>
    38 #endif
    39 
    40 
    41 /*
    42  * rcg06132001 various mixer tests. Define the ones you want.
    43  */
    44 /*#define TEST_MIX_DECODERS*/
    45 /*#define TEST_MIX_VERSIONS*/
    46 /*#define TEST_MIX_CHANNELFINISHED*/
    47 /*#define TEST_MIX_PANNING*/
    48 /*#define TEST_MIX_DISTANCE*/
    49 /*#define TEST_MIX_POSITION*/
    50 
    51 
    52 #if (defined TEST_MIX_POSITION)
    53 
    54 #if (defined TEST_MIX_PANNING)
    55 #error TEST_MIX_POSITION interferes with TEST_MIX_PANNING.
    56 #endif
    57 
    58 #if (defined TEST_MIX_DISTANCE)
    59 #error TEST_MIX_POSITION interferes with TEST_MIX_DISTANCE.
    60 #endif
    61 
    62 #endif
    63 
    64 
    65 /* rcg06192001 for debugging purposes. */
    66 static void output_test_warnings(void)
    67 {
    68 #if (defined TEST_MIX_CHANNELFINISHED)
    69 	fprintf(stderr, "Warning: TEST_MIX_CHANNELFINISHED is enabled in this binary...\n");
    70 #endif
    71 #if (defined TEST_MIX_PANNING)
    72 	fprintf(stderr, "Warning: TEST_MIX_PANNING is enabled in this binary...\n");
    73 #endif
    74 #if (defined TEST_MIX_VERSIONS)
    75 	fprintf(stderr, "Warning: TEST_MIX_VERSIONS is enabled in this binary...\n");
    76 #endif
    77 #if (defined TEST_MIX_DISTANCE)
    78 	fprintf(stderr, "Warning: TEST_MIX_DISTANCE is enabled in this binary...\n");
    79 #endif
    80 #if (defined TEST_MIX_POSITION)
    81 	fprintf(stderr, "Warning: TEST_MIX_POSITION is enabled in this binary...\n");
    82 #endif
    83 }
    84 
    85 
    86 static int audio_open = 0;
    87 static Mix_Chunk *wave = NULL;
    88 
    89 /* rcg06042009 Report available decoders. */
    90 #if (defined TEST_MIX_DECODERS)
    91 static void report_decoders(void)
    92 {
    93 	int i, total;
    94 
    95     printf("Supported decoders...\n");
    96 	total = Mix_GetNumChunkDecoders();
    97 	for (i = 0; i < total; i++) {
    98 		fprintf(stderr, " - chunk decoder: %s\n", Mix_GetChunkDecoder(i));
    99 	}
   100 
   101 	total = Mix_GetNumMusicDecoders();
   102 	for (i = 0; i < total; i++) {
   103 		fprintf(stderr, " - music decoder: %s\n", Mix_GetMusicDecoder(i));
   104 	}
   105 }
   106 #endif
   107 
   108 /* rcg06192001 Check new Mixer version API. */
   109 #if (defined TEST_MIX_VERSIONS)
   110 static void output_versions(const char *libname, const SDL_version *compiled,
   111 							const SDL_version *linked)
   112 {
   113 	fprintf(stderr,
   114 			"This program was compiled against %s %d.%d.%d,\n"
   115 			" and is dynamically linked to %d.%d.%d.\n", libname,
   116 			compiled->major, compiled->minor, compiled->patch,
   117 			linked->major, linked->minor, linked->patch);
   118 }
   119 
   120 static void test_versions(void)
   121 {
   122 	SDL_version compiled;
   123 	const SDL_version *linked;
   124 
   125 	SDL_VERSION(&compiled);
   126 	linked = SDL_Linked_Version();
   127 	output_versions("SDL", &compiled, linked);
   128 
   129 	SDL_MIXER_VERSION(&compiled);
   130 	linked = Mix_Linked_Version();
   131 	output_versions("SDL_mixer", &compiled, linked);
   132 }
   133 #endif
   134 
   135 
   136 #ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
   137 static volatile int channel_is_done = 0;
   138 static void channel_complete_callback(int chan)
   139 {
   140 	Mix_Chunk *done_chunk = Mix_GetChunk(chan);
   141 	fprintf(stderr, "We were just alerted that Mixer channel #%d is done.\n", chan);
   142 	fprintf(stderr, "Channel's chunk pointer is (%p).\n", done_chunk);
   143 	fprintf(stderr, " Which %s correct.\n", (wave == done_chunk) ? "is" : "is NOT");
   144 	channel_is_done = 1;
   145 }
   146 #endif
   147 
   148 
   149 /* rcg06192001 abstract this out for testing purposes. */
   150 static int still_playing(void)
   151 {
   152 #ifdef TEST_MIX_CHANNELFINISHED
   153 	return(!channel_is_done);
   154 #else
   155 	return(Mix_Playing(0));
   156 #endif
   157 }
   158 
   159 
   160 #if (defined TEST_MIX_PANNING)
   161 static void do_panning_update(void)
   162 {
   163 	static Uint8 leftvol = 128;
   164 	static Uint8 rightvol = 128;
   165 	static Uint8 leftincr = -1;
   166 	static Uint8 rightincr = 1;
   167 	static int panningok = 1;
   168 	static Uint32 next_panning_update = 0;
   169 
   170 	if ((panningok) && (SDL_GetTicks() >= next_panning_update)) {
   171 		panningok = Mix_SetPanning(0, leftvol, rightvol);
   172 		if (!panningok) {
   173 			fprintf(stderr, "Mix_SetPanning(0, %d, %d) failed!\n",
   174 					(int) leftvol, (int) rightvol);
   175 			fprintf(stderr, "Reason: [%s].\n", Mix_GetError());
   176 		}
   177 
   178 		if ((leftvol == 255) || (leftvol == 0)) {
   179 			if (leftvol == 255)
   180 				printf("All the way in the left speaker.\n");
   181 				leftincr *= -1;
   182 		}
   183 
   184 		if ((rightvol == 255) || (rightvol == 0)) {
   185 			if (rightvol == 255)
   186 				printf("All the way in the right speaker.\n");
   187 			rightincr *= -1;
   188 		}
   189 
   190 		leftvol += leftincr;
   191 		rightvol += rightincr;
   192 		next_panning_update = SDL_GetTicks() + 10;
   193 	}
   194 }
   195 #endif
   196 
   197 
   198 #if (defined TEST_MIX_DISTANCE)
   199 static void do_distance_update(void)
   200 {
   201 	static Uint8 distance = 1;
   202 	static Uint8 distincr = 1;
   203 	static int distanceok = 1;
   204 	static Uint32 next_distance_update = 0;
   205 
   206 	if ((distanceok) && (SDL_GetTicks() >= next_distance_update)) {
   207 		distanceok = Mix_SetDistance(0, distance);
   208 		if (!distanceok) {
   209 			fprintf(stderr, "Mix_SetDistance(0, %d) failed!\n", (int) distance);
   210 			fprintf(stderr, "Reason: [%s].\n", Mix_GetError());
   211 		}
   212 
   213 		if (distance == 0) {
   214 			printf("Distance at nearest point.\n");
   215 			distincr *= -1;
   216 		}
   217 		else if (distance == 255) {
   218 			printf("Distance at furthest point.\n");
   219 			distincr *= -1;
   220 		}
   221 
   222 		distance += distincr;
   223 		next_distance_update = SDL_GetTicks() + 15;
   224 	}
   225 }
   226 #endif
   227 
   228 
   229 #if (defined TEST_MIX_POSITION)
   230 static void do_position_update(void)
   231 {
   232 	static Sint16 distance = 1;
   233 	static Sint8 distincr = 1;
   234 	static Uint16 angle = 0;
   235 	static Sint8 angleincr = 1;
   236 	static int positionok = 1;
   237 	static Uint32 next_position_update = 0;
   238 
   239 	if ((positionok) && (SDL_GetTicks() >= next_position_update)) {
   240 		positionok = Mix_SetPosition(0, angle, distance);
   241 		if (!positionok) {
   242 			fprintf(stderr, "Mix_SetPosition(0, %d, %d) failed!\n",
   243 					(int) angle, (int) distance);
   244 			fprintf(stderr, "Reason: [%s].\n", Mix_GetError());
   245 		}
   246 
   247 		if (angle == 0) {
   248 			printf("Due north; now rotating clockwise...\n");
   249 			angleincr = 1;
   250 		}
   251 
   252 		else if (angle == 360) {
   253 			printf("Due north; now rotating counter-clockwise...\n");
   254 			angleincr = -1;
   255 		}
   256 
   257 		distance += distincr;
   258 
   259 		if (distance < 0) {
   260 			distance = 0;
   261 			distincr = 3;
   262 			printf("Distance is very, very near. Stepping away by threes...\n");
   263 		} else if (distance > 255) {
   264 			distance = 255;
   265 			distincr = -3;
   266 			printf("Distance is very, very far. Stepping towards by threes...\n");
   267 		}
   268 
   269 		angle += angleincr;
   270 		next_position_update = SDL_GetTicks() + 30;
   271 	}
   272 }
   273 #endif
   274 
   275 
   276 static void CleanUp(int exitcode)
   277 {
   278 	if ( wave ) {
   279 		Mix_FreeChunk(wave);
   280 		wave = NULL;
   281 	}
   282 	if ( audio_open ) {
   283 		Mix_CloseAudio();
   284 		audio_open = 0;
   285 	}
   286 	SDL_Quit();
   287 
   288 	exit(exitcode);
   289 }
   290 
   291 
   292 static void Usage(char *argv0)
   293 {
   294 	fprintf(stderr, "Usage: %s [-8] [-r rate] [-c channels] [-f] [-F] [-l] [-m] <wavefile>\n", argv0);
   295 }
   296 
   297 
   298 /*
   299  * rcg06182001 This is sick, but cool.
   300  *
   301  *  Actually, it's meant to be an example of how to manipulate a voice
   302  *  without having to use the mixer effects API. This is more processing
   303  *  up front, but no extra during the mixing process. Also, in a case like
   304  *  this, when you need to touch the whole sample at once, it's the only
   305  *  option you've got. And, with the effects API, you are altering a copy of
   306  *  the original sample for each playback, and thus, your changes aren't
   307  *  permanent; here, you've got a reversed sample, and that's that until
   308  *  you either reverse it again, or reload it.
   309  */
   310 static void flip_sample(Mix_Chunk *wave)
   311 {
   312 	Uint16 format;
   313 	int channels, i, incr;
   314 	Uint8 *start = wave->abuf;
   315 	Uint8 *end = wave->abuf + wave->alen;
   316 
   317 	Mix_QuerySpec(NULL, &format, &channels);
   318 	incr = (format & 0xFF) * channels;
   319 
   320 	end -= incr;
   321 
   322 	switch (incr) {
   323 		case 8:
   324 			for (i = wave->alen / 2; i >= 0; i -= 1) {
   325 				Uint8 tmp = *start;
   326 				*start = *end;
   327 				*end = tmp;
   328 				start++;
   329 				end--;
   330 			}
   331 			break;
   332 
   333 		case 16:
   334 			for (i = wave->alen / 2; i >= 0; i -= 2) {
   335 				Uint16 tmp = *start;
   336 				*((Uint16 *) start) = *((Uint16 *) end);
   337 				*((Uint16 *) end) = tmp;
   338 				start += 2;
   339 				end -= 2;
   340 			}
   341 			break;
   342 
   343 		case 32:
   344 			for (i = wave->alen / 2; i >= 0; i -= 4) {
   345 				Uint32 tmp = *start;
   346 				*((Uint32 *) start) = *((Uint32 *) end);
   347 				*((Uint32 *) end) = tmp;
   348 				start += 4;
   349 				end -= 4;
   350 			}
   351 			break;
   352 
   353 		default:
   354 			fprintf(stderr, "Unhandled format in sample flipping.\n");
   355 			return;
   356 	}
   357 }
   358 
   359 
   360 int main(int argc, char *argv[])
   361 {
   362 	int audio_rate;
   363 	Uint16 audio_format;
   364 	int audio_channels;
   365 	int loops = 0;
   366 	int i;
   367 	int reverse_stereo = 0;
   368 	int reverse_sample = 0;
   369 
   370 #ifdef HAVE_SETBUF
   371 	setbuf(stdout, NULL);    /* rcg06132001 for debugging purposes. */
   372 	setbuf(stderr, NULL);    /* rcg06192001 for debugging purposes, too. */
   373 #endif
   374 	output_test_warnings();
   375 
   376 	/* Initialize variables */
   377 	audio_rate = MIX_DEFAULT_FREQUENCY;
   378 	audio_format = MIX_DEFAULT_FORMAT;
   379 	audio_channels = 2;
   380 
   381 	/* Check command line usage */
   382 	for ( i=1; argv[i] && (*argv[i] == '-'); ++i ) {
   383 		if ( (strcmp(argv[i], "-r") == 0) && argv[i+1] ) {
   384 			++i;
   385 			audio_rate = atoi(argv[i]);
   386 		} else
   387 		if ( strcmp(argv[i], "-m") == 0 ) {
   388 			audio_channels = 1;
   389 		} else
   390 		if ( (strcmp(argv[i], "-c") == 0) && argv[i+1] ) {
   391 			++i;
   392 			audio_channels = atoi(argv[i]);
   393 		} else
   394 		if ( strcmp(argv[i], "-l") == 0 ) {
   395 			loops = -1;
   396 		} else
   397 		if ( strcmp(argv[i], "-8") == 0 ) {
   398 			audio_format = AUDIO_U8;
   399 		} else
   400 		if ( strcmp(argv[i], "-f") == 0 ) { /* rcg06122001 flip stereo */
   401 			reverse_stereo = 1;
   402 		} else
   403 		if ( strcmp(argv[i], "-F") == 0 ) { /* rcg06172001 flip sample */
   404 			reverse_sample = 1;
   405 		} else {
   406 			Usage(argv[0]);
   407 			return(1);
   408 		}
   409 	}
   410 	if ( ! argv[i] ) {
   411 		Usage(argv[0]);
   412 		return(1);
   413 	}
   414 
   415 	/* Initialize the SDL library */
   416 	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
   417 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
   418 		return(255);
   419 	}
   420 #ifdef HAVE_SIGNAL_H
   421 	signal(SIGINT, CleanUp);
   422 	signal(SIGTERM, CleanUp);
   423 #endif
   424 
   425 	/* Open the audio device */
   426 	if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
   427 		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
   428 		CleanUp(2);
   429 	} else {
   430 		Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
   431 		printf("Opened audio at %d Hz %d bit %s", audio_rate,
   432 			(audio_format&0xFF),
   433 			(audio_channels > 2) ? "surround" :
   434 			(audio_channels > 1) ? "stereo" : "mono");
   435 		if ( loops ) {
   436 		  printf(" (looping)\n");
   437 		} else {
   438 		  putchar('\n');
   439 		}
   440 	}
   441 	audio_open = 1;
   442 
   443 #if (defined TEST_MIX_VERSIONS)
   444 	test_versions();
   445 #endif
   446 
   447 #if (defined TEST_MIX_DECODERS)
   448 	report_decoders();
   449 #endif
   450 
   451 	/* Load the requested wave file */
   452 	wave = Mix_LoadWAV(argv[i]);
   453 	if ( wave == NULL ) {
   454 		fprintf(stderr, "Couldn't load %s: %s\n",
   455 						argv[i], SDL_GetError());
   456 		CleanUp(2);
   457 	}
   458 
   459 	if (reverse_sample) {
   460 		flip_sample(wave);
   461 	}
   462 
   463 #ifdef TEST_MIX_CHANNELFINISHED  /* rcg06072001 */
   464 	Mix_ChannelFinished(channel_complete_callback);
   465 #endif
   466 
   467 	if ( (!Mix_SetReverseStereo(MIX_CHANNEL_POST, reverse_stereo)) &&
   468 		 (reverse_stereo) )
   469 	{
   470 		printf("Failed to set up reverse stereo effect!\n");
   471 		printf("Reason: [%s].\n", Mix_GetError());
   472 	}
   473 
   474 	/* Play and then exit */
   475 	Mix_PlayChannel(0, wave, loops);
   476 
   477 	while (still_playing()) {
   478 
   479 #if (defined TEST_MIX_PANNING)  /* rcg06132001 */
   480 		do_panning_update();
   481 #endif
   482 
   483 #if (defined TEST_MIX_DISTANCE) /* rcg06192001 */
   484 		do_distance_update();
   485 #endif
   486 
   487 #if (defined TEST_MIX_POSITION) /* rcg06202001 */
   488 		do_position_update();
   489 #endif
   490 
   491 		SDL_Delay(1);
   492 
   493 	} /* while still_playing() loop... */
   494 
   495 	CleanUp(0);
   496 
   497 	/* Not reached, but fixes compiler warnings */
   498 	return 0;
   499 }
   500 
   501 /* end of playwave.c ... */
   502