aboutsummaryrefslogtreecommitdiffstats
path: root/utils/openal-info.c
blob: 8ef6ebde5c8a2029b1a81a4cb6d9b09e522b0e17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * OpenAL Info Utility
 *
 * Copyright (c) 2010 by Chris Robinson <chris.kcat@gmail.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "AL/alc.h"
#include "AL/al.h"
#include "AL/alext.h"

#include "win_main_utf8.h"

/* C doesn't allow casting between function and non-function pointer types, so
 * with C99 we need to use a union to reinterpret the pointer type. Pre-C99
 * still needs to use a normal cast and live with the warning (C++ is fine with
 * a regular reinterpret_cast).
 */
#if __STDC_VERSION__ >= 199901L
#define FUNCTION_CAST(T, ptr) (union{void *p; T f;}){ptr}.f
#else
#define FUNCTION_CAST(T, ptr) (T)(ptr)
#endif

#define MAX_WIDTH  80

static void printList(const char *list, char separator)
{
    size_t col = MAX_WIDTH, len;
    const char *indent = "    ";
    const char *next;

    if(!list || *list == '\0')
    {
        fprintf(stdout, "\n%s!!! none !!!\n", indent);
        return;
    }

    do {
        next = strchr(list, separator);
        if(next)
        {
            len = (size_t)(next-list);
            do {
                next++;
            } while(*next == separator);
        }
        else
            len = strlen(list);

        if(len + col + 2 >= MAX_WIDTH)
        {
            fprintf(stdout, "\n%s", indent);
            col = strlen(indent);
        }
        else
        {
            fputc(' ', stdout);
            col++;
        }

        len = fwrite(list, 1, len, stdout);
        col += len;

        if(!next || *next == '\0')
            break;
        fputc(',', stdout);
        col++;

        list = next;
    } while(1);
    fputc('\n', stdout);
}

static void printDeviceList(const char *list)
{
    if(!list || *list == '\0')
        printf("    !!! none !!!\n");
    else do {
        printf("    %s\n", list);
        list += strlen(list) + 1;
    } while(*list != '\0');
}


static ALenum checkALErrors(int linenum)
{
    ALenum err = alGetError();
    if(err != AL_NO_ERROR)
        printf("OpenAL Error: %s (0x%x), @ %d\n", alGetString(err), err, linenum);
    return err;
}
#define checkALErrors() checkALErrors(__LINE__)

static ALCenum checkALCErrors(ALCdevice *device, int linenum)
{
    ALCenum err = alcGetError(device);
    if(err != ALC_NO_ERROR)
        printf("ALC Error: %s (0x%x), @ %d\n", alcGetString(device, err), err, linenum);
    return err;
}
#define checkALCErrors(x) checkALCErrors((x),__LINE__)


static void printALCInfo(ALCdevice *device)
{
    ALCint major, minor;

    if(device)
    {
        const ALCchar *devname = NULL;
        printf("\n");
        if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
            devname = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
        if(checkALCErrors(device) != ALC_NO_ERROR || !devname)
            devname = alcGetString(device, ALC_DEVICE_SPECIFIER);
        printf("** Info for device \"%s\" **\n", devname);
    }
    alcGetIntegerv(device, ALC_MAJOR_VERSION, 1, &major);
    alcGetIntegerv(device, ALC_MINOR_VERSION, 1, &minor);
    if(checkALCErrors(device) == ALC_NO_ERROR)
        printf("ALC version: %d.%d\n", major, minor);
    if(device)
    {
        printf("ALC extensions:");
        printList(alcGetString(device, ALC_EXTENSIONS), ' ');
        checkALCErrors(device);
    }
}

static void printHRTFInfo(ALCdevice *device)
{
    LPALCGETSTRINGISOFT alcGetStringiSOFT;
    ALCint num_hrtfs;

    if(alcIsExtensionPresent(device, "ALC_SOFT_HRTF") == ALC_FALSE)
    {
        printf("HRTF extension not available\n");
        return;
    }

    alcGetStringiSOFT = FUNCTION_CAST(LPALCGETSTRINGISOFT,
        alcGetProcAddress(device, "alcGetStringiSOFT"));

    alcGetIntegerv(device, ALC_NUM_HRTF_SPECIFIERS_SOFT, 1, &num_hrtfs);
    if(!num_hrtfs)
        printf("No HRTF profiles found\n");
    else
    {
        ALCint i;
        printf("Available HRTF profiles:\n");
        for(i = 0;i < num_hrtfs;++i)
        {
            const ALCchar *name = alcGetStringiSOFT(device, ALC_HRTF_SPECIFIER_SOFT, i);
            printf("    %s\n", name);
        }
    }
    checkALCErrors(device);
}

