linux_snd.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. ===========================================================================
  3. Copyright (C) 1999-2005 Id Software, Inc.
  4. This file is part of Quake III Arena source code.
  5. Quake III Arena source code is free software; you can redistribute it
  6. and/or modify it under the terms of the GNU General Public License as
  7. published by the Free Software Foundation; either version 2 of the License,
  8. or (at your option) any later version.
  9. Quake III Arena source code is distributed in the hope that it will be
  10. useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with Foobar; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. ===========================================================================
  17. */
  18. #include <unistd.h>
  19. #include <fcntl.h>
  20. #include <stdlib.h>
  21. #include <sys/types.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/mman.h>
  24. #include <sys/shm.h>
  25. #include <sys/wait.h>
  26. #ifdef __linux__ // rb0101023 - guard this
  27. #include <linux/soundcard.h>
  28. #endif
  29. #ifdef __FreeBSD__ // rb0101023 - added
  30. #include <sys/soundcard.h>
  31. #endif
  32. #include <stdio.h>
  33. #include "../game/q_shared.h"
  34. #include "../client/snd_local.h"
  35. int audio_fd;
  36. int snd_inited=0;
  37. cvar_t *sndbits;
  38. cvar_t *sndspeed;
  39. cvar_t *sndchannels;
  40. cvar_t *snddevice;
  41. /* Some devices may work only with 48000 */
  42. static int tryrates[] = { 22050, 11025, 44100, 48000, 8000 };
  43. static qboolean use_custom_memset = qfalse;
  44. // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=371
  45. void Snd_Memset (void* dest, const int val, const size_t count)
  46. {
  47. int *pDest;
  48. int i, iterate;
  49. if (!use_custom_memset)
  50. {
  51. Com_Memset(dest,val,count);
  52. return;
  53. }
  54. iterate = count / sizeof(int);
  55. pDest = (int*)dest;
  56. for(i=0; i<iterate; i++)
  57. {
  58. pDest[i] = val;
  59. }
  60. }
  61. qboolean SNDDMA_Init(void)
  62. {
  63. int rc;
  64. int fmt;
  65. int tmp;
  66. int i;
  67. // char *s; // bk001204 - unused
  68. struct audio_buf_info info;
  69. int caps;
  70. extern uid_t saved_euid;
  71. if (snd_inited)
  72. return 1;
  73. if (!snddevice) {
  74. sndbits = Cvar_Get("sndbits", "16", CVAR_ARCHIVE);
  75. sndspeed = Cvar_Get("sndspeed", "0", CVAR_ARCHIVE);
  76. sndchannels = Cvar_Get("sndchannels", "2", CVAR_ARCHIVE);
  77. snddevice = Cvar_Get("snddevice", "/dev/dsp", CVAR_ARCHIVE);
  78. }
  79. // open /dev/dsp, confirm capability to mmap, and get size of dma buffer
  80. if (!audio_fd) {
  81. seteuid(saved_euid);
  82. audio_fd = open(snddevice->string, O_RDWR);
  83. seteuid(getuid());
  84. if (audio_fd < 0) {
  85. perror(snddevice->string);
  86. Com_Printf("Could not open %s\n", snddevice->string);
  87. return 0;
  88. }
  89. }
  90. if (ioctl(audio_fd, SNDCTL_DSP_GETCAPS, &caps) == -1) {
  91. perror(snddevice->string);
  92. Com_Printf("Sound driver too old\n");
  93. close(audio_fd);
  94. return 0;
  95. }
  96. if (!(caps & DSP_CAP_TRIGGER) || !(caps & DSP_CAP_MMAP)) {
  97. Com_Printf("Sorry but your soundcard can't do this\n");
  98. close(audio_fd);
  99. return 0;
  100. }
  101. /* SNDCTL_DSP_GETOSPACE moved to be called later */
  102. // set sample bits & speed
  103. dma.samplebits = (int)sndbits->value;
  104. if (dma.samplebits != 16 && dma.samplebits != 8) {
  105. ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &fmt);
  106. if (fmt & AFMT_S16_LE)
  107. dma.samplebits = 16;
  108. else if (fmt & AFMT_U8)
  109. dma.samplebits = 8;
  110. }
  111. dma.speed = (int)sndspeed->value;
  112. if (!dma.speed) {
  113. for (i=0 ; i<sizeof(tryrates)/4 ; i++)
  114. if (!ioctl(audio_fd, SNDCTL_DSP_SPEED, &tryrates[i]))
  115. break;
  116. dma.speed = tryrates[i];
  117. }
  118. dma.channels = (int)sndchannels->value;
  119. if (dma.channels < 1 || dma.channels > 2)
  120. dma.channels = 2;
  121. /* mmap() call moved forward */
  122. tmp = 0;
  123. if (dma.channels == 2)
  124. tmp = 1;
  125. rc = ioctl(audio_fd, SNDCTL_DSP_STEREO, &tmp);
  126. if (rc < 0) {
  127. perror(snddevice->string);
  128. Com_Printf("Could not set %s to stereo=%d", snddevice->string, dma.channels);
  129. close(audio_fd);
  130. return 0;
  131. }
  132. if (tmp)
  133. dma.channels = 2;
  134. else
  135. dma.channels = 1;
  136. rc = ioctl(audio_fd, SNDCTL_DSP_SPEED, &dma.speed);
  137. if (rc < 0) {
  138. perror(snddevice->string);
  139. Com_Printf("Could not set %s speed to %d", snddevice->string, dma.speed);
  140. close(audio_fd);
  141. return 0;
  142. }
  143. if (dma.samplebits == 16) {
  144. rc = AFMT_S16_LE;
  145. rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
  146. if (rc < 0) {
  147. perror(snddevice->string);
  148. Com_Printf("Could not support 16-bit data. Try 8-bit.\n");
  149. close(audio_fd);
  150. return 0;
  151. }
  152. } else if (dma.samplebits == 8) {
  153. rc = AFMT_U8;
  154. rc = ioctl(audio_fd, SNDCTL_DSP_SETFMT, &rc);
  155. if (rc < 0) {
  156. perror(snddevice->string);
  157. Com_Printf("Could not support 8-bit data.\n");
  158. close(audio_fd);
  159. return 0;
  160. }
  161. } else {
  162. perror(snddevice->string);
  163. Com_Printf("%d-bit sound not supported.", dma.samplebits);
  164. close(audio_fd);
  165. return 0;
  166. }
  167. if (ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info)==-1) {
  168. perror("GETOSPACE");
  169. Com_Printf("Um, can't do GETOSPACE?\n");
  170. close(audio_fd);
  171. return 0;
  172. }
  173. dma.samples = info.fragstotal * info.fragsize / (dma.samplebits/8);
  174. dma.submission_chunk = 1;
  175. // memory map the dma buffer
  176. // TTimo 2001/10/08 added PROT_READ to the mmap
  177. // https://zerowing.idsoftware.com/bugzilla/show_bug.cgi?id=371
  178. // checking Alsa bug, doesn't allow dma alloc with PROT_READ?
  179. if (!dma.buffer)
  180. dma.buffer = (unsigned char *) mmap(NULL, info.fragstotal
  181. * info.fragsize, PROT_WRITE|PROT_READ, MAP_FILE|MAP_SHARED, audio_fd, 0);
  182. if (dma.buffer == MAP_FAILED)
  183. {
  184. Com_Printf("Could not mmap dma buffer PROT_WRITE|PROT_READ\n");
  185. Com_Printf("trying mmap PROT_WRITE (with associated better compatibility / less performance code)\n");
  186. dma.buffer = (unsigned char *) mmap(NULL, info.fragstotal
  187. * info.fragsize, PROT_WRITE, MAP_FILE|MAP_SHARED, audio_fd, 0);
  188. // NOTE TTimo could add a variable to force using regular memset on systems that are known to be safe
  189. use_custom_memset = qtrue;
  190. }
  191. if (dma.buffer == MAP_FAILED) {
  192. perror(snddevice->string);
  193. Com_Printf("Could not mmap %s\n", snddevice->string);
  194. close(audio_fd);
  195. return 0;
  196. }
  197. // toggle the trigger & start her up
  198. tmp = 0;
  199. rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
  200. if (rc < 0) {
  201. perror(snddevice->string);
  202. Com_Printf("Could not toggle.\n");
  203. close(audio_fd);
  204. return 0;
  205. }
  206. tmp = PCM_ENABLE_OUTPUT;
  207. rc = ioctl(audio_fd, SNDCTL_DSP_SETTRIGGER, &tmp);
  208. if (rc < 0) {
  209. perror(snddevice->string);
  210. Com_Printf("Could not toggle.\n");
  211. close(audio_fd);
  212. return 0;
  213. }
  214. snd_inited = 1;
  215. return 1;
  216. }
  217. int SNDDMA_GetDMAPos(void)
  218. {
  219. struct count_info count;
  220. if (!snd_inited) return 0;
  221. if (ioctl(audio_fd, SNDCTL_DSP_GETOPTR, &count) == -1) {
  222. perror(snddevice->string);
  223. Com_Printf("Uh, sound dead.\n");
  224. close(audio_fd);
  225. snd_inited = 0;
  226. return 0;
  227. }
  228. return count.ptr / (dma.samplebits / 8);
  229. }
  230. void SNDDMA_Shutdown(void)
  231. {
  232. }
  233. /*
  234. ==============
  235. SNDDMA_Submit
  236. Send sound to device if buffer isn't really the dma buffer
  237. ===============
  238. */
  239. void SNDDMA_Submit(void)
  240. {
  241. }
  242. void SNDDMA_BeginPainting (void)
  243. {
  244. }