comparison playsound/playsound.c @ 135:69cd80e80363

Added Torbj�rn Andersson's command lines, and cleaned up the usage output.
author Ryan C. Gordon <icculus@icculus.org>
date Fri, 12 Oct 2001 16:54:35 +0000
parents 3fcb23da06ba
children e3f4767d6037
comparison
equal deleted inserted replaced
134:3ce7ede90d24 135:69cd80e80363
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 2 36 #define PLAYSOUND_VER_PATCH 3
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;
139 139
140 140
141 static void output_usage(const char *argv0) 141 static void output_usage(const char *argv0)
142 { 142 {
143 fprintf(stderr, 143 fprintf(stderr,
144 "USAGE: %s [--decoders] [soundFile1] ... [soundFileN]\n", 144 "USAGE: %s [...options...] [soundFile1] ... [soundFileN]\n"
145 "\n"
146 " Options:\n"
147 " --rate x Playback at sample rate of x HZ.\n"
148 " --format fmt Playback in fmt format (see below).\n"
149 " --channels n Playback on n channels (1 or 2).\n"
150 " --version Display version information and exit.\n"
151 " --decoders List supported sound formats and exit.\n"
152 " --help Display this information and exit.\n"
153 "\n"
154 " Valid arguments to the --format option are:\n"
155 " U8 Unsigned 8-bit.\n"
156 " S8 Signed 8-bit.\n"
157 " U16LSB Unsigned 16-bit (least significant byte first).\n"
158 " U16MSB Unsigned 16-bit (most significant byte first).\n"
159 " S16LSB Signed 16-bit (least significant byte first).\n"
160 " S16MSB Signed 16-bit (most significant byte first).\n"
161 "\n",
145 argv0); 162 argv0);
146 } /* output_usage */ 163 } /* output_usage */
164
165
166 static int str_to_fmt(char *str)
167 {
168 if (strcmp(str, "U8") == 0)
169 return AUDIO_U8;
170 if (strcmp(str, "S8") == 0)
171 return AUDIO_S8;
172 if (strcmp(str, "U16LSB") == 0)
173 return AUDIO_U16LSB;
174 if (strcmp(str, "S16LSB") == 0)
175 return AUDIO_S16LSB;
176 if (strcmp(str, "U16MSB") == 0)
177 return AUDIO_U16MSB;
178 if (strcmp(str, "S16MSB") == 0)
179 return AUDIO_S16MSB;
180 return 0;
181 } /* str_to_fmt */
147 182
148 183
149 int main(int argc, char **argv) 184 int main(int argc, char **argv)
150 { 185 {
151 Sound_AudioInfo sound_desired; 186 Sound_AudioInfo sound_desired;
152 SDL_AudioSpec sdl_desired; 187 SDL_AudioSpec sdl_desired;
153 SDL_AudioSpec sdl_actual; 188 SDL_AudioSpec sdl_actual;
154 Sound_Sample *sample; 189 Sound_Sample *sample;
190 int use_specific_audiofmt = 0;
155 int i; 191 int i;
156 192
157 /* !!! FIXME: Move this to a parse_cmdline() function... */ 193 /* !!! FIXME: Move this to a parse_cmdline() function... */
158 if (argc < 2) 194 if (argc < 2)
159 { 195 {
160 output_usage(argv[0]); 196 output_usage(argv[0]);
161 return(42); 197 return(42);
162 } /* if */ 198 } /* if */
163 199
200 memset(&sound_desired, '\0', sizeof (sound_desired));
201
164 for (i = 0; i < argc; i++) 202 for (i = 0; i < argc; i++)
165 { 203 {
166 if (strncmp(argv[i], "--", 2) != 0) 204 if (strncmp(argv[i], "--", 2) != 0)
167 continue; 205 continue;
168 206
169 if ( (strcmp(argv[i], "--version") == 0) || 207 if (strcmp(argv[i], "--version") == 0)
170 (strcmp(argv[i], "--help") == 0 ) )
171 { 208 {
172 output_versions(argv[0]); 209 output_versions(argv[0]);
210 return(42);
211 } /* if */
212
213 else if (strcmp(argv[i], "--help") == 0)
214 {
173 output_usage(argv[0]); 215 output_usage(argv[0]);
174 } /* if */ 216 return(42);
217 } /* if */
218
219 else if (strcmp(argv[i], "--rate") == 0 && argc > i + 1)
220 {
221 use_specific_audiofmt = 1;
222 sound_desired.rate = atoi(argv[++i]);
223 if (sound_desired.rate <= 0)
224 {
225 fprintf(stderr, "Bad argument to --rate!\n");
226 return(42);
227 }
228 } /* else if */
229
230 else if (strcmp(argv[i], "--format") == 0 && argc > i + 1)
231 {
232 use_specific_audiofmt = 1;
233 sound_desired.format = str_to_fmt(argv[++i]);
234 if (sound_desired.format == 0)
235 {
236 fprintf(stderr, "Bad argument to --format! Try one of:\n"
237 "U8, S8, U16LSB, S16LSB, U16MSB, S16MSB\n");
238 return(42);
239 }
240 } /* else if */
241
242 else if (strcmp(argv[i], "--channels") == 0 && argc > i + 1)
243 {
244 use_specific_audiofmt = 1;
245 sound_desired.channels = atoi(argv[++i]);
246 if (sound_desired.channels < 1 || sound_desired.channels > 2)
247 {
248 fprintf(stderr,
249 "Bad argument to --channels! Try 1 (mono) or 2 "
250 "(stereo).\n");
251 return(42);
252 }
253 } /* else if */
175 254
176 else if (strcmp(argv[i], "--decoders") == 0) 255 else if (strcmp(argv[i], "--decoders") == 0)
177 { 256 {
178 if (!Sound_Init()) 257 if (!Sound_Init())
179 { 258 {
193 fprintf(stderr, "unknown option: \"%s\"\n", argv[i]); 272 fprintf(stderr, "unknown option: \"%s\"\n", argv[i]);
194 return(42); 273 return(42);
195 } /* else */ 274 } /* else */
196 } /* for */ 275 } /* for */
197 276
277 /* Pick sensible defaults for any value not explicitly specified. */
278 if (use_specific_audiofmt)
279 {
280 if (sound_desired.rate == 0)
281 sound_desired.rate = 44100;
282 if (sound_desired.format == 0)
283 sound_desired.format = AUDIO_S16SYS;
284 if (sound_desired.channels == 0)
285 sound_desired.channels = 2;
286 } /* if */
287
198 if (SDL_Init(SDL_INIT_AUDIO) == -1) 288 if (SDL_Init(SDL_INIT_AUDIO) == -1)
199 { 289 {
200 fprintf(stderr, "SDL_Init(SDL_INIT_AUDIO) failed!\n" 290 fprintf(stderr, "SDL_Init(SDL_INIT_AUDIO) failed!\n"
201 " reason: [%s].\n", SDL_GetError()); 291 " reason: [%s].\n", SDL_GetError());
202 return(42); 292 return(42);
210 return(42); 300 return(42);
211 } /* if */ 301 } /* if */
212 302
213 for (i = 1; i < argc; i++) 303 for (i = 1; i < argc; i++)
214 { 304 {
305 /* !!! FIXME: This is ugly! */
306 if ( (strcmp(argv[i], "--rate") == 0) ||
307 (strcmp(argv[i], "--format") == 0) ||
308 (strcmp(argv[i], "--channels") == 0) )
309 {
310 i++;
311 continue;
312 } /* if */
313
215 if (strncmp(argv[i], "--", 2) == 0) 314 if (strncmp(argv[i], "--", 2) == 0)
216 continue; 315 continue;
217 316
218 sample = Sound_NewSampleFromFile(argv[i], NULL, 4096 * 4); 317 sample = Sound_NewSampleFromFile(argv[i],
318 use_specific_audiofmt ? &sound_desired : NULL, 4096 * 4);
319
219 if (!sample) 320 if (!sample)
220 { 321 {
221 fprintf(stderr, "Couldn't load \"%s\"!\n" 322 fprintf(stderr, "Couldn't load \"%s\"!\n"
222 " reason: [%s].\n", argv[i], Sound_GetError()); 323 " reason: [%s].\n", argv[i], Sound_GetError());
223 continue; 324 continue;
224 } /* if */ 325 } /* if */
225 326
226 sdl_desired.freq = sample->actual.rate; 327 /*
227 sdl_desired.format = sample->actual.format; 328 * Unless explicitly specified, pick the format from the sound
228 sdl_desired.channels = sample->actual.channels; 329 * to be played.
229 sdl_desired.samples = 4096; 330 */
331 if (use_specific_audiofmt)
332 {
333 sdl_desired.freq = sound_desired.rate;
334 sdl_desired.format = sound_desired.format;
335 sdl_desired.channels = sound_desired.channels;
336 } /* if */
337 else
338 {
339 sdl_desired.freq = sample->actual.rate;
340 sdl_desired.format = sample->actual.format;
341 sdl_desired.channels = sample->actual.channels;
342 } /* else */
343
344 sdl_desired.samples = 4096 * 2;
230 sdl_desired.callback = audio_callback; 345 sdl_desired.callback = audio_callback;
231 sdl_desired.userdata = sample; 346 sdl_desired.userdata = sample;
232 347
233 if (SDL_OpenAudio(&sdl_desired, NULL) < 0) 348 if (SDL_OpenAudio(&sdl_desired, NULL) < 0)
234 { 349 {