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