Mercurial > SDL_sound_CoreAudio
comparison playsound/playsound.c @ 58:4a51162099e0
Renamed from test/test_sdlsound.c ... lots of other updates, too.
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Mon, 24 Sep 2001 15:28:32 +0000 |
parents | |
children | 7505dcf8d3b7 |
comparison
equal
deleted
inserted
replaced
57:1b89d5720dcb | 58:4a51162099e0 |
---|---|
1 /* | |
2 * SDL_sound -- An abstract sound format decoding API. | |
3 * Copyright (C) 2001 Ryan C. Gordon. | |
4 * | |
5 * This library is free software; you can redistribute it and/or | |
6 * modify it under the terms of the GNU Lesser General Public | |
7 * License as published by the Free Software Foundation; either | |
8 * version 2.1 of the License, or (at your option) any later version. | |
9 * | |
10 * This library is distributed in the hope that it will be useful, | |
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Lesser General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Lesser General Public | |
16 * License along with this library; if not, write to the Free Software | |
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 */ | |
19 | |
20 /** | |
21 * This is a quick and dirty test of SDL_sound. | |
22 * | |
23 * Please see the file LICENSE in the source's root directory. | |
24 * | |
25 * This file written by Ryan C. Gordon. (icculus@clutteredmind.org) | |
26 */ | |
27 | |
28 #include <stdio.h> | |
29 #include <string.h> | |
30 #include <assert.h> | |
31 #include "SDL.h" | |
32 #include "SDL_sound.h" | |
33 | |
34 #define PLAYSOUND_VER_MAJOR 0 | |
35 #define PLAYSOUND_VER_MINOR 1 | |
36 #define PLAYSOUND_VER_PATCH 1 | |
37 | |
38 | |
39 static void output_versions(const char *argv0) | |
40 { | |
41 Sound_Version compiled; | |
42 Sound_Version linked; | |
43 SDL_version sdl_compiled; | |
44 const SDL_version *sdl_linked; | |
45 | |
46 SOUND_VERSION(&compiled); | |
47 Sound_GetLinkedVersion(&linked); | |
48 SDL_VERSION(&sdl_compiled); | |
49 sdl_linked = SDL_Linked_Version(); | |
50 | |
51 fprintf(stderr, | |
52 "%s version %d.%d.%d.\n" | |
53 " Compiled against SDL_sound version %d.%d.%d,\n" | |
54 " and linked against %d.%d.%d.\n" | |
55 " Compiled against SDL version %d.%d.%d,\n" | |
56 " and linked against %d.%d.%d.\n\n", | |
57 argv0, | |
58 PLAYSOUND_VER_MAJOR, PLAYSOUND_VER_MINOR, PLAYSOUND_VER_PATCH, | |
59 compiled.major, compiled.minor, compiled.patch, | |
60 linked.major, linked.minor, linked.patch, | |
61 sdl_compiled.major, sdl_compiled.minor, sdl_compiled.patch, | |
62 sdl_linked->major, sdl_linked->minor, sdl_linked->patch); | |
63 } /* output_versions */ | |
64 | |
65 | |
66 static void output_decoders(void) | |
67 { | |
68 const Sound_DecoderInfo **rc = Sound_AvailableDecoders(); | |
69 const Sound_DecoderInfo **i; | |
70 | |
71 fprintf(stderr, "Supported sound formats:\n"); | |
72 if (rc == NULL) | |
73 fprintf(stderr, " * Apparently, NONE!\n"); | |
74 else | |
75 { | |
76 for (i = rc; *i != NULL; i++) | |
77 { | |
78 fprintf(stderr, " * %s: %s\n Written by %s.\n %s\n", | |
79 (*i)->extension, (*i)->description, | |
80 (*i)->author, (*i)->url); | |
81 } /* for */ | |
82 } /* else */ | |
83 | |
84 fprintf(stderr, "\n"); | |
85 } /* output_decoders */ | |
86 | |
87 | |
88 static volatile int done_flag = 0; | |
89 | |
90 static void audio_callback(void *userdata, Uint8 *stream, int len) | |
91 { | |
92 static Uint8 overflow[16384]; /* this is a hack. */ | |
93 static Uint32 overflowBytes = 0; | |
94 Sound_Sample *sample = *((Sound_Sample **) userdata); | |
95 Uint32 bw = 0; /* bytes written to stream*/ | |
96 Uint32 rc; /* return code */ | |
97 | |
98 if (overflowBytes > 0) | |
99 { | |
100 bw = (overflowBytes < len) ? overflowBytes : len; | |
101 memcpy(stream, overflow, bw); | |
102 overflowBytes -= bw; | |
103 } /* if */ | |
104 | |
105 while ((bw < len) && (!done_flag)) | |
106 { | |
107 rc = Sound_Decode(sample); | |
108 if (rc > 0) | |
109 { | |
110 if (bw + rc > len) | |
111 { | |
112 overflowBytes = (bw + rc) - len; | |
113 memcpy(overflow, | |
114 ((Uint8 *) sample->buffer) + (rc - overflowBytes), | |
115 overflowBytes); | |
116 rc -= overflowBytes; | |
117 } /* if */ | |
118 | |
119 memcpy(stream + bw, sample->buffer, rc); | |
120 bw += rc; | |
121 } /* if */ | |
122 | |
123 if (sample->flags & SOUND_SAMPLEFLAG_EOF) | |
124 done_flag = 1; | |
125 | |
126 else if (sample->flags & SOUND_SAMPLEFLAG_ERROR) | |
127 { | |
128 fprintf(stderr, "Error condition in decoding!\n" | |
129 " problem: [%s].\n", Sound_GetError()); | |
130 done_flag = 1; | |
131 } /* else if */ | |
132 } /* while */ | |
133 | |
134 assert(bw <= len); | |
135 | |
136 if (bw < len) | |
137 memset(stream + bw, '\0', len - bw); | |
138 } /* audio_callback */ | |
139 | |
140 | |
141 static void output_usage(const char *argv0) | |
142 { | |
143 fprintf(stderr, "USAGE: %s <soundFile1> [soundFile2] ... [soundFileN]\n", | |
144 argv0); | |
145 } /* output_usage */ | |
146 | |
147 | |
148 int main(int argc, char **argv) | |
149 { | |
150 Sound_AudioInfo sound_desired; | |
151 SDL_AudioSpec sdl_desired; | |
152 Sound_Sample *sample; | |
153 int i; | |
154 | |
155 if (argc < 2) | |
156 { | |
157 output_usage(argv[0]); | |
158 return(42); | |
159 } /* if */ | |
160 | |
161 for (i = 0; i < argc; i++) | |
162 { | |
163 if (strncmp(argv[i], "--", 2) != 0) | |
164 continue; | |
165 | |
166 if ( (strcmp(argv[i], "--version") == 0) || | |
167 (strcmp(argv[i], "--help") == 0 ) ) | |
168 { | |
169 output_versions(argv[0]); | |
170 output_usage(argv[0]); | |
171 } /* if */ | |
172 | |
173 else if (strcmp(argv[i], "--decoders") == 0) | |
174 { | |
175 if (!Sound_Init()) | |
176 { | |
177 fprintf(stderr, "Sound_Init() failed!\n" | |
178 " reason: [%s].\n", Sound_GetError()); | |
179 SDL_Quit(); | |
180 return(42); | |
181 } /* if */ | |
182 | |
183 output_decoders(); | |
184 Sound_Quit(); | |
185 return(0); | |
186 } /* else if */ | |
187 | |
188 else | |
189 { | |
190 fprintf(stderr, "unknown option: \"%s\"\n", argv[i]); | |
191 return(42); | |
192 } /* else */ | |
193 } /* for */ | |
194 | |
195 if (SDL_Init(SDL_INIT_AUDIO) == -1) | |
196 { | |
197 fprintf(stderr, "SDL_Init(SDL_INIT_AUDIO) failed!\n" | |
198 " reason: [%s].\n", SDL_GetError()); | |
199 return(42); | |
200 } /* if */ | |
201 | |
202 if (!Sound_Init()) | |
203 { | |
204 fprintf(stderr, "Sound_Init() failed!\n" | |
205 " reason: [%s].\n", Sound_GetError()); | |
206 SDL_Quit(); | |
207 return(42); | |
208 } /* if */ | |
209 | |
210 sound_desired.rate = 44100; | |
211 sound_desired.channels = 2; | |
212 sound_desired.format = AUDIO_S16SYS; | |
213 | |
214 sdl_desired.freq = 44100; | |
215 sdl_desired.format = AUDIO_S16SYS; | |
216 sdl_desired.channels = 2; | |
217 sdl_desired.samples = 4096; | |
218 sdl_desired.callback = audio_callback; | |
219 sdl_desired.userdata = &sample; | |
220 | |
221 if ( SDL_OpenAudio(&sdl_desired, NULL) < 0 ) | |
222 { | |
223 fprintf(stderr, "Couldn't open audio device!\n" | |
224 " reason: [%s].\n", SDL_GetError()); | |
225 Sound_Quit(); | |
226 SDL_Quit(); | |
227 return(42); | |
228 } /* if */ | |
229 | |
230 for (i = 1; i < argc; i++) | |
231 { | |
232 if (strncmp(argv[i], "--", 2) == 0) | |
233 continue; | |
234 | |
235 printf("Now playing [%s]...\n", argv[i]); | |
236 | |
237 sample = Sound_NewSampleFromFile(argv[i], &sound_desired, 4096 * 4); | |
238 if (!sample) | |
239 { | |
240 fprintf(stderr, "Couldn't load \"%s\"!\n" | |
241 " reason: [%s].\n", argv[i], Sound_GetError()); | |
242 continue; | |
243 } /* if */ | |
244 | |
245 done_flag = 0; | |
246 SDL_PauseAudio(0); | |
247 while (!done_flag) | |
248 SDL_Delay(10); | |
249 SDL_PauseAudio(1); | |
250 | |
251 Sound_FreeSample(sample); | |
252 } /* for */ | |
253 | |
254 Sound_Quit(); | |
255 SDL_Quit(); | |
256 return(0); | |
257 } /* main */ | |
258 | |
259 /* end of playsound.c ... */ | |
260 |