Mercurial > SDL_sound_CoreAudio
annotate playsound/test_sdlsound.c @ 29:0e27c909c952 release-0.1.1
Upped version to 0.1.1
author | Ryan C. Gordon <icculus@icculus.org> |
---|---|
date | Wed, 19 Sep 2001 14:34:52 +0000 |
parents | 4acb5260d684 |
children | 3d34b03167f2 |
rev | line source |
---|---|
4 | 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 <assert.h> | |
30 #include "SDL.h" | |
31 #include "SDL_sound.h" | |
32 | |
33 #define TEST_VERSION_MAJOR 0 | |
34 #define TEST_VERSION_MINOR 1 | |
35 #define TEST_VERSION_PATCH 0 | |
36 | |
37 | |
38 static void output_versions(void) | |
39 { | |
40 Sound_Version compiled; | |
41 Sound_Version linked; | |
42 SDL_version sdl_compiled; | |
43 const SDL_version *sdl_linked; | |
44 | |
45 SOUND_VERSION(&compiled); | |
46 Sound_GetLinkedVersion(&linked); | |
47 SDL_VERSION(&sdl_compiled); | |
48 sdl_linked = SDL_Linked_Version(); | |
49 | |
50 printf("test_sdlsound version %d.%d.%d.\n" | |
51 " Compiled against SDL_sound version %d.%d.%d,\n" | |
52 " and linked against %d.%d.%d.\n" | |
53 " Compiled against SDL version %d.%d.%d,\n" | |
54 " and linked against %d.%d.%d.\n\n", | |
55 TEST_VERSION_MAJOR, TEST_VERSION_MINOR, TEST_VERSION_PATCH, | |
56 compiled.major, compiled.minor, compiled.patch, | |
57 linked.major, linked.minor, linked.patch, | |
58 sdl_compiled.major, sdl_compiled.minor, sdl_compiled.patch, | |
59 sdl_linked->major, sdl_linked->minor, sdl_linked->patch); | |
60 } /* output_versions */ | |
61 | |
62 | |
63 static void output_decoders(void) | |
64 { | |
65 const Sound_DecoderInfo **rc = Sound_AvailableDecoders(); | |
66 const Sound_DecoderInfo **i; | |
67 | |
13 | 68 printf("Supported sound formats:\n"); |
4 | 69 if (*rc == NULL) |
70 printf(" * Apparently, NONE!\n"); | |
71 else | |
72 { | |
73 for (i = rc; *i != NULL; i++) | |
74 { | |
75 printf(" * %s: %s\n Written by %s.\n %s\n", | |
76 (*i)->extension, (*i)->description, | |
77 (*i)->author, (*i)->url); | |
78 } /* for */ | |
79 } /* else */ | |
80 | |
81 printf("\n"); | |
82 } /* output_decoders */ | |
83 | |
84 | |
85 static volatile int done_flag = 0; | |
86 | |
87 void test_callback(void *userdata, Uint8 *stream, int len) | |
88 { | |
25
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
89 static Uint8 overflow[16384]; /* this is a hack. */ |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
90 static Uint32 overflowBytes = 0; |
4 | 91 Sound_Sample *sample = (Sound_Sample *) userdata; |
25
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
92 Uint32 bw = 0; /* bytes written to stream*/ |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
93 Uint32 rc; /* return code */ |
4 | 94 |
25
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
95 if (overflowBytes > 0) |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
96 { |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
97 memcpy(stream, overflow, overflowBytes); |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
98 bw = overflowBytes; |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
99 overflowBytes = 0; |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
100 } /* if */ |
4 | 101 |
25
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
102 while ((bw < len) && (!done_flag)) |
4 | 103 { |
25
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
104 rc = Sound_Decode(sample); |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
105 if (rc > 0) |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
106 { |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
107 if (bw + rc > len) |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
108 { |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
109 overflowBytes = (bw + rc) - len; |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
110 memcpy(overflow, |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
111 ((Uint8 *) sample->buffer) + (rc - overflowBytes), |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
112 overflowBytes); |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
113 rc -= overflowBytes; |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
114 } /* if */ |
4 | 115 |
25
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
116 memcpy(stream + bw, sample->buffer, rc); |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
117 bw += rc; |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
118 } /* if */ |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
119 |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
120 if (sample->flags & SOUND_SAMPLEFLAG_EOF) |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
121 done_flag = 1; |
4 | 122 |
25
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
123 else if (sample->flags & SOUND_SAMPLEFLAG_ERROR) |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
124 { |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
125 printf("Error condition in decoding!\n"); |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
126 done_flag = 1; |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
127 } /* else if */ |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
128 } /* while */ |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
129 |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
130 assert(bw <= len); |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
131 |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
132 if (bw < len) |
4acb5260d684
Made the SDL audio callback much more robust.
Ryan C. Gordon <icculus@icculus.org>
parents:
13
diff
changeset
|
133 memset(stream + bw, '\0', len - bw); |
4 | 134 } /* test_callback */ |
135 | |
136 | |
137 int main(int argc, char **argv) | |
138 { | |
139 Sound_AudioInfo sound_desired; | |
140 SDL_AudioSpec sdl_desired; | |
141 Sound_Sample *sample; | |
142 | |
143 if (SDL_Init(SDL_INIT_AUDIO) == -1) | |
144 { | |
145 printf("SDL_Init(SDL_INIT_AUDIO) failed!\n" | |
146 " reason: [%s].\n", SDL_GetError()); | |
147 return(42); | |
148 } /* if */ | |
149 | |
150 if (!Sound_Init()) | |
151 { | |
152 printf("Sound_Init() failed!\n" | |
153 " reason: [%s].\n", Sound_GetError()); | |
154 SDL_Quit(); | |
155 return(42); | |
156 } /* if */ | |
157 | |
158 output_versions(); | |
159 output_decoders(); | |
160 | |
161 if (argc != 2) | |
162 { | |
163 printf("USAGE: %s <oneSupportedFile>\n", argv[0]); | |
164 Sound_Quit(); | |
165 SDL_Quit(); | |
166 return(42); | |
167 } /* if */ | |
168 | |
169 sound_desired.rate = 44100; | |
170 sound_desired.channels = 2; | |
171 sound_desired.format = AUDIO_S16; | |
172 | |
173 sample = Sound_NewSampleFromFile(argv[1], &sound_desired, 4096 * 4); | |
174 if (!sample) | |
175 { | |
176 printf("Sound_NewSampleFromFile(\"%s\") failed!\n" | |
177 " reason: [%s].\n", argv[1], Sound_GetError()); | |
178 Sound_Quit(); | |
179 SDL_Quit(); | |
180 return(42); | |
181 } /* if */ | |
182 | |
183 sdl_desired.freq = 44100; | |
184 sdl_desired.format = AUDIO_S16; | |
185 sdl_desired.channels = 2; | |
186 sdl_desired.samples = 4096; | |
187 sdl_desired.callback = test_callback; | |
188 sdl_desired.userdata = sample; | |
189 | |
13 | 190 #if 1 |
4 | 191 if ( SDL_OpenAudio(&sdl_desired, NULL) < 0 ) |
192 { | |
193 printf("SDL_OpenAudio() failed!\n" | |
194 " reason: [%s].\n", SDL_GetError()); | |
195 Sound_Quit(); | |
196 SDL_Quit(); | |
197 return(42); | |
198 } /* if */ | |
199 | |
200 SDL_PauseAudio(0); | |
201 while (!done_flag) | |
202 SDL_Delay(10); | |
13 | 203 SDL_PauseAudio(1); |
204 | |
205 /* | |
206 * This just decodes the file, and dumps the decoded waveform to | |
207 * stderr. Use with caution. | |
208 */ | |
209 #else | |
210 { | |
211 Uint32 rc; | |
212 while ((rc = Sound_Decode(sample)) == sample->buffer_size) | |
213 fwrite(sample->buffer, rc, 1, stderr); | |
214 } | |
215 #endif | |
216 | |
4 | 217 |
218 Sound_FreeSample(sample); | |
219 | |
220 Sound_Quit(); | |
221 SDL_Quit(); | |
222 return(0); | |
223 } /* main */ | |
224 | |
225 /* end of test_sdlsound.c ... */ | |
226 |