]> git.mxchange.org Git - flightgear.git/blob - tests/alcinfo.cxx
85d2a27ecdcac1f6eb15c720b5800780d55de86e
[flightgear.git] / tests / alcinfo.cxx
1 /* -*- mode: C; tab-width:8; c-basic-offset:8 -*-
2  * vi:set ts=8:
3  *
4  * alcinfo.x
5  *
6  * alcinfo display info about a ALC extension and OpenAL renderer
7  *
8  * This file is in the Public Domain and comes with no warranty.
9  * Erik Hofman <erik@ehofman.com>
10  *
11  */
12
13 #if HAVE_CONFIG_H
14 # include "config.h"
15 #endif
16
17 #ifdef __APPLE__
18 # include <OpenAL/al.h>
19 # include <OpenAL/alc.h>
20 #else
21 # include <AL/al.h>
22 # include <AL/alc.h>
23 # include <AL/alext.h>
24 #endif
25
26 #ifndef AL_VERSION_1_1
27 # ifdef __APPLE__
28 #  include <OpenAL/altypes.h>
29 #  include <OpenAL/alctypes.h>
30 #else
31 #  include <AL/altypes.h>
32 #  include <AL/alctypes.h>
33 # endif
34 #endif
35
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdio.h>
39 #include <string>
40
41 using std::string;
42
43 #ifndef ALC_ALL_DEVICES_SPECIFIER
44 # define ALC_ALL_DEVICES_SPECIFIER      0x1013
45 #endif
46
47 #define MAX_DATA        16
48 static const int indentation = 4;
49 static const int maxmimumWidth = 79;
50
51 void printExtensions (const char *, char, const char *);
52 void displayDevices(const char *, const char *);
53 char *getDeviceName(int, char **);
54 void testForError(void *, const string&);
55 void testForALCError(ALCdevice *);
56
57 int main(int argc, char **argv)
58 {
59    ALCint data[MAX_DATA];
60    ALCdevice *device = NULL;
61    ALCcontext *context = NULL;
62    ALenum error;
63    char *s;
64
65    if (alcIsExtensionPresent(NULL, "ALC_enumeration_EXT") == AL_TRUE)
66    {
67       if (alcIsExtensionPresent(NULL, "ALC_enumerate_all_EXT") == AL_FALSE)
68          s = (char *)alcGetString(NULL, ALC_DEVICE_SPECIFIER);
69       else
70          s = (char *)alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
71       displayDevices("output", s);
72
73       s = (char *)alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
74       displayDevices("input", s);
75    }
76
77    s = getDeviceName(argc, argv);
78    device = alcOpenDevice(s);
79    testForError(device, "Audio device not available.");
80
81    context = alcCreateContext(device, NULL);
82    testForError(context, "Unable to create a valid context.");
83
84    alcMakeContextCurrent(context);
85    testForALCError(device);
86
87    s = (char *)alcGetString(device, ALC_DEFAULT_DEVICE_SPECIFIER);
88    printf("default output device: %s\n", s);
89    testForALCError(device);
90
91    error = alcIsExtensionPresent(device, "ALC_EXT_capture");
92    if (error)
93    {
94       s = (char *)alcGetString(device, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER);
95       printf("default input device:  %s\n", s);
96       testForALCError(device);
97    }
98    printf("capture support: %s\n", (error) ? "yes" : "no");
99
100    alcGetIntegerv(device, ALC_FREQUENCY, 1, data);
101    printf("mixer frequency: %u hz\n", data[0]);
102    testForALCError(device);
103
104    alcGetIntegerv(device, ALC_REFRESH, 1, data+1);
105    printf("refresh rate : %u hz\n", data[0]/data[1]);
106    testForALCError(device);
107
108    data[0] = 0;
109    alcGetIntegerv(device, ALC_MONO_SOURCES, 1, data);
110    error = alcGetError(device);
111    if (error == AL_NONE) {
112       printf("supported sources; mono: %u, ", data[0]);
113
114       data[0] = 0;
115       alcGetIntegerv(device, ALC_STEREO_SOURCES, 1, data);
116       printf("stereo: %u\n", data[0]);
117       testForALCError(device);
118    }
119
120    printf("ALC version: ");
121    alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, data);
122    printf("%i.", *data);
123    alcGetIntegerv(device, ALC_MINOR_VERSION, 1, data);
124    printf("%i\n", *data);
125    testForALCError(device);
126
127    s = (char *)alcGetString(device, ALC_EXTENSIONS);
128    printExtensions ("ALC extensions", ' ', s);
129    testForALCError(device);
130
131    s = (char *)alGetString(AL_VENDOR);
132    error = alGetError();
133    if ((error = alGetError()) != AL_NO_ERROR)
134       printf("Error #%x: %s\n", error, alGetString(error));
135    else
136       printf("OpenAL vendor string: %s\n", s);
137
138    s = (char *)alGetString(AL_RENDERER);
139    if ((error = alGetError()) != AL_NO_ERROR)
140       printf("Error #%x: %s\n", error, alGetString(error));
141    else
142       printf("OpenAL renderer string: %s\n", s);
143
144    s = (char *)alGetString(AL_VERSION);
145    if ((error = alGetError()) != AL_NO_ERROR)
146       printf("Error #%x: %s\n", error, alGetString(error));
147    else if (!s)
148       printf("Querying AL_VERSION returned NULL pointer!\n");
149    else
150       printf("OpenAL version string: %s\n", s);
151
152    s = (char *)alGetString(AL_EXTENSIONS);
153    printExtensions ("OpenAL extensions", ' ', s);
154    testForALCError(device);
155
156 /* alut testing mechanism */
157    context = alcGetCurrentContext();
158    if (context == NULL)
159    {
160       printf("Error: no current context\n");
161    }
162    else
163    {
164       if (alGetError () != AL_NO_ERROR)
165       {
166          printf("Alert: AL error on entry\n");
167       }
168       else
169       {
170          if (alcGetError (alcGetContextsDevice (context)) != ALC_NO_ERROR)
171          {
172             printf("Alert: ALC error on entry\n");
173          }
174       }
175    }
176 /* end of alut test */
177  
178    
179    if (alcMakeContextCurrent(NULL) == 0)
180       printf("alcMakeContextCurrent failed.\n");
181
182    device = alcGetContextsDevice(context);
183    alcDestroyContext(context);
184    testForALCError(device);
185
186    if (alcCloseDevice(device) == 0)
187       printf("alcCloseDevice failed.\n");
188
189    return 0;
190 }
191
192 /* -------------------------------------------------------------------------- */
193
194 void
195 printChar (int c, int *width)
196 {
197   putchar (c);
198   *width = (c == '\n') ? 0 : (*width + 1);
199 }
200
201 void
202 indent (int *width)
203 {
204   int i;
205   for (i = 0; i < indentation; i++)
206     {
207       printChar (' ', width);
208     }
209 }
210
211 void
212 printExtensions (const char *header, char separator, const char *extensions)
213 {
214   int width = 0, start = 0, end = 0;
215
216   printf ("%s:\n", header);
217   if (extensions == NULL || extensions[0] == '\0')
218     {
219       return;
220     }
221
222   indent (&width);
223   while (1)
224     {
225       if (extensions[end] == separator || extensions[end] == '\0')
226         {
227           if (width + end - start + 2 > maxmimumWidth)
228             {
229               printChar ('\n', &width);
230               indent (&width);
231             }
232           while (start < end)
233             {
234               printChar (extensions[start], &width);
235               start++;
236             }
237           if (extensions[end] == '\0')
238             {
239               break;
240             }
241           start++;
242           end++;
243           if (extensions[end] == '\0')
244             {
245               break;
246             }
247           printChar (',', &width);
248           printChar (' ', &width);
249         }
250       end++;
251     }
252   printChar ('\n', &width);
253 }
254
255 char *
256 getCommandLineOption(int argc, char **argv, const string& option)
257 {
258    int slen = option.size();
259    char *rv = 0;
260    int i;
261
262    for (i=0; i<argc; i++)
263    {
264       if (strncmp(argv[i], option.c_str(), slen) == 0)
265       {
266          i++;
267          if (i<argc) rv = argv[i];
268       }
269    }
270
271    return rv;
272 }
273
274 char *
275 getDeviceName(int argc, char **argv)
276 {
277    static char devname[255];
278    int len = 255;
279    char *s;
280
281    s = getCommandLineOption(argc, argv, "-d");
282    if (s)
283    {
284       strncpy((char *)&devname, s, len);
285       len -= strlen(s);
286
287       s = getCommandLineOption(argc, argv, "-r");
288       if (s)
289       {
290          strncat((char *)&devname, " on ", len);
291          len -= 4;
292
293          strncat((char *)&devname, s, len);
294       }
295       s = (char *)&devname;
296    }
297
298    return s;
299 }
300
301 void
302 displayDevices(const char *type, const char *list)
303 {
304    ALCchar *ptr, *nptr;
305
306    ptr = (ALCchar *)list;
307    printf("list of all available %s devices:\n", type);
308    if (!list)
309    {
310       printf("none\n");
311    }
312    else
313    {
314       nptr = ptr;
315       while (*(nptr += strlen(ptr)+1) != 0)
316       {
317          printf("  %s\n", ptr);
318          ptr = nptr;
319       }
320       printf("  %s\n", ptr);
321    }
322
323
324 void
325 testForError(void *p, const string& s)
326 {
327    if (p == NULL)
328    {
329       printf("\nError: %s\n\n", s.c_str());
330       exit(-1);
331    }
332 }
333
334 void
335 testForALCError(ALCdevice *device)
336 {
337    ALenum error;
338    error = alcGetError(device);
339    if (error != ALC_NO_ERROR)
340       printf("\nALC Error %x occurred: %s\n", error, alcGetString(device, error));
341 }