PageRenderTime 58ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/external/qemu/distrib/sdl-1.2.15/src/audio/dsp/SDL_dspaudio.c

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
C | 340 lines | 249 code | 37 blank | 54 comment | 41 complexity | 9814c68fd01f464a95710e2ebd35a6cf MD5 | raw file
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2012 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. slouken@libsdl.org
  17. Modified in Oct 2004 by Hannu Savolainen
  18. hannu@opensound.com
  19. */
  20. #include "SDL_config.h"
  21. /* Allow access to a raw mixing buffer */
  22. #include <stdio.h> /* For perror() */
  23. #include <string.h> /* For strerror() */
  24. #include <errno.h>
  25. #include <unistd.h>
  26. #include <fcntl.h>
  27. #include <signal.h>
  28. #include <sys/time.h>
  29. #include <sys/ioctl.h>
  30. #include <sys/stat.h>
  31. #if SDL_AUDIO_DRIVER_OSS_SOUNDCARD_H
  32. /* This is installed on some systems */
  33. #include <soundcard.h>
  34. #else
  35. /* This is recommended by OSS */
  36. #include <sys/soundcard.h>
  37. #endif
  38. #include "SDL_timer.h"
  39. #include "SDL_audio.h"
  40. #include "../SDL_audiomem.h"
  41. #include "../SDL_audio_c.h"
  42. #include "../SDL_audiodev_c.h"
  43. #include "SDL_dspaudio.h"
  44. /* The tag name used by DSP audio */
  45. #define DSP_DRIVER_NAME "dsp"
  46. /* Open the audio device for playback, and don't block if busy */
  47. #define OPEN_FLAGS (O_WRONLY|O_NONBLOCK)
  48. /* Audio driver functions */
  49. static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec);
  50. static void DSP_WaitAudio(_THIS);
  51. static void DSP_PlayAudio(_THIS);
  52. static Uint8 *DSP_GetAudioBuf(_THIS);
  53. static void DSP_CloseAudio(_THIS);
  54. /* Audio driver bootstrap functions */
  55. static int Audio_Available(void)
  56. {
  57. int fd;
  58. int available;
  59. available = 0;
  60. fd = SDL_OpenAudioPath(NULL, 0, OPEN_FLAGS, 0);
  61. if ( fd >= 0 ) {
  62. available = 1;
  63. close(fd);
  64. }
  65. return(available);
  66. }
  67. static void Audio_DeleteDevice(SDL_AudioDevice *device)
  68. {
  69. SDL_free(device->hidden);
  70. SDL_free(device);
  71. }
  72. static SDL_AudioDevice *Audio_CreateDevice(int devindex)
  73. {
  74. SDL_AudioDevice *this;
  75. /* Initialize all variables that we clean on shutdown */
  76. this = (SDL_AudioDevice *)SDL_malloc(sizeof(SDL_AudioDevice));
  77. if ( this ) {
  78. SDL_memset(this, 0, (sizeof *this));
  79. this->hidden = (struct SDL_PrivateAudioData *)
  80. SDL_malloc((sizeof *this->hidden));
  81. }
  82. if ( (this == NULL) || (this->hidden == NULL) ) {
  83. SDL_OutOfMemory();
  84. if ( this ) {
  85. SDL_free(this);
  86. }
  87. return(0);
  88. }
  89. SDL_memset(this->hidden, 0, (sizeof *this->hidden));
  90. audio_fd = -1;
  91. /* Set the function pointers */
  92. this->OpenAudio = DSP_OpenAudio;
  93. this->WaitAudio = DSP_WaitAudio;
  94. this->PlayAudio = DSP_PlayAudio;
  95. this->GetAudioBuf = DSP_GetAudioBuf;
  96. this->CloseAudio = DSP_CloseAudio;
  97. this->free = Audio_DeleteDevice;
  98. return this;
  99. }
  100. AudioBootStrap DSP_bootstrap = {
  101. DSP_DRIVER_NAME, "OSS /dev/dsp standard audio",
  102. Audio_Available, Audio_CreateDevice
  103. };
  104. /* This function waits until it is possible to write a full sound buffer */
  105. static void DSP_WaitAudio(_THIS)
  106. {
  107. /* Not needed at all since OSS handles waiting automagically */
  108. }
  109. static void DSP_PlayAudio(_THIS)
  110. {
  111. if (write(audio_fd, mixbuf, mixlen)==-1)
  112. {
  113. perror("Audio write");
  114. this->enabled = 0;
  115. }
  116. #ifdef DEBUG_AUDIO
  117. fprintf(stderr, "Wrote %d bytes of audio data\n", mixlen);
  118. #endif
  119. }
  120. static Uint8 *DSP_GetAudioBuf(_THIS)
  121. {
  122. return(mixbuf);
  123. }
  124. static void DSP_CloseAudio(_THIS)
  125. {
  126. if ( mixbuf != NULL ) {
  127. SDL_FreeAudioMem(mixbuf);
  128. mixbuf = NULL;
  129. }
  130. if ( audio_fd >= 0 ) {
  131. close(audio_fd);
  132. audio_fd = -1;
  133. }
  134. }
  135. static int DSP_OpenAudio(_THIS, SDL_AudioSpec *spec)
  136. {
  137. char audiodev[1024];
  138. int format;
  139. int value;
  140. int frag_spec;
  141. Uint16 test_format;
  142. /* Make sure fragment size stays a power of 2, or OSS fails. */
  143. /* I don't know which of these are actually legal values, though... */
  144. if (spec->channels > 8)
  145. spec->channels = 8;
  146. else if (spec->channels > 4)
  147. spec->channels = 4;
  148. else if (spec->channels > 2)
  149. spec->channels = 2;
  150. /* Open the audio device */
  151. audio_fd = SDL_OpenAudioPath(audiodev, sizeof(audiodev), OPEN_FLAGS, 0);
  152. if ( audio_fd < 0 ) {
  153. SDL_SetError("Couldn't open %s: %s", audiodev, strerror(errno));
  154. return(-1);
  155. }
  156. mixbuf = NULL;
  157. /* Make the file descriptor use blocking writes with fcntl() */
  158. { long flags;
  159. flags = fcntl(audio_fd, F_GETFL);
  160. flags &= ~O_NONBLOCK;
  161. if ( fcntl(audio_fd, F_SETFL, flags) < 0 ) {
  162. SDL_SetError("Couldn't set audio blocking mode");
  163. DSP_CloseAudio(this);
  164. return(-1);
  165. }
  166. }
  167. /* Get a list of supported hardware formats */
  168. if ( ioctl(audio_fd, SNDCTL_DSP_GETFMTS, &value) < 0 ) {
  169. perror("SNDCTL_DSP_GETFMTS");
  170. SDL_SetError("Couldn't get audio format list");
  171. DSP_CloseAudio(this);
  172. return(-1);
  173. }
  174. /* Try for a closest match on audio format */
  175. format = 0;
  176. for ( test_format = SDL_FirstAudioFormat(spec->format);
  177. ! format && test_format; ) {
  178. #ifdef DEBUG_AUDIO
  179. fprintf(stderr, "Trying format 0x%4.4x\n", test_format);
  180. #endif
  181. switch ( test_format ) {
  182. case AUDIO_U8:
  183. if ( value & AFMT_U8 ) {
  184. format = AFMT_U8;
  185. }
  186. break;
  187. case AUDIO_S16LSB:
  188. if ( value & AFMT_S16_LE ) {
  189. format = AFMT_S16_LE;
  190. }
  191. break;
  192. case AUDIO_S16MSB:
  193. if ( value & AFMT_S16_BE ) {
  194. format = AFMT_S16_BE;
  195. }
  196. break;
  197. #if 0
  198. /*
  199. * These formats are not used by any real life systems so they are not
  200. * needed here.
  201. */
  202. case AUDIO_S8:
  203. if ( value & AFMT_S8 ) {
  204. format = AFMT_S8;
  205. }
  206. break;
  207. case AUDIO_U16LSB:
  208. if ( value & AFMT_U16_LE ) {
  209. format = AFMT_U16_LE;
  210. }
  211. break;
  212. case AUDIO_U16MSB:
  213. if ( value & AFMT_U16_BE ) {
  214. format = AFMT_U16_BE;
  215. }
  216. break;
  217. #endif
  218. default:
  219. format = 0;
  220. break;
  221. }
  222. if ( ! format ) {
  223. test_format = SDL_NextAudioFormat();
  224. }
  225. }
  226. if ( format == 0 ) {
  227. SDL_SetError("Couldn't find any hardware audio formats");
  228. DSP_CloseAudio(this);
  229. return(-1);
  230. }
  231. spec->format = test_format;
  232. /* Set the audio format */
  233. value = format;
  234. if ( (ioctl(audio_fd, SNDCTL_DSP_SETFMT, &value) < 0) ||
  235. (value != format) ) {
  236. perror("SNDCTL_DSP_SETFMT");
  237. SDL_SetError("Couldn't set audio format");
  238. DSP_CloseAudio(this);
  239. return(-1);
  240. }
  241. /* Set the number of channels of output */
  242. value = spec->channels;
  243. if ( ioctl(audio_fd, SNDCTL_DSP_CHANNELS, &value) < 0 ) {
  244. perror("SNDCTL_DSP_CHANNELS");
  245. SDL_SetError("Cannot set the number of channels");
  246. DSP_CloseAudio(this);
  247. return(-1);
  248. }
  249. spec->channels = value;
  250. /* Set the DSP frequency */
  251. value = spec->freq;
  252. if ( ioctl(audio_fd, SNDCTL_DSP_SPEED, &value) < 0 ) {
  253. perror("SNDCTL_DSP_SPEED");
  254. SDL_SetError("Couldn't set audio frequency");
  255. DSP_CloseAudio(this);
  256. return(-1);
  257. }
  258. spec->freq = value;
  259. /* Calculate the final parameters for this audio specification */
  260. SDL_CalculateAudioSpec(spec);
  261. /* Determine the power of two of the fragment size */
  262. for ( frag_spec = 0; (0x01U<<frag_spec) < spec->size; ++frag_spec );
  263. if ( (0x01U<<frag_spec) != spec->size ) {
  264. SDL_SetError("Fragment size must be a power of two");
  265. DSP_CloseAudio(this);
  266. return(-1);
  267. }
  268. frag_spec |= 0x00020000; /* two fragments, for low latency */
  269. /* Set the audio buffering parameters */
  270. #ifdef DEBUG_AUDIO
  271. fprintf(stderr, "Requesting %d fragments of size %d\n",
  272. (frag_spec >> 16), 1<<(frag_spec&0xFFFF));
  273. #endif
  274. if ( ioctl(audio_fd, SNDCTL_DSP_SETFRAGMENT, &frag_spec) < 0 ) {
  275. perror("SNDCTL_DSP_SETFRAGMENT");
  276. }
  277. #ifdef DEBUG_AUDIO
  278. { audio_buf_info info;
  279. ioctl(audio_fd, SNDCTL_DSP_GETOSPACE, &info);
  280. fprintf(stderr, "fragments = %d\n", info.fragments);
  281. fprintf(stderr, "fragstotal = %d\n", info.fragstotal);
  282. fprintf(stderr, "fragsize = %d\n", info.fragsize);
  283. fprintf(stderr, "bytes = %d\n", info.bytes);
  284. }
  285. #endif
  286. /* Allocate mixing buffer */
  287. mixlen = spec->size;
  288. mixbuf = (Uint8 *)SDL_AllocAudioMem(mixlen);
  289. if ( mixbuf == NULL ) {
  290. DSP_CloseAudio(this);
  291. return(-1);
  292. }
  293. SDL_memset(mixbuf, spec->silence, spec->size);
  294. /* Get the parent process id (we're the parent of the audio thread) */
  295. parent = getpid();
  296. /* We're ready to rock and roll. :-) */
  297. return(0);
  298. }