SDL

Форк
0
/
testaudioinfo.c 
120 строк · 3.9 Кб
1
/*
2
  Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
3

4
  This software is provided 'as-is', without any express or implied
5
  warranty.  In no event will the authors be held liable for any damages
6
  arising from the use of this software.
7

8
  Permission is granted to anyone to use this software for any purpose,
9
  including commercial applications, and to alter it and redistribute it
10
  freely.
11
*/
12
#include <SDL3/SDL.h>
13
#include <SDL3/SDL_main.h>
14
#include <SDL3/SDL_test.h>
15

16
static void
17
print_devices(SDL_bool recording)
18
{
19
    SDL_AudioSpec spec;
20
    const char *typestr = (recording ? "recording" : "playback");
21
    int n = 0;
22
    int frames;
23
    SDL_AudioDeviceID *devices = recording ? SDL_GetAudioRecordingDevices(&n) : SDL_GetAudioPlaybackDevices(&n);
24

25
    if (!devices) {
26
        SDL_Log("  Driver failed to report %s devices: %s\n\n", typestr, SDL_GetError());
27
    } else if (n == 0) {
28
        SDL_Log("  No %s devices found.\n\n", typestr);
29
    } else {
30
        int i;
31
        SDL_Log("Found %d %s device%s:\n", n, typestr, n != 1 ? "s" : "");
32
        for (i = 0; i < n; i++) {
33
            const char *name = SDL_GetAudioDeviceName(devices[i]);
34
            if (name) {
35
                SDL_Log("  %d: %s\n", i, name);
36
            } else {
37
                SDL_Log("  %d Error: %s\n", i, SDL_GetError());
38
            }
39

40
            if (SDL_GetAudioDeviceFormat(devices[i], &spec, &frames) == 0) {
41
                SDL_Log("     Sample Rate: %d\n", spec.freq);
42
                SDL_Log("     Channels: %d\n", spec.channels);
43
                SDL_Log("     SDL_AudioFormat: %X\n", spec.format);
44
                SDL_Log("     Buffer Size: %d frames\n", frames);
45
            }
46
        }
47
        SDL_Log("\n");
48
    }
49
    SDL_free(devices);
50
}
51

52
int main(int argc, char **argv)
53
{
54
    SDL_AudioSpec spec;
55
    int i;
56
    int n;
57
    int frames;
58
    SDLTest_CommonState *state;
59

60
    /* Initialize test framework */
61
    state = SDLTest_CommonCreateState(argv, 0);
62
    if (!state) {
63
        return 1;
64
    }
65

66
    /* Enable standard application logging */
67
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
68

69
    /* Parse commandline */
70
    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
71
        return 1;
72
    }
73

74
    /* Load the SDL library */
75
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
76
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
77
        return 1;
78
    }
79

80
    /* Print available audio drivers */
81
    n = SDL_GetNumAudioDrivers();
82
    if (n == 0) {
83
        SDL_Log("No built-in audio drivers\n\n");
84
    } else {
85
        SDL_Log("Built-in audio drivers:\n");
86
        for (i = 0; i < n; ++i) {
87
            SDL_Log("  %d: %s\n", i, SDL_GetAudioDriver(i));
88
        }
89
        SDL_Log("Select a driver with the SDL_AUDIO_DRIVER environment variable.\n");
90
    }
91

92
    SDL_Log("Using audio driver: %s\n\n", SDL_GetCurrentAudioDriver());
93

94
    print_devices(SDL_FALSE);
95
    print_devices(SDL_TRUE);
96

97
    if (SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, &frames) < 0) {
98
        SDL_Log("Error when calling SDL_GetAudioDeviceFormat(default playback): %s\n", SDL_GetError());
99
    } else {
100
        SDL_Log("Default Playback Device:\n");
101
        SDL_Log("Sample Rate: %d\n", spec.freq);
102
        SDL_Log("Channels: %d\n", spec.channels);
103
        SDL_Log("SDL_AudioFormat: %X\n", spec.format);
104
        SDL_Log("Buffer Size: %d frames\n", frames);
105
    }
106

107
    if (SDL_GetAudioDeviceFormat(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &spec, &frames) < 0) {
108
        SDL_Log("Error when calling SDL_GetAudioDeviceFormat(default recording): %s\n", SDL_GetError());
109
    } else {
110
        SDL_Log("Default Recording Device:\n");
111
        SDL_Log("Sample Rate: %d\n", spec.freq);
112
        SDL_Log("Channels: %d\n", spec.channels);
113
        SDL_Log("SDL_AudioFormat: %X\n", spec.format);
114
        SDL_Log("Buffer Size: %d frames\n", frames);
115
    }
116

117
    SDL_Quit();
118
    SDLTest_CommonDestroyState(state);
119
    return 0;
120
}
121

122

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.