alloopback.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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*4];
  56. ALuint buffer;
  57. ALenum err;
  58. ALuint i;
  59. for(i = 0;i < 44100*4;i++)
  60. data[i] = (ALshort)(sin(i/44100.0 * 1000.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(int argc, char *argv[])
  77. {
  78. PlaybackInfo playback = { NULL, NULL, 0 };
  79. SDL_AudioSpec desired, obtained;
  80. ALuint source, buffer;
  81. ALCint attrs[16];
  82. ALenum state;
  83. (void)argc;
  84. (void)argv;
  85. /* Print out error if extension is missing. */
  86. if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
  87. {
  88. fprintf(stderr, "Error: ALC_SOFT_loopback not supported!\n");
  89. return 1;
  90. }
  91. /* Define a macro to help load the function pointers. */
  92. #define LOAD_PROC(x) ((x) = alcGetProcAddress(NULL, #x))
  93. LOAD_PROC(alcLoopbackOpenDeviceSOFT);
  94. LOAD_PROC(alcIsRenderFormatSupportedSOFT);
  95. LOAD_PROC(alcRenderSamplesSOFT);
  96. #undef LOAD_PROC
  97. if(SDL_Init(SDL_INIT_AUDIO) == -1)
  98. {
  99. fprintf(stderr, "Failed to init SDL audio: %s\n", SDL_GetError());
  100. return 1;
  101. }
  102. /* Set up SDL audio with our requested format and callback. */
  103. desired.channels = 2;
  104. desired.format = AUDIO_S16SYS;
  105. desired.freq = 44100;
  106. desired.padding = 0;
  107. desired.samples = 4096;
  108. desired.callback = RenderSDLSamples;
  109. desired.userdata = &playback;
  110. if(SDL_OpenAudio(&desired, &obtained) != 0)
  111. {
  112. SDL_Quit();
  113. fprintf(stderr, "Failed to open SDL audio: %s\n", SDL_GetError());
  114. return 1;
  115. }
  116. /* Set up our OpenAL attributes based on what we got from SDL. */
  117. attrs[0] = ALC_FORMAT_CHANNELS_SOFT;
  118. if(obtained.channels == 1)
  119. attrs[1] = ALC_MONO_SOFT;
  120. else if(obtained.channels == 2)
  121. attrs[1] = ALC_STEREO_SOFT;
  122. else
  123. {
  124. fprintf(stderr, "Unhandled SDL channel count: %d\n", obtained.channels);
  125. goto error;
  126. }
  127. attrs[2] = ALC_FORMAT_TYPE_SOFT;
  128. if(obtained.format == AUDIO_U8)
  129. attrs[3] = ALC_UNSIGNED_BYTE_SOFT;
  130. else if(obtained.format == AUDIO_S8)
  131. attrs[3] = ALC_BYTE_SOFT;
  132. else if(obtained.format == AUDIO_U16SYS)
  133. attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
  134. else if(obtained.format == AUDIO_S16SYS)
  135. attrs[3] = ALC_SHORT_SOFT;
  136. else
  137. {
  138. fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
  139. goto error;
  140. }
  141. attrs[4] = ALC_FREQUENCY;
  142. attrs[5] = obtained.freq;
  143. attrs[6] = 0; /* end of list */
  144. playback.FrameSize = FramesToBytes(1, attrs[1], attrs[3]);
  145. /* Initialize OpenAL loopback device, using our format attributes. */
  146. playback.Device = alcLoopbackOpenDeviceSOFT(NULL);
  147. if(!playback.Device)
  148. {
  149. fprintf(stderr, "Failed to open loopback device!\n");
  150. goto error;
  151. }
  152. /* Make sure the format is supported before setting them on the device. */
  153. if(alcIsRenderFormatSupportedSOFT(playback.Device, attrs[5], attrs[1], attrs[3]) == ALC_FALSE)
  154. {
  155. fprintf(stderr, "Render format not supported: %s, %s, %dhz\n",
  156. ChannelsName(attrs[1]), TypeName(attrs[3]), attrs[5]);
  157. goto error;
  158. }
  159. playback.Context = alcCreateContext(playback.Device, attrs);
  160. if(!playback.Context || alcMakeContextCurrent(playback.Context) == ALC_FALSE)
  161. {
  162. fprintf(stderr, "Failed to set an OpenAL audio context\n");
  163. goto error;
  164. }
  165. /* Start SDL playing. Our callback (thus alcRenderSamplesSOFT) will now
  166. * start being called regularly to update the AL playback state. */
  167. SDL_PauseAudio(0);
  168. /* Load the sound into a buffer. */
  169. buffer = CreateSineWave();
  170. if(!buffer)
  171. {
  172. SDL_CloseAudio();
  173. alcDestroyContext(playback.Context);
  174. alcCloseDevice(playback.Device);
  175. SDL_Quit();
  176. return 1;
  177. }
  178. /* Create the source to play the sound with. */
  179. source = 0;
  180. alGenSources(1, &source);
  181. alSourcei(source, AL_BUFFER, buffer);
  182. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  183. /* Play the sound until it finishes. */
  184. alSourcePlay(source);
  185. do {
  186. Sleep(10);
  187. alGetSourcei(source, AL_SOURCE_STATE, &state);
  188. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  189. /* All done. Delete resources, and close OpenAL. */
  190. alDeleteSources(1, &source);
  191. alDeleteBuffers(1, &buffer);
  192. /* Stop SDL playing. */
  193. SDL_PauseAudio(1);
  194. /* Close up OpenAL and SDL. */
  195. SDL_CloseAudio();
  196. alcDestroyContext(playback.Context);
  197. alcCloseDevice(playback.Device);
  198. SDL_Quit();
  199. return 0;
  200. error:
  201. SDL_CloseAudio();
  202. if(playback.Context)
  203. alcDestroyContext(playback.Context);
  204. if(playback.Device)
  205. alcCloseDevice(playback.Device);
  206. SDL_Quit();
  207. return 1;
  208. }