SDL_bsdaudio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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_BSD
  20. /*
  21. * Driver for native OpenBSD/NetBSD audio(4).
  22. * vedge@vedge.com.ar.
  23. */
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <sys/time.h>
  28. #include <sys/ioctl.h>
  29. #include <sys/stat.h>
  30. #include <sys/types.h>
  31. #include <sys/audioio.h>
  32. #include "SDL_timer.h"
  33. #include "SDL_audio.h"
  34. #include "../SDL_audiomem.h"
  35. #include "../SDL_audio_c.h"
  36. #include "../SDL_audiodev_c.h"
  37. #include "SDL_bsdaudio.h"
  38. /* Use timer for synchronization */
  39. /* #define USE_TIMER_SYNC */
  40. /* #define DEBUG_AUDIO */
  41. /* #define DEBUG_AUDIO_STREAM */
  42. static void
  43. BSDAUDIO_DetectDevices(int iscapture, SDL_AddAudioDevice addfn)
  44. {
  45. SDL_EnumUnixAudioDevices(iscapture, 0, NULL, addfn);
  46. }
  47. static void
  48. BSDAUDIO_Status(_THIS)
  49. {
  50. #ifdef DEBUG_AUDIO
  51. /* *INDENT-OFF* */
  52. audio_info_t info;
  53. if (ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info) < 0) {
  54. fprintf(stderr, "AUDIO_GETINFO failed.\n");
  55. return;
  56. }
  57. fprintf(stderr, "\n"
  58. "[play/record info]\n"
  59. "buffer size : %d bytes\n"
  60. "sample rate : %i Hz\n"
  61. "channels : %i\n"
  62. "precision : %i-bit\n"
  63. "encoding : 0x%x\n"
  64. "seek : %i\n"
  65. "sample count : %i\n"
  66. "EOF count : %i\n"
  67. "paused : %s\n"
  68. "error occured : %s\n"
  69. "waiting : %s\n"
  70. "active : %s\n"
  71. "",
  72. info.play.buffer_size,
  73. info.play.sample_rate,
  74. info.play.channels,
  75. info.play.precision,
  76. info.play.encoding,
  77. info.play.seek,
  78. info.play.samples,
  79. info.play.eof,
  80. info.play.pause ? "yes" : "no",
  81. info.play.error ? "yes" : "no",
  82. info.play.waiting ? "yes" : "no",
  83. info.play.active ? "yes" : "no");
  84. fprintf(stderr, "\n"
  85. "[audio info]\n"
  86. "monitor_gain : %i\n"
  87. "hw block size : %d bytes\n"
  88. "hi watermark : %i\n"
  89. "lo watermark : %i\n"
  90. "audio mode : %s\n"
  91. "",
  92. info.monitor_gain,
  93. info.blocksize,
  94. info.hiwat, info.lowat,
  95. (info.mode == AUMODE_PLAY) ? "PLAY"
  96. : (info.mode = AUMODE_RECORD) ? "RECORD"
  97. : (info.mode == AUMODE_PLAY_ALL ? "PLAY_ALL" : "?"));
  98. /* *INDENT-ON* */
  99. #endif /* DEBUG_AUDIO */
  100. }
  101. /* This function waits until it is possible to write a full sound buffer */
  102. static void
  103. BSDAUDIO_WaitDevice(_THIS)
  104. {
  105. #ifndef USE_BLOCKING_WRITES /* Not necessary when using blocking writes */
  106. /* See if we need to use timed audio synchronization */
  107. if (this->hidden->frame_ticks) {
  108. /* Use timer for general audio synchronization */
  109. Sint32 ticks;
  110. ticks = ((Sint32) (this->hidden->next_frame - SDL_GetTicks())) - FUDGE_TICKS;
  111. if (ticks > 0) {
  112. SDL_Delay(ticks);
  113. }
  114. } else {
  115. /* Use select() for audio synchronization */
  116. fd_set fdset;
  117. struct timeval timeout;
  118. FD_ZERO(&fdset);
  119. FD_SET(this->hidden->audio_fd, &fdset);
  120. timeout.tv_sec = 10;
  121. timeout.tv_usec = 0;
  122. #ifdef DEBUG_AUDIO
  123. fprintf(stderr, "Waiting for audio to get ready\n");
  124. #endif
  125. if (select(this->hidden->audio_fd + 1, NULL, &fdset, NULL, &timeout)
  126. <= 0) {
  127. const char *message =
  128. "Audio timeout - buggy audio driver? (disabled)";
  129. /* In general we should never print to the screen,
  130. but in this case we have no other way of letting
  131. the user know what happened.
  132. */
  133. fprintf(stderr, "SDL: %s\n", message);
  134. this->enabled = 0;
  135. /* Don't try to close - may hang */
  136. this->hidden->audio_fd = -1;
  137. #ifdef DEBUG_AUDIO
  138. fprintf(stderr, "Done disabling audio\n");
  139. #endif
  140. }
  141. #ifdef DEBUG_AUDIO
  142. fprintf(stderr, "Ready!\n");
  143. #endif
  144. }
  145. #endif /* !USE_BLOCKING_WRITES */
  146. }
  147. static void
  148. BSDAUDIO_PlayDevice(_THIS)
  149. {
  150. int written, p = 0;
  151. /* Write the audio data, checking for EAGAIN on broken audio drivers */
  152. do {
  153. written = write(this->hidden->audio_fd,
  154. &this->hidden->mixbuf[p], this->hidden->mixlen - p);
  155. if (written > 0)
  156. p += written;
  157. if (written == -1 && errno != 0 && errno != EAGAIN && errno != EINTR) {
  158. /* Non recoverable error has occurred. It should be reported!!! */
  159. perror("audio");
  160. break;
  161. }
  162. if (p < written
  163. || ((written < 0) && ((errno == 0) || (errno == EAGAIN)))) {
  164. SDL_Delay(1); /* Let a little CPU time go by */
  165. }
  166. } while (p < written);
  167. /* If timer synchronization is enabled, set the next write frame */
  168. if (this->hidden->frame_ticks) {
  169. this->hidden->next_frame += this->hidden->frame_ticks;
  170. }
  171. /* If we couldn't write, assume fatal error for now */
  172. if (written < 0) {
  173. this->enabled = 0;
  174. }
  175. #ifdef DEBUG_AUDIO
  176. fprintf(stderr, "Wrote %d bytes of audio data\n", written);
  177. #endif
  178. }
  179. static Uint8 *
  180. BSDAUDIO_GetDeviceBuf(_THIS)
  181. {
  182. return (this->hidden->mixbuf);
  183. }
  184. static void
  185. BSDAUDIO_CloseDevice(_THIS)
  186. {
  187. if (this->hidden != NULL) {
  188. SDL_FreeAudioMem(this->hidden->mixbuf);
  189. this->hidden->mixbuf = NULL;
  190. if (this->hidden->audio_fd >= 0) {
  191. close(this->hidden->audio_fd);
  192. this->hidden->audio_fd = -1;
  193. }
  194. SDL_free(this->hidden);
  195. this->hidden = NULL;
  196. }
  197. }
  198. static int
  199. BSDAUDIO_OpenDevice(_THIS, const char *devname, int iscapture)
  200. {
  201. const int flags = ((iscapture) ? OPEN_FLAGS_INPUT : OPEN_FLAGS_OUTPUT);
  202. SDL_AudioFormat format = 0;
  203. audio_info_t info;
  204. /* We don't care what the devname is...we'll try to open anything. */
  205. /* ...but default to first name in the list... */
  206. if (devname == NULL) {
  207. devname = SDL_GetAudioDeviceName(0, iscapture);
  208. if (devname == NULL) {
  209. return SDL_SetError("No such audio device");
  210. }
  211. }
  212. /* Initialize all variables that we clean on shutdown */
  213. this->hidden = (struct SDL_PrivateAudioData *)
  214. SDL_malloc((sizeof *this->hidden));
  215. if (this->hidden == NULL) {
  216. return SDL_OutOfMemory();
  217. }
  218. SDL_memset(this->hidden, 0, (sizeof *this->hidden));
  219. /* Open the audio device */
  220. this->hidden->audio_fd = open(devname, flags, 0);
  221. if (this->hidden->audio_fd < 0) {
  222. return SDL_SetError("Couldn't open %s: %s", devname, strerror(errno));
  223. }
  224. AUDIO_INITINFO(&info);
  225. /* Calculate the final parameters for this audio specification */
  226. SDL_CalculateAudioSpec(&this->spec);
  227. /* Set to play mode */
  228. info.mode = AUMODE_PLAY;
  229. if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) < 0) {
  230. BSDAUDIO_CloseDevice(this);
  231. return SDL_SetError("Couldn't put device into play mode");
  232. }
  233. AUDIO_INITINFO(&info);
  234. for (format = SDL_FirstAudioFormat(this->spec.format);
  235. format; format = SDL_NextAudioFormat()) {
  236. switch (format) {
  237. case AUDIO_U8:
  238. info.play.encoding = AUDIO_ENCODING_ULINEAR;
  239. info.play.precision = 8;
  240. break;
  241. case AUDIO_S8:
  242. info.play.encoding = AUDIO_ENCODING_SLINEAR;
  243. info.play.precision = 8;
  244. break;
  245. case AUDIO_S16LSB:
  246. info.play.encoding = AUDIO_ENCODING_SLINEAR_LE;
  247. info.play.precision = 16;
  248. break;
  249. case AUDIO_S16MSB:
  250. info.play.encoding = AUDIO_ENCODING_SLINEAR_BE;
  251. info.play.precision = 16;
  252. break;
  253. case AUDIO_U16LSB:
  254. info.play.encoding = AUDIO_ENCODING_ULINEAR_LE;
  255. info.play.precision = 16;
  256. break;
  257. case AUDIO_U16MSB:
  258. info.play.encoding = AUDIO_ENCODING_ULINEAR_BE;
  259. info.play.precision = 16;
  260. break;
  261. default:
  262. continue;
  263. }
  264. if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) == 0) {
  265. break;
  266. }
  267. }
  268. if (!format) {
  269. BSDAUDIO_CloseDevice(this);
  270. return SDL_SetError("No supported encoding for 0x%x", this->spec.format);
  271. }
  272. this->spec.format = format;
  273. AUDIO_INITINFO(&info);
  274. info.play.channels = this->spec.channels;
  275. if (ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info) == -1) {
  276. this->spec.channels = 1;
  277. }
  278. AUDIO_INITINFO(&info);
  279. info.play.sample_rate = this->spec.freq;
  280. info.blocksize = this->spec.size;
  281. info.hiwat = 5;
  282. info.lowat = 3;
  283. (void) ioctl(this->hidden->audio_fd, AUDIO_SETINFO, &info);
  284. (void) ioctl(this->hidden->audio_fd, AUDIO_GETINFO, &info);
  285. this->spec.freq = info.play.sample_rate;
  286. /* Allocate mixing buffer */
  287. this->hidden->mixlen = this->spec.size;
  288. this->hidden->mixbuf = (Uint8 *) SDL_AllocAudioMem(this->hidden->mixlen);
  289. if (this->hidden->mixbuf == NULL) {
  290. BSDAUDIO_CloseDevice(this);
  291. return SDL_OutOfMemory();
  292. }
  293. SDL_memset(this->hidden->mixbuf, this->spec.silence, this->spec.size);
  294. BSDAUDIO_Status(this);
  295. /* We're ready to rock and roll. :-) */
  296. return 0;
  297. }
  298. static int
  299. BSDAUDIO_Init(SDL_AudioDriverImpl * impl)
  300. {
  301. /* Set the function pointers */
  302. impl->DetectDevices = BSDAUDIO_DetectDevices;
  303. impl->OpenDevice = BSDAUDIO_OpenDevice;
  304. impl->PlayDevice = BSDAUDIO_PlayDevice;
  305. impl->WaitDevice = BSDAUDIO_WaitDevice;
  306. impl->GetDeviceBuf = BSDAUDIO_GetDeviceBuf;
  307. impl->CloseDevice = BSDAUDIO_CloseDevice;
  308. return 1; /* this audio target is available. */
  309. }
  310. AudioBootStrap BSD_AUDIO_bootstrap = {
  311. "bsd", "BSD audio", BSDAUDIO_Init, 0
  312. };
  313. #endif /* SDL_AUDIO_DRIVER_BSD */
  314. /* vi: set ts=4 sw=4 expandtab: */