]> git.tdb.fi Git - ext/openal.git/blob - utils/openal-info.c
Tweak some types to work around an MSVC compile error
[ext/openal.git] / utils / openal-info.c
1 /*
2  * OpenAL Info Utility
3  *
4  * Copyright (c) 2010 by Chris Robinson <chris.kcat@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "AL/alc.h"
32 #include "AL/al.h"
33 #include "AL/alext.h"
34
35 #include "win_main_utf8.h"
36
37 /* C doesn't allow casting between function and non-function pointer types, so
38  * with C99 we need to use a union to reinterpret the pointer type. Pre-C99
39  * still needs to use a normal cast and live with the warning (C++ is fine with
40  * a regular reinterpret_cast).
41  */
42 #if __STDC_VERSION__ >= 199901L
43 #define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f
44 #else
45 #define FUNCTION_CAST(T, ptr) (T)(ptr)
46 #endif
47
48 #define MAX_WIDTH  80
49
50 static void printList(const char *list, char separator)
51 {
52     size_t col = MAX_WIDTH, len;
53     const char *indent = "    ";
54     const char *next;
55
56     if(!list || *list == '\0')
57     {
58         fprintf(stdout, "\n%s!!! none !!!\n", indent);
59         return;
60     }
61
62     do {
63         next = strchr(list, separator);
64         if(next)
65         {
66             len = (size_t)(next-list);
67             do {
68                 next++;
69             } while(*next == separator);
70         }
71         else
72             len = strlen(list);
73
74         if(len + col + 2 >= MAX_WIDTH)
75         {
76             fprintf(stdout, "\n%s", indent);
77             col = strlen(indent);
78         }
79         else
80         {
81             fputc(' ', stdout);
82             col++;
83         }
84
85         len = fwrite(list, 1, len, stdout);
86         col += len;
87
88         if(!next || *next == '\0')
89             break;
90         fputc(',', stdout);
91         col++;
92
93         list = next;
94     } while(1);
95     fputc('\n', stdout);
96 }
97
98 static void printDeviceList(const char *list)
99 {
100     if(!list || *list == '\0')
101         printf("    !!! none !!!\n");
102     else do {
103         printf("    %s\n", list);
104         list += strlen(list) + 1;
105     } while(*list != '\0');
106 }
107
108
109 static ALenum checkALErrors(int linenum)
110 {
111     ALenum err = alGetError();
112     if(err != AL_NO_ERROR)
113         printf("OpenAL Error: %s (0x%x), @ %d\n", alGetString(err), err, linenum);
114     return err;
115 }
116 #define checkALErrors() checkALErrors(__LINE__)
117
118 static ALCenum checkALCErrors(ALCdevice *device, int linenum)
119 {
120     ALCenum err = alcGetError(device);
121     if(err != ALC_NO_ERROR)
122         printf("ALC Error: %s (0x%x), @ %d\n", alcGetString(device, err), err, linenum);
123     return err;
124 }
125 #define checkALCErrors(x) checkALCErrors((x),__LINE__)
126
127
128 static void printALCInfo(ALCdevice *device)
129 {
130     ALCint major, minor;
131
132     if(device)
133     {
134         const ALCchar *devname = NULL;
135         printf("\n");
136         if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
137             devname = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
138         if(checkALCErrors(device) != ALC_NO_ERROR || !devname)
139             devname = alcGetString(device, ALC_DEVICE_SPECIFIER);
140         printf("** Info for device \"%s\" **\n", devname);
141     }
142     alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &major);
143     alcGetIntegerv(device, ALC_MINOR_VERSION, 1, &minor);
144     if(checkALCErrors(device) == ALC_NO_ERROR)
145         printf("ALC version: %d.%d\n", major, minor);
146     if(device)
147     {
148         printf("ALC extensions:");
149         printList(alcGetString(device, ALC_EXTENSIONS), ' ');
150         checkALCErrors(device);
151     }
152 }
153
154 static void printHRTFInfo(ALCdevice *device)
155 {
156     LPALCGETSTRINGISOFT alcGetStringiSOFT;
157     ALCint num_hrtfs;
158
159     if(alcIsExtensionPresent(device, "ALC_SOFT_HRTF") == ALC_FALSE)
160     {
161         printf("HRTF extension not available\n");
162         return;
163     }
164
165     alcGetStringiSOFT = FUNCTION_CAST(LPALCGETSTRINGISOFT,
166         alcGetProcAddress(device, "alcGetStringiSOFT"));
167
168     alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtfs);
169     if(!num_hrtfs)
170         printf("No HRTFs found\n");
171     else
172     {
173         ALCint i;
174         printf("Available HRTFs:\n");
175         for(i = 0;i < num_hrtfs;++i)
176         {
177             const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i);
178             printf("    %s\n", name);
179         }
180     }
181     checkALCErrors(device);
182 }
183
184 static void printModeInfo(ALCdevice *device)
185 {
186     ALCint srate = 0;
187
188     if(alcIsExtensionPresent(device, "ALC_SOFT_output_mode"))
189     {
190         const char *modename = "(error)";
191         ALCenum mode = 0;
192
193         alcGetIntegerv(device, ALC_OUTPUT_MODE_SOFT, 1, &mode);
194         checkALCErrors(device);
195         switch(mode)
196         {
197         case ALC_ANY_SOFT: modename = "Unknown / unspecified"; break;
198         case ALC_MONO_SOFT: modename = "Mono"; break;
199         case ALC_STEREO_SOFT: modename = "Stereo (unspecified encoding)"; break;
200         case ALC_STEREO_BASIC_SOFT: modename = "Stereo (basic)"; break;
201         case ALC_STEREO_UHJ_SOFT: modename = "Stereo (UHJ)"; break;
202         case ALC_STEREO_HRTF_SOFT: modename = "Stereo (HRTF)"; break;
203         case ALC_QUAD_SOFT: modename = "Quadraphonic"; break;
204         case ALC_SURROUND_5_1_SOFT: modename = "5.1 Surround"; break;
205         case ALC_SURROUND_6_1_SOFT: modename = "6.1 Surround"; break;
206         case ALC_SURROUND_7_1_SOFT: modename = "7.1 Surround"; break;
207         }
208         printf("Device output mode: %s\n", modename);
209     }
210     else
211         printf("Output mode extension not available\n");
212
213     alcGetIntegerv(device, ALC_FREQUENCY, 1, &srate);
214     if(checkALCErrors(device) == ALC_NO_ERROR)
215         printf("Device sample rate: %dhz\n", srate);
216 }
217
218 static void printALInfo(void)
219 {
220     printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR));
221     printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER));
222     printf("OpenAL version string: %s\n", alGetString(AL_VERSION));
223     printf("OpenAL extensions:");
224     printList(alGetString(AL_EXTENSIONS), ' ');
225     checkALErrors();
226 }
227
228 static void printResamplerInfo(void)
229 {
230     LPALGETSTRINGISOFT alGetStringiSOFT;
231     ALint num_resamplers;
232     ALint def_resampler;
233
234     if(!alIsExtensionPresent("AL_SOFT_source_resampler"))
235     {
236         printf("Resampler info not available\n");
237         return;
238     }
239
240     alGetStringiSOFT = FUNCTION_CAST(LPALGETSTRINGISOFT, alGetProcAddress("alGetStringiSOFT"));
241
242     num_resamplers = alGetInteger(AL_NUM_RESAMPLERS_SOFT);
243     def_resampler = alGetInteger(AL_DEFAULT_RESAMPLER_SOFT);
244
245     if(!num_resamplers)
246         printf("!!! No resamplers found !!!\n");
247     else
248     {
249         ALint i;
250         printf("Available resamplers:\n");
251         for(i = 0;i < num_resamplers;++i)
252         {
253             const ALchar *name = alGetStringiSOFT(AL_RESAMPLER_NAME_SOFT, i);
254             printf("    %s%s\n", name, (i==def_resampler)?" *":"");
255         }
256     }
257     checkALErrors();
258 }
259
260 static void printEFXInfo(ALCdevice *device)
261 {
262     static LPALGENFILTERS palGenFilters;
263     static LPALDELETEFILTERS palDeleteFilters;
264     static LPALFILTERI palFilteri;
265     static LPALGENEFFECTS palGenEffects;
266     static LPALDELETEEFFECTS palDeleteEffects;
267     static LPALEFFECTI palEffecti;
268
269     static const ALint filters[] = {
270         AL_FILTER_LOWPASS, AL_FILTER_HIGHPASS, AL_FILTER_BANDPASS,
271         AL_FILTER_NULL
272     };
273     char filterNames[] = "Low-pass,High-pass,Band-pass,";
274     static const ALint effects[] = {
275         AL_EFFECT_EAXREVERB, AL_EFFECT_REVERB, AL_EFFECT_CHORUS,
276         AL_EFFECT_DISTORTION, AL_EFFECT_ECHO, AL_EFFECT_FLANGER,
277         AL_EFFECT_FREQUENCY_SHIFTER, AL_EFFECT_VOCAL_MORPHER,
278         AL_EFFECT_PITCH_SHIFTER, AL_EFFECT_RING_MODULATOR,
279         AL_EFFECT_AUTOWAH, AL_EFFECT_COMPRESSOR, AL_EFFECT_EQUALIZER,
280         AL_EFFECT_NULL
281     };
282     static const ALint dedeffects[] = {
283         AL_EFFECT_DEDICATED_DIALOGUE, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT,
284         AL_EFFECT_NULL
285     };
286     char effectNames[] = "EAX Reverb,Reverb,Chorus,Distortion,Echo,Flanger,"
287         "Frequency Shifter,Vocal Morpher,Pitch Shifter,Ring Modulator,Autowah,"
288         "Compressor,Equalizer,Dedicated Dialog,Dedicated LFE,";
289     ALCint major, minor, sends;
290     ALuint object;
291     char *current;
292     int i;
293
294     if(alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_FALSE)
295     {
296         printf("EFX not available\n");
297         return;
298     }
299
300     palGenFilters = FUNCTION_CAST(LPALGENFILTERS, alGetProcAddress("alGenFilters"));
301     palDeleteFilters = FUNCTION_CAST(LPALDELETEFILTERS, alGetProcAddress("alDeleteFilters"));
302     palFilteri = FUNCTION_CAST(LPALFILTERI, alGetProcAddress("alFilteri"));
303     palGenEffects = FUNCTION_CAST(LPALGENEFFECTS, alGetProcAddress("alGenEffects"));
304     palDeleteEffects = FUNCTION_CAST(LPALDELETEEFFECTS, alGetProcAddress("alDeleteEffects"));
305     palEffecti = FUNCTION_CAST(LPALEFFECTI, alGetProcAddress("alEffecti"));
306
307     alcGetIntegerv(device, ALC_EFX_MAJOR_VERSION, 1, &major);
308     alcGetIntegerv(device, ALC_EFX_MINOR_VERSION, 1, &minor);
309     if(checkALCErrors(device) == ALC_NO_ERROR)
310         printf("EFX version: %d.%d\n", major, minor);
311     alcGetIntegerv(device, ALC_MAX_AUXILIARY_SENDS, 1, &sends);
312     if(checkALCErrors(device) == ALC_NO_ERROR)
313         printf("Max auxiliary sends: %d\n", sends);
314
315     palGenFilters(1, &object);
316     checkALErrors();
317
318     current = filterNames;
319     for(i = 0;filters[i] != AL_FILTER_NULL;i++)
320     {
321         char *next = strchr(current, ',');
322         assert(next != NULL);
323
324         palFilteri(object, AL_FILTER_TYPE, filters[i]);
325         if(alGetError() != AL_NO_ERROR)
326             memmove(current, next+1, strlen(next));
327         else
328             current = next+1;
329     }
330     printf("Supported filters:");
331     printList(filterNames, ',');
332
333     palDeleteFilters(1, &object);
334     palGenEffects(1, &object);
335     checkALErrors();
336
337     current = effectNames;
338     for(i = 0;effects[i] != AL_EFFECT_NULL;i++)
339     {
340         char *next = strchr(current, ',');
341         assert(next != NULL);
342
343         palEffecti(object, AL_EFFECT_TYPE, effects[i]);
344         if(alGetError() != AL_NO_ERROR)
345             memmove(current, next+1, strlen(next));
346         else
347             current = next+1;
348     }
349     if(alcIsExtensionPresent(device, "ALC_EXT_DEDICATED"))
350     {
351         for(i = 0;dedeffects[i] != AL_EFFECT_NULL;i++)
352         {
353             char *next = strchr(current, ',');
354             assert(next != NULL);
355
356             palEffecti(object, AL_EFFECT_TYPE, dedeffects[i]);
357             if(alGetError() != AL_NO_ERROR)
358                 memmove(current, next+1, strlen(next));
359             else
360                 current = next+1;
361         }
362     }
363     else
364     {
365         for(i = 0;dedeffects[i] != AL_EFFECT_NULL;i++)
366         {
367             char *next = strchr(current, ',');
368             assert(next != NULL);
369             memmove(current, next+1, strlen(next));
370         }
371     }
372     printf("Supported effects:");
373     printList(effectNames, ',');
374
375     palDeleteEffects(1, &object);
376     checkALErrors();
377 }
378
379 int main(int argc, char *argv[])
380 {
381     ALCdevice *device;
382     ALCcontext *context;
383
384 #ifdef _WIN32
385     /* OpenAL Soft gives UTF-8 strings, so set the console to expect that. */
386     SetConsoleOutputCP(CP_UTF8);
387 #endif
388
389     if(argc > 1 && (strcmp(argv[1], "--help") == 0 ||
390                     strcmp(argv[1], "-h") == 0))
391     {
392         printf("Usage: %s [playback device]\n", argv[0]);
393         return 0;
394     }
395
396     printf("Available playback devices:\n");
397     if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
398         printDeviceList(alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER));
399     else
400         printDeviceList(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
401     printf("Available capture devices:\n");
402     printDeviceList(alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER));
403
404     if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
405         printf("Default playback device: %s\n",
406                alcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER));
407     else
408         printf("Default playback device: %s\n",
409                alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER));
410     printf("Default capture device: %s\n",
411            alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
412
413     printALCInfo(NULL);
414
415     device = alcOpenDevice((argc>1) ? argv[1] : NULL);
416     if(!device)
417     {
418         printf("\n!!! Failed to open %s !!!\n\n", ((argc>1) ? argv[1] : "default device"));
419         return 1;
420     }
421     printALCInfo(device);
422     printHRTFInfo(device);
423
424     context = alcCreateContext(device, NULL);
425     if(!context || alcMakeContextCurrent(context) == ALC_FALSE)
426     {
427         if(context)
428             alcDestroyContext(context);
429         alcCloseDevice(device);
430         printf("\n!!! Failed to set a context !!!\n\n");
431         return 1;
432     }
433
434     printModeInfo(device);
435     printALInfo();
436     printResamplerInfo();
437     printEFXInfo(device);
438
439     alcMakeContextCurrent(NULL);
440     alcDestroyContext(context);
441     alcCloseDevice(device);
442
443     return 0;
444 }