| 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 <[email protected]> |
| 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 | if (alcMakeContextCurrent(NULL) == 0) |
| 157 | printf("alcMakeContextCurrent failed.\n"); |
| 158 | |
| 159 | device = alcGetContextsDevice(context); |
| 160 | alcDestroyContext(context); |
| 161 | testForALCError(device); |
| 162 | |
| 163 | if (alcCloseDevice(device) == 0) |
| 164 | printf("alcCloseDevice failed.\n"); |
| 165 | |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | /* -------------------------------------------------------------------------- */ |
| 170 | |
| 171 | void |
| 172 | printChar (int c, int *width) |
| 173 | { |
| 174 | putchar (c); |
| 175 | *width = (c == '\n') ? 0 : (*width + 1); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | indent (int *width) |
| 180 | { |
| 181 | int i; |
| 182 | for (i = 0; i < indentation; i++) |
| 183 | { |
| 184 | printChar (' ', width); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | printExtensions (const char *header, char separator, const char *extensions) |
| 190 | { |
| 191 | int width = 0, start = 0, end = 0; |
| 192 | |
| 193 | printf ("%s:\n", header); |
| 194 | if (extensions == NULL || extensions[0] == '\0') |
| 195 | { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | indent (&width); |
| 200 | while (1) |
| 201 | { |
| 202 | if (extensions[end] == separator || extensions[end] == '\0') |
| 203 | { |
| 204 | if (width + end - start + 2 > maxmimumWidth) |
| 205 | { |
| 206 | printChar ('\n', &width); |
| 207 | indent (&width); |
| 208 | } |
| 209 | while (start < end) |
| 210 | { |
| 211 | printChar (extensions[start], &width); |
| 212 | start++; |
| 213 | } |
| 214 | if (extensions[end] == '\0') |
| 215 | { |
| 216 | break; |
| 217 | } |
| 218 | start++; |
| 219 | end++; |
| 220 | if (extensions[end] == '\0') |
| 221 | { |
| 222 | break; |
| 223 | } |
| 224 | printChar (',', &width); |
| 225 | printChar (' ', &width); |
| 226 | } |
| 227 | end++; |
| 228 | } |
| 229 | printChar ('\n', &width); |
| 230 | } |
| 231 | |
| 232 | char * |
| 233 | getCommandLineOption(int argc, char **argv, const string& option) |
| 234 | { |
| 235 | int slen = option.size(); |
| 236 | char *rv = 0; |
| 237 | int i; |
| 238 | |
| 239 | for (i=0; i<argc; i++) |
| 240 | { |
| 241 | if (strncmp(argv[i], option.c_str(), slen) == 0) |
| 242 | { |
| 243 | i++; |
| 244 | if (i<argc) rv = argv[i]; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | return rv; |
| 249 | } |
| 250 | |
| 251 | char * |
| 252 | getDeviceName(int argc, char **argv) |
| 253 | { |
| 254 | static char devname[255]; |
| 255 | int len = 255; |
| 256 | char *s; |
| 257 | |
| 258 | s = getCommandLineOption(argc, argv, "-d"); |
| 259 | if (s) |
| 260 | { |
| 261 | strncpy((char *)&devname, s, len); |
| 262 | len -= strlen(s); |
| 263 | |
| 264 | s = getCommandLineOption(argc, argv, "-r"); |
| 265 | if (s) |
| 266 | { |
| 267 | strncat((char *)&devname, " on ", len); |
| 268 | len -= 4; |
| 269 | |
| 270 | strncat((char *)&devname, s, len); |
| 271 | } |
| 272 | s = (char *)&devname; |
| 273 | } |
| 274 | |
| 275 | return s; |
| 276 | } |
| 277 | |
| 278 | void |
| 279 | displayDevices(const char *type, const char *list) |
| 280 | { |
| 281 | ALCchar *ptr, *nptr; |
| 282 | |
| 283 | ptr = (ALCchar *)list; |
| 284 | printf("list of all available %s devices:\n", type); |
| 285 | if (!list) |
| 286 | { |
| 287 | printf("none\n"); |
| 288 | } |
| 289 | else |
| 290 | { |
| 291 | nptr = ptr; |
| 292 | while (*(nptr += strlen(ptr)+1) != 0) |
| 293 | { |
| 294 | printf(" %s\n", ptr); |
| 295 | ptr = nptr; |
| 296 | } |
| 297 | printf(" %s\n", ptr); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void |
| 302 | testForError(void *p, const string& s) |
| 303 | { |
| 304 | if (p == NULL) |
| 305 | { |
| 306 | printf("\nError: %s\n\n", s.c_str()); |
| 307 | exit(-1); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | testForALCError(ALCdevice *device) |
| 313 | { |
| 314 | ALenum error; |
| 315 | error = alcGetError(device); |
| 316 | if (error != ALC_NO_ERROR) |
| 317 | printf("\nALC Error %x occurred: %s\n", error, alcGetString(device, error)); |
| 318 | } |