static void printModeInfo(ALCdevice *device)
{
    ALCint srate = 0;

    if(alcIsExtensionPresent(device, "ALC_SOFT_output_mode"))
    {
        const char *modename = "(error)";
        ALCenum mode = 0;

        alcGetIntegerv(device, ALC_OUTPUT_MODE_SOFT, 1, &mode);
        checkALCErrors(device);
        switch(mode)
        {
        case ALC_ANY_SOFT: modename = "Unknown / unspecified"; break;
        case ALC_MONO_SOFT: modename = "Mono"; break;
        case ALC_STEREO_SOFT: modename = "Stereo (unspecified encoding)"; break;
        case ALC_STEREO_BASIC_SOFT: modename = "Stereo (basic)"; break;
        case ALC_STEREO_UHJ_SOFT: modename = "Stereo (UHJ)"; break;
        case ALC_STEREO_HRTF_SOFT: modename = "Stereo (HRTF)"; break;
        case ALC_QUAD_SOFT: modename = "Quadraphonic"; break;
        case ALC_SURROUND_5_1_SOFT: modename = "5.1 Surround"; break;
        case ALC_SURROUND_6_1_SOFT: modename = "6.1 Surround"; break;
        case ALC_SURROUND_7_1_SOFT: modename = "7.1 Surround"; break;
        }
        printf("Device output mode: %s\n", modename);
    }
    else
        printf("Output mode extension not available\n");

    alcGetIntegerv(device, ALC_FREQUENCY, 1, &srate);
    if(checkALCErrors(device) == ALC_NO_ERROR)
        printf("Device sample rate: %dhz\n", srate);

    if(alcIsExtensionPresent(device, "ALC_SOFT_HRTF"))
    {
        const ALCchar *hrtfname = "(disabled)";
        ALCint isenabled = 0;

        alcGetIntegerv(device, ALC_HRTF_SOFT, 1, &isenabled);
        checkALCErrors(device);
        if(isenabled == ALC_TRUE)
        {
            hrtfname = alcGetString(device, ALC_HRTF_SPECIFIER_SOFT);
            checkALCErrors(device);
        }
        printf("Device HRTF profile: %s\n", hrtfname ? hrtfname : "<null>");
    }
}

static void printALCSOFTSystemEventIsSupportedResult(LPALCEVENTISSUPPORTEDSOFT alcEventIsSupportedSOFT, ALCenum eventType, ALCenum deviceType)
{
    if (alcEventIsSupportedSOFT == NULL)
    {
        printf("ERROR (alcEventIsSupportedSOFT missing)\n");
        return;
    }
    ALCenum supported = alcEventIsSupportedSOFT(eventType, deviceType);
    if (supported == ALC_EVENT_SUPPORTED_SOFT)
    {
        printf("SUPPORTED\n");
    }
    else if (supported == ALC_EVENT_NOT_SUPPORTED_SOFT)
    {
        printf("NOT SUPPORTED\n");
    }
    else
    {
        printf("UNEXPECTED VALUE : %d\n", supported);
    }
}

static void printALC_SOFT_system_event(void)
{
    if(alcIsExtensionPresent(NULL, "ALC_SOFT_system_events"))
    {
        LPALCEVENTISSUPPORTEDSOFT alcEventIsSupportedSOFT;
        alcEventIsSupportedSOFT = FUNCTION_CAST(LPALCEVENTISSUPPORTEDSOFT, alGetProcAddress("alcEventIsSupportedSOFT"));
        printf("Available events:\n");
        printf("    ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT for ALC_PLAYBACK_DEVICE_SOFT - ");
        printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT, ALC_PLAYBACK_DEVICE_SOFT);
        printf("    ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT for ALC_CAPTURE_DEVICE_SOFT - ");
        printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEFAULT_DEVICE_CHANGED_SOFT, ALC_CAPTURE_DEVICE_SOFT);
        printf("    ALC_EVENT_TYPE_DEVICE_ADDED_SOFT for ALC_PLAYBACK_DEVICE_SOFT - ");
        printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_ADDED_SOFT, ALC_PLAYBACK_DEVICE_SOFT);
        printf("    ALC_EVENT_TYPE_DEVICE_ADDED_SOFT for ALC_CAPTURE_DEVICE_SOFT - ");
        printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_ADDED_SOFT, ALC_CAPTURE_DEVICE_SOFT);
        printf("    ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT for ALC_PLAYBACK_DEVICE_SOFT - ");
        printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT, ALC_PLAYBACK_DEVICE_SOFT);
        printf("    ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT for ALC_CAPTURE_DEVICE_SOFT - ");
        printALCSOFTSystemEventIsSupportedResult(alcEventIsSupportedSOFT, ALC_EVENT_TYPE_DEVICE_REMOVED_SOFT, ALC_CAPTURE_DEVICE_SOFT);
    }
}

