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

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