🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
5.9 KiB

  1. /*
  2. Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Program to test surround sound audio channels */
  11. #include "SDL_config.h"
  12. #include "SDL.h"
  13. static int total_channels;
  14. static int active_channel;
  15. #define SAMPLE_RATE_HZ 48000
  16. #define QUICK_TEST_TIME_MSEC 100
  17. #define CHANNEL_TEST_TIME_SEC 5
  18. #define MAX_AMPLITUDE SDL_MAX_SINT16
  19. #define SINE_FREQ_HZ 500
  20. #define LFE_SINE_FREQ_HZ 50
  21. /* The channel layout is defined in SDL_audio.h */
  22. const char *
  23. get_channel_name(int channel_index, int channel_count)
  24. {
  25. switch (channel_index) {
  26. case 0:
  27. return "Front Left";
  28. case 1:
  29. return "Front Right";
  30. case 2:
  31. switch (channel_count) {
  32. case 3:
  33. case 5:
  34. return "Low Frequency Effects";
  35. case 4:
  36. return "Back Left";
  37. default:
  38. return "Front Center";
  39. }
  40. case 3:
  41. switch (channel_count) {
  42. case 4:
  43. return "Back Right";
  44. case 5:
  45. return "Back Left";
  46. default:
  47. return "Low Frequency Effects";
  48. }
  49. case 4:
  50. switch (channel_count) {
  51. case 5:
  52. return "Back Right";
  53. case 6:
  54. return "Side Left";
  55. case 7:
  56. return "Back Center";
  57. case 8:
  58. return "Back Left";
  59. }
  60. SDL_assert(0);
  61. case 5:
  62. switch (channel_count) {
  63. case 6:
  64. return "Side Right";
  65. case 7:
  66. return "Side Left";
  67. case 8:
  68. return "Back Right";
  69. }
  70. SDL_assert(0);
  71. case 6:
  72. switch (channel_count) {
  73. case 7:
  74. return "Side Right";
  75. case 8:
  76. return "Side Left";
  77. }
  78. SDL_assert(0);
  79. case 7:
  80. return "Side Right";
  81. }
  82. return NULL;
  83. }
  84. SDL_bool
  85. is_lfe_channel(int channel_index, int channel_count)
  86. {
  87. return (channel_count == 3 && channel_index == 2) || (channel_count >= 6 && channel_index == 3);
  88. }
  89. void SDLCALL
  90. fill_buffer(void *unused, Uint8 *stream, int len)
  91. {
  92. Sint16 *buffer = (Sint16 *)stream;
  93. int samples = len / sizeof(Sint16);
  94. static int total_samples = 0;
  95. int i;
  96. SDL_memset(stream, 0, len);
  97. /* This can happen for a short time when switching devices */
  98. if (active_channel == total_channels) {
  99. return;
  100. }
  101. /* Play a sine wave on the active channel only */
  102. for (i = active_channel; i < samples; i += total_channels) {
  103. float time = (float)total_samples++ / SAMPLE_RATE_HZ;
  104. int sine_freq = is_lfe_channel(active_channel, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ;
  105. int amplitude;
  106. /* Gradually ramp up and down to avoid audible pops when switching between channels */
  107. if (total_samples < SAMPLE_RATE_HZ) {
  108. amplitude = total_samples * MAX_AMPLITUDE / SAMPLE_RATE_HZ;
  109. } else if (total_samples > (CHANNEL_TEST_TIME_SEC - 1) * SAMPLE_RATE_HZ) {
  110. amplitude = (CHANNEL_TEST_TIME_SEC * SAMPLE_RATE_HZ - total_samples) * MAX_AMPLITUDE / SAMPLE_RATE_HZ;
  111. } else {
  112. amplitude = MAX_AMPLITUDE;
  113. }
  114. buffer[i] = (Sint16)(SDL_sin(6.283185f * sine_freq * time) * amplitude);
  115. /* Reset our state for next callback if this channel test is finished */
  116. if (total_samples == CHANNEL_TEST_TIME_SEC * SAMPLE_RATE_HZ) {
  117. total_samples = 0;
  118. active_channel++;
  119. break;
  120. }
  121. }
  122. }
  123. int main(int argc, char *argv[])
  124. {
  125. int i;
  126. /* Enable standard application logging */
  127. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  128. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  129. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  130. return 1;
  131. }
  132. /* Show the list of available drivers */
  133. SDL_Log("Available audio drivers:");
  134. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  135. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  136. }
  137. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  138. for (i = 0; i < SDL_GetNumAudioDevices(0); i++) {
  139. const char *devname = SDL_GetAudioDeviceName(i, 0);
  140. int j;
  141. SDL_AudioSpec spec;
  142. SDL_AudioDeviceID dev;
  143. if (SDL_GetAudioDeviceSpec(i, 0, &spec) != 0) {
  144. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GetAudioSpec() failed: %s\n", SDL_GetError());
  145. continue;
  146. }
  147. spec.freq = SAMPLE_RATE_HZ;
  148. spec.format = AUDIO_S16SYS;
  149. spec.samples = 4096;
  150. spec.callback = fill_buffer;
  151. dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  152. if (dev == 0) {
  153. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenAudioDevice() failed: %s\n", SDL_GetError());
  154. continue;
  155. }
  156. SDL_Log("Testing audio device: %s (%d channels)\n", devname, spec.channels);
  157. /* These are used by the fill_buffer callback */
  158. total_channels = spec.channels;
  159. active_channel = 0;
  160. SDL_PauseAudioDevice(dev, 0);
  161. for (j = 0; j < total_channels; j++) {
  162. int sine_freq = is_lfe_channel(j, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ;
  163. SDL_Log("Playing %d Hz test tone on channel: %s\n", sine_freq, get_channel_name(j, total_channels));
  164. /* fill_buffer() will increment the active channel */
  165. if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
  166. SDL_Delay(QUICK_TEST_TIME_MSEC);
  167. } else {
  168. SDL_Delay(CHANNEL_TEST_TIME_SEC * 1000);
  169. }
  170. }
  171. SDL_CloseAudioDevice(dev);
  172. }
  173. SDL_Quit();
  174. return 0;
  175. }