static void printALInfo(void)
{
    printf("OpenAL vendor string: %s\n", alGetString(AL_VENDOR));
    printf("OpenAL renderer string: %s\n", alGetString(AL_RENDERER));
    printf("OpenAL version string: %s\n", alGetString(AL_VERSION));
    printf("OpenAL extensions:");
    printList(alGetString(AL_EXTENSIONS), ' ');
    checkALErrors();
}

static void printResamplerInfo(void)
{
    LPALGETSTRINGISOFT alGetStringiSOFT;
    ALint num_resamplers;
    ALint def_resampler;

    if(!alIsExtensionPresent("AL_SOFT_source_resampler"))
    {
        printf("Resampler info not available\n");
        return;
    }

    alGetStringiSOFT = FUNCTION_CAST(LPALGETSTRINGISOFT, alGetProcAddress("alGetStringiSOFT"));

    num_resamplers = alGetInteger(AL_NUM_RESAMPLERS_SOFT);
    def_resampler = alGetInteger(AL_DEFAULT_RESAMPLER_SOFT);

    if(!num_resamplers)
        printf("!!! No resamplers found !!!\n");
    else
    {
        ALint i;
        printf("Available resamplers:\n");
        for(i = 0;i < num_resamplers;++i)
        {
            const ALchar *name = alGetStringiSOFT(AL_RESAMPLER_NAME_SOFT, i);
            printf("    %s%s\n", name, (i==def_resampler)?" *":"");
        }
    }
    checkALErrors();
}

static void printEFXInfo(ALCdevice *device)
{
    static LPALGENFILTERS palGenFilters;
    static LPALDELETEFILTERS palDeleteFilters;
    static LPALFILTERI palFilteri;
    static LPALGENEFFECTS palGenEffects;
    static LPALDELETEEFFECTS palDeleteEffects;
    static LPALEFFECTI palEffecti;

    static const ALint filters[] = {
        AL_FILTER_LOWPASS, AL_FILTER_HIGHPASS, AL_FILTER_BANDPASS,
        AL_FILTER_NULL
    };
    char filterNames[] = "Low-pass,High-pass,Band-pass,";
    static const ALint effects[] = {
        AL_EFFECT_EAXREVERB, AL_EFFECT_REVERB, AL_EFFECT_CHORUS,
        AL_EFFECT_DISTORTION, AL_EFFECT_ECHO, AL_EFFECT_FLANGER,
        AL_EFFECT_FREQUENCY_SHIFTER, AL_EFFECT_VOCAL_MORPHER,
        AL_EFFECT_PITCH_SHIFTER, AL_EFFECT_RING_MODULATOR,
        AL_EFFECT_AUTOWAH, AL_EFFECT_COMPRESSOR, AL_EFFECT_EQUALIZER,
        AL_EFFECT_NULL
    };
    static const ALint dedeffects[] = {
        AL_EFFECT_DEDICATED_DIALOGUE, AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT,
        AL_EFFECT_NULL
    };
    char effectNames[] = "EAX Reverb,Reverb,Chorus,Distortion,Echo,Flanger,"
        "Frequency Shifter,Vocal Morpher,Pitch Shifter,Ring Modulator,Autowah,"
        "Compressor,Equalizer,Dedicated Dialog,Dedicated LFE,";
    ALCint major, minor, sends;
    ALuint object;
    char *current;
    int i;

    if(alcIsExtensionPresent(device, "ALC_EXT_EFX") == AL_FALSE)
    {
        printf("EFX not available\n");
        return;
    }

    palGenFilters = FUNCTION_CAST(LPALGENFILTERS, alGetProcAddress("alGenFilters"));
    palDeleteFilters = FUNCTION_CAST(LPALDELETEFILTERS, alGetProcAddress("alDeleteFilters"));
    palFilteri = FUNCTION_CAST(LPALFILTERI, alGetProcAddress("alFilteri"));
    palGenEffects = FUNCTION_CAST(LPALGENEFFECTS, alGetProcAddress("alGenEffects"));
    palDeleteEffects = FUNCTION_CAST(LPALDELETEEFFECTS, alGetProcAddress("alDeleteEffects"));
    palEffecti = FUNCTION_CAST(LPALEFFECTI, alGetProcAddress("alEffecti"));

    alcGetIntegerv(device, ALC_EFX_MAJOR_VERSION, 1, &major);
    alcGetIntegerv(device, ALC_EFX_MINOR_VERSION, 1, &minor);
    if(checkALCErrors(device) == ALC_NO_ERROR)
        printf("EFX version: %d.%d\n", major, minor);
    alcGetIntegerv(device, ALC_MAX_AUXILIARY_SENDS, 1, &sends);
    if(checkALCErrors(device) == ALC_NO_ERROR)
        printf("Max auxiliary sends: %d\n", sends);

    palGenFilters(1, &object);
    checkALErrors();

    current = filterNames;
    for(i = 0;filters[i] != AL_FILTER_NULL;i++)
    {
        char *next = strchr(current, ',');
        assert(next != NULL);

        palFilteri(object, AL_FILTER_TYPE, filters[i]);
        if(alGetError() != AL_NO_ERROR)
            memmove(current, next+1, strlen(next));
        else
            current = next+1;
    }
    printf("Supported filters:");
    printList(filterNames, ',');

    palDeleteFilters(1, &object);
    palGenEffects(1, &object);
    checkALErrors();

    current = effectNames;
    for(i = 0;effects[i] != AL_EFFECT_NULL;i++)
    {
        char *next = strchr(current, ',');
        assert(next != NULL);

        palEffecti(object, AL_EFFECT_TYPE, effects[i]);
        if(alGetError() != AL_NO_ERROR)
            memmove(current, next+1, strlen(next));
        else
            current = next+1;
    }
    if(alcIsExtensionPresent(device, "ALC_EXT_DEDICATED"))
    {
        for(i = 0;dedeffects[i] != AL_EFFECT_NULL;i++)
        {
            char *next = strchr(current, ',');
            assert(next != NULL);

            palEffecti(object, AL_EFFECT_TYPE, dedeffects[i]);
            if(alGetError() != AL_NO_ERROR)
                memmove(current, next+1, strlen(next));
            else
                current = next+1;
        }
    }
    else
    {
        for(i = 0;dedeffects[i] != AL_EFFECT_NULL;i++)
        {
            char *next = strchr(current, ',');
            assert(next != NULL);
            memmove(current, next+1, strlen(next));
        }
    }
    printf("Supported effects:");
    printList(effectNames, ',');

    palDeleteEffects(1, &object);
    checkALErrors();
}

