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