openal-info.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * OpenAL Info Utility
  3. *
  4. * Copyright (c) 2010 by Chris Robinson <[email protected]>
  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. #include <stdarg.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include "AL/alc.h"
  29. #include "AL/al.h"
  30. #include "AL/alext.h"
  31. #include "win_main_utf8.h"
  32. #ifndef ALC_ENUMERATE_ALL_EXT
  33. #define ALC_DEFAULT_ALL_DEVICES_SPECIFIER 0x1012
  34. #define ALC_ALL_DEVICES_SPECIFIER 0x1013
  35. #endif
  36. #ifndef ALC_EXT_EFX
  37. #define ALC_EFX_MAJOR_VERSION 0x20001
  38. #define ALC_EFX_MINOR_VERSION 0x20002
  39. #define ALC_MAX_AUXILIARY_SENDS 0x20003
  40. #endif
  41. #ifdef _WIN32
  42. #define WIN32_LEAN_AND_MEAN
  43. #include <windows.h>
  44. static WCHAR *FromUTF8(const char *str)
  45. {
  46. WCHAR *out = NULL;
  47. int len;
  48. if((len=MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0)) > 0)
  49. {
  50. out = calloc(sizeof(WCHAR), (unsigned int)(len));
  51. MultiByteToWideChar(CP_UTF8, 0, str, -1, out, len);
  52. }
  53. return out;
  54. }
  55. /* Override printf, fprintf, and fwrite so we can print UTF-8 strings. */
  56. static void al_fprintf(FILE *file, const char *fmt, ...)
  57. {
  58. char str[1024];
  59. WCHAR *wstr;
  60. va_list ap;
  61. va_start(ap, fmt);
  62. vsnprintf(str, sizeof(str), fmt, ap);
  63. va_end(ap);
  64. str[sizeof(str)-1] = 0;
  65. wstr = FromUTF8(str);
  66. if(!wstr)
  67. fprintf(file, "<UTF-8 error> %s", str);
  68. else
  69. fprintf(file, "%ls", wstr);
  70. free(wstr);
  71. }
  72. #define fprintf al_fprintf
  73. #define printf(...) al_fprintf(stdout, __VA_ARGS__)
  74. static size_t al_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *file)
  75. {
  76. char str[1024];
  77. WCHAR *wstr;
  78. size_t len;
  79. len = size * nmemb;
  80. if(len > sizeof(str)-1)
  81. len = sizeof(str)-1;
  82. memcpy(str, ptr, len);
  83. str[len] = 0;
  84. wstr = FromUTF8(str);
  85. if(!wstr)
  86. fprintf(file, "<UTF-8 error> %s", str);
  87. else
  88. fprintf(file, "%ls", wstr);
  89. free(wstr);
  90. return len / size;
  91. }
  92. #define fwrite al_fwrite
  93. #endif
  94. #define MAX_WIDTH 80
  95. static void printList(const char *list, char separator)
  96. {
  97. size_t col = MAX_WIDTH, len;
  98. const char *indent = " ";
  99. const char *next;
  100. if(!list || *list == '\0')
  101. {
  102. fprintf(stdout, "\n%s!!! none !!!\n", indent);
  103. return;
  104. }
  105. do {
  106. next = strchr(list, separator);
  107. if(next)
  108. {
  109. len = (size_t)(next-list);
  110. do {
  111. next++;
  112. } while(*next == separator);
  113. }
  114. else
  115. len = strlen(list);
  116. if(len + col + 2 >= MAX_WIDTH)
  117. {
  118. fprintf(stdout, "\n%s", indent);
  119. col = strlen(indent);
  120. }
  121. else
  122. {
  123. fputc(' ', stdout);
  124. col++;
  125. }
  126. len = fwrite(list, 1, len, stdout);
  127. col += len;
  128. if(!next || *next == '\0')
  129. break;
  130. fputc(',', stdout);
  131. col++;
  132. list = next;
  133. } while(1);
  134. fputc('\n', stdout);
  135. }
  136. static void printDeviceList(const char *list)
  137. {
  138. if(!list || *list == '\0')
  139. printf(" !!! none !!!\n");
  140. else do {
  141. printf(" %s\n", list);
  142. list += strlen(list) + 1;
  143. } while(*list != '\0');
  144. }
  145. static ALenum checkALErrors(int linenum)
  146. {
  147. ALenum err = alGetError();
  148. if(err != AL_NO_ERROR)
  149. printf("OpenAL Error: %s (0x%x), @ %d\n", alGetString(err), err, linenum);
  150. return err;
  151. }
  152. #define checkALErrors() checkALErrors(__LINE__)
  153. static ALCenum checkALCErrors(ALCdevice *device, int linenum)
  154. {
  155. ALCenum err = alcGetError(device);
  156. if(err != ALC_NO_ERROR)
  157. printf("ALC Error: %s (0x%x), @ %d\n", alcGetString(device, err), err, linenum);
  158. return err;
  159. }
  160. #define checkALCErrors(x) checkALCErrors((x),__LINE__)
  161. static void printALCInfo(ALCdevice *device)
  162. {
  163. ALCint major, minor;
  164. if(device)
  165. {
  166. const ALCchar *devname = NULL;
  167. printf("\n");
  168. if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
  169. devname = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
  170. if(checkALCErrors(device) != ALC_NO_ERROR || !devname)
  171. devname = alcGetString(device, ALC_DEVICE_SPECIFIER);
  172. printf("** Info for device \"%s\" **\n", devname);
  173. }
  174. alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &major);
  175. alcGetIntegerv(device, ALC_MINOR_VERSION, 1, &minor);
  176. if(checkALCErrors(device) == ALC_NO_ERROR)
  177. printf("ALC version: %d.%d\n", major, minor);
  178. if(device)
  179. {
  180. printf("ALC extensions:");
  181. printList(alcGetString(device, ALC_EXTENSIONS), ' ');
  182. checkALCErrors(device);
  183. }
  184. }
  185. static void printHRTFInfo(ALCdevice *device)
  186. {
  187. LPALCGETSTRINGISOFT alcGetStringiSOFT;
  188. ALCint num_hrtfs;
  189. if(alcIsExtensionPresent(device, "ALC_SOFT_HRTF") == ALC_FALSE)
  190. {
  191. printf("HRTF extension not available\n");
  192. return;
  193. }
  194. alcGetStringiSOFT = (LPALCGETSTRINGISOFT)alcGetProcAddress(device, "alcGetStringiSOFT");
  195. alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtfs);
  196. if(!num_hrtfs)
  197. printf("No HRTFs found\n");
  198. else
  199. {
  200. ALCint i;
  201. printf("Available HRTFs:\n");
  202. for(i = 0;i < num_hrtfs;++i)
  203. {
  204. const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i);
  205. printf(" %s\n", name);
  206. }
  207. }
  208. checkALCErrors(device);
  209. }
  210. static void printALInfo(void)
  211. {
  212. printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR));
  213. printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER));
  214. printf("OpenAL version string: %s\n", alGetString(AL_VERSION));
  215. printf("OpenAL extensions:");
  216. printList(alGetString(AL_EXTENSIONS), ' ');
  217. checkALErrors();
  218. }
  219. static void printResamplerInfo(void)
  220. {
  221. LPALGETSTRINGISOFT alGetStringiSOFT;
  222. ALint num_resamplers;
  223. ALint def_resampler;
  224. if(!alIsExtensionPresent("AL_SOFT_source_resampler"))
  225. {
  226. printf("Resampler info not available\n");
  227. return;
  228. }
  229. alGetStringiSOFT = (LPALGETSTRINGISOFT)alGetProcAddress("alGetStringiSOFT");
  230. num_resamplers = alGetInteger(AL_NUM_RESAMPLERS_SOFT);
  231. def_resampler = alGetInteger(AL_DEFAULT_RESAMPLER_SOFT);
  232. if(!num_resamplers)
  233. printf("!!! No resamplers found !!!\n");
  234. else
  235. {
  236. ALint i;
  237. printf("Available resamplers:\n");
  238. for(i = 0;i < num_resamplers;++i)
  239. {
  240. const ALchar *name = alGetStringiSOFT(AL_RESAMPLER_NAME_SOFT, i);
  241. printf(" %s%s\n", name, (i==def_resampler)?" *":"");
  242. }
  243. }
  244. checkALErrors();
  245. }
  246. static void printEFXInfo(ALCdevice *device)
  247. {
  248. ALCint major, minor, sends;
  249. static const ALchar filters[][32] = {
  250. "AL_FILTER_LOWPASS", "AL_FILTER_HIGHPASS", "AL_FILTER_BANDPASS", ""
  251. };
  252. char filterNames[] = "Low-pass,High-pass,Band-pass,";
  253. static const ALchar effects[][32] = {
  254. "AL_EFFECT_EAXREVERB", "AL_EFFECT_REVERB", "AL_EFFECT_CHORUS",
  255. "AL_EFFECT_DISTORTION", "AL_EFFECT_ECHO", "AL_EFFECT_FLANGER",
  256. "AL_EFFECT_FREQUENCY_SHIFTER", "AL_EFFECT_VOCAL_MORPHER",
  257. "AL_EFFECT_PITCH_SHIFTER", "AL_EFFECT_RING_MODULATOR",
  258. "AL_EFFECT_AUTOWAH", "AL_EFFECT_COMPRESSOR", "AL_EFFECT_EQUALIZER", ""
  259. };
  260. static const ALchar dedeffects[][64] = {
  261. "AL_EFFECT_DEDICATED_DIALOGUE",
  262. "AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT", ""
  263. };
  264. char effectNames[] = "EAX Reverb,Reverb,Chorus,Distortion,Echo,Flanger,"
  265. "Frequency Shifter,Vocal Morpher,Pitch Shifter,"
  266. "Ring Modulator,Autowah,Compressor,Equalizer,"
  267. "Dedicated Dialog,Dedicated LFE,";
  268. char *current;
  269. int i;
  270. if(alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_FALSE)
  271. {
  272. printf("EFX not available\n");
  273. return;
  274. }
  275. alcGetIntegerv(device, ALC_EFX_MAJOR_VERSION, 1, &major);
  276. alcGetIntegerv(device, ALC_EFX_MINOR_VERSION, 1, &minor);
  277. if(checkALCErrors(device) == ALC_NO_ERROR)
  278. printf("EFX version: %d.%d\n", major, minor);
  279. alcGetIntegerv(device, ALC_MAX_AUXILIARY_SENDS, 1, &sends);
  280. if(checkALCErrors(device) == ALC_NO_ERROR)
  281. printf("Max auxiliary sends: %d\n", sends);
  282. current = filterNames;
  283. for(i = 0;filters[i][0];i++)
  284. {
  285. char *next = strchr(current, ',');
  286. ALenum val;
  287. val = alGetEnumValue(filters[i]);
  288. if(alGetError() != AL_NO_ERROR || val == 0 || val == -1)
  289. memmove(current, next+1, strlen(next));
  290. else
  291. current = next+1;
  292. }
  293. printf("Supported filters:");
  294. printList(filterNames, ',');
  295. current = effectNames;
  296. for(i = 0;effects[i][0];i++)
  297. {
  298. char *next = strchr(current, ',');
  299. ALenum val;
  300. val = alGetEnumValue(effects[i]);
  301. if(alGetError() != AL_NO_ERROR || val == 0 || val == -1)
  302. memmove(current, next+1, strlen(next));
  303. else
  304. current = next+1;
  305. }
  306. if(alcIsExtensionPresent(device, "ALC_EXT_DEDICATED"))
  307. {
  308. for(i = 0;dedeffects[i][0];i++)
  309. {
  310. char *next = strchr(current, ',');
  311. ALenum val;
  312. val = alGetEnumValue(dedeffects[i]);
  313. if(alGetError() != AL_NO_ERROR || val == 0 || val == -1)
  314. memmove(current, next+1, strlen(next));
  315. else
  316. current = next+1;
  317. }
  318. }
  319. else
  320. {
  321. for(i = 0;dedeffects[i][0];i++)
  322. {
  323. char *next = strchr(current, ',');
  324. memmove(current, next+1, strlen(next));
  325. }
  326. }
  327. printf("Supported effects:");
  328. printList(effectNames, ',');
  329. }
  330. int main(int argc, char *argv[])
  331. {
  332. ALCdevice *device;
  333. ALCcontext *context;
  334. if(argc > 1 && (strcmp(argv[1], "--help") == 0 ||
  335. strcmp(argv[1], "-h") == 0))
  336. {
  337. printf("Usage: %s [playback device]\n", argv[0]);
  338. return 0;
  339. }
  340. printf("Available playback devices:\n");
  341. if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
  342. printDeviceList(alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER));
  343. else
  344. printDeviceList(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
  345. printf("Available capture devices:\n");
  346. printDeviceList(alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER));
  347. if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
  348. printf("Default playback device: %s\n",
  349. alcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER));
  350. else
  351. printf("Default playback device: %s\n",
  352. alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER));
  353. printf("Default capture device: %s\n",
  354. alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));
  355. printALCInfo(NULL);
  356. device = alcOpenDevice((argc>1) ? argv[1] : NULL);
  357. if(!device)
  358. {
  359. printf("\n!!! Failed to open %s !!!\n\n", ((argc>1) ? argv[1] : "default device"));
  360. return 1;
  361. }
  362. printALCInfo(device);
  363. printHRTFInfo(device);
  364. context = alcCreateContext(device, NULL);
  365. if(!context || alcMakeContextCurrent(context) == ALC_FALSE)
  366. {
  367. if(context)
  368. alcDestroyContext(context);
  369. alcCloseDevice(device);
  370. printf("\n!!! Failed to set a context !!!\n\n");
  371. return 1;
  372. }
  373. printALInfo();
  374. printResamplerInfo();
  375. printEFXInfo(device);
  376. alcMakeContextCurrent(NULL);
  377. alcDestroyContext(context);
  378. alcCloseDevice(device);
  379. return 0;
  380. }