alloopback.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <stdio.h>
  28. #include <assert.h>
  29. #include <math.h>
  30. #include <SDL.h>
  31. #include "AL/al.h"
  32. #include "AL/alc.h"
  33. #include "AL/alext.h"
  34. #include "common/alhelpers.h"
  35. #ifndef M_PI
  36. #define M_PI (3.14159265358979323846)
  37. #endif
  38. typedef struct {
  39. ALCdevice *Device;
  40. ALCcontext *Context;
  41. ALCsizei FrameSize;
  42. } PlaybackInfo;
  43. static LPALCLOOPBACKOPENDEVICESOFT alcLoopbackOpenDeviceSOFT;
  44. static LPALCISRENDERFORMATSUPPORTEDSOFT alcIsRenderFormatSupportedSOFT;
  45. static LPALCRENDERSAMPLESSOFT alcRenderSamplesSOFT;
  46. void SDLCALL RenderSDLSamples(void *userdata, Uint8 *stream, int len)
  47. {
  48. PlaybackInfo *playback = (PlaybackInfo*)userdata;
  49. alcRenderSamplesSOFT(playback->Device, stream, len/playback->FrameSize);
  50. }
  51. /* Creates a one second buffer containing a sine wave, and returns the new
  52. * buffer ID. */
  53. static ALuint CreateSineWave(void)
  54. {
  55. ALshort data[44100];
  56. ALuint buffer;
  57. ALenum err;
  58. ALuint i;
  59. for(i = 0;i < 44100;i++)
  60. data[i] = (ALshort)(sin(i * 441.0 / 44100.0 * 2.0*M_PI)*32767.0);
  61. /* Buffer the audio data into a new buffer object. */
  62. buffer = 0;
  63. alGenBuffers(1, &buffer);
  64. alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(data), 44100);
  65. /* Check if an error occured, and clean up if so. */
  66. err = alGetError();
  67. if(err != AL_NO_ERROR)
  68. {
  69. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  70. if(alIsBuffer(buffer))
  71. alDeleteBuffers(1, &buffer);
  72. return 0;
  73. }
  74. return buffer;
  75. }
  76. int main()
  77. {
  78. PlaybackInfo playback = { NULL, NULL, 0 };
  79. SDL_AudioSpec desired, obtained;
  80. ALuint source, buffer;
  81. ALCint attrs[16];
  82. ALenum state;
  83. /* Print out error if extension is missing. */
  84. if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
  85. {
  86. fprintf(stderr, "Error: ALC_SOFT_loopback not supported!\n");
  87. return 1;
  88. }
  89. /* Define a macro to help load the function pointers. */
  90. #define LOAD_PROC(x) ((x) = alcGetProcAddress(NULL, #x))
  91. LOAD_PROC(alcLoopbackOpenDeviceSOFT);
  92. LOAD_PROC(alcIsRenderFormatSupportedSOFT);
  93. LOAD_PROC(alcRenderSamplesSOFT);
  94. #undef LOAD_PROC
  95. if(SDL_Init(SDL_INIT_AUDIO) == -1)
  96. {
  97. fprintf(stderr, "Failed to init SDL audio: %s\n", SDL_GetError());
  98. return 1;
  99. }
  100. /* Set up SDL audio with our requested format and callback. */
  101. desired.channels = 2;
  102. desired.format = AUDIO_S16SYS;
  103. desired.freq = 44100;
  104. desired.padding = 0;
  105. desired.samples = 4096;
  106. desired.callback = RenderSDLSamples;
  107. desired.userdata = &playback;
  108. if(SDL_OpenAudio(&desired, &obtained) != 0)
  109. {
  110. SDL_Quit();
  111. fprintf(stderr, "Failed to open SDL audio: %s\n", SDL_GetError());
  112. return 1;
  113. }
  114. /* Set up our OpenAL attributes based on what we got from SDL. */
  115. attrs[0] = ALC_FORMAT_CHANNELS_SOFT;
  116. if(obtained.channels == 1)
  117. attrs[1] = ALC_MONO_SOFT;
  118. else if(obtained.channels == 2)
  119. attrs[1] = ALC_STEREO_SOFT;
  120. else
  121. {
  122. fprintf(stderr, "Unhandled SDL channel count: %d\n", obtained.channels);
  123. goto error;
  124. }
  125. attrs[2] = ALC_FORMAT_TYPE_SOFT;
  126. if(obtained.format == AUDIO_U8)
  127. attrs[3] = ALC_UNSIGNED_BYTE_SOFT;
  128. else if(obtained.format == AUDIO_S8)
  129. attrs[3] = ALC_BYTE_SOFT;
  130. else if(obtained.format == AUDIO_U16SYS)
  131. attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
  132. else if(obtained.format == AUDIO_S16SYS)
  133. attrs[3] = ALC_SHORT_SOFT;
  134. else
  135. {
  136. fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
  137. goto error;
  138. }
  139. attrs[4] = ALC_FREQUENCY;
  140. attrs[5] = obtained.freq;
  141. attrs[6] = 0; /* end of list */
  142. /* Initialize OpenAL loopback device, using our format attributes. */
  143. playback.Device = alcLoopbackOpenDeviceSOFT(NULL);
  144. if(!playback.Device)
  145. {
  146. fprintf(stderr, "Failed to open loopback device!\n");
  147. goto error;
  148. }
  149. /* Make sure the format is supported before setting them on the device. */
  150. if(alcIsRenderFormatSupportedSOFT(playback.Device, attrs[5], attrs[1], attrs[3]) == ALC_FALSE)
  151. {
  152. fprintf(stderr, "Render format not supported: %s, %s, %dhz\n",
  153. ChannelsName(attrs[1]), TypeName(attrs[3]), attrs[5]);
  154. goto error;
  155. }
  156. playback.Context = alcCreateContext(playback.Device, attrs);
  157. if(!playback.Context || alcMakeContextCurrent(playback.Context) == ALC_FALSE)
  158. {
  159. fprintf(stderr, "Failed to set an OpenAL audio context\n");
  160. goto error;
  161. }
  162. playback.FrameSize = FramesToBytes(1, attrs[1], attrs[3]);
  163. /* Start SDL playing. Our callback (thus alcRenderSamplesSOFT) will now
  164. * start being called regularly to update the AL playback state. */
  165. SDL_PauseAudio(0);
  166. /* Load the sound into a buffer. */
  167. buffer = CreateSineWave();
  168. if(!buffer)
  169. {
  170. SDL_CloseAudio();
  171. alcDestroyContext(playback.Context);
  172. alcCloseDevice(playback.Device);
  173. SDL_Quit();
  174. return 1;
  175. }
  176. /* Create the source to play the sound with. */
  177. source = 0;
  178. alGenSources(1, &source);
  179. alSourcei(source, AL_BUFFER, buffer);
  180. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  181. /* Play the sound until it finishes. */
  182. alSourcePlay(source);
  183. do {
  184. Sleep(10);
  185. alGetSourcei(source, AL_SOURCE_STATE, &state);
  186. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  187. /* All done. Delete resources, and close OpenAL. */
  188. alDeleteSources(1, &source);
  189. alDeleteBuffers(1, &buffer);
  190. /* Stop SDL playing. */
  191. SDL_PauseAudio(1);
  192. /* Close up OpenAL and SDL. */
  193. SDL_CloseAudio();
  194. alcDestroyContext(playback.Context);
  195. alcCloseDevice(playback.Device);
  196. SDL_Quit();
  197. return 0;
  198. error:
  199. SDL_CloseAudio();
  200. if(playback.Context)
  201. alcDestroyContext(playback.Context);
  202. if(playback.Device)
  203. alcCloseDevice(playback.Device);
  204. SDL_Quit();
  205. return 1;
  206. }