🛠️🐜 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.

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