alloopback.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * OpenAL Loopback Example
  3. *
  4. * Copyright (c) 2013 by Chris Robinson <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* This file contains an example for using the loopback device for custom
  25. * output handling.
  26. */
  27. #include <assert.h>
  28. #include <math.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #define SDL_MAIN_HANDLED
  32. #include "SDL.h"
  33. #include "SDL_audio.h"
  34. #include "SDL_error.h"
  35. #include "SDL_stdinc.h"
  36. #include "AL/al.h"
  37. #include "AL/alc.h"
  38. #include "AL/alext.h"
  39. #include "common/alhelpers.h"
  40. #ifndef SDL_AUDIO_MASK_BITSIZE
  41. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  42. #endif
  43. #ifndef SDL_AUDIO_BITSIZE
  44. #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
  45. #endif
  46. #ifndef M_PI
  47. #define M_PI (3.14159265358979323846)
  48. #endif
  49. typedef struct {
  50. ALCdevice *Device;
  51. ALCcontext *Context;
  52. ALCsizei FrameSize;
  53. } PlaybackInfo;
  54. static LPALCLOOPBACKOPENDEVICESOFT alcLoopbackOpenDeviceSOFT;
  55. static LPALCISRENDERFORMATSUPPORTEDSOFT alcIsRenderFormatSupportedSOFT;
  56. static LPALCRENDERSAMPLESSOFT alcRenderSamplesSOFT;
  57. void SDLCALL RenderSDLSamples(void *userdata, Uint8 *stream, int len)
  58. {
  59. PlaybackInfo *playback = (PlaybackInfo*)userdata;
  60. alcRenderSamplesSOFT(playback->Device, stream, len/playback->FrameSize);
  61. }
  62. static const char *ChannelsName(ALCenum chans)
  63. {
  64. switch(chans)
  65. {
  66. case ALC_MONO_SOFT: return "Mono";
  67. case ALC_STEREO_SOFT: return "Stereo";
  68. case ALC_QUAD_SOFT: return "Quadraphonic";
  69. case ALC_5POINT1_SOFT: return "5.1 Surround";
  70. case ALC_6POINT1_SOFT: return "6.1 Surround";
  71. case ALC_7POINT1_SOFT: return "7.1 Surround";
  72. }
  73. return "Unknown Channels";
  74. }
  75. static const char *TypeName(ALCenum type)
  76. {
  77. switch(type)
  78. {
  79. case ALC_BYTE_SOFT: return "S8";
  80. case ALC_UNSIGNED_BYTE_SOFT: return "U8";
  81. case ALC_SHORT_SOFT: return "S16";
  82. case ALC_UNSIGNED_SHORT_SOFT: return "U16";
  83. case ALC_INT_SOFT: return "S32";
  84. case ALC_UNSIGNED_INT_SOFT: return "U32";
  85. case ALC_FLOAT_SOFT: return "Float32";
  86. }
  87. return "Unknown Type";
  88. }
  89. /* Creates a one second buffer containing a sine wave, and returns the new
  90. * buffer ID. */
  91. static ALuint CreateSineWave(void)
  92. {
  93. ALshort data[44100*4];
  94. ALuint buffer;
  95. ALenum err;
  96. ALuint i;
  97. for(i = 0;i < 44100*4;i++)
  98. data[i] = (ALshort)(sin(i/44100.0 * 1000.0 * 2.0*M_PI) * 32767.0);
  99. /* Buffer the audio data into a new buffer object. */
  100. buffer = 0;
  101. alGenBuffers(1, &buffer);
  102. alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(data), 44100);
  103. /* Check if an error occurred, and clean up if so. */
  104. err = alGetError();
  105. if(err != AL_NO_ERROR)
  106. {
  107. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  108. if(alIsBuffer(buffer))
  109. alDeleteBuffers(1, &buffer);
  110. return 0;
  111. }
  112. return buffer;
  113. }
  114. int main(int argc, char *argv[])
  115. {
  116. PlaybackInfo playback = { NULL, NULL, 0 };
  117. SDL_AudioSpec desired, obtained;
  118. ALuint source, buffer;
  119. ALCint attrs[16];
  120. ALenum state;
  121. (void)argc;
  122. (void)argv;
  123. SDL_SetMainReady();
  124. /* Print out error if extension is missing. */
  125. if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
  126. {
  127. fprintf(stderr, "Error: ALC_SOFT_loopback not supported!\n");
  128. return 1;
  129. }
  130. /* Define a macro to help load the function pointers. */
  131. #define LOAD_PROC(T, x) ((x) = FUNCTION_CAST(T, alcGetProcAddress(NULL, #x)))
  132. LOAD_PROC(LPALCLOOPBACKOPENDEVICESOFT, alcLoopbackOpenDeviceSOFT);
  133. LOAD_PROC(LPALCISRENDERFORMATSUPPORTEDSOFT, alcIsRenderFormatSupportedSOFT);
  134. LOAD_PROC(LPALCRENDERSAMPLESSOFT, alcRenderSamplesSOFT);
  135. #undef LOAD_PROC
  136. if(SDL_Init(SDL_INIT_AUDIO) == -1)
  137. {
  138. fprintf(stderr, "Failed to init SDL audio: %s\n", SDL_GetError());
  139. return 1;
  140. }
  141. /* Set up SDL audio with our requested format and callback. */
  142. desired.channels = 2;
  143. desired.format = AUDIO_S16SYS;
  144. desired.freq = 44100;
  145. desired.padding = 0;
  146. desired.samples = 4096;
  147. desired.callback = RenderSDLSamples;
  148. desired.userdata = &playback;
  149. if(SDL_OpenAudio(&desired, &obtained) != 0)
  150. {
  151. SDL_Quit();
  152. fprintf(stderr, "Failed to open SDL audio: %s\n", SDL_GetError());
  153. return 1;
  154. }
  155. /* Set up our OpenAL attributes based on what we got from SDL. */
  156. attrs[0] = ALC_FORMAT_CHANNELS_SOFT;
  157. if(obtained.channels == 1)
  158. attrs[1] = ALC_MONO_SOFT;
  159. else if(obtained.channels == 2)
  160. attrs[1] = ALC_STEREO_SOFT;
  161. else
  162. {
  163. fprintf(stderr, "Unhandled SDL channel count: %d\n", obtained.channels);
  164. goto error;
  165. }
  166. attrs[2] = ALC_FORMAT_TYPE_SOFT;
  167. if(obtained.format == AUDIO_U8)
  168. attrs[3] = ALC_UNSIGNED_BYTE_SOFT;
  169. else if(obtained.format == AUDIO_S8)
  170. attrs[3] = ALC_BYTE_SOFT;
  171. else if(obtained.format == AUDIO_U16SYS)
  172. attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
  173. else if(obtained.format == AUDIO_S16SYS)
  174. attrs[3] = ALC_SHORT_SOFT;
  175. else if(obtained.format == AUDIO_S32SYS)
  176. attrs[3] = ALC_INT_SOFT;
  177. else if(obtained.format == AUDIO_F32SYS)
  178. attrs[3] = ALC_FLOAT_SOFT;
  179. else
  180. {
  181. fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
  182. goto error;
  183. }
  184. attrs[4] = ALC_FREQUENCY;
  185. attrs[5] = obtained.freq;
  186. attrs[6] = 0; /* end of list */
  187. playback.FrameSize = obtained.channels * SDL_AUDIO_BITSIZE(obtained.format) / 8;
  188. /* Initialize OpenAL loopback device, using our format attributes. */
  189. playback.Device = alcLoopbackOpenDeviceSOFT(NULL);
  190. if(!playback.Device)
  191. {
  192. fprintf(stderr, "Failed to open loopback device!\n");
  193. goto error;
  194. }
  195. /* Make sure the format is supported before setting them on the device. */
  196. if(alcIsRenderFormatSupportedSOFT(playback.Device, attrs[5], attrs[1], attrs[3]) == ALC_FALSE)
  197. {
  198. fprintf(stderr, "Render format not supported: %s, %s, %dhz\n",
  199. ChannelsName(attrs[1]), TypeName(attrs[3]), attrs[5]);
  200. goto error;
  201. }
  202. playback.Context = alcCreateContext(playback.Device, attrs);
  203. if(!playback.Context || alcMakeContextCurrent(playback.Context) == ALC_FALSE)
  204. {
  205. fprintf(stderr, "Failed to set an OpenAL audio context\n");
  206. goto error;
  207. }
  208. /* Start SDL playing. Our callback (thus alcRenderSamplesSOFT) will now
  209. * start being called regularly to update the AL playback state. */
  210. SDL_PauseAudio(0);
  211. /* Load the sound into a buffer. */
  212. buffer = CreateSineWave();
  213. if(!buffer)
  214. {
  215. SDL_CloseAudio();
  216. alcDestroyContext(playback.Context);
  217. alcCloseDevice(playback.Device);
  218. SDL_Quit();
  219. return 1;
  220. }
  221. /* Create the source to play the sound with. */
  222. source = 0;
  223. alGenSources(1, &source);
  224. alSourcei(source, AL_BUFFER, (ALint)buffer);
  225. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  226. /* Play the sound until it finishes. */
  227. alSourcePlay(source);
  228. do {
  229. al_nssleep(10000000);
  230. alGetSourcei(source, AL_SOURCE_STATE, &state);
  231. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  232. /* All done. Delete resources, and close OpenAL. */
  233. alDeleteSources(1, &source);
  234. alDeleteBuffers(1, &buffer);
  235. /* Stop SDL playing. */
  236. SDL_PauseAudio(1);
  237. /* Close up OpenAL and SDL. */
  238. SDL_CloseAudio();
  239. alcDestroyContext(playback.Context);
  240. alcCloseDevice(playback.Device);
  241. SDL_Quit();
  242. return 0;
  243. error:
  244. SDL_CloseAudio();
  245. if(playback.Context)
  246. alcDestroyContext(playback.Context);
  247. if(playback.Device)
  248. alcCloseDevice(playback.Device);
  249. SDL_Quit();
  250. return 1;
  251. }