testmultiaudio.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (C) 1997-2014 Sam Lantinga <[email protected]>
  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. SDL_CreateWindow("testmultiaudio", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 240, 0);
  48. #endif
  49. if (devcount > 64) {
  50. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Too many devices (%d), clamping to 64...\n",
  51. devcount);
  52. devcount = 64;
  53. }
  54. spec.callback = play_through_once;
  55. for (i = 0; i < devcount; i++) {
  56. const char *devname = SDL_GetAudioDeviceName(i, 0);
  57. SDL_Log("playing on device #%d: ('%s')...", i, devname);
  58. fflush(stdout);
  59. SDL_memset(&cbd[0], '\0', sizeof(callback_data));
  60. spec.userdata = &cbd[0];
  61. cbd[0].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  62. if (cbd[0].dev == 0) {
  63. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device failed: %s\n", SDL_GetError());
  64. } else {
  65. SDL_PauseAudioDevice(cbd[0].dev, 0);
  66. while (!cbd[0].done) {
  67. #ifdef __ANDROID__
  68. while (SDL_PollEvent(&event)){}
  69. #endif
  70. SDL_Delay(100);
  71. }
  72. SDL_PauseAudioDevice(cbd[0].dev, 1);
  73. SDL_Log("done.\n");
  74. SDL_CloseAudioDevice(cbd[0].dev);
  75. }
  76. }
  77. SDL_memset(cbd, '\0', sizeof(cbd));
  78. SDL_Log("playing on all devices...\n");
  79. for (i = 0; i < devcount; i++) {
  80. const char *devname = SDL_GetAudioDeviceName(i, 0);
  81. spec.userdata = &cbd[i];
  82. cbd[i].dev = SDL_OpenAudioDevice(devname, 0, &spec, NULL, 0);
  83. if (cbd[i].dev == 0) {
  84. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Open device %d failed: %s\n", i, SDL_GetError());
  85. }
  86. }
  87. for (i = 0; i < devcount; i++) {
  88. if (cbd[i].dev) {
  89. SDL_PauseAudioDevice(cbd[i].dev, 0);
  90. }
  91. }
  92. while (keep_going) {
  93. keep_going = 0;
  94. for (i = 0; i < devcount; i++) {
  95. if ((cbd[i].dev) && (!cbd[i].done)) {
  96. keep_going = 1;
  97. }
  98. }
  99. #ifdef __ANDROID__
  100. while (SDL_PollEvent(&event)){}
  101. #endif
  102. SDL_Delay(100);
  103. }
  104. for (i = 0; i < devcount; i++) {
  105. if (cbd[i].dev) {
  106. SDL_PauseAudioDevice(cbd[i].dev, 1);
  107. SDL_CloseAudioDevice(cbd[i].dev);
  108. }
  109. }
  110. SDL_Log("All done!\n");
  111. }
  112. int
  113. main(int argc, char **argv)
  114. {
  115. int devcount = 0;
  116. /* Enable standard application logging */
  117. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  118. /* Load the SDL library */
  119. if (SDL_Init(SDL_INIT_AUDIO) < 0) {
  120. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  121. return (1);
  122. }
  123. SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
  124. devcount = SDL_GetNumAudioDevices(0);
  125. if (devcount < 1) {
  126. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Don't see any specific audio devices!\n");
  127. } else {
  128. if (argv[1] == NULL) {
  129. argv[1] = "sample.wav";
  130. }
  131. /* Load the wave file into memory */
  132. if (SDL_LoadWAV(argv[1], &spec, &sound, &soundlen) == NULL) {
  133. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1],
  134. SDL_GetError());
  135. } else {
  136. test_multi_audio(devcount);
  137. SDL_FreeWAV(sound);
  138. }
  139. }
  140. SDL_Quit();
  141. return 0;
  142. }