testmultiaudio.c 4.9 KB

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