SDL_dspaudio.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_AUDIO_DRIVER_OSS
  20. /* Allow access to a raw mixing buffer */
  21. #include <stdio.h> /* For perror() */
  22. #include <string.h> /* For strerror() */
  23. #include <errno.h>
  24. #include <unistd.h>
  25. #include <fcntl.h>
  26. #include <signal.h>
  27. #include <sys/time.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/stat.h>
  30. #include <sys/soundcard.h>
  31. #include "SDL_timer.h"
  32. #include "SDL_audio.h"
  33. #include "../SDL_audio_c.h"
  34. #include "../SDL_audiodev_c.h"
  35. #include "SDL_dspaudio.h"
  36. static void DSP_DetectDevices(void)
  37. {
  38. SDL_EnumUnixAudioDevices(0, NULL);
  39. }
  40. static void DSP_CloseDevice(_THIS)
  41. {
  42. if (this->hidden->audio_fd >= 0) {
  43. close(this->hidden->audio_fd);
  44. }
  45. SDL_free(this->hidden->mixbuf);
  46. SDL_free(this->hidden);
  47. }
  48. static int DSP_OpenDevice(_THIS, const char *devname)
  49. {
  50. SDL_bool iscapture = this->iscapture;
  51. const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
  52. int format;
  53. int value;
  54. int frag_spec;
  55. SDL_AudioFormat test_format;
  56. /* We don't care what the devname is...we'll try to open anything. */
  57. /* ...but default to first name in the list... */
  58. if (devname == NULL) {
  59. devname = SDL_GetAudioDeviceName(0, iscapture);
  60. if (devname == NULL) {
  61. return SDL_SetError("No such audio device");
  62. }
  63. }
  64. /* Make sure fragment size stays a power of 2, or OSS fails. */
  65. /* I don't know which of these are actually legal values, though... */
  66. if (this->spec.channels > 8) {
  67. this->spec.channels = 8;
  68. } else if (this->spec.channels > 4) {
  69. this->spec.channels = 4;
  70. } else if (this->spec.channels > 2) {
  71. this->spec.channels = 2;
  72. }
  73. /* Initialize all variables that we clean on shutdown */
  74. this->hidden = (struct SDL_PrivateAudioData *)
  75. SDL_malloc((sizeof *this->hidden));
  76. if (this->hidden == NULL) {
  77. return SDL_OutOfMemory();
  78. }
  79. SDL_zerop(this->hidden);
  80. /* Open the audio device */
  81. this->hidden->audio_fd = open(devname, flags | O_CLOEXEC, 0);
  82. if (this->hidden->audio_fd < 0) {
  83. return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
  84. }
  85. /* Make the file descriptor use blocking i/o with fcntl() */
  86. {
  87. long ctlflags;
  88. ctlflags = fcntl(this->hidden->audio_fd, F_GETFL);
  89. ctlflags &= ~O_NONBLOCK;
  90. if (fcntl(this->hidden->audio_fd, F_SETFL, ctlflags) < 0) {
  91. return SDL_SetError("Couldn't set audio blocking mode");
  92. }
  93. }
  94. /* Get a list of supported hardware formats */
  95. if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0) {
  96. perror("SNDCTL_DSP_GETFMTS");
  97. return SDL_SetError("Couldn't get audio format list");
  98. }
  99. /* Try for a closest match on audio format */
  100. format = 0;
  101. for (test_format = SDL_FirstAudioFormat(this->spec.format);
  102. !format && test_format;) {
  103. #ifdef DEBUG_AUDIO
  104. fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
  105. #endif
  106. switch (test_format) {
  107. case AUDIO_U8:
  108. if (value & AFMT_U8) {
  109. format = AFMT_U8;
  110. }
  111. break;
  112. case AUDIO_S16LSB:
  113. if (value & AFMT_S16_LE) {
  114. format = AFMT_S16_LE;
  115. }
  116. break;
  117. case AUDIO_S16MSB:
  118. if (value & AFMT_S16_BE) {
  119. format = AFMT_S16_BE;
  120. }
  121. break;
  122. #if 0
  123. /*
  124. * These formats are not used by any real life systems so they are not
  125. * needed here.
  126. */
  127. case AUDIO_S8:
  128. if (value & AFMT_S8) {
  129. format = AFMT_S8;
  130. }
  131. break;
  132. case AUDIO_U16LSB:
  133. if (value & AFMT_U16_LE) {
  134. format = AFMT_U16_LE;
  135. }
  136. break;
  137. case AUDIO_U16MSB:
  138. if (value & AFMT_U16_BE) {
  139. format = AFMT_U16_BE;
  140. }
  141. break;
  142. #endif
  143. default:
  144. format = 0;
  145. break;
  146. }
  147. if (!format) {
  148. test_format = SDL_NextAudioFormat();
  149. }
  150. }
  151. if (format == 0) {
  152. return SDL_SetError("Couldn't find any hardware audio formats");
  153. }
  154. this->spec.format = test_format;
  155. /* Set the audio format */
  156. value = format;
  157. if ((ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
  158. (value != format)) {
  159. perror("SNDCTL_DSP_SETFMT");
  160. return SDL_SetError("Couldn't set audio format");
  161. }
  162. /* Set the number of channels of output */
  163. value = this->spec.channels;
  164. if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0) {
  165. perror("SNDCTL_DSP_CHANNELS");
  166. return SDL_SetError("Cannot set the number of channels");
  167. }
  168. this->spec.channels = value;
  169. /* Set the DSP frequency */
  170. value = this->spec.freq;
  171. if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SPEED, &value) < 0) {
  172. perror("SNDCTL_DSP_SPEED");
  173. return SDL_SetError("Couldn't set audio frequency");
  174. }
  175. this->spec.freq = value;
  176. /* Calculate the final parameters for this audio specification */
  177. SDL_CalculateAudioSpec(&this->spec);
  178. /* Determine the power of two of the fragment size */
  179. for (frag_spec = 0; (0x01U << frag_spec) < this->spec.size; ++frag_spec)
  180. ;
  181. if ((0x01U << frag_spec) != this->spec.size) {
  182. return SDL_SetError("Fragment size must be a power of two");
  183. }
  184. frag_spec |= 0x00020000; /* two fragments, for low latency */
  185. /* Set the audio buffering parameters */
  186. #ifdef DEBUG_AUDIO
  187. fprintf(stderr, "Requesting %d fragments of size %d\n",
  188. (frag_spec >> 16), 1 << (frag_spec & 0xFFFF));
  189. #endif
  190. if (ioctl(this->hidden->audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0) {
  191. perror("SNDCTL_DSP_SETFRAGMENT");
  192. }
  193. #ifdef DEBUG_AUDIO
  194. {
  195. audio_buf_info info;
  196. ioctl(this->hidden->audio_fd, SNDCTL_DSP_GETOSPACE, &info);
  197. fprintf(stderr, "fragments = %d\n", info.fragments);
  198. fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
  199. fprintf(stderr, "fragsize = %d\n", info.fragsize);
  200. fprintf(stderr, "bytes = %d\n", info.bytes);
  201. }
  202. #endif
  203. /* Allocate mixing buffer */
  204. if (!iscapture) {
  205. this->hidden->mixlen = this->spec.size;
  206. this->hidden->mixbuf = (Uint8 *)SDL_malloc(this->hidden->mixlen);
  207. if (this->hidden->mixbuf == NULL) {
  208. return SDL_OutOfMemory();
  209. }
  210. SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
  211. }
  212. /* We're ready to rock and roll. :-) */
  213. return 0;
  214. }
  215. static void DSP_PlayDevice(_THIS)
  216. {
  217. struct SDL_PrivateAudioData *h = this->hidden;
  218. if (write(h->audio_fd, h->mixbuf, h->mixlen) == -1) {
  219. perror("Audio write");
  220. SDL_OpenedAudioDeviceDisconnected(this);
  221. }
  222. #ifdef DEBUG_AUDIO
  223. fprintf(stderr, "Wrote %d bytes of audio data\n", h->mixlen);
  224. #endif
  225. }
  226. static Uint8 *DSP_GetDeviceBuf(_THIS)
  227. {
  228. return this->hidden->mixbuf;
  229. }
  230. static int DSP_CaptureFromDevice(_THIS, void *buffer, int buflen)
  231. {
  232. return (int)read(this->hidden->audio_fd, buffer, buflen);
  233. }
  234. static void DSP_FlushCapture(_THIS)
  235. {
  236. struct SDL_PrivateAudioData *h = this->hidden;
  237. audio_buf_info info;
  238. if (ioctl(h->audio_fd, SNDCTL_DSP_GETISPACE, &info) == 0) {
  239. while (info.bytes > 0) {
  240. char buf[512];
  241. const size_t len = SDL_min(sizeof(buf), info.bytes);
  242. const ssize_t br = read(h->audio_fd, buf, len);
  243. if (br <= 0) {
  244. break;
  245. }
  246. info.bytes -= br;
  247. }
  248. }
  249. }
  250. static SDL_bool InitTimeDevicesExist = SDL_FALSE;
  251. static int look_for_devices_test(int fd)
  252. {
  253. InitTimeDevicesExist = SDL_TRUE; /* note that _something_ exists. */
  254. /* Don't add to the device list, we're just seeing if any devices exist. */
  255. return 0;
  256. }
  257. static SDL_bool DSP_Init(SDL_AudioDriverImpl *impl)
  258. {
  259. InitTimeDevicesExist = SDL_FALSE;
  260. SDL_EnumUnixAudioDevices(0, look_for_devices_test);
  261. if (!InitTimeDevicesExist) {
  262. SDL_SetError("dsp: No such audio device");
  263. return SDL_FALSE; /* maybe try a different backend. */
  264. }
  265. /* Set the function pointers */
  266. impl->DetectDevices = DSP_DetectDevices;
  267. impl->OpenDevice = DSP_OpenDevice;
  268. impl->PlayDevice = DSP_PlayDevice;
  269. impl->GetDeviceBuf = DSP_GetDeviceBuf;
  270. impl->CloseDevice = DSP_CloseDevice;
  271. impl->CaptureFromDevice = DSP_CaptureFromDevice;
  272. impl->FlushCapture = DSP_FlushCapture;
  273. impl->AllowsArbitraryDeviceNames = SDL_TRUE;
  274. impl->HasCaptureSupport = SDL_TRUE;
  275. return SDL_TRUE; /* this audio target is available. */
  276. }
  277. AudioBootStrap DSP_bootstrap = {
  278. "dsp", "OSS /dev/dsp standard audio", DSP_Init, SDL_FALSE
  279. };
  280. #endif /* SDL_AUDIO_DRIVER_OSS */
  281. /* vi: set ts=4 sw=4 expandtab: */