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 {
|
|
89 Sound_Sample *sample = (Sound_Sample *) userdata;
|
|
90 Uint32 rc = Sound_Decode(sample);
|
|
91
|
|
92 if (sample->flags & SOUND_SAMPLEFLAG_EOF)
|
|
93 done_flag = 1;
|
|
94
|
|
95 else if (sample->flags & SOUND_SAMPLEFLAG_ERROR)
|
|
96 {
|
13
|
97 printf("Error condition in decoding!\n");
|
4
|
98 done_flag = 1;
|
|
99 } /* else if */
|
|
100
|
|
101 assert(rc <= len);
|
|
102
|
|
103 memcpy(stream, sample->buffer, rc);
|
|
104 if (rc < len)
|
|
105 memset(stream + rc, '\0', len - rc); /* (*shrug*) */
|
|
106 } /* test_callback */
|
|
107
|
|
108
|
|
109 int main(int argc, char **argv)
|
|
110 {
|
|
111 Sound_AudioInfo sound_desired;
|
|
112 SDL_AudioSpec sdl_desired;
|
|
113 Sound_Sample *sample;
|
|
114
|
|
115 if (SDL_Init(SDL_INIT_AUDIO) == -1)
|
|
116 {
|
|
117 printf("SDL_Init(SDL_INIT_AUDIO) failed!\n"
|
|
118 " reason: [%s].\n", SDL_GetError());
|
|
119 return(42);
|
|
120 } /* if */
|
|
121
|
|
122 if (!Sound_Init())
|
|
123 {
|
|
124 printf("Sound_Init() failed!\n"
|
|
125 " reason: [%s].\n", Sound_GetError());
|
|
126 SDL_Quit();
|
|
127 return(42);
|
|
128 } /* if */
|
|
129
|
|
130 output_versions();
|
|
131 output_decoders();
|
|
132
|
|
133 if (argc != 2)
|
|
134 {
|
|
135 printf("USAGE: %s <oneSupportedFile>\n", argv[0]);
|
|
136 Sound_Quit();
|
|
137 SDL_Quit();
|
|
138 return(42);
|
|
139 } /* if */
|
|
140
|
|
141 sound_desired.rate = 44100;
|
|
142 sound_desired.channels = 2;
|
|
143 sound_desired.format = AUDIO_S16;
|
|
144
|
|
145 sample = Sound_NewSampleFromFile(argv[1], &sound_desired, 4096 * 4);
|
|
146 if (!sample)
|
|
147 {
|
|
148 printf("Sound_NewSampleFromFile(\"%s\") failed!\n"
|
|
149 " reason: [%s].\n", argv[1], Sound_GetError());
|
|
150 Sound_Quit();
|
|
151 SDL_Quit();
|
|
152 return(42);
|
|
153 } /* if */
|
|
154
|
|
155 sdl_desired.freq = 44100;
|
|
156 sdl_desired.format = AUDIO_S16;
|
|
157 sdl_desired.channels = 2;
|
|
158 sdl_desired.samples = 4096;
|
|
159 sdl_desired.callback = test_callback;
|
|
160 sdl_desired.userdata = sample;
|
|
161
|
13
|
162 #if 1
|
4
|
163 if ( SDL_OpenAudio(&sdl_desired, NULL) < 0 )
|
|
164 {
|
|
165 printf("SDL_OpenAudio() failed!\n"
|
|
166 " reason: [%s].\n", SDL_GetError());
|
|
167 Sound_Quit();
|
|
168 SDL_Quit();
|
|
169 return(42);
|
|
170 } /* if */
|
|
171
|
|
172 SDL_PauseAudio(0);
|
|
173 while (!done_flag)
|
|
174 SDL_Delay(10);
|
13
|
175 SDL_PauseAudio(1);
|
|
176
|
|
177 /*
|
|
178 * This just decodes the file, and dumps the decoded waveform to
|
|
179 * stderr. Use with caution.
|
|
180 */
|
|
181 #else
|
|
182 {
|
|
183 Uint32 rc;
|
|
184 while ((rc = Sound_Decode(sample)) == sample->buffer_size)
|
|
185 fwrite(sample->buffer, rc, 1, stderr);
|
|
186 }
|
|
187 #endif
|
|
188
|
4
|
189
|
|
190 Sound_FreeSample(sample);
|
|
191
|
|
192 Sound_Quit();
|
|
193 SDL_Quit();
|
|
194 return(0);
|
|
195 } /* main */
|
|
196
|
|
197 /* end of test_sdlsound.c ... */
|
|
198
|