comparison playsound/playsound.c @ 112:3fcb23da06ba

Made more robust; fixes most sample rate, etc incompatibilities.
author Ryan C. Gordon <icculus@icculus.org>
date Thu, 04 Oct 2001 00:59:12 +0000
parents 7505dcf8d3b7
children 69cd80e80363
comparison
equal deleted inserted replaced
111:b99770def077 112:3fcb23da06ba
31 #include "SDL.h" 31 #include "SDL.h"
32 #include "SDL_sound.h" 32 #include "SDL_sound.h"
33 33
34 #define PLAYSOUND_VER_MAJOR 0 34 #define PLAYSOUND_VER_MAJOR 0
35 #define PLAYSOUND_VER_MINOR 1 35 #define PLAYSOUND_VER_MINOR 1
36 #define PLAYSOUND_VER_PATCH 1 36 #define PLAYSOUND_VER_PATCH 2
37 37
38 38
39 static void output_versions(const char *argv0) 39 static void output_versions(const char *argv0)
40 { 40 {
41 Sound_Version compiled; 41 Sound_Version compiled;
89 89
90 static void audio_callback(void *userdata, Uint8 *stream, int len) 90 static void audio_callback(void *userdata, Uint8 *stream, int len)
91 { 91 {
92 static Uint8 overflow[16384]; /* this is a hack. */ 92 static Uint8 overflow[16384]; /* this is a hack. */
93 static int overflowBytes = 0; 93 static int overflowBytes = 0;
94 Sound_Sample *sample = *((Sound_Sample **) userdata); 94 Sound_Sample *sample = (Sound_Sample *) userdata;
95 int bw = 0; /* bytes written to stream*/ 95 int bw = 0; /* bytes written to stream*/
96 Uint32 rc; /* return code */ 96 Uint32 rc; /* return code */
97 97
98 if (overflowBytes > 0) 98 if (overflowBytes > 0)
99 { 99 {
148 148
149 int main(int argc, char **argv) 149 int main(int argc, char **argv)
150 { 150 {
151 Sound_AudioInfo sound_desired; 151 Sound_AudioInfo sound_desired;
152 SDL_AudioSpec sdl_desired; 152 SDL_AudioSpec sdl_desired;
153 SDL_AudioSpec sdl_actual;
153 Sound_Sample *sample; 154 Sound_Sample *sample;
154 int i; 155 int i;
155 156
157 /* !!! FIXME: Move this to a parse_cmdline() function... */
156 if (argc < 2) 158 if (argc < 2)
157 { 159 {
158 output_usage(argv[0]); 160 output_usage(argv[0]);
159 return(42); 161 return(42);
160 } /* if */ 162 } /* if */
206 " reason: [%s].\n", Sound_GetError()); 208 " reason: [%s].\n", Sound_GetError());
207 SDL_Quit(); 209 SDL_Quit();
208 return(42); 210 return(42);
209 } /* if */ 211 } /* if */
210 212
211 sound_desired.rate = 44100;
212 sound_desired.channels = 2;
213 sound_desired.format = AUDIO_S16SYS;
214
215 sdl_desired.freq = 44100;
216 sdl_desired.format = AUDIO_S16SYS;
217 sdl_desired.channels = 2;
218 sdl_desired.samples = 4096;
219 sdl_desired.callback = audio_callback;
220 sdl_desired.userdata = &sample;
221
222 if ( SDL_OpenAudio(&sdl_desired, NULL) < 0 )
223 {
224 fprintf(stderr, "Couldn't open audio device!\n"
225 " reason: [%s].\n", SDL_GetError());
226 Sound_Quit();
227 SDL_Quit();
228 return(42);
229 } /* if */
230
231 for (i = 1; i < argc; i++) 213 for (i = 1; i < argc; i++)
232 { 214 {
233 if (strncmp(argv[i], "--", 2) == 0) 215 if (strncmp(argv[i], "--", 2) == 0)
234 continue; 216 continue;
235 217
236 printf("Now playing [%s]...\n", argv[i]); 218 sample = Sound_NewSampleFromFile(argv[i], NULL, 4096 * 4);
237
238 sample = Sound_NewSampleFromFile(argv[i], &sound_desired, 4096 * 4);
239 if (!sample) 219 if (!sample)
240 { 220 {
241 fprintf(stderr, "Couldn't load \"%s\"!\n" 221 fprintf(stderr, "Couldn't load \"%s\"!\n"
242 " reason: [%s].\n", argv[i], Sound_GetError()); 222 " reason: [%s].\n", argv[i], Sound_GetError());
243 continue; 223 continue;
244 } /* if */ 224 } /* if */
225
226 sdl_desired.freq = sample->actual.rate;
227 sdl_desired.format = sample->actual.format;
228 sdl_desired.channels = sample->actual.channels;
229 sdl_desired.samples = 4096;
230 sdl_desired.callback = audio_callback;
231 sdl_desired.userdata = sample;
232
233 if (SDL_OpenAudio(&sdl_desired, NULL) < 0)
234 {
235 fprintf(stderr, "Couldn't open audio device!\n"
236 " reason: [%s].\n", SDL_GetError());
237 Sound_Quit();
238 SDL_Quit();
239 return(42);
240 } /* if */
241
242 printf("Now playing [%s]...\n", argv[i]);
245 243
246 done_flag = 0; 244 done_flag = 0;
247 SDL_PauseAudio(0); 245 SDL_PauseAudio(0);
248 while (!done_flag) 246 while (!done_flag)
249 SDL_Delay(10); 247 SDL_Delay(10);
250 SDL_PauseAudio(1); 248 SDL_PauseAudio(1);
249 SDL_CloseAudio(); /* reopen with next sample's format if possible */
251 250
252 Sound_FreeSample(sample); 251 Sound_FreeSample(sample);
253 } /* for */ 252 } /* for */
254 253
255 Sound_Quit(); 254 Sound_Quit();