testmultiaudio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright (C) 1997-2014 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. #include "SDL.h"
  11. static SDL_AudioSpec spec;
  12. static Uint8 *sound = NULL; /* Pointer to wave data */
  13. static Uint32 soundlen = 0; /* Length of wave data */
  14. typedef struct
  15. {
  16. SDL_AudioDeviceID dev;
  17. int soundpos;
  18. volatile int done;
  19. } callback_data;
  20. void SDLCALL
  21. play_through_once(void *arg, Uint8 * stream, int len)
  22. {
  23. callback_data *cbd = (callback_data *) arg;
  24. Uint8 *waveptr = sound + cbd->soundpos;
  25. int waveleft = soundlen - cbd->soundpos;
  26. int cpy = len;
  27. if (cpy > waveleft)
  28. cpy = waveleft;
  29. SDL_memcpy(stream, waveptr, cpy);
  30. len -= cpy;
  31. cbd->soundpos += cpy;
  32. if (len > 0) {
  33. stream += cpy;
  34. SDL_memset(stream, spec.silence, len);
  35. cbd->done++;
  36. }
  37. }
  38. static void
  39. test_multi_audio(int devcount)
  40. {
  41. callback_data cbd[64];
  42. int keep_going = 1;
  43. int i;
  44. if (devcount > 64) {
  45. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n",
  46. devcount);
  47. devcount = 64;
  48. }
  49. spec.callback = play_through_once;
  50. for (i = 0; i < devcount; i++) {
  51. const char *devname = SDL_GetAudioDeviceName(i, 0);
  52. SDL_Log("playing on device #%d: ('%s')...", i, devname);
  53. fflush(stdout);
  54. SDL_memset(&cbd[0], '\0', sizeof(callback_data));
  55. spec.userdata = &cbd[0];
  56. cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  57. if (cbd[0].dev == 0) {
  58. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device failed: %s\n", SDL_GetError());
  59. } else {
  60. SDL_PauseAudioDevice(cbd[0].dev, 0);
  61. while (!cbd[0].done)
  62. SDL_Delay(100);
  63. SDL_PauseAudioDevice(cbd[0].dev, 1);
  64. SDL_Log("done.\n");
  65. SDL_CloseAudioDevice(cbd[0].dev);
  66. }
  67. }
  68. SDL_memset(cbd, '\0', sizeof(cbd));
  69. SDL_Log("playing on all devices...\n");
  70. for (i = 0; i < devcount; i++) {
  71. const char *devname = SDL_GetAudioDeviceName(i, 0);
  72. spec.userdata = &cbd[i];
  73. cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  74. if (cbd[i].dev == 0) {
  75. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device %d failed: %s\n", i, SDL_GetError());
  76. }
  77. }
  78. for (i = 0; i < devcount; i++) {
  79. if (cbd[i].dev) {
  80. SDL_PauseAudioDevice(cbd[i].dev, 0);
  81. }
  82. }
  83. while (keep_going) {
  84. keep_going = 0;
  85. for (i = 0; i < devcount; i++) {
  86. if ((cbd[i].dev) && (!cbd[i].done)) {
  87. keep_going = 1;
  88. }
  89. }
  90. SDL_Delay(100);
  91. }
  92. for (i = 0; i < devcount; i++) {
  93. if (cbd[i].dev) {
  94. SDL_PauseAudioDevice(cbd[i].dev, 1);
  95. SDL_CloseAudioDevice(cbd[i].dev);
  96. }
  97. }
  98. SDL_Log("All done!\n");
  99. }
  100. int
  101. main(int argc, char **argv)
  102. {
  103. int devcount = 0;
  104. /* Enable standard application logging */
  105. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  106. /* Load the SDL library */
  107. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  108. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  109. return (1);
  110. }
  111. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  112. devcount = SDL_GetNumAudioDevices(0);
  113. if (devcount < 1) {
  114. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!\n");
  115. } else {
  116. if (argv[1] == NULL) {
  117. argv[1] = "sample.wav";
  118. }
  119. /* Load the wave file into memory */
  120. if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
  121. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1],
  122. SDL_GetError());
  123. } else {
  124. test_multi_audio(devcount);
  125. SDL_FreeWAV(sound);
  126. }
  127. }
  128. SDL_Quit();
  129. return 0;
  130. }