annotate playsound/playsound_simple.c @ 504:6455549cd562

Clean up gcc4 compiler warnings.
author Ryan C. Gordon <icculus@icculus.org>
date Sat, 17 Dec 2005 18:46:20 +0000
parents 355129c687b7
children 6ae286a80da5
rev   line source
494
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
1 /*
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
2 * SDL_sound -- An abstract sound format decoding API.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
3 * Copyright (C) 2001 Ryan C. Gordon.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
4 *
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
5 * This library is free software; you can redistribute it and/or
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
6 * modify it under the terms of the GNU Lesser General Public
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
7 * License as published by the Free Software Foundation; either
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
8 * version 2.1 of the License, or (at your option) any later version.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
9 *
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
10 * This library is distributed in the hope that it will be useful,
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
13 * Lesser General Public License for more details.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
14 *
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
15 * You should have received a copy of the GNU Lesser General Public
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
16 * License along with this library; if not, write to the Free Software
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
18 */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
19
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
20 /**
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
21 * This is just a simple "decode sound, play it through SDL" example.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
22 * The much more complex, fancy, and robust code is playsound.c.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
23 *
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
24 * Please see the file COPYING in the source's root directory.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
25 *
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
26 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
27 */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
28
504
6455549cd562 Clean up gcc4 compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 494
diff changeset
29 #include <stdio.h>
6455549cd562 Clean up gcc4 compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 494
diff changeset
30 #include <stdlib.h>
6455549cd562 Clean up gcc4 compiler warnings.
Ryan C. Gordon <icculus@icculus.org>
parents: 494
diff changeset
31
494
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
32 #include "SDL.h"
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
33 #include "SDL_sound.h"
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
34
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
35 /* global decoding state. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
36 typedef struct
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
37 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
38 Sound_Sample *sample;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
39 SDL_AudioSpec devformat;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
40 Uint8 *decoded_ptr;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
41 Uint32 decoded_bytes;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
42 } PlaysoundAudioCallbackData;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
43
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
44 /*
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
45 * This variable is flipped to non-zero when the audio callback has
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
46 * finished playing the whole file.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
47 */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
48 static volatile int global_done_flag = 0;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
49
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
50
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
51 /*
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
52 * The audio callback. SDL calls this frequently to feed the audio device.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
53 * We decode the audio file being played in here in small chunks and feed
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
54 * the device as necessary. Other solutions may want to predecode more
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
55 * (or all) of the file, since this needs to run fast and frequently,
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
56 * but since we're only sitting here and waiting for the file to play,
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
57 * the only real requirement is that we can decode a given audio file
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
58 * faster than realtime, which isn't really a problem with any modern format
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
59 * on even pretty old hardware at this point.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
60 */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
61 static void audio_callback(void *userdata, Uint8 *stream, int len)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
62 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
63 PlaysoundAudioCallbackData *data = (PlaysoundAudioCallbackData *) userdata;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
64 Sound_Sample *sample = data->sample;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
65 int bw = 0; /* bytes written to stream this time through the callback */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
66
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
67 while (bw < len)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
68 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
69 int cpysize; /* bytes to copy on this iteration of the loop. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
70
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
71 if (data->decoded_bytes == 0) /* need more data! */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
72 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
73 /* if there wasn't previously an error or EOF, read more. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
74 if ( ((sample->flags & SOUND_SAMPLEFLAG_ERROR) == 0) &&
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
75 ((sample->flags & SOUND_SAMPLEFLAG_EOF) == 0) )
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
76 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
77 data->decoded_bytes = Sound_Decode(sample);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
78 data->decoded_ptr = sample->buffer;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
79 } /* if */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
80
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
81 if (data->decoded_bytes == 0)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
82 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
83 /* ...there isn't any more data to read! */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
84 memset(stream + bw, '\0', len - bw); /* write silence. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
85 global_done_flag = 1;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
86 return; /* we're done playback, one way or another. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
87 } /* if */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
88 } /* if */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
89
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
90 /* we have data decoded and ready to write to the device... */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
91 cpysize = len - bw; /* len - bw == amount device still wants. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
92 if (cpysize > data->decoded_bytes)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
93 cpysize = data->decoded_bytes; /* clamp to what we have left. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
94
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
95 /* if it's 0, next iteration will decode more or decide we're done. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
96 if (cpysize > 0)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
97 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
98 /* write this iteration's data to the device. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
99 memcpy(stream + bw, (Uint8 *) data->decoded_ptr, cpysize);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
100
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
101 /* update state for next iteration or callback */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
102 bw += cpysize;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
103 data->decoded_ptr += cpysize;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
104 data->decoded_bytes -= cpysize;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
105 } /* if */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
106 } /* while */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
107 } /* audio_callback */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
108
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
109
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
110
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
111 static void playOneSoundFile(const char *fname)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
112 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
113 PlaysoundAudioCallbackData data;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
114
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
115 memset(&data, '\0', sizeof (PlaysoundAudioCallbackData));
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
116 data.sample = Sound_NewSampleFromFile(fname, NULL, 65536);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
117 if (data.sample == NULL)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
118 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
119 fprintf(stderr, "Couldn't load '%s': %s.\n", fname, Sound_GetError());
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
120 return;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
121 } /* if */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
122
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
123 /*
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
124 * Open device in format of the the sound to be played.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
125 * We open and close the device for each sound file, so that SDL
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
126 * handles the data conversion to hardware format; this is the
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
127 * easy way out, but isn't practical for most apps. Usually you'll
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
128 * want to pick one format for all the data or one format for the
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
129 * audio device and convert the data when needed. This is a more
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
130 * complex issue than I can describe in a source code comment, though.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
131 */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
132 data.devformat.freq = data.sample->actual.rate;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
133 data.devformat.format = data.sample->actual.format;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
134 data.devformat.channels = data.sample->actual.channels;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
135 data.devformat.samples = 4096; /* I just picked a largish number here. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
136 data.devformat.callback = audio_callback;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
137 data.devformat.userdata = &data;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
138 if (SDL_OpenAudio(&data.devformat, NULL) < 0)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
139 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
140 fprintf(stderr, "Couldn't open audio device: %s.\n", SDL_GetError());
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
141 Sound_FreeSample(data.sample);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
142 return;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
143 } /* if */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
144
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
145 printf("Now playing [%s]...\n", fname);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
146 SDL_PauseAudio(0); /* SDL audio device is "paused" right after opening. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
147
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
148 global_done_flag = 0; /* the audio callback will flip this flag. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
149 while (!global_done_flag)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
150 SDL_Delay(10); /* just wait for the audio callback to finish. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
151
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
152 /* at this point, we've played the entire audio file. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
153 SDL_PauseAudio(1); /* so stop the device. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
154
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
155 /*
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
156 * Sleep two buffers' worth of audio before closing, in order
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
157 * to allow the playback to finish. This isn't always enough;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
158 * perhaps SDL needs a way to explicitly wait for device drain?
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
159 * Most apps don't have this issue, since they aren't explicitly
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
160 * closing the device as soon as a sound file is done playback.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
161 * As an alternative for this app, you could also change the callback
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
162 * to write silence for a call or two before flipping global_done_flag.
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
163 */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
164 SDL_Delay(2 * 1000 * data.devformat.samples / data.devformat.freq);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
165
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
166 /* if there was an error, tell the user. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
167 if (data.sample->flags & SOUND_SAMPLEFLAG_ERROR)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
168 fprintf(stderr, "Error decoding file: %s\n", Sound_GetError());
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
169
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
170 Sound_FreeSample(data.sample); /* clean up SDL_Sound resources... */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
171 SDL_CloseAudio(); /* will reopen with next file's format. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
172 } /* playOneSoundFile */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
173
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
174
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
175 int main(int argc, char **argv)
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
176 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
177 int i;
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
178
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
179 if (!Sound_Init()) /* this calls SDL_Init(SDL_INIT_AUDIO) ... */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
180 {
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
181 fprintf(stderr, "Sound_Init() failed: %s.\n", Sound_GetError());
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
182 SDL_Quit();
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
183 return(42);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
184 } /* if */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
185
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
186 for (i = 1; i < argc; i++) /* each arg is an audio file to play. */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
187 playOneSoundFile(argv[i]);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
188
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
189 /* Shutdown the libraries... */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
190 Sound_Quit();
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
191 SDL_Quit();
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
192 return(0);
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
193 } /* main */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
194
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
195 /* end of playsound-simple.c ... */
355129c687b7 Added playsound_simple.c, as a basic example.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff changeset
196