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