alsa_device.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. Copyright (C) 2004-2006 Jean-Marc Valin
  3. Copyright (C) 2006 Commonwealth Scientific and Industrial Research
  4. Organisation (CSIRO) Australia
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are
  7. met:
  8. 1. Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  19. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  23. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  24. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  25. POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "alsa_device.h"
  28. #include <stdlib.h>
  29. #include <alsa/asoundlib.h>
  30. struct AlsaDevice_ {
  31. char *device_name;
  32. int channels;
  33. int period;
  34. snd_pcm_t *capture_handle;
  35. snd_pcm_t *playback_handle;
  36. int readN, writeN;
  37. struct pollfd *read_fd, *write_fd;
  38. };
  39. AlsaDevice *alsa_device_open(char *device_name, unsigned int rate, int channels, int period)
  40. {
  41. int dir;
  42. int err;
  43. snd_pcm_hw_params_t *hw_params;
  44. snd_pcm_sw_params_t *sw_params;
  45. snd_pcm_uframes_t period_size = period;
  46. snd_pcm_uframes_t buffer_size = 2*period;
  47. static snd_output_t *jcd_out;
  48. AlsaDevice *dev = malloc(sizeof(*dev));
  49. if (!dev)
  50. return NULL;
  51. dev->device_name = malloc(1+strlen(device_name));
  52. if (!dev->device_name)
  53. {
  54. free(dev);
  55. return NULL;
  56. }
  57. strcpy(dev->device_name, device_name);
  58. dev->channels = channels;
  59. dev->period = period;
  60. err = snd_output_stdio_attach(&jcd_out, stdout, 0);
  61. if ((err = snd_pcm_open (&dev->capture_handle, dev->device_name, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
  62. fprintf (stderr, "cannot open audio device %s (%s)\n",
  63. dev->device_name,
  64. snd_strerror (err));
  65. assert(0);
  66. }
  67. if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
  68. fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
  69. snd_strerror (err));
  70. assert(0);
  71. }
  72. if ((err = snd_pcm_hw_params_any (dev->capture_handle, hw_params)) < 0) {
  73. fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
  74. snd_strerror (err));
  75. assert(0);
  76. }
  77. if ((err = snd_pcm_hw_params_set_access (dev->capture_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
  78. fprintf (stderr, "cannot set access type (%s)\n",
  79. snd_strerror (err));
  80. assert(0);
  81. }
  82. if ((err = snd_pcm_hw_params_set_format (dev->capture_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
  83. fprintf (stderr, "cannot set sample format (%s)\n",
  84. snd_strerror (err));
  85. assert(0);
  86. }
  87. if ((err = snd_pcm_hw_params_set_rate_near (dev->capture_handle, hw_params, &rate, 0)) < 0) {
  88. fprintf (stderr, "cannot set sample rate (%s)\n",
  89. snd_strerror (err));
  90. assert(0);
  91. }
  92. /*fprintf (stderr, "rate = %d\n", rate);*/
  93. if ((err = snd_pcm_hw_params_set_channels (dev->capture_handle, hw_params, channels)) < 0) {
  94. fprintf (stderr, "cannot set channel count (%s)\n",
  95. snd_strerror (err));
  96. assert(0);
  97. }
  98. period_size = period;
  99. dir = 0;
  100. if ((err = snd_pcm_hw_params_set_period_size_near (dev->capture_handle, hw_params, &period_size, &dir)) < 0) {
  101. fprintf (stderr, "cannot set period size (%s)\n",
  102. snd_strerror (err));
  103. assert(0);
  104. }
  105. if ((err = snd_pcm_hw_params_set_periods (dev->capture_handle, hw_params, 2, 0)) < 0) {
  106. fprintf (stderr, "cannot set number of periods (%s)\n",
  107. snd_strerror (err));
  108. assert(0);
  109. }
  110. buffer_size = period_size * 2;
  111. dir=0;
  112. if ((err = snd_pcm_hw_params_set_buffer_size_near (dev->capture_handle, hw_params, &buffer_size)) < 0) {
  113. fprintf (stderr, "cannot set buffer time (%s)\n",
  114. snd_strerror (err));
  115. assert(0);
  116. }
  117. if ((err = snd_pcm_hw_params (dev->capture_handle, hw_params)) < 0) {
  118. fprintf (stderr, "cannot set capture parameters (%s)\n",
  119. snd_strerror (err));
  120. assert(0);
  121. }
  122. /*snd_pcm_dump_setup(dev->capture_handle, jcd_out);*/
  123. snd_pcm_hw_params_free (hw_params);
  124. if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
  125. fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
  126. snd_strerror (err));
  127. assert(0);
  128. }
  129. if ((err = snd_pcm_sw_params_current (dev->capture_handle, sw_params)) < 0) {
  130. fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
  131. snd_strerror (err));
  132. assert(0);
  133. }
  134. if ((err = snd_pcm_sw_params_set_avail_min (dev->capture_handle, sw_params, period)) < 0) {
  135. fprintf (stderr, "cannot set minimum available count (%s)\n",
  136. snd_strerror (err));
  137. assert(0);
  138. }
  139. if ((err = snd_pcm_sw_params (dev->capture_handle, sw_params)) < 0) {
  140. fprintf (stderr, "cannot set software parameters (%s)\n",
  141. snd_strerror (err));
  142. assert(0);
  143. }
  144. if ((err = snd_pcm_open (&dev->playback_handle, dev->device_name, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  145. fprintf (stderr, "cannot open audio device %s (%s)\n",
  146. dev->device_name,
  147. snd_strerror (err));
  148. assert(0);
  149. }
  150. if ((err = snd_pcm_hw_params_malloc (&hw_params)) < 0) {
  151. fprintf (stderr, "cannot allocate hardware parameter structure (%s)\n",
  152. snd_strerror (err));
  153. assert(0);
  154. }
  155. if ((err = snd_pcm_hw_params_any (dev->playback_handle, hw_params)) < 0) {
  156. fprintf (stderr, "cannot initialize hardware parameter structure (%s)\n",
  157. snd_strerror (err));
  158. assert(0);
  159. }
  160. if ((err = snd_pcm_hw_params_set_access (dev->playback_handle, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED)) < 0) {
  161. fprintf (stderr, "cannot set access type (%s)\n",
  162. snd_strerror (err));
  163. assert(0);
  164. }
  165. if ((err = snd_pcm_hw_params_set_format (dev->playback_handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
  166. fprintf (stderr, "cannot set sample format (%s)\n",
  167. snd_strerror (err));
  168. assert(0);
  169. }
  170. if ((err = snd_pcm_hw_params_set_rate_near (dev->playback_handle, hw_params, &rate, 0)) < 0) {
  171. fprintf (stderr, "cannot set sample rate (%s)\n",
  172. snd_strerror (err));
  173. assert(0);
  174. }
  175. /*fprintf (stderr, "rate = %d\n", rate);*/
  176. if ((err = snd_pcm_hw_params_set_channels (dev->playback_handle, hw_params, channels)) < 0) {
  177. fprintf (stderr, "cannot set channel count (%s)\n",
  178. snd_strerror (err));
  179. assert(0);
  180. }
  181. period_size = period;
  182. dir = 0;
  183. if ((err = snd_pcm_hw_params_set_period_size_near (dev->playback_handle, hw_params, &period_size, &dir)) < 0) {
  184. fprintf (stderr, "cannot set period size (%s)\n",
  185. snd_strerror (err));
  186. assert(0);
  187. }
  188. if ((err = snd_pcm_hw_params_set_periods (dev->playback_handle, hw_params, 2, 0)) < 0) {
  189. fprintf (stderr, "cannot set number of periods (%s)\n",
  190. snd_strerror (err));
  191. assert(0);
  192. }
  193. buffer_size = period_size * 2;
  194. dir=0;
  195. if ((err = snd_pcm_hw_params_set_buffer_size_near (dev->playback_handle, hw_params, &buffer_size)) < 0) {
  196. fprintf (stderr, "cannot set buffer time (%s)\n",
  197. snd_strerror (err));
  198. assert(0);
  199. }
  200. if ((err = snd_pcm_hw_params (dev->playback_handle, hw_params)) < 0) {
  201. fprintf (stderr, "cannot set playback parameters (%s)\n",
  202. snd_strerror (err));
  203. assert(0);
  204. }
  205. /*snd_pcm_dump_setup(dev->playback_handle, jcd_out);*/
  206. snd_pcm_hw_params_free (hw_params);
  207. if ((err = snd_pcm_sw_params_malloc (&sw_params)) < 0) {
  208. fprintf (stderr, "cannot allocate software parameters structure (%s)\n",
  209. snd_strerror (err));
  210. assert(0);
  211. }
  212. if ((err = snd_pcm_sw_params_current (dev->playback_handle, sw_params)) < 0) {
  213. fprintf (stderr, "cannot initialize software parameters structure (%s)\n",
  214. snd_strerror (err));
  215. assert(0);
  216. }
  217. if ((err = snd_pcm_sw_params_set_avail_min (dev->playback_handle, sw_params, period)) < 0) {
  218. fprintf (stderr, "cannot set minimum available count (%s)\n",
  219. snd_strerror (err));
  220. assert(0);
  221. }
  222. if ((err = snd_pcm_sw_params_set_start_threshold (dev->playback_handle, sw_params, period)) < 0) {
  223. fprintf (stderr, "cannot set start mode (%s)\n",
  224. snd_strerror (err));
  225. assert(0);
  226. }
  227. if ((err = snd_pcm_sw_params (dev->playback_handle, sw_params)) < 0) {
  228. fprintf (stderr, "cannot set software parameters (%s)\n",
  229. snd_strerror (err));
  230. assert(0);
  231. }
  232. snd_pcm_link(dev->capture_handle, dev->playback_handle);
  233. if ((err = snd_pcm_prepare (dev->capture_handle)) < 0) {
  234. fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
  235. snd_strerror (err));
  236. assert(0);
  237. }
  238. if ((err = snd_pcm_prepare (dev->playback_handle)) < 0) {
  239. fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
  240. snd_strerror (err));
  241. assert(0);
  242. }
  243. dev->readN = snd_pcm_poll_descriptors_count(dev->capture_handle);
  244. dev->writeN = snd_pcm_poll_descriptors_count(dev->playback_handle);
  245. dev->read_fd = malloc(dev->readN*sizeof(*dev->read_fd));
  246. /*printf ("descriptors: %d %d\n", dev->readN, dev->writeN);*/
  247. if (snd_pcm_poll_descriptors(dev->capture_handle, dev->read_fd, dev->readN) != dev->readN)
  248. {
  249. fprintf (stderr, "cannot obtain capture file descriptors (%s)\n",
  250. snd_strerror (err));
  251. assert(0);
  252. }
  253. dev->write_fd = malloc(dev->writeN*sizeof(*dev->read_fd));
  254. if (snd_pcm_poll_descriptors(dev->playback_handle, dev->write_fd, dev->writeN) != dev->writeN)
  255. {
  256. fprintf (stderr, "cannot obtain playback file descriptors (%s)\n",
  257. snd_strerror (err));
  258. assert(0);
  259. }
  260. return dev;
  261. }
  262. void alsa_device_close(AlsaDevice *dev)
  263. {
  264. snd_pcm_close(dev->capture_handle);
  265. snd_pcm_close(dev->playback_handle);
  266. free(dev->device_name);
  267. free(dev);
  268. }
  269. int alsa_device_read(AlsaDevice *dev, short *pcm, int len)
  270. {
  271. int err;
  272. /*fprintf (stderr, "-");*/
  273. if ((err = snd_pcm_readi (dev->capture_handle, pcm, len)) != len)
  274. {
  275. if (err<0)
  276. {
  277. //fprintf(stderr, "error %d, EPIPE = %d\n", err, EPIPE);
  278. if (err == -EPIPE)
  279. {
  280. fprintf (stderr, "An overrun has occurred, resetting capture\n");
  281. } else
  282. {
  283. fprintf (stderr, "read from audio interface failed (%s)\n",
  284. snd_strerror (err));
  285. }
  286. if ((err = snd_pcm_prepare (dev->capture_handle)) < 0)
  287. {
  288. fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
  289. snd_strerror (err));
  290. }
  291. if ((err = snd_pcm_start (dev->capture_handle)) < 0)
  292. {
  293. fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
  294. snd_strerror (err));
  295. }
  296. /*alsa_device_read(dev,pcm,len);*/
  297. } else {
  298. fprintf (stderr, "Couldn't read as many samples as I wanted (%d instead of %d)\n", err, len);
  299. }
  300. return 1;
  301. }
  302. return 0;
  303. }
  304. int alsa_device_write(AlsaDevice *dev, const short *pcm, int len)
  305. {
  306. int err;
  307. /*fprintf (stderr, "+");*/
  308. if ((err = snd_pcm_writei (dev->playback_handle, pcm, len)) != len)
  309. {
  310. if (err<0)
  311. {
  312. if (err == -EPIPE)
  313. {
  314. fprintf (stderr, "An underrun has occurred, resetting playback, len=%d\n", len);
  315. } else
  316. {
  317. fprintf (stderr, "write to audio interface failed (%s)\n",
  318. snd_strerror (err));
  319. }
  320. if ((err = snd_pcm_prepare (dev->playback_handle)) < 0)
  321. {
  322. fprintf (stderr, "cannot prepare audio interface for use (%s)\n",
  323. snd_strerror (err));
  324. }
  325. } else {
  326. fprintf (stderr, "Couldn't write as many samples as I wanted (%d instead of %d)\n", err, len);
  327. }
  328. /*alsa_device_write(dev,pcm,len);*/
  329. return 1;
  330. }
  331. return 0;
  332. }
  333. int alsa_device_capture_ready(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds)
  334. {
  335. unsigned short revents=0;
  336. int err;
  337. if ((err = snd_pcm_poll_descriptors_revents(dev->capture_handle, pfds, dev->readN, &revents)) < 0)
  338. {
  339. //cerr << "error in snd_pcm_poll_descriptors_revents for capture: " << snd_strerror (err) << endl;
  340. //FIXME: This is a kludge
  341. fprintf (stderr, "error in alsa_device_capture_ready: %s\n", snd_strerror (err));
  342. return pfds[0].revents & POLLIN;
  343. }
  344. //cerr << (revents & POLLERR) << endl;
  345. return revents & POLLIN;
  346. }
  347. int alsa_device_playback_ready(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds)
  348. {
  349. unsigned short revents=0;
  350. int err;
  351. if ((err = snd_pcm_poll_descriptors_revents(dev->playback_handle, pfds+dev->readN, dev->writeN, &revents)) < 0)
  352. {
  353. //cerr << "error in snd_pcm_poll_descriptors_revents for playback: " << snd_strerror (err) << endl;
  354. //FIXME: This is a kludge
  355. fprintf (stderr, "error in alsa_device_playback_ready: %s\n", snd_strerror (err));
  356. return pfds[1].revents & POLLOUT;
  357. }
  358. //cerr << (revents & POLLERR) << endl;
  359. return revents & POLLOUT;
  360. }
  361. void alsa_device_start(AlsaDevice *dev)
  362. {
  363. int i;
  364. short pcm[dev->period*dev->channels];
  365. for (i=0;i<dev->period*dev->channels;i++)
  366. pcm[i] = 0;
  367. alsa_device_write(dev, pcm, dev->period);
  368. alsa_device_write(dev, pcm, dev->period);
  369. snd_pcm_start(dev->capture_handle);
  370. snd_pcm_start(dev->playback_handle);
  371. }
  372. int alsa_device_nfds(AlsaDevice *dev)
  373. {
  374. return dev->writeN+dev->readN;
  375. }
  376. void alsa_device_getfds(AlsaDevice *dev, struct pollfd *pfds, unsigned int nfds)
  377. {
  378. int i;
  379. assert (nfds >= dev->writeN+dev->readN);
  380. for (i=0;i<dev->readN;i++)
  381. pfds[i] = dev->read_fd[i];
  382. for (i=0;i<dev->writeN;i++)
  383. pfds[i+dev->readN] = dev->write_fd[i];
  384. }