🛠️🐜 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.

205 lines
5.7 KiB

  1. /*
  2. Copyright (C) 1997-2022 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. return "Low Frequency Effects";
  34. case 4:
  35. return "Back Left";
  36. default:
  37. return "Front Center";
  38. }
  39. case 3:
  40. switch (channel_count) {
  41. case 4:
  42. return "Back Right";
  43. case 5:
  44. return "Back Left";
  45. default:
  46. return "Low Frequency Effects";
  47. }
  48. case 4:
  49. switch (channel_count) {
  50. case 5:
  51. return "Back Right";
  52. case 7:
  53. return "Back Center";
  54. case 6:
  55. case 8:
  56. return "Back Left";
  57. }
  58. case 5:
  59. switch (channel_count) {
  60. case 7:
  61. return "Back Left";
  62. case 6:
  63. case 8:
  64. return "Back Right";
  65. }
  66. case 6:
  67. switch (channel_count) {
  68. case 7:
  69. return "Back Right";
  70. case 8:
  71. return "Side Left";
  72. }
  73. case 7:
  74. return "Side Right";
  75. }
  76. return NULL;
  77. }
  78. SDL_bool
  79. is_lfe_channel(int channel_index, int channel_count)
  80. {
  81. return (channel_count == 3 && channel_index == 2) || (channel_count >= 6 && channel_index == 3);
  82. }
  83. void SDLCALL
  84. fill_buffer(void* unused, Uint8* stream, int len)
  85. {
  86. Sint16* buffer = (Sint16*)stream;
  87. int samples = len / sizeof(Sint16);
  88. static int total_samples = 0;
  89. int i;
  90. SDL_memset(stream, 0, len);
  91. /* This can happen for a short time when switching devices */
  92. if (active_channel == total_channels) {
  93. return;
  94. }
  95. /* Play a sine wave on the active channel only */
  96. for (i = active_channel; i < samples; i += total_channels) {
  97. float time = (float)total_samples++ / SAMPLE_RATE_HZ;
  98. int sine_freq = is_lfe_channel(active_channel, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ;
  99. int amplitude;
  100. /* Gradually ramp up and down to avoid audible pops when switching between channels */
  101. if (total_samples < SAMPLE_RATE_HZ) {
  102. amplitude = total_samples * MAX_AMPLITUDE / SAMPLE_RATE_HZ;
  103. } else if (total_samples > (CHANNEL_TEST_TIME_SEC - 1) * SAMPLE_RATE_HZ) {
  104. amplitude = (CHANNEL_TEST_TIME_SEC * SAMPLE_RATE_HZ - total_samples) * MAX_AMPLITUDE / SAMPLE_RATE_HZ;
  105. } else {
  106. amplitude = MAX_AMPLITUDE;
  107. }
  108. buffer[i] = (Sint16)(SDL_sin(6.283185f * sine_freq * time) * amplitude);
  109. /* Reset our state for next callback if this channel test is finished */
  110. if (total_samples == CHANNEL_TEST_TIME_SEC * SAMPLE_RATE_HZ) {
  111. total_samples = 0;
  112. active_channel++;
  113. break;
  114. }
  115. }
  116. }
  117. int
  118. main(int argc, char *argv[])
  119. {
  120. int i;
  121. /* Enable standard application logging */
  122. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  123. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  124. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  125. return 1;
  126. }
  127. /* Show the list of available drivers */
  128. SDL_Log("Available audio drivers:");
  129. for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
  130. SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
  131. }
  132. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  133. for (i = 0; i < SDL_GetNumAudioDevices(0); i++) {
  134. const char *devname = SDL_GetAudioDeviceName(i, 0);
  135. int j;
  136. SDL_AudioSpec spec;
  137. SDL_AudioDeviceID dev;
  138. if (SDL_GetAudioDeviceSpec(i, 0, &spec) != 0) {
  139. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GetAudioSpec() failed: %s\n", SDL_GetError());
  140. continue;
  141. }
  142. spec.freq = SAMPLE_RATE_HZ;
  143. spec.format = AUDIO_S16SYS;
  144. spec.samples = 4096;
  145. spec.callback = fill_buffer;
  146. dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  147. if (dev == 0) {
  148. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_OpenAudioDevice() failed: %s\n", SDL_GetError());
  149. continue;
  150. }
  151. SDL_Log("Testing audio device: %s (%d channels)\n", devname, spec.channels);
  152. /* These are used by the fill_buffer callback */
  153. total_channels = spec.channels;
  154. active_channel = 0;
  155. SDL_PauseAudioDevice(dev, 0);
  156. for (j = 0; j < total_channels; j++) {
  157. int sine_freq = is_lfe_channel(j, total_channels) ? LFE_SINE_FREQ_HZ : SINE_FREQ_HZ;
  158. SDL_Log("Playing %d Hz test tone on channel: %s\n", sine_freq, get_channel_name(j, total_channels));
  159. /* fill_buffer() will increment the active channel */
  160. if (SDL_getenv("SDL_TESTS_QUICK") != NULL) {
  161. SDL_Delay(QUICK_TEST_TIME_MSEC);
  162. } else {
  163. SDL_Delay(CHANNEL_TEST_TIME_SEC * 1000);
  164. }
  165. }
  166. SDL_CloseAudioDevice(dev);
  167. }
  168. SDL_Quit();
  169. return 0;
  170. }