🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
8.1 KiB

  1. /*
  2. * OpenAL Loopback Example
  3. *
  4. * Copyright (c) 2013 by Chris Robinson <chris.kcat@gmail.com>
  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 SDL_AUDIO_MASK_BITSIZE
  36. #define SDL_AUDIO_MASK_BITSIZE (0xFF)
  37. #endif
  38. #ifndef SDL_AUDIO_BITSIZE
  39. #define SDL_AUDIO_BITSIZE(x) (x & SDL_AUDIO_MASK_BITSIZE)
  40. #endif
  41. #ifndef M_PI
  42. #define M_PI (3.14159265358979323846)
  43. #endif
  44. typedef struct {
  45. ALCdevice *Device;
  46. ALCcontext *Context;
  47. ALCsizei FrameSize;
  48. } PlaybackInfo;
  49. static LPALCLOOPBACKOPENDEVICESOFT alcLoopbackOpenDeviceSOFT;
  50. static LPALCISRENDERFORMATSUPPORTEDSOFT alcIsRenderFormatSupportedSOFT;
  51. static LPALCRENDERSAMPLESSOFT alcRenderSamplesSOFT;
  52. void SDLCALL RenderSDLSamples(void *userdata, Uint8 *stream, int len)
  53. {
  54. PlaybackInfo *playback = (PlaybackInfo*)userdata;
  55. alcRenderSamplesSOFT(playback->Device, stream, len/playback->FrameSize);
  56. }
  57. static const char *ChannelsName(ALCenum chans)
  58. {
  59. switch(chans)
  60. {
  61. case ALC_MONO_SOFT: return "Mono";
  62. case ALC_STEREO_SOFT: return "Stereo";
  63. case ALC_QUAD_SOFT: return "Quadraphonic";
  64. case ALC_5POINT1_SOFT: return "5.1 Surround";
  65. case ALC_6POINT1_SOFT: return "6.1 Surround";
  66. case ALC_7POINT1_SOFT: return "7.1 Surround";
  67. }
  68. return "Unknown Channels";
  69. }
  70. static const char *TypeName(ALCenum type)
  71. {
  72. switch(type)
  73. {
  74. case ALC_BYTE_SOFT: return "S8";
  75. case ALC_UNSIGNED_BYTE_SOFT: return "U8";
  76. case ALC_SHORT_SOFT: return "S16";
  77. case ALC_UNSIGNED_SHORT_SOFT: return "U16";
  78. case ALC_INT_SOFT: return "S32";
  79. case ALC_UNSIGNED_INT_SOFT: return "U32";
  80. case ALC_FLOAT_SOFT: return "Float32";
  81. }
  82. return "Unknown Type";
  83. }
  84. /* Creates a one second buffer containing a sine wave, and returns the new
  85. * buffer ID. */
  86. static ALuint CreateSineWave(void)
  87. {
  88. ALshort data[44100*4];
  89. ALuint buffer;
  90. ALenum err;
  91. ALuint i;
  92. for(i = 0;i < 44100*4;i++)
  93. data[i] = (ALshort)(sin(i/44100.0 * 1000.0 * 2.0*M_PI) * 32767.0);
  94. /* Buffer the audio data into a new buffer object. */
  95. buffer = 0;
  96. alGenBuffers(1, &buffer);
  97. alBufferData(buffer, AL_FORMAT_MONO16, data, sizeof(data), 44100);
  98. /* Check if an error occured, and clean up if so. */
  99. err = alGetError();
  100. if(err != AL_NO_ERROR)
  101. {
  102. fprintf(stderr, "OpenAL Error: %s\n", alGetString(err));
  103. if(alIsBuffer(buffer))
  104. alDeleteBuffers(1, &buffer);
  105. return 0;
  106. }
  107. return buffer;
  108. }
  109. int main(int argc, char *argv[])
  110. {
  111. PlaybackInfo playback = { NULL, NULL, 0 };
  112. SDL_AudioSpec desired, obtained;
  113. ALuint source, buffer;
  114. ALCint attrs[16];
  115. ALenum state;
  116. (void)argc;
  117. (void)argv;
  118. /* Print out error if extension is missing. */
  119. if(!alcIsExtensionPresent(NULL, "ALC_SOFT_loopback"))
  120. {
  121. fprintf(stderr, "Error: ALC_SOFT_loopback not supported!\n");
  122. return 1;
  123. }
  124. /* Define a macro to help load the function pointers. */
  125. #define LOAD_PROC(x) ((x) = alcGetProcAddress(NULL, #x))
  126. LOAD_PROC(alcLoopbackOpenDeviceSOFT);
  127. LOAD_PROC(alcIsRenderFormatSupportedSOFT);
  128. LOAD_PROC(alcRenderSamplesSOFT);
  129. #undef LOAD_PROC
  130. if(SDL_Init(SDL_INIT_AUDIO) == -1)
  131. {
  132. fprintf(stderr, "Failed to init SDL audio: %s\n", SDL_GetError());
  133. return 1;
  134. }
  135. /* Set up SDL audio with our requested format and callback. */
  136. desired.channels = 2;
  137. desired.format = AUDIO_S16SYS;
  138. desired.freq = 44100;
  139. desired.padding = 0;
  140. desired.samples = 4096;
  141. desired.callback = RenderSDLSamples;
  142. desired.userdata = &playback;
  143. if(SDL_OpenAudio(&desired, &obtained) != 0)
  144. {
  145. SDL_Quit();
  146. fprintf(stderr, "Failed to open SDL audio: %s\n", SDL_GetError());
  147. return 1;
  148. }
  149. /* Set up our OpenAL attributes based on what we got from SDL. */
  150. attrs[0] = ALC_FORMAT_CHANNELS_SOFT;
  151. if(obtained.channels == 1)
  152. attrs[1] = ALC_MONO_SOFT;
  153. else if(obtained.channels == 2)
  154. attrs[1] = ALC_STEREO_SOFT;
  155. else
  156. {
  157. fprintf(stderr, "Unhandled SDL channel count: %d\n", obtained.channels);
  158. goto error;
  159. }
  160. attrs[2] = ALC_FORMAT_TYPE_SOFT;
  161. if(obtained.format == AUDIO_U8)
  162. attrs[3] = ALC_UNSIGNED_BYTE_SOFT;
  163. else if(obtained.format == AUDIO_S8)
  164. attrs[3] = ALC_BYTE_SOFT;
  165. else if(obtained.format == AUDIO_U16SYS)
  166. attrs[3] = ALC_UNSIGNED_SHORT_SOFT;
  167. else if(obtained.format == AUDIO_S16SYS)
  168. attrs[3] = ALC_SHORT_SOFT;
  169. else
  170. {
  171. fprintf(stderr, "Unhandled SDL format: 0x%04x\n", obtained.format);
  172. goto error;
  173. }
  174. attrs[4] = ALC_FREQUENCY;
  175. attrs[5] = obtained.freq;
  176. attrs[6] = 0; /* end of list */
  177. playback.FrameSize = obtained.channels * SDL_AUDIO_BITSIZE(obtained.format) / 8;
  178. /* Initialize OpenAL loopback device, using our format attributes. */
  179. playback.Device = alcLoopbackOpenDeviceSOFT(NULL);
  180. if(!playback.Device)
  181. {
  182. fprintf(stderr, "Failed to open loopback device!\n");
  183. goto error;
  184. }
  185. /* Make sure the format is supported before setting them on the device. */
  186. if(alcIsRenderFormatSupportedSOFT(playback.Device, attrs[5], attrs[1], attrs[3]) == ALC_FALSE)
  187. {
  188. fprintf(stderr, "Render format not supported: %s, %s, %dhz\n",
  189. ChannelsName(attrs[1]), TypeName(attrs[3]), attrs[5]);
  190. goto error;
  191. }
  192. playback.Context = alcCreateContext(playback.Device, attrs);
  193. if(!playback.Context || alcMakeContextCurrent(playback.Context) == ALC_FALSE)
  194. {
  195. fprintf(stderr, "Failed to set an OpenAL audio context\n");
  196. goto error;
  197. }
  198. /* Start SDL playing. Our callback (thus alcRenderSamplesSOFT) will now
  199. * start being called regularly to update the AL playback state. */
  200. SDL_PauseAudio(0);
  201. /* Load the sound into a buffer. */
  202. buffer = CreateSineWave();
  203. if(!buffer)
  204. {
  205. SDL_CloseAudio();
  206. alcDestroyContext(playback.Context);
  207. alcCloseDevice(playback.Device);
  208. SDL_Quit();
  209. return 1;
  210. }
  211. /* Create the source to play the sound with. */
  212. source = 0;
  213. alGenSources(1, &source);
  214. alSourcei(source, AL_BUFFER, buffer);
  215. assert(alGetError()==AL_NO_ERROR && "Failed to setup sound source");
  216. /* Play the sound until it finishes. */
  217. alSourcePlay(source);
  218. do {
  219. al_nssleep(10000000);
  220. alGetSourcei(source, AL_SOURCE_STATE, &state);
  221. } while(alGetError() == AL_NO_ERROR && state == AL_PLAYING);
  222. /* All done. Delete resources, and close OpenAL. */
  223. alDeleteSources(1, &source);
  224. alDeleteBuffers(1, &buffer);
  225. /* Stop SDL playing. */
  226. SDL_PauseAudio(1);
  227. /* Close up OpenAL and SDL. */
  228. SDL_CloseAudio();
  229. alcDestroyContext(playback.Context);
  230. alcCloseDevice(playback.Device);
  231. SDL_Quit();
  232. return 0;
  233. error:
  234. SDL_CloseAudio();
  235. if(playback.Context)
  236. alcDestroyContext(playback.Context);
  237. if(playback.Device)
  238. alcCloseDevice(playback.Device);
  239. SDL_Quit();
  240. return 1;
  241. }