Make a function static that's only used in one source file
[openal-soft.git] / utils / openal-info.c
blob617550f24d4f9cc126ea0017f6bff2fb104276c4
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 <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
29 #include "AL/alc.h"
30 #include "AL/al.h"
31 #include "AL/alext.h"
33 #ifndef ALC_ENUMERATE_ALL_EXT
34 #define ALC_DEFAULT_ALL_DEVICES_SPECIFIER 0x1012
35 #define ALC_ALL_DEVICES_SPECIFIER 0x1013
36 #endif
38 #ifndef ALC_EXT_EFX
39 #define ALC_EFX_MAJOR_VERSION 0x20001
40 #define ALC_EFX_MINOR_VERSION 0x20002
41 #define ALC_MAX_AUXILIARY_SENDS 0x20003
42 #endif
45 #ifdef _WIN32
46 #define WIN32_LEAN_AND_MEAN
47 #include <windows.h>
49 static WCHAR *FromUTF8(const char *str)
51 WCHAR *out = NULL;
52 int len;
54 if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
56 out = calloc(sizeof(WCHAR), len);
57 MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
59 return out;
62 /* Override printf, fprintf, and fwrite so we can print UTF-8 strings. */
63 static void al_fprintf(FILE *file, const char *fmt, ...)
65 char str[1024];
66 WCHAR *wstr;
67 va_list ap;
69 va_start(ap, fmt);
70 vsnprintf(str, sizeof(str), fmt, ap);
71 va_end(ap);
73 str[sizeof(str)-1] = 0;
74 wstr = FromUTF8(str);
75 if(!wstr)
76 fprintf(file, "<UTF-8 error> %s", str);
77 else
78 fprintf(file, "%ls", wstr);
79 free(wstr);
81 #define fprintf al_fprintf
82 #define printf(...) al_fprintf(stdout, __VA_ARGS__)
84 static int al_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *file)
86 char str[1024];
87 WCHAR *wstr;
88 size_t len;
90 len = size * nmemb;
91 if(len > sizeof(str)-1)
92 len = sizeof(str)-1;
93 memcpy(str, ptr, len);
94 str[len] = 0;
96 wstr = FromUTF8(str);
97 if(!wstr)
98 fprintf(file, "<UTF-8 error> %s", str);
99 else
100 fprintf(file, "%ls", wstr);
101 free(wstr);
103 return len / size;
105 #define fwrite al_fwrite
106 #endif
109 #define MAX_WIDTH 80
111 static void printList(const char *list, char separator)
113 size_t col = MAX_WIDTH, len;
114 const char *indent = " ";
115 const char *next;
117 if(!list || *list == '\0')
119 fprintf(stdout, "\n%s!!! none !!!\n", indent);
120 return;
123 do {
124 next = strchr(list, separator);
125 if(next)
127 len = next-list;
128 do {
129 next++;
130 } while(*next == separator);
132 else
133 len = strlen(list);
135 if(len + col + 2 >= MAX_WIDTH)
137 fprintf(stdout, "\n%s", indent);
138 col = strlen(indent);
140 else
142 fputc(' ', stdout);
143 col++;
146 len = fwrite(list, 1, len, stdout);
147 col += len;
149 if(!next || *next == '\0')
150 break;
151 fputc(',', stdout);
152 col++;
154 list = next;
155 } while(1);
156 fputc('\n', stdout);
159 static void printDeviceList(const char *list)
161 if(!list || *list == '\0')
162 printf(" !!! none !!!\n");
163 else do {
164 printf(" %s\n", list);
165 list += strlen(list) + 1;
166 } while(*list != '\0');
170 static ALenum checkALErrors(int linenum)
172 ALenum err = alGetError();
173 if(err != AL_NO_ERROR)
174 printf("OpenAL Error: %s (0x%x), @ %d\n", alGetString(err), err, linenum);
175 return err;
177 #define checkALErrors() checkALErrors(__LINE__)
179 static ALCenum checkALCErrors(ALCdevice *device, int linenum)
181 ALCenum err = alcGetError(device);
182 if(err != ALC_NO_ERROR)
183 printf("ALC Error: %s (0x%x), @ %d\n", alcGetString(device, err), err, linenum);
184 return err;
186 #define checkALCErrors(x) checkALCErrors((x),__LINE__)
189 static void printALCInfo(ALCdevice *device)
191 ALCint major, minor;
193 if(device)
195 const ALCchar *devname = NULL;
196 printf("\n");
197 if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
198 devname = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
199 if(checkALCErrors(device) != ALC_NO_ERROR || !devname)
200 devname = alcGetString(device, ALC_DEVICE_SPECIFIER);
201 printf("** Info for device \"%s\" **\n", devname);
203 alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &major);
204 alcGetIntegerv(device, ALC_MINOR_VERSION, 1, &minor);
205 if(checkALCErrors(device) == ALC_NO_ERROR)
206 printf("ALC version: %d.%d\n", major, minor);
207 if(device)
209 printf("ALC extensions:");
210 printList(alcGetString(device, ALC_EXTENSIONS), ' ');
211 checkALCErrors(device);
215 static void printHRTFInfo(ALCdevice *device)
217 LPALCGETSTRINGISOFT alcGetStringiSOFT;
218 ALCint num_hrtfs;
220 if(alcIsExtensionPresent(device, "ALC_SOFT_HRTF") == ALC_FALSE)
222 printf("HRTF extension not available\n");
223 return;
226 alcGetStringiSOFT = alcGetProcAddress(device, "alcGetStringiSOFT");
228 alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtfs);
229 if(!num_hrtfs)
230 printf("No HRTFs found\n");
231 else
233 ALCint i;
234 printf("Available HRTFs:\n");
235 for(i = 0;i < num_hrtfs;++i)
237 const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i);
238 printf(" %s\n", name);
241 checkALCErrors(device);
244 static void printALInfo(void)
246 printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR));
247 printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER));
248 printf("OpenAL version string: %s\n", alGetString(AL_VERSION));
249 printf("OpenAL extensions:");
250 printList(alGetString(AL_EXTENSIONS), ' ');
251 checkALErrors();
254 static void printResamplerInfo(void)
256 LPALGETSTRINGISOFT alGetStringiSOFT;
257 ALint num_resamplers;
258 ALint def_resampler;
260 if(!alIsExtensionPresent("AL_SOFT_source_resampler"))
262 printf("Resampler info not available\n");
263 return;
266 alGetStringiSOFT = alGetProcAddress("alGetStringiSOFT");
268 num_resamplers = alGetInteger(AL_NUM_RESAMPLERS_SOFT);
269 def_resampler = alGetInteger(AL_DEFAULT_RESAMPLER_SOFT);
271 if(!num_resamplers)
272 printf("!!! No resamplers found !!!\n");
273 else
275 ALint i;
276 printf("Available resamplers:\n");
277 for(i = 0;i < num_resamplers;++i)
279 const ALchar *name = alGetStringiSOFT(AL_RESAMPLER_NAME_SOFT, i);
280 printf(" %s%s\n", name, (i==def_resampler)?" *":"");
283 checkALErrors();
286 static void printEFXInfo(ALCdevice *device)
288 ALCint major, minor, sends;
289 static const ALchar filters[][32] = {
290 "AL_FILTER_LOWPASS", "AL_FILTER_HIGHPASS", "AL_FILTER_BANDPASS", ""
292 char filterNames[] = "Low-pass,High-pass,Band-pass,";
293 static const ALchar effects[][32] = {
294 "AL_EFFECT_EAXREVERB", "AL_EFFECT_REVERB", "AL_EFFECT_CHORUS",
295 "AL_EFFECT_DISTORTION", "AL_EFFECT_ECHO", "AL_EFFECT_FLANGER",
296 "AL_EFFECT_FREQUENCY_SHIFTER", "AL_EFFECT_VOCAL_MORPHER",
297 "AL_EFFECT_PITCH_SHIFTER", "AL_EFFECT_RING_MODULATOR",
298 "AL_EFFECT_AUTOWAH", "AL_EFFECT_COMPRESSOR", "AL_EFFECT_EQUALIZER", ""
300 static const ALchar dedeffects[][64] = {
301 "AL_EFFECT_DEDICATED_DIALOGUE",
302 "AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT", ""
304 char effectNames[] = "EAX Reverb,Reverb,Chorus,Distortion,Echo,Flanger,"
305 "Frequency Shifter,Vocal Morpher,Pitch Shifter,"
306 "Ring Modulator,Autowah,Compressor,Equalizer,"
307 "Dedicated Dialog,Dedicated LFE,";
308 char *current;
309 int i;
311 if(alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_FALSE)
313 printf("EFX not available\n");
314 return;
317 alcGetIntegerv(device, ALC_EFX_MAJOR_VERSION, 1, &major);
318 alcGetIntegerv(device, ALC_EFX_MINOR_VERSION, 1, &minor);
319 if(checkALCErrors(device) == ALC_NO_ERROR)
320 printf("EFX version: %d.%d\n", major, minor);
321 alcGetIntegerv(device, ALC_MAX_AUXILIARY_SENDS, 1, &sends);
322 if(checkALCErrors(device) == ALC_NO_ERROR)
323 printf("Max auxiliary sends: %d\n", sends);
325 current = filterNames;
326 for(i = 0;filters[i][0];i++)
328 char *next = strchr(current, ',');
329 ALenum val;
331 val = alGetEnumValue(filters[i]);
332 if(alGetError() != AL_NO_ERROR || val == 0 || val == -1)
333 memmove(current, next+1, strlen(next));
334 else
335 current = next+1;
337 printf("Supported filters:");
338 printList(filterNames, ',');
340 current = effectNames;
341 for(i = 0;effects[i][0];i++)
343 char *next = strchr(current, ',');
344 ALenum val;
346 val = alGetEnumValue(effects[i]);
347 if(alGetError() != AL_NO_ERROR || val == 0 || val == -1)
348 memmove(current, next+1, strlen(next));
349 else
350 current = next+1;
352 if(alcIsExtensionPresent(device, "ALC_EXT_DEDICATED"))
354 for(i = 0;dedeffects[i][0];i++)
356 char *next = strchr(current, ',');
357 ALenum val;
359 val = alGetEnumValue(dedeffects[i]);
360 if(alGetError() != AL_NO_ERROR || val == 0 || val == -1)
361 memmove(current, next+1, strlen(next));
362 else
363 current = next+1;
366 else
368 for(i = 0;dedeffects[i][0];i++)
370 char *next = strchr(current, ',');
371 memmove(current, next+1, strlen(next));
374 printf("Supported effects:");
375 printList(effectNames, ',');
378 int main(int argc, char *argv[])
380 ALCdevice *device;
381 ALCcontext *context;
383 if(argc > 1 && (strcmp(argv[1], "--help") == 0 ||
384 strcmp(argv[1], "-h") == 0))
386 printf("Usage: %s [playback device]\n", argv[0]);
387 return 0;
390 printf("Available playback devices:\n");
391 if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
392 printDeviceList(alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER));
393 else
394 printDeviceList(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
395 printf("Available capture devices:\n");
396 printDeviceList(alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER));
398 if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
399 printf("Default playback device: %s\n",
400 alcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER));
401 else
402 printf("Default playback device: %s\n",
403 alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER));
404 printf("Default capture device: %s\n",
405 alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
407 printALCInfo(NULL);
409 device = alcOpenDevice((argc>1) ? argv[1] : NULL);
410 if(!device)
412 printf("\n!!! Failed to open %s !!!\n\n", ((argc>1) ? argv[1] : "default device"));
413 return 1;
415 printALCInfo(device);
416 printHRTFInfo(device);
418 context = alcCreateContext(device, NULL);
419 if(!context || alcMakeContextCurrent(context) == ALC_FALSE)
421 if(context)
422 alcDestroyContext(context);
423 alcCloseDevice(device);
424 printf("\n!!! Failed to set a context !!!\n\n");
425 return 1;
428 printALInfo();
429 printResamplerInfo();
430 printEFXInfo(device);
432 alcMakeContextCurrent(NULL);
433 alcDestroyContext(context);
434 alcCloseDevice(device);
436 return 0;