int main(int argc, char *argv[])
{
    ALCdevice *device;
    ALCcontext *context;

#ifdef _WIN32
    /* OpenAL Soft gives UTF-8 strings, so set the console to expect that. */
    SetConsoleOutputCP(CP_UTF8);
#endif

    if(argc > 1 && (strcmp(argv[1], "--help") == 0 ||
                    strcmp(argv[1], "-h") == 0))
    {
        printf("Usage: %s [playback device]\n", argv[0]);
        return 0;
    }

    printf("Available playback devices:\n");
    if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
        printDeviceList(alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER));
    else
        printDeviceList(alcGetString(NULL, ALC_DEVICE_SPECIFIER));
    printf("Available capture devices:\n");
    printDeviceList(alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER));

    if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT") != AL_FALSE)
        printf("Default playback device: %s\n",
               alcGetString(NULL, ALC_DEFAULT_ALL_DEVICES_SPECIFIER));
    else
        printf("Default playback device: %s\n",
               alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER));
    printf("Default capture device: %s\n",
           alcGetString(NULL, ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER));

    printALCInfo(NULL);

    device = alcOpenDevice((argc>1) ? argv[1] : NULL);
    if(!device)
    {
        printf("\n!!! Failed to open %s !!!\n\n", ((argc>1) ? argv[1] : "default device"));
        return 1;
    }
    printALCInfo(device);
    printHRTFInfo(device);
    printALC_SOFT_system_event();

    context = alcCreateContext(device, NULL);
    if(!context || alcMakeContextCurrent(context) == ALC_FALSE)
    {
        if(context)
            alcDestroyContext(context);
        alcCloseDevice(device);
        printf("\n!!! Failed to set a context !!!\n\n");
        return 1;
    }

    printModeInfo(device);
    printALInfo();
    printResamplerInfo();
    printEFXInfo(device);

    alcMakeContextCurrent(NULL);
    alcDestroyContext(context);
    alcCloseDevice(device);

    return 0;
}