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