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

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