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

193 lines
5.4 KiB

  1. /*
  2. * OpenAL Helpers
  3. *
  4. * Copyright (c) 2011 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 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 "alhelpers.h"
  30. #include <stdio.h>
  31. #include <errno.h>
  32. #include <string.h>
  33. #include "AL/al.h"
  34. #include "AL/alc.h"
  35. #include "AL/alext.h"
  36. /* InitAL opens a device and sets up a context using default attributes, making
  37. * the program ready to call OpenAL functions. */
  38. int InitAL(char ***argv, int *argc)
  39. {
  40. const ALCchar *name;
  41. ALCdevice *device;
  42. ALCcontext *ctx;
  43. /* Open and initialize a device */
  44. device = NULL;
  45. if(argc && argv && *argc > 1 && strcmp((*argv)[0], "-device") == 0)
  46. {
  47. device = alcOpenDevice((*argv)[1]);
  48. if(!device)
  49. fprintf(stderr, "Failed to open \"%s\", trying default\n", (*argv)[1]);
  50. (*argv) += 2;
  51. (*argc) -= 2;
  52. }
  53. if(!device)
  54. device = alcOpenDevice(NULL);
  55. if(!device)
  56. {
  57. fprintf(stderr, "Could not open a device!\n");
  58. return 1;
  59. }
  60. ctx = alcCreateContext(device, NULL);
  61. if(ctx == NULL || alcMakeContextCurrent(ctx) == ALC_FALSE)
  62. {
  63. if(ctx != NULL)
  64. alcDestroyContext(ctx);
  65. alcCloseDevice(device);
  66. fprintf(stderr, "Could not set a context!\n");
  67. return 1;
  68. }
  69. name = NULL;
  70. if(alcIsExtensionPresent(device, "ALC_ENUMERATE_ALL_EXT"))
  71. name = alcGetString(device, ALC_ALL_DEVICES_SPECIFIER);
  72. if(!name || alcGetError(device) != AL_NO_ERROR)
  73. name = alcGetString(device, ALC_DEVICE_SPECIFIER);
  74. printf("Opened \"%s\"\n", name);
  75. return 0;
  76. }
  77. /* CloseAL closes the device belonging to the current context, and destroys the
  78. * context. */
  79. void CloseAL(void)
  80. {
  81. ALCdevice *device;
  82. ALCcontext *ctx;
  83. ctx = alcGetCurrentContext();
  84. if(ctx == NULL)
  85. return;
  86. device = alcGetContextsDevice(ctx);
  87. alcMakeContextCurrent(NULL);
  88. alcDestroyContext(ctx);
  89. alcCloseDevice(device);
  90. }
  91. const char *FormatName(ALenum format)
  92. {
  93. switch(format)
  94. {
  95. case AL_FORMAT_MONO8: return "Mono, U8";
  96. case AL_FORMAT_MONO16: return "Mono, S16";
  97. case AL_FORMAT_MONO_FLOAT32: return "Mono, Float32";
  98. case AL_FORMAT_STEREO8: return "Stereo, U8";
  99. case AL_FORMAT_STEREO16: return "Stereo, S16";
  100. case AL_FORMAT_STEREO_FLOAT32: return "Stereo, Float32";
  101. case AL_FORMAT_BFORMAT2D_8: return "B-Format 2D, U8";
  102. case AL_FORMAT_BFORMAT2D_16: return "B-Format 2D, S16";
  103. case AL_FORMAT_BFORMAT2D_FLOAT32: return "B-Format 2D, Float32";
  104. case AL_FORMAT_BFORMAT3D_8: return "B-Format 3D, U8";
  105. case AL_FORMAT_BFORMAT3D_16: return "B-Format 3D, S16";
  106. case AL_FORMAT_BFORMAT3D_FLOAT32: return "B-Format 3D, Float32";
  107. }
  108. return "Unknown Format";
  109. }
  110. #ifdef _WIN32
  111. #define WIN32_LEAN_AND_MEAN
  112. #include <windows.h>
  113. #include <mmsystem.h>
  114. int altime_get(void)
  115. {
  116. static int start_time = 0;
  117. int cur_time;
  118. union {
  119. FILETIME ftime;
  120. ULARGE_INTEGER ulint;
  121. } systime;
  122. GetSystemTimeAsFileTime(&systime.ftime);
  123. /* FILETIME is in 100-nanosecond units, or 1/10th of a microsecond. */
  124. cur_time = (int)(systime.ulint.QuadPart/10000);
  125. if(!start_time)
  126. start_time = cur_time;
  127. return cur_time - start_time;
  128. }
  129. void al_nssleep(unsigned long nsec)
  130. {
  131. Sleep(nsec / 1000000);
  132. }
  133. #else
  134. #include <sys/time.h>
  135. #include <unistd.h>
  136. #include <time.h>
  137. int altime_get(void)
  138. {
  139. static int start_time = 0u;
  140. int cur_time;
  141. #if _POSIX_TIMERS > 0
  142. struct timespec ts;
  143. int ret = clock_gettime(CLOCK_REALTIME, &ts);
  144. if(ret != 0) return 0;
  145. cur_time = (int)(ts.tv_sec*1000 + ts.tv_nsec/1000000);
  146. #else /* _POSIX_TIMERS > 0 */
  147. struct timeval tv;
  148. int ret = gettimeofday(&tv, NULL);
  149. if(ret != 0) return 0;
  150. cur_time = (int)(tv.tv_sec*1000 + tv.tv_usec/1000);
  151. #endif
  152. if(!start_time)
  153. start_time = cur_time;
  154. return cur_time - start_time;
  155. }
  156. void al_nssleep(unsigned long nsec)
  157. {
  158. struct timespec ts, rem;
  159. ts.tv_sec = (time_t)(nsec / 1000000000ul);
  160. ts.tv_nsec = (long)(nsec % 1000000000ul);
  161. while(nanosleep(&ts, &rem) == -1 && errno == EINTR)
  162. ts = rem;
  163. }
  164. #endif