annotate test/testmultiaudio.c @ 3822:748707e2ddd1 SDL-ryan-multiple-audio-device

Minor NAS tweak.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 07 Oct 2006 06:17:54 +0000
parents 2d77a732bd20
children
rev   line source
3794
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 #include "SDL.h"
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 static SDL_AudioSpec spec;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 static Uint8 *sound = NULL; /* Pointer to wave data */
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 static Uint32 soundlen = 0; /* Length of wave data */
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 typedef struct
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 SDL_AudioDeviceID dev;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 int soundpos;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 volatile int done;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 } callback_data;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 void SDLCALL play_through_once(void *arg, Uint8 * stream, int len)
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 callback_data *cbd = (callback_data *) arg;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 Uint8 *waveptr = sound + cbd->soundpos;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 int waveleft = soundlen - cbd->soundpos;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19 int cpy = len;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 if (cpy > waveleft)
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 cpy = waveleft;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 memcpy(stream, waveptr, cpy);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 len -= cpy;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 cbd->soundpos += cpy;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 if (len > 0) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 stream += cpy;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28 memset(stream, spec.silence, len);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
29 cbd->done++;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
30 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
31 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 static void test_multi_audio(int devcount)
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34 {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 callback_data cbd[64];
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 int keep_going = 1;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 int i;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 if (devcount > 64) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 fprintf(stderr, "Too many devices (%d), clamping to 64...\n", devcount);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 devcount = 64;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 spec.callback = play_through_once;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 for (i = 0; i < devcount; i++) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47 const char *devname = SDL_GetAudioDeviceName(i, 0);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 printf("playing on device #%d: ('%s')...", i, devname);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49 fflush(stdout);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 memset(&cbd[0], '\0', sizeof (callback_data));
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 spec.userdata = &cbd[0];
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 if (cbd[0].dev == 0) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 printf("Open device failed: %s\n", SDL_GetError());
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 } else {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 SDL_PauseAudioDevice(cbd[0].dev, 0);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 while (!cbd[0].done)
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 SDL_Delay(100);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 SDL_PauseAudioDevice(cbd[0].dev, 1);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 printf("done.\n");
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 SDL_CloseAudioDevice(cbd[0].dev);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66 memset(cbd, '\0', sizeof (cbd));
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 printf("playing on all devices...\n");
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 for (i = 0; i < devcount; i++) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70 const char *devname = SDL_GetAudioDeviceName(i, 0);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 spec.userdata = &cbd[i];
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 if (cbd[i].dev == 0) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 printf("Open device %d failed: %s\n", i, SDL_GetError());
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 for (i = 0; i < devcount; i++) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 if (cbd[i].dev) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80 SDL_PauseAudioDevice(cbd[i].dev, 0);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 while (keep_going) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 keep_going = 0;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 for (i = 0; i < devcount; i++) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 if ((cbd[i].dev) && (!cbd[i].done)) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 keep_going = 1;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 SDL_Delay(100);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94 for (i = 0; i < devcount; i++) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 if (cbd[i].dev) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 SDL_PauseAudioDevice(cbd[i].dev, 1);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 SDL_CloseAudioDevice(cbd[i].dev);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 printf("All done!\n");
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 int main(int argc, char **argv)
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 int devcount = 0;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109 /* Load the SDL library */
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110 if (SDL_Init(SDL_INIT_AUDIO) < 0) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 return (1);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 printf("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 devcount = SDL_GetNumAudioDevices(0);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 if (devcount < 1) {
3800
2d77a732bd20 Changed wording on test output.
Ryan C. Gordon <icculus@icculus.org>
parents: 3794
diff changeset
119 fprintf(stderr, "Don't see any specific audio devices!\n");
3794
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 } else {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 if (argv[1] == NULL) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122 argv[1] = "sample.wav";
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 /* Load the wave file into memory */
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 fprintf(stderr, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 } else {
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 test_multi_audio(devcount);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 SDL_FreeWAV(sound);
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 }
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 SDL_Quit();
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 return 0;
db24e43972ac Test program to play audio to every available device, one at a time, then all
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 }