Skip to content

Latest commit

 

History

History
494 lines (409 loc) · 11.6 KB

playwave.c

File metadata and controls

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