Use a smart pointer to auto-free some WinAPI memory
[openal-soft.git] / utils / openal-info.c
blobc37e85c7799308b9dccf1c20d271ac67647e3fe6
1 /*
2 * OpenAL Info Utility
4 * Copyright (c) 2010 by Chris Robinson <chris.kcat@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
31 #include "AL/alc.h"
32 #include "AL/al.h"
33 #include "AL/alext.h"
35 #include "win_main_utf8.h"
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).
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
48 enum { MaxWidth = 80 };
50 static void printList(const char *list, char separator)
52 size_t col = MaxWidth, len;
53 const char *indent = " ";
54 const char *next;
56 if(!list || *list == '\0')
58 printf("\n%s!!! none !!!\n", indent);
59 return;
62 do {
63 next = strchr(list, separator);
64 if(next)
66 len = (size_t)(next-list);
67 do {
68 next++;
69 } while(*next == separator);
71 else
72 len = strlen(list);
74 if(len + col + 2 >= MaxWidth)
76 printf("\n%s", indent);
77 col = strlen(indent);
79 else
81 fputc(' ', stdout);
82 col++;
85 len = fwrite(list, 1, len, stdout);
86 col += len;
88 if(!next || *next == '\0')
89 break;
90 fputc(',', stdout);
91 col++;
93 list = next;
94 } while(1);
95 fputc('\n', stdout);
98 static void printDeviceList(const char *list)
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');
109 static ALenum checkALErrors(int linenum)
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;
116 #define checkALErrors() checkALErrors(__LINE__)
118 static ALCenum checkALCErrors(ALCdevice *device, int linenum)
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;
125 #define checkALCErrors(x) checkALCErrors((x),__LINE__)
128 static void printALCInfo(ALCdevice *device)
130 ALCint major, minor;
132 if(device)
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);
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)
148 printf("ALC extensions:");
149 printList(alcGetString(device, ALC_EXTENSIONS), ' ');
150 checkALCErrors(device);
154 static void printHRTFInfo(ALCdevice *device)
156 LPALCGETSTRINGISOFT alcGetStringiSOFT;
157 ALCint num_hrtfs;
159 if(alcIsExtensionPresent(device, "ALC_SOFT_HRTF") == ALC_FALSE)
161 printf("HRTF extension not available\n");
162 return;
165 alcGetStringiSOFT = FUNCTION_CAST(LPALCGETSTRINGISOFT,
166 alcGetProcAddress(device, "alcGetStringiSOFT"));
168 alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtfs);
169 if(!num_hrtfs)
170 printf("No HRTF profiles found\n");
171 else
173 ALCint i;
174 printf("Available HRTF profiles:\n");
175 for(i = 0;i < num_hrtfs;++i)
177 const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i);
178 printf(" %s\n", name);
181 checkALCErrors(device);
184 static void printALCIntegerValue(ALCdevice *device, ALCenum enumValue, char* enumName)
186 ALCint value;
187 alcGetIntegerv(device, enumValue, 1, &value);
188 if (checkALCErrors(device) == ALC_NO_ERROR)
189 printf("%s: %d\n", enumName, value);
192 static void printModeInfo(ALCdevice *device)
194 ALCint srate = 0;
196 if(alcIsExtensionPresent(device, "ALC_SOFT_output_mode"))
198 const char *modename = "(error)";
199 ALCenum mode = 0;
201 alcGetIntegerv(device, ALC_OUTPUT_MODE_SOFT, 1, &mode);
202 checkALCErrors(device);
203 switch(mode)
205 case ALC_ANY_SOFT: modename = "Unknown / unspecified"; break;
206 case ALC_MONO_SOFT: modename = "Mono"; break;
207 case ALC_STEREO_SOFT: modename = "Stereo (unspecified encoding)"; break;
208 case ALC_STEREO_BASIC_SOFT: modename = "Stereo (basic)"; break;
209 case ALC_STEREO_UHJ_SOFT: modename = "Stereo (UHJ)"; break;
210 case ALC_STEREO_HRTF_SOFT: modename = "Stereo (HRTF)"; break;
211 case ALC_QUAD_SOFT: modename = "Quadraphonic"; break;
212 case ALC_SURROUND_5_1_SOFT: modename = "5.1 Surround"; break;
213 case ALC_SURROUND_6_1_SOFT: modename = "6.1 Surround"; break;
214 case ALC_SURROUND_7_1_SOFT: modename = "7.1 Surround"; break;
216 printf("Device output mode: %s\n", modename);
218 else
219 printf("Output mode extension not available\n");
221 alcGetIntegerv(device, ALC_FREQUENCY, 1, &srate);
222 if(checkALCErrors(device) == ALC_NO_ERROR)
223 printf("Device sample rate: %dhz\n", srate);
225 if(alcIsExtensionPresent(device, "ALC_SOFT_HRTF"))
227 const ALCchar *hrtfname = "(disabled)";
228 ALCint isenabled = 0;
230 alcGetIntegerv(device, ALC_HRTF_SOFT, 1, &isenabled);
231 checkALCErrors(device);
232 if(isenabled == ALC_TRUE)
234 hrtfname = alcGetString(device, ALC_HRTF_SPECIFIER_SOFT);
235 checkALCErrors(device);
237 printf("Device HRTF profile: %s\n", hrtfname ? hrtfname : "<null>");
240 printALCIntegerValue(device, ALC_MONO_SOURCES, "Device number of mono sources");
241 printALCIntegerValue(device, ALC_STEREO_SOURCES, "Device number of stereo sources");
244 static void printALCSOFTSystemEventIsSupportedResult(LPALCEVENTISSUPPORTEDSOFT alcEventIsSupportedSOFT, ALCenum eventType, ALCenum deviceType)
246 if (alcEventIsSupportedSOFT == NULL)
248 printf("ERROR (alcEventIsSupportedSOFT missing)\n");
249 return;
251 ALCenum supported = alcEventIsSupportedSOFT(eventType, deviceType);
252 if (supported == ALC_EVENT_SUPPORTED_SOFT)
254 printf("SUPPORTED\n");
256 else if (supported == ALC_EVENT_NOT_SUPPORTED_SOFT)
258 printf("NOT SUPPORTED\n");
260 else
262 printf("UNEXPECTED VALUE : %d\n", supported);
266 static void printALC_SOFT_system_event(void)
268 if(alcIsExtensionPresent(NULL, "ALC_SOFT_system_events"))
270 LPALCEVENTISSUPPORTEDSOFT alcEventIsSupportedSOFT;
271 alcEventIsSupportedSOFT = FUNCTION_CAST(LPALCEVENTISSUPPORTEDSOFT, alGetProcAddress("alcEventIsSupportedSOFT"));
272 printf("Available events:\n");
273 printf(" ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT for ALC_PLAYBACK_DEVICE_SOFT - ");
274 printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT, ALC_PLAYBACK_DEVICE_SOFT);
275 printf(" ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT for ALC_CAPTURE_DEVICE_SOFT - ");
276 printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT, ALC_CAPTURE_DEVICE_SOFT);
277 printf(" ALC_EVENT_TYPE_DEVICE_ADDED_SOFT for ALC_PLAYBACK_DEVICE_SOFT - ");
278 printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_ADDED_SOFT, ALC_PLAYBACK_DEVICE_SOFT);
279 printf(" ALC_EVENT_TYPE_DEVICE_ADDED_SOFT for ALC_CAPTURE_DEVICE_SOFT - ");
280 printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_ADDED_SOFT, ALC_CAPTURE_DEVICE_SOFT);
281 printf(" ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT for ALC_PLAYBACK_DEVICE_SOFT - ");
282 printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT, ALC_PLAYBACK_DEVICE_SOFT);
283 printf(" ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT for ALC_CAPTURE_DEVICE_SOFT - ");
284 printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT, ALC_CAPTURE_DEVICE_SOFT);
288 static void printALInfo(void)
290 printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR));
291 printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER));
292 printf("OpenAL version string: %s\n", alGetString(AL_VERSION));
293 printf("OpenAL extensions:");
294 printList(alGetString(AL_EXTENSIONS), ' ');
295 checkALErrors();
298 static void printResamplerInfo(void)
300 LPALGETSTRINGISOFT alGetStringiSOFT;
301 ALint num_resamplers;
302 ALint def_resampler;
304 if(!alIsExtensionPresent("AL_SOFT_source_resampler"))
306 printf("Resampler info not available\n");
307 return;
310 alGetStringiSOFT = FUNCTION_CAST(LPALGETSTRINGISOFT, alGetProcAddress("alGetStringiSOFT"));
312 num_resamplers = alGetInteger(AL_NUM_RESAMPLERS_SOFT);
313 def_resampler = alGetInteger(AL_DEFAULT_RESAMPLER_SOFT);
315 if(!num_resamplers)
316 printf("!!! No resamplers found !!!\n");
317 else
319 ALint i;
320 printf("Available resamplers:\n");
321 for(i = 0;i < num_resamplers;++i)
323 const ALchar *name = alGetStringiSOFT(AL_RESAMPLER_NAME_SOFT, i);
324 printf(" %s%s\n", name, (i==def_resampler)?" *":"");
327 checkALErrors();
330 static void printEFXInfo(ALCdevice *device)
332 static LPALGENFILTERS palGenFilters;
333 static LPALDELETEFILTERS palDeleteFilters;
334 static LPALFILTERI palFilteri;
335 static LPALGENEFFECTS palGenEffects;
336 static LPALDELETEEFFECTS palDeleteEffects;
337 static LPALEFFECTI palEffecti;
339 static const ALint filters[] = {
340 AL_FILTER_LOWPASS, AL_FILTER_HIGHPASS, AL_FILTER_BANDPASS,
341 AL_FILTER_NULL
343 char filterNames[] = "Low-pass,High-pass,Band-pass,";
344 static const ALint effects[] = {
345 AL_EFFECT_EAXREVERB, AL_EFFECT_REVERB, AL_EFFECT_CHORUS,
346 AL_EFFECT_DISTORTION, AL_EFFECT_ECHO, AL_EFFECT_FLANGER,
347 AL_EFFECT_FREQUENCY_SHIFTER, AL_EFFECT_VOCAL_MORPHER,
348 AL_EFFECT_PITCH_SHIFTER, AL_EFFECT_RING_MODULATOR,
349 AL_EFFECT_AUTOWAH, AL_EFFECT_COMPRESSOR, AL_EFFECT_EQUALIZER,
350 AL_EFFECT_NULL
352 static const ALint dedeffects[] = {
353 AL_EFFECT_DEDICATED_DIALOGUE, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT,
354 AL_EFFECT_NULL
356 char effectNames[] = "EAX Reverb,Reverb,Chorus,Distortion,Echo,Flanger,"
357 "Frequency Shifter,Vocal Morpher,Pitch Shifter,Ring Modulator,Autowah,"
358 "Compressor,Equalizer,Dedicated Dialog,Dedicated LFE,";
359 ALCint major, minor, sends;
360 ALuint object;
361 char *current;
362 int i;
364 if(alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_FALSE)
366 printf("EFX not available\n");
367 return;
370 palGenFilters = FUNCTION_CAST(LPALGENFILTERS, alGetProcAddress("alGenFilters"));
371 palDeleteFilters = FUNCTION_CAST(LPALDELETEFILTERS, alGetProcAddress("alDeleteFilters"));
372 palFilteri = FUNCTION_CAST(LPALFILTERI, alGetProcAddress("alFilteri"));
373 palGenEffects = FUNCTION_CAST(LPALGENEFFECTS, alGetProcAddress("alGenEffects"));
374 palDeleteEffects = FUNCTION_CAST(LPALDELETEEFFECTS, alGetProcAddress("alDeleteEffects"));
375 palEffecti = FUNCTION_CAST(LPALEFFECTI, alGetProcAddress("alEffecti"));
377 alcGetIntegerv(device, ALC_EFX_MAJOR_VERSION, 1, &major);
378 alcGetIntegerv(device, ALC_EFX_MINOR_VERSION, 1, &minor);
379 if(checkALCErrors(device) == ALC_NO_ERROR)
380 printf("EFX version: %d.%d\n", major, minor);
381 alcGetIntegerv(device, ALC_MAX_AUXILIARY_SENDS, 1, &sends);
382 if(checkALCErrors(device) == ALC_NO_ERROR)
383 printf("Max auxiliary sends: %d\n", sends);
385 palGenFilters(1, &object);
386 checkALErrors();
388 current = filterNames;
389 for(i = 0;filters[i] != AL_FILTER_NULL;i++)
391 char *next = strchr(current, ',');
392 assert(next != NULL);
394 palFilteri(object, AL_FILTER_TYPE, filters[i]);
395 if(alGetError() != AL_NO_ERROR)
396 memmove(current, next+1, strlen(next)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
397 else
398 current = next+1;
400 printf("Supported filters:");
401 printList(filterNames, ',');
403 palDeleteFilters(1, &object);
404 palGenEffects(1, &object);
405 checkALErrors();
407 current = effectNames;
408 for(i = 0;effects[i] != AL_EFFECT_NULL;i++)
410 char *next = strchr(current, ',');
411 assert(next != NULL);
413 palEffecti(object, AL_EFFECT_TYPE, effects[i]);
414 if(alGetError() != AL_NO_ERROR)
415 memmove(current, next+1, strlen(next)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
416 else
417 current = next+1;
419 if(alcIsExtensionPresent(device, "ALC_EXT_DEDICATED"))
421 for(i = 0;dedeffects[i] != AL_EFFECT_NULL;i++)
423 char *next = strchr(current, ',');
424 assert(next != NULL);
426 palEffecti(object, AL_EFFECT_TYPE, dedeffects[i]);
427 if(alGetError() != AL_NO_ERROR)
428 memmove(current, next+1, strlen(next)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
429 else
430 current = next+1;
433 else
435 for(i = 0;dedeffects[i] != AL_EFFECT_NULL;i++)
437 char *next = strchr(current, ',');
438 assert(next != NULL);
439 memmove(current, next+1, strlen(next)); /* NOLINT(clang-analyzer-security.insecureAPI.*) */
442 printf("Supported effects:");
443 printList(effectNames, ',');
445 palDeleteEffects(1, &object);
446 checkALErrors();
449 int main(int argc, char *argv[])
451 ALCdevice *device;
452 ALCcontext *context;
454 #ifdef _WIN32
455 /* OpenAL Soft gives UTF-8 strings, so set the console to expect that. */
456 SetConsoleOutputCP(CP_UTF8);
457 #endif
459 if(argc > 1 && (strcmp(argv[1], "--help") == 0 ||
460 strcmp(argv[1], "-h") == 0))
462 printf("Usage: %s [playback device]\n", argv[0]);
463 return 0;
466 printf("Available playback devices:\n");
467 if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
468 printDeviceList(alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER));
469 else
470 printDeviceList(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
471 printf("Available capture devices:\n");
472 printDeviceList(alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER));
474 if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
475 printf("Default playback device: %s\n",
476 alcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER));
477 else
478 printf("Default playback device: %s\n",
479 alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER));
480 printf("Default capture device: %s\n",
481 alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
483 printALCInfo(NULL);
485 device = alcOpenDevice((argc>1) ? argv[1] : NULL);
486 if(!device)
488 printf("\n!!! Failed to open %s !!!\n\n", ((argc>1) ? argv[1] : "default device"));
489 return 1;
491 printALCInfo(device);
492 printHRTFInfo(device);
493 printALC_SOFT_system_event();
495 context = alcCreateContext(device, NULL);
496 if(!context || alcMakeContextCurrent(context) == ALC_FALSE)
498 if(context)
499 alcDestroyContext(context);
500 alcCloseDevice(device);
501 printf("\n!!! Failed to set a context !!!\n\n");
502 return 1;
505 printModeInfo(device);
506 printALInfo();
507 printResamplerInfo();
508 printEFXInfo(device);
510 alcMakeContextCurrent(NULL);
511 alcDestroyContext(context);
512 alcCloseDevice(device);
514 return 0;