| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 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 <stdio.h> |
| 13 |
#include "SDL.h" |
| 14 |
#include "SDL_test.h" |
| 15 |
|
| 16 |
static void |
| 17 |
print_devices(int iscapture) |
| 18 |
{ |
| 19 |
SDL_AudioSpec spec; |
| 20 |
const char *typestr = ((iscapture) ? "capture" : "output"); |
| 21 |
int n = SDL_GetNumAudioDevices(iscapture); |
| 22 |
|
| 23 |
SDL_Log("Found %d %s device%s:\n", n, typestr, n != 1 ? "s" : ""); |
| 24 |
|
| 25 |
if (n == -1) { |
| 26 |
SDL_Log(" Driver can't detect specific %s devices.\n\n", typestr); |
| 27 |
} else if (n == 0) { |
| 28 |
SDL_Log(" No %s devices found.\n\n", typestr); |
| 29 |
} else { |
| 30 |
int i; |
| 31 |
for (i = 0; i < n; i++) { |
| 32 |
const char *name = SDL_GetAudioDeviceName(i, iscapture); |
| 33 |
if (name) { |
| 34 |
SDL_Log(" %d: %s\n", i, name); |
| 35 |
} else { |
| 36 |
SDL_Log(" %d Error: %s\n", i, SDL_GetError()); |
| 37 |
} |
| 38 |
|
| 39 |
if (SDL_GetAudioDeviceSpec(i, iscapture, &spec) == 0) { |
| 40 |
SDL_Log(" Sample Rate: %d\n", spec.freq); |
| 41 |
SDL_Log(" Channels: %d\n", spec.channels); |
| 42 |
SDL_Log(" SDL_AudioFormat: %X\n", spec.format); |
| 43 |
} |
| 44 |
} |
| 45 |
SDL_Log("\n"); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
int main(int argc, char **argv) |
| 50 |
{ |
| 51 |
char *deviceName = NULL; |
| 52 |
SDL_AudioSpec spec; |
| 53 |
int n; |
| 54 |
SDLTest_CommonState *state; |
| 55 |
|
| 56 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_AUDIO); |
| 57 |
if (!state) { |
| 58 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLTest_CommonCreateState failed: %s\n", SDL_GetError()); |
| 59 |
return 1; |
| 60 |
} |
| 61 |
|
| 62 |
/* Enable standard application logging */ |
| 63 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 64 |
|
| 65 |
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { |
| 66 |
return 1; |
| 67 |
} |
| 68 |
|
| 69 |
if (!SDLTest_CommonInit(state)) { |
| 70 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 71 |
return 1; |
| 72 |
} |
| 73 |
|
| 74 |
/* Print available audio drivers */ |
| 75 |
n = SDL_GetNumAudioDrivers(); |
| 76 |
if (n == 0) { |
| 77 |
SDL_Log("No built-in audio drivers\n\n"); |
| 78 |
} else { |
| 79 |
int i; |
| 80 |
SDL_Log("Built-in audio drivers:\n"); |
| 81 |
for (i = 0; i < n; ++i) { |
| 82 |
SDL_Log(" %d: %s\n", i, SDL_GetAudioDriver(i)); |
| 83 |
} |
| 84 |
SDL_Log("Select a driver with the SDL_AUDIODRIVER environment variable.\n"); |
| 85 |
} |
| 86 |
|
| 87 |
SDL_Log("Using audio driver: %s\n\n", SDL_GetCurrentAudioDriver()); |
| 88 |
|
| 89 |
print_devices(0); |
| 90 |
print_devices(1); |
| 91 |
|
| 92 |
if (SDL_GetDefaultAudioInfo(&deviceName, &spec, 0) < 0) { |
| 93 |
SDL_Log("Error when calling SDL_GetDefaultAudioInfo: %s\n", SDL_GetError()); |
| 94 |
} else { |
| 95 |
SDL_Log("Default Output Name: %s\n", deviceName ? deviceName : "unknown"); |
| 96 |
SDL_free(deviceName); |
| 97 |
SDL_Log("Sample Rate: %d\n", spec.freq); |
| 98 |
SDL_Log("Channels: %d\n", spec.channels); |
| 99 |
SDL_Log("SDL_AudioFormat: %X\n", spec.format); |
| 100 |
} |
| 101 |
|
| 102 |
if (SDL_GetDefaultAudioInfo(&deviceName, &spec, 1) < 0) { |
| 103 |
SDL_Log("Error when calling SDL_GetDefaultAudioInfo: %s\n", SDL_GetError()); |
| 104 |
} else { |
| 105 |
SDL_Log("Default Capture Name: %s\n", deviceName ? deviceName : "unknown"); |
| 106 |
SDL_free(deviceName); |
| 107 |
SDL_Log("Sample Rate: %d\n", spec.freq); |
| 108 |
SDL_Log("Channels: %d\n", spec.channels); |
| 109 |
SDL_Log("SDL_AudioFormat: %X\n", spec.format); |
| 110 |
} |
| 111 |
|
| 112 |
SDLTest_CommonQuit(state); |
| 113 |
return 0; |
| 114 |
} |