alhelpers.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * OpenAL Helpers
  3. *
  4. * Copyright (c) 2011 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 routines to help with some menial OpenAL-related tasks,
  25. * such as opening a device and setting up a context, closing the device and
  26. * destroying its context, converting between frame counts and byte lengths,
  27. * finding an appropriate buffer format, and getting readable strings for
  28. * channel configs and sample types. */
  29. #include <stdio.h>
  30. #include "AL/al.h"
  31. #include "AL/alc.h"
  32. #include "AL/alext.h"
  33. #include "alhelpers.h"
  34. /* InitAL opens the default device and sets up a context using default
  35. * attributes, making the program ready to call OpenAL functions. */
  36. int InitAL(void)
  37. {
  38. ALCdevice *device;
  39. ALCcontext *ctx;
  40. /* Open and initialize a device with default settings */
  41. device = alcOpenDevice(NULL);
  42. if(!device)
  43. {
  44. fprintf(stderr, "Could not open a device!\n");
  45. return 1;
  46. }
  47. ctx = alcCreateContext(device, NULL);
  48. if(ctx == NULL || alcMakeContextCurrent(ctx) == ALC_FALSE)
  49. {
  50. if(ctx != NULL)
  51. alcDestroyContext(ctx);
  52. alcCloseDevice(device);
  53. fprintf(stderr, "Could not set a context!\n");
  54. return 1;
  55. }
  56. printf("Opened \"%s\"\n", alcGetString(device, ALC_DEVICE_SPECIFIER));
  57. return 0;
  58. }
  59. /* CloseAL closes the device belonging to the current context, and destroys the
  60. * context. */
  61. void CloseAL(void)
  62. {
  63. ALCdevice *device;
  64. ALCcontext *ctx;
  65. ctx = alcGetCurrentContext();
  66. if(ctx == NULL)
  67. return;
  68. device = alcGetContextsDevice(ctx);
  69. alcMakeContextCurrent(NULL);
  70. alcDestroyContext(ctx);
  71. alcCloseDevice(device);
  72. }
  73. /* GetFormat retrieves a compatible buffer format given the channel config and
  74. * sample type. If an alIsBufferFormatSupportedSOFT-compatible function is
  75. * provided, it will be called to find the closest-matching format from
  76. * AL_SOFT_buffer_samples. Returns AL_NONE (0) if no supported format can be
  77. * found. */
  78. ALenum GetFormat(ALenum channels, ALenum type, LPALISBUFFERFORMATSUPPORTEDSOFT palIsBufferFormatSupportedSOFT)
  79. {
  80. ALenum format = AL_NONE;
  81. /* If using AL_SOFT_buffer_samples, try looking through its formats */
  82. if(palIsBufferFormatSupportedSOFT)
  83. {
  84. /* AL_SOFT_buffer_samples is more lenient with matching formats. The
  85. * specified sample type does not need to match the returned format,
  86. * but it is nice to try to get something close. */
  87. if(type == AL_UNSIGNED_BYTE_SOFT || type == AL_BYTE_SOFT)
  88. {
  89. if(channels == AL_MONO_SOFT) format = AL_MONO8_SOFT;
  90. else if(channels == AL_STEREO_SOFT) format = AL_STEREO8_SOFT;
  91. else if(channels == AL_QUAD_SOFT) format = AL_QUAD8_SOFT;
  92. else if(channels == AL_5POINT1_SOFT) format = AL_5POINT1_8_SOFT;
  93. else if(channels == AL_6POINT1_SOFT) format = AL_6POINT1_8_SOFT;
  94. else if(channels == AL_7POINT1_SOFT) format = AL_7POINT1_8_SOFT;
  95. }
  96. else if(type == AL_UNSIGNED_SHORT_SOFT || type == AL_SHORT_SOFT)
  97. {
  98. if(channels == AL_MONO_SOFT) format = AL_MONO16_SOFT;
  99. else if(channels == AL_STEREO_SOFT) format = AL_STEREO16_SOFT;
  100. else if(channels == AL_QUAD_SOFT) format = AL_QUAD16_SOFT;
  101. else if(channels == AL_5POINT1_SOFT) format = AL_5POINT1_16_SOFT;
  102. else if(channels == AL_6POINT1_SOFT) format = AL_6POINT1_16_SOFT;
  103. else if(channels == AL_7POINT1_SOFT) format = AL_7POINT1_16_SOFT;
  104. }
  105. else if(type == AL_UNSIGNED_BYTE3_SOFT || type == AL_BYTE3_SOFT ||
  106. type == AL_UNSIGNED_INT_SOFT || type == AL_INT_SOFT ||
  107. type == AL_FLOAT_SOFT || type == AL_DOUBLE_SOFT)
  108. {
  109. if(channels == AL_MONO_SOFT) format = AL_MONO32F_SOFT;
  110. else if(channels == AL_STEREO_SOFT) format = AL_STEREO32F_SOFT;
  111. else if(channels == AL_QUAD_SOFT) format = AL_QUAD32F_SOFT;
  112. else if(channels == AL_5POINT1_SOFT) format = AL_5POINT1_32F_SOFT;
  113. else if(channels == AL_6POINT1_SOFT) format = AL_6POINT1_32F_SOFT;
  114. else if(channels == AL_7POINT1_SOFT) format = AL_7POINT1_32F_SOFT;
  115. }
  116. if(format != AL_NONE && !palIsBufferFormatSupportedSOFT(format))
  117. format = AL_NONE;
  118. /* A matching format was not found or supported. Try 32-bit float. */
  119. if(format == AL_NONE)
  120. {
  121. if(channels == AL_MONO_SOFT) format = AL_MONO32F_SOFT;
  122. else if(channels == AL_STEREO_SOFT) format = AL_STEREO32F_SOFT;
  123. else if(channels == AL_QUAD_SOFT) format = AL_QUAD32F_SOFT;
  124. else if(channels == AL_5POINT1_SOFT) format = AL_5POINT1_32F_SOFT;
  125. else if(channels == AL_6POINT1_SOFT) format = AL_6POINT1_32F_SOFT;
  126. else if(channels == AL_7POINT1_SOFT) format = AL_7POINT1_32F_SOFT;
  127. if(format != AL_NONE && !palIsBufferFormatSupportedSOFT(format))
  128. format = AL_NONE;
  129. }
  130. /* 32-bit float not supported. Try 16-bit int. */
  131. if(format == AL_NONE)
  132. {
  133. if(channels == AL_MONO_SOFT) format = AL_MONO16_SOFT;
  134. else if(channels == AL_STEREO_SOFT) format = AL_STEREO16_SOFT;
  135. else if(channels == AL_QUAD_SOFT) format = AL_QUAD16_SOFT;
  136. else if(channels == AL_5POINT1_SOFT) format = AL_5POINT1_16_SOFT;
  137. else if(channels == AL_6POINT1_SOFT) format = AL_6POINT1_16_SOFT;
  138. else if(channels == AL_7POINT1_SOFT) format = AL_7POINT1_16_SOFT;
  139. if(format != AL_NONE && !palIsBufferFormatSupportedSOFT(format))
  140. format = AL_NONE;
  141. }
  142. /* 16-bit int not supported. Try 8-bit int. */
  143. if(format == AL_NONE)
  144. {
  145. if(channels == AL_MONO_SOFT) format = AL_MONO8_SOFT;
  146. else if(channels == AL_STEREO_SOFT) format = AL_STEREO8_SOFT;
  147. else if(channels == AL_QUAD_SOFT) format = AL_QUAD8_SOFT;
  148. else if(channels == AL_5POINT1_SOFT) format = AL_5POINT1_8_SOFT;
  149. else if(channels == AL_6POINT1_SOFT) format = AL_6POINT1_8_SOFT;
  150. else if(channels == AL_7POINT1_SOFT) format = AL_7POINT1_8_SOFT;
  151. if(format != AL_NONE && !palIsBufferFormatSupportedSOFT(format))
  152. format = AL_NONE;
  153. }
  154. return format;
  155. }
  156. /* We use the AL_EXT_MCFORMATS extension to provide output of Quad, 5.1,
  157. * and 7.1 channel configs, AL_EXT_FLOAT32 for 32-bit float samples, and
  158. * AL_EXT_DOUBLE for 64-bit float samples. */
  159. if(type == AL_UNSIGNED_BYTE_SOFT)
  160. {
  161. if(channels == AL_MONO_SOFT)
  162. format = AL_FORMAT_MONO8;
  163. else if(channels == AL_STEREO_SOFT)
  164. format = AL_FORMAT_STEREO8;
  165. else if(alIsExtensionPresent("AL_EXT_MCFORMATS"))
  166. {
  167. if(channels == AL_QUAD_SOFT)
  168. format = alGetEnumValue("AL_FORMAT_QUAD8");
  169. else if(channels == AL_5POINT1_SOFT)
  170. format = alGetEnumValue("AL_FORMAT_51CHN8");
  171. else if(channels == AL_6POINT1_SOFT)
  172. format = alGetEnumValue("AL_FORMAT_61CHN8");
  173. else if(channels == AL_7POINT1_SOFT)
  174. format = alGetEnumValue("AL_FORMAT_71CHN8");
  175. }
  176. }
  177. else if(type == AL_SHORT_SOFT)
  178. {
  179. if(channels == AL_MONO_SOFT)
  180. format = AL_FORMAT_MONO16;
  181. else if(channels == AL_STEREO_SOFT)
  182. format = AL_FORMAT_STEREO16;
  183. else if(alIsExtensionPresent("AL_EXT_MCFORMATS"))
  184. {
  185. if(channels == AL_QUAD_SOFT)
  186. format = alGetEnumValue("AL_FORMAT_QUAD16");
  187. else if(channels == AL_5POINT1_SOFT)
  188. format = alGetEnumValue("AL_FORMAT_51CHN16");
  189. else if(channels == AL_6POINT1_SOFT)
  190. format = alGetEnumValue("AL_FORMAT_61CHN16");
  191. else if(channels == AL_7POINT1_SOFT)
  192. format = alGetEnumValue("AL_FORMAT_71CHN16");
  193. }
  194. }
  195. else if(type == AL_FLOAT_SOFT && alIsExtensionPresent("AL_EXT_FLOAT32"))
  196. {
  197. if(channels == AL_MONO_SOFT)
  198. format = alGetEnumValue("AL_FORMAT_MONO_FLOAT32");
  199. else if(channels == AL_STEREO_SOFT)
  200. format = alGetEnumValue("AL_FORMAT_STEREO_FLOAT32");
  201. else if(alIsExtensionPresent("AL_EXT_MCFORMATS"))
  202. {
  203. if(channels == AL_QUAD_SOFT)
  204. format = alGetEnumValue("AL_FORMAT_QUAD32");
  205. else if(channels == AL_5POINT1_SOFT)
  206. format = alGetEnumValue("AL_FORMAT_51CHN32");
  207. else if(channels == AL_6POINT1_SOFT)
  208. format = alGetEnumValue("AL_FORMAT_61CHN32");
  209. else if(channels == AL_7POINT1_SOFT)
  210. format = alGetEnumValue("AL_FORMAT_71CHN32");
  211. }
  212. }
  213. else if(type == AL_DOUBLE_SOFT && alIsExtensionPresent("AL_EXT_DOUBLE"))
  214. {
  215. if(channels == AL_MONO_SOFT)
  216. format = alGetEnumValue("AL_FORMAT_MONO_DOUBLE");
  217. else if(channels == AL_STEREO_SOFT)
  218. format = alGetEnumValue("AL_FORMAT_STEREO_DOUBLE");
  219. }
  220. /* NOTE: It seems OSX returns -1 from alGetEnumValue for unknown enums, as
  221. * opposed to 0. Correct it. */
  222. if(format == -1)
  223. format = 0;
  224. return format;
  225. }
  226. void AL_APIENTRY wrap_BufferSamples(ALuint buffer, ALuint samplerate,
  227. ALenum internalformat, ALsizei samples,
  228. ALenum channels, ALenum type,
  229. const ALvoid *data)
  230. {
  231. alBufferData(buffer, internalformat, data,
  232. FramesToBytes(samples, channels, type),
  233. samplerate);
  234. }
  235. const char *ChannelsName(ALenum chans)
  236. {
  237. switch(chans)
  238. {
  239. case AL_MONO_SOFT: return "Mono";
  240. case AL_STEREO_SOFT: return "Stereo";
  241. case AL_REAR_SOFT: return "Rear";
  242. case AL_QUAD_SOFT: return "Quadraphonic";
  243. case AL_5POINT1_SOFT: return "5.1 Surround";
  244. case AL_6POINT1_SOFT: return "6.1 Surround";
  245. case AL_7POINT1_SOFT: return "7.1 Surround";
  246. }
  247. return "Unknown Channels";
  248. }
  249. const char *TypeName(ALenum type)
  250. {
  251. switch(type)
  252. {
  253. case AL_BYTE_SOFT: return "S8";
  254. case AL_UNSIGNED_BYTE_SOFT: return "U8";
  255. case AL_SHORT_SOFT: return "S16";
  256. case AL_UNSIGNED_SHORT_SOFT: return "U16";
  257. case AL_INT_SOFT: return "S32";
  258. case AL_UNSIGNED_INT_SOFT: return "U32";
  259. case AL_FLOAT_SOFT: return "Float32";
  260. case AL_DOUBLE_SOFT: return "Float64";
  261. }
  262. return "Unknown Type";
  263. }
  264. ALsizei FramesToBytes(ALsizei size, ALenum channels, ALenum type)
  265. {
  266. switch(channels)
  267. {
  268. case AL_MONO_SOFT: size *= 1; break;
  269. case AL_STEREO_SOFT: size *= 2; break;
  270. case AL_REAR_SOFT: size *= 2; break;
  271. case AL_QUAD_SOFT: size *= 4; break;
  272. case AL_5POINT1_SOFT: size *= 6; break;
  273. case AL_6POINT1_SOFT: size *= 7; break;
  274. case AL_7POINT1_SOFT: size *= 8; break;
  275. }
  276. switch(type)
  277. {
  278. case AL_BYTE_SOFT: size *= sizeof(ALbyte); break;
  279. case AL_UNSIGNED_BYTE_SOFT: size *= sizeof(ALubyte); break;
  280. case AL_SHORT_SOFT: size *= sizeof(ALshort); break;
  281. case AL_UNSIGNED_SHORT_SOFT: size *= sizeof(ALushort); break;
  282. case AL_INT_SOFT: size *= sizeof(ALint); break;
  283. case AL_UNSIGNED_INT_SOFT: size *= sizeof(ALuint); break;
  284. case AL_FLOAT_SOFT: size *= sizeof(ALfloat); break;
  285. case AL_DOUBLE_SOFT: size *= sizeof(ALdouble); break;
  286. }
  287. return size;
  288. }
  289. ALsizei BytesToFrames(ALsizei size, ALenum channels, ALenum type)
  290. {
  291. return size / FramesToBytes(1, channels, type);
  292. }