PageRenderTime 62ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/src/engine/AlsaDriver.cpp

#
C++ | 1667 lines | 1242 code | 331 blank | 94 comment | 276 complexity | f75f6af21a43a346825630969e5c2d17 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-2.0
  1. /*
  2. Copyright (C) 2005-2010 Remon Sijrier
  3. (December 2005) Ported to C++ for Traverso by Remon Sijrier
  4. Copyright (C) 2001 Paul Davis
  5. This file is part of Traverso
  6. Traverso is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. /* ALSA driver based on jack-audio-connection-kit-0.109.0 alsa_driver.c */
  19. #include "AlsaDriver.h"
  20. #include "AudioChannel.h"
  21. #include <Utils.h>
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include <memory.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <errno.h>
  28. #include <stdarg.h>
  29. #include <signal.h>
  30. #include <sys/types.h>
  31. #include <regex.h>
  32. #include <string.h>
  33. #include <sys/time.h>
  34. #include <time.h>
  35. #include "AudioDevice.h"
  36. // Always put me below _all_ includes, this is needed
  37. // in case we run with memory leak detection enabled!
  38. #include "Debugger.h"
  39. #undef DEBUG_WAKEUP
  40. /* Delay (in process calls) before Traverso will report an xrun */
  41. #define XRUN_REPORT_DELAY 0
  42. AlsaDriver::AlsaDriver(AudioDevice* dev, int rate, nframes_t bufferSize)
  43. : TAudioDriver(dev, rate, bufferSize)
  44. {
  45. read = MakeDelegate(this, &AlsaDriver::_read);
  46. write = MakeDelegate(this, &AlsaDriver::_write);
  47. run_cycle = RunCycleCallback(this, &AlsaDriver::_run_cycle);
  48. }
  49. AlsaDriver::~AlsaDriver()
  50. {
  51. PENTERDES;
  52. if (capture_handle) {
  53. snd_pcm_close (capture_handle);
  54. }
  55. if (playback_handle) {
  56. snd_pcm_close (playback_handle);
  57. }
  58. if (capture_hw_params) {
  59. snd_pcm_hw_params_free (capture_hw_params);
  60. }
  61. if (playback_hw_params) {
  62. snd_pcm_hw_params_free (playback_hw_params);
  63. }
  64. if (capture_sw_params) {
  65. snd_pcm_sw_params_free (capture_sw_params);
  66. }
  67. if (playback_sw_params) {
  68. snd_pcm_sw_params_free (playback_sw_params);
  69. }
  70. if (pfd) {
  71. free (pfd);
  72. }
  73. free (alsa_name_playback);
  74. free (alsa_name_capture);
  75. release_channel_dependent_memory ();
  76. }
  77. int AlsaDriver::setup(bool capture, bool playback, const QString& devicename, const QString& ditherShape)
  78. {
  79. unsigned long user_nperiods = device->get_driver_property("numberofperiods", 3).toInt();
  80. QString pcmName = "default";
  81. if (devicename != "default") {
  82. pcmName = QString("hw:%1").arg(get_device_id_by_name(devicename));
  83. }
  84. char *playback_pcm_name = strdup(pcmName.toAscii().data());
  85. char *capture_pcm_name = strdup(pcmName.toAscii().data());
  86. int shorts_first = false;
  87. /* duplex is the default */
  88. if (!capture && !playback) {
  89. capture = true;
  90. playback = true;
  91. }
  92. int err;
  93. playback_handle = (snd_pcm_t*) 0;
  94. capture_handle = (snd_pcm_t*) 0;
  95. ctl_handle = 0;
  96. capture_and_playback_not_synced = false;
  97. capture_interleaved = false;
  98. playback_interleaved = false;
  99. max_nchannels = 0;
  100. user_nchannels = 0;
  101. playback_nchannels = 0;
  102. capture_nchannels = 0;
  103. playback_sample_bytes = (shorts_first ? 2:4);
  104. capture_sample_bytes = (shorts_first ? 2:4);
  105. capture_frame_latency = 0;
  106. playback_frame_latency = 0;
  107. channels_done = 0;
  108. channels_not_done = 0;
  109. dither_state = 0;
  110. playback_addr = 0;
  111. capture_addr = 0;
  112. playback_interleave_skip = NULL;
  113. capture_interleave_skip = NULL;
  114. playback_hw_params = 0;
  115. capture_hw_params = 0;
  116. playback_sw_params = 0;
  117. capture_sw_params = 0;
  118. silent = 0;
  119. pfd = 0;
  120. playback_nfds = 0;
  121. capture_nfds = 0;
  122. if (ditherShape == "Rectangular") {
  123. dither = Rectangular;
  124. } else if (ditherShape == "Shaped") {
  125. dither = Shaped;
  126. } else if (ditherShape == "Triangular") {
  127. dither = Triangular;
  128. } else {
  129. dither = None;
  130. }
  131. soft_mode = false;
  132. quirk_bswap = 0;
  133. process_count = 0;
  134. alsa_name_playback = strdup (playback_pcm_name);
  135. alsa_name_capture = strdup (capture_pcm_name);
  136. printf ("creating alsa driver ... %s|%s|%d|%lu|%d|%d|%d|%s|%s\n",
  137. playback ? playback_pcm_name : "-",
  138. capture ? capture_pcm_name : "-",
  139. frames_per_cycle, user_nperiods, frame_rate,
  140. (int)capture_nchannels, (int)playback_nchannels,
  141. soft_mode ? "soft-mode":"-",
  142. shorts_first ? "16bit":"32bit");
  143. if (playback) {
  144. if (snd_pcm_open (&playback_handle, alsa_name_playback, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK) < 0) {
  145. switch (errno) {
  146. case EBUSY:
  147. device->driverSetupMessage(tr("The playback device '%1'' is already in use. Please stop the"
  148. "application using it and run Traverso again").
  149. arg(playback_pcm_name), AudioDevice::DRIVER_SETUP_FAILURE);
  150. return -1;
  151. break;
  152. case EPERM:
  153. device->driverSetupMessage(tr("You do not have permission to open the audio device '%1' for playback").
  154. arg(playback_pcm_name), AudioDevice::DRIVER_SETUP_FAILURE);
  155. return -1;
  156. break;
  157. default:
  158. device->driverSetupMessage(tr("Opening Playback Device '%1' failed with unknown error type").
  159. arg(playback_pcm_name), AudioDevice::DRIVER_SETUP_WARNING);
  160. }
  161. playback_handle = 0;
  162. }
  163. if (playback_handle) {
  164. snd_pcm_nonblock (playback_handle, 0);
  165. }
  166. }
  167. if (capture) {
  168. if (snd_pcm_open (&capture_handle, alsa_name_capture, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK) < 0) {
  169. switch (errno) {
  170. case EBUSY:
  171. device->driverSetupMessage(tr("The Capture Device %1 is already in use. Please stop the"
  172. " application using it and run Traverso again").arg(capture_pcm_name), AudioDevice::DRIVER_SETUP_FAILURE);
  173. return -1;
  174. break;
  175. case EPERM:
  176. device->driverSetupMessage(tr("You do not have permission to open Device %1 for capture").
  177. arg(capture_pcm_name), AudioDevice::DRIVER_SETUP_FAILURE);
  178. return -1;
  179. break;
  180. default:
  181. device->driverSetupMessage(tr("Opening Capture Device %1 failed with unknown error type").
  182. arg(capture_pcm_name), AudioDevice::DRIVER_SETUP_WARNING);
  183. }
  184. capture_handle = 0;
  185. }
  186. if (capture_handle) {
  187. snd_pcm_nonblock (capture_handle, 0);
  188. }
  189. }
  190. if (playback_handle == 0) {
  191. if (playback) {
  192. if (capture_handle == 0) {
  193. /* can't do anything */
  194. device->driverSetupMessage(tr("Unable to configure Device %1").arg(pcmName), AudioDevice::DRIVER_SETUP_FAILURE);
  195. return -1;
  196. }
  197. /* they asked for playback, but we can't do it */
  198. device->driverSetupMessage(tr("Falling back to capture-only mode for device %1").arg(playback_pcm_name), AudioDevice::DRIVER_SETUP_WARNING);
  199. playback = false;
  200. }
  201. }
  202. if (capture_handle == 0) {
  203. if (capture) {
  204. if (playback_handle == 0) {
  205. /* can't do anything */
  206. device->driverSetupMessage(tr("Unable to configure Device %1").arg(pcmName), AudioDevice::DRIVER_SETUP_FAILURE);
  207. return -1;
  208. }
  209. /* they asked for capture, but we can't do it */
  210. device->driverSetupMessage(tr("Falling back to playback-only mode for device %1").arg(playback_pcm_name), AudioDevice::DRIVER_SETUP_WARNING);
  211. capture = false;
  212. }
  213. }
  214. if (playback_handle) {
  215. if ((err = snd_pcm_hw_params_malloc (&playback_hw_params)) < 0) {
  216. PWARN ("ALSA: could not allocate playback hw params structure");
  217. return -1;
  218. }
  219. if ((err = snd_pcm_sw_params_malloc (&playback_sw_params)) < 0) {
  220. PWARN ("ALSA: could not allocate playback sw params structure");
  221. return -1;
  222. }
  223. }
  224. if (capture_handle) {
  225. if ((err = snd_pcm_hw_params_malloc (&capture_hw_params)) < 0) {
  226. PWARN ("ALSA: could not allocate capture hw params structure");
  227. return -1;
  228. }
  229. if ((err = snd_pcm_sw_params_malloc (&capture_sw_params)) < 0) {
  230. PWARN ("ALSA: could not allocate capture sw params structure");
  231. return -1;
  232. }
  233. }
  234. if (set_parameters (frames_per_cycle, user_nperiods, frame_rate)) {
  235. return -1;
  236. }
  237. capture_and_playback_not_synced = false;
  238. if (capture_handle && playback_handle) {
  239. if (snd_pcm_link (playback_handle, capture_handle) != 0) {
  240. capture_and_playback_not_synced = true;
  241. }
  242. }
  243. return 1;
  244. }
  245. void AlsaDriver::release_channel_dependent_memory ()
  246. {
  247. bitset_destroy (&channels_done);
  248. bitset_destroy (&channels_not_done);
  249. if (playback_addr) {
  250. free(playback_addr);
  251. playback_addr = 0;
  252. }
  253. if (capture_addr) {
  254. free(capture_addr);
  255. capture_addr = 0;
  256. }
  257. if (playback_interleave_skip) {
  258. free (playback_interleave_skip);
  259. playback_interleave_skip = NULL;
  260. }
  261. if (capture_interleave_skip) {
  262. free (capture_interleave_skip);
  263. capture_interleave_skip = NULL;
  264. }
  265. if (silent) {
  266. free(silent);
  267. silent = 0;
  268. }
  269. if (dither_state) {
  270. free(dither_state);
  271. dither_state = 0;
  272. }
  273. }
  274. void AlsaDriver::setup_io_function_pointers()
  275. {
  276. switch (playback_sample_bytes) {
  277. case 2:
  278. if (playback_interleaved) {
  279. channel_copy = memcpy_interleave_d16_s16;
  280. } else {
  281. channel_copy = memcpy_fake;
  282. }
  283. switch (dither) {
  284. case Rectangular:
  285. fprintf (stderr,"Rectangular dithering at 16 bits\n");
  286. write_via_copy = quirk_bswap?
  287. sample_move_dither_rect_d16_sSs:
  288. sample_move_dither_rect_d16_sS;
  289. break;
  290. case Triangular:
  291. printf("Triangular dithering at 16 bits\n");
  292. write_via_copy = quirk_bswap?
  293. sample_move_dither_tri_d16_sSs:
  294. sample_move_dither_tri_d16_sS;
  295. break;
  296. case Shaped:
  297. printf("Noise-shaped dithering at 16 bits\n");
  298. write_via_copy = quirk_bswap?
  299. sample_move_dither_shaped_d16_sSs:
  300. sample_move_dither_shaped_d16_sS;
  301. break;
  302. default:
  303. write_via_copy = quirk_bswap?
  304. sample_move_d16_sSs : sample_move_d16_sS;
  305. break;
  306. }
  307. break;
  308. case 3:
  309. if (playback_interleaved) {
  310. channel_copy = memcpy_interleave_d24_s24;
  311. } else {
  312. channel_copy = memcpy_fake;
  313. }
  314. switch (dither) {
  315. case Rectangular:
  316. printf("Rectangular dithering at 24 bits\n");
  317. write_via_copy = quirk_bswap?
  318. sample_move_dither_rect_d24_sSs:
  319. sample_move_dither_rect_d24_sS;
  320. break;
  321. case Triangular:
  322. printf("Triangular dithering at 24 bits\n");
  323. write_via_copy = quirk_bswap?
  324. sample_move_dither_tri_d24_sSs:
  325. sample_move_dither_tri_d24_sS;
  326. break;
  327. case Shaped:
  328. printf("Noise-shaped dithering at 24 bits\n");
  329. write_via_copy = quirk_bswap?
  330. sample_move_dither_shaped_d24_sSs:
  331. sample_move_dither_shaped_d24_sS;
  332. break;
  333. default:
  334. write_via_copy = quirk_bswap?
  335. sample_move_d24_sSs : sample_move_d24_sS;
  336. break;
  337. }
  338. break;
  339. case 4:
  340. if (playback_interleaved) {
  341. channel_copy = memcpy_interleave_d32_s32;
  342. } else {
  343. channel_copy = memcpy_fake;
  344. }
  345. switch (dither) {
  346. case Rectangular:
  347. printf("Rectangular dithering at 32 bits\n");
  348. write_via_copy = quirk_bswap?
  349. sample_move_dither_rect_d32u24_sSs:
  350. sample_move_dither_rect_d32u24_sS;
  351. break;
  352. case Triangular:
  353. printf("Triangular dithering at 16 bits\n");
  354. write_via_copy = quirk_bswap?
  355. sample_move_dither_tri_d32u24_sSs:
  356. sample_move_dither_tri_d32u24_sS;
  357. break;
  358. case Shaped:
  359. printf("Noise-shaped dithering at 32 bits\n");
  360. write_via_copy = quirk_bswap?
  361. sample_move_dither_shaped_d32u24_sSs:
  362. sample_move_dither_shaped_d32u24_sS;
  363. break;
  364. default:
  365. write_via_copy = quirk_bswap?
  366. sample_move_d32u24_sSs : sample_move_d32u24_sS;
  367. break;
  368. }
  369. break;
  370. }
  371. switch (capture_sample_bytes) {
  372. case 2:
  373. read_via_copy = quirk_bswap?
  374. sample_move_dS_s16s : sample_move_dS_s16;
  375. break;
  376. case 3:
  377. read_via_copy = quirk_bswap?
  378. sample_move_dS_s24s : sample_move_dS_s24;
  379. break;
  380. case 4:
  381. read_via_copy = quirk_bswap?
  382. sample_move_dS_s32u24s : sample_move_dS_s32u24;
  383. break;
  384. }
  385. }
  386. int AlsaDriver::configure_stream(char *device_name,
  387. const char *stream_name,
  388. snd_pcm_t *handle,
  389. snd_pcm_hw_params_t *hw_params,
  390. snd_pcm_sw_params_t *sw_params,
  391. unsigned int *nperiodsp,
  392. unsigned long *nchns,
  393. unsigned long sample_width)
  394. {
  395. int err, format;
  396. snd_pcm_uframes_t stop_th;
  397. static struct {
  398. char Name[32];
  399. snd_pcm_format_t format;
  400. int swapped;
  401. int bitdepth;
  402. } formats[] = {
  403. {"32bit little-endian", SND_PCM_FORMAT_S32_LE, IS_LE, 32},
  404. {"32bit big-endian", SND_PCM_FORMAT_S32_BE, IS_BE, 32},
  405. {"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE, 24},
  406. {"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE, 24},
  407. {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE, 16},
  408. {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE, 16},
  409. };
  410. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  411. #define FIRST_16BIT_FORMAT 4
  412. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  413. printf("ALSA: no playback configurations available (%s)\n", snd_strerror (err));
  414. return -1;
  415. }
  416. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params)) < 0) {
  417. printf("ALSA: cannot restrict period size to integral value.\n");
  418. return -1;
  419. }
  420. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  421. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  422. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
  423. printf("ALSA: mmap-based access is not possible for the %s "
  424. "stream of this audio interface\n", stream_name);
  425. return -1;
  426. }
  427. }
  428. }
  429. format = (sample_width == 4) ? 0 : (NUMFORMATS - 1);
  430. while (1) {
  431. if ((err = snd_pcm_hw_params_set_format ( handle, hw_params, formats[format].format)) < 0) {
  432. if (( (sample_width == 4) ? (format++ >= int(NUMFORMATS) - 1) : (format-- <= 0))) {
  433. printf("ALSA Driver: Sorry. The audio interface \"%s\" doesn't support any of the"
  434. " hardware sample formats that Traverso's alsa-driver can use.\n", device_name);
  435. return -1;
  436. }
  437. } else {
  438. if (formats[format].swapped) {
  439. quirk_bswap = 1;
  440. } else {
  441. quirk_bswap = 0;
  442. }
  443. printf("ALSA: final selected sample format for %s: %s\n", stream_name, formats[format].Name);
  444. device->set_bit_depth(formats[format].bitdepth);
  445. break;
  446. }
  447. }
  448. if ( (err = snd_pcm_hw_params_set_rate_near (handle, hw_params, &frame_rate, NULL)) < 0) {
  449. printf("ALSA: cannot set sample/frame rate to % for %s\n", (double)frame_rate, stream_name);
  450. return -1;
  451. }
  452. if (!*nchns) {
  453. /*if not user-specified, try to find the maximum number of channels */
  454. unsigned int channels_max ;
  455. err = snd_pcm_hw_params_get_channels_max (hw_params, &channels_max);
  456. *nchns = channels_max;
  457. if (*nchns > 1024) {
  458. /* the hapless user is an unwitting victim of
  459. the "default" ALSA PCM device, which can
  460. support up to 16 million channels. since
  461. they can't be bothered to set up a proper
  462. default device, limit the number of
  463. channels for them to a sane default.
  464. */
  465. PERROR (
  466. "ALSA Driver: You appear to be using the ALSA software \"plug\" layer, probably\n"
  467. "a result of using the \"default\" ALSA device. This is less\n"
  468. "efficient than it could be. Consider using a hardware device\n"
  469. "instead rather than using the plug layer. Usually the name of the\n"
  470. "hardware device that corresponds to the first sound card is hw:0\n"
  471. );
  472. *nchns = 2;
  473. }
  474. }
  475. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, *nchns)) < 0) {
  476. printf("ALSA: cannot set channel count to %lu for %s\n", *nchns, stream_name);
  477. return -1;
  478. }
  479. int frperscycle = frames_per_cycle;
  480. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params, frames_per_cycle, 0)) < 0) {
  481. printf("ALSA: cannot set period size to %d frames for %s\n", frperscycle, stream_name);
  482. return -1;
  483. }
  484. *nperiodsp = user_nperiods;
  485. snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
  486. if (*nperiodsp < user_nperiods)
  487. *nperiodsp = user_nperiods;
  488. if (snd_pcm_hw_params_set_periods_near (handle, hw_params, nperiodsp, NULL) < 0) {
  489. printf("ALSA: cannot set number of periods to %u for %s\n", *nperiodsp, stream_name);
  490. return -1;
  491. }
  492. if (*nperiodsp < user_nperiods) {
  493. printf("ALSA: use %d periods for %s\n", *nperiodsp, stream_name);
  494. return -1;
  495. }
  496. if (!is_power_of_two(frames_per_cycle)) {
  497. printf("Traverso: frames must be a power of two (64, 512, 1024, ...)\n");
  498. return -1;
  499. }
  500. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params, *nperiodsp * frames_per_cycle)) < 0) {
  501. printf("ALSA: cannot set buffer length to %d for %s\n", *nperiodsp * frames_per_cycle, stream_name);
  502. return -1;
  503. }
  504. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  505. printf("ALSA: cannot set hardware parameters for %s\n", stream_name);
  506. device->driverSetupMessage(tr("Unable to configure device %1, is it in use by another application?").
  507. arg(device_name), AudioDevice::DRIVER_SETUP_FAILURE);
  508. return -1;
  509. }
  510. snd_pcm_sw_params_current (handle, sw_params);
  511. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, 0U)) < 0) {
  512. printf("ALSA: cannot set start mode for %s\n", stream_name);
  513. return -1;
  514. }
  515. stop_th = *nperiodsp * frames_per_cycle;
  516. if (soft_mode) {
  517. stop_th = (snd_pcm_uframes_t)-1;
  518. }
  519. if ((err = snd_pcm_sw_params_set_stop_threshold (handle, sw_params, stop_th)) < 0) {
  520. printf("ALSA: cannot set stop mode for %s\n", stream_name);
  521. return -1;
  522. }
  523. if ((err = snd_pcm_sw_params_set_silence_threshold (handle, sw_params, 0)) < 0) {
  524. printf("ALSA: cannot set silence threshold for %s\n", stream_name);
  525. return -1;
  526. }
  527. #if 0
  528. fprintf (stderr, "set silence size to %lu * %lu = %lu\n",
  529. frames_per_cycle, *nperiodsp,
  530. frames_per_cycle * *nperiodsp);
  531. if ((err = snd_pcm_sw_params_set_silence_size (
  532. handle, sw_params,
  533. frames_per_cycle * *nperiodsp)) < 0) {
  534. PERROR ("ALSA: cannot set silence size for %s",
  535. stream_name);
  536. return -1;
  537. }
  538. #endif
  539. if (handle == playback_handle)
  540. err = snd_pcm_sw_params_set_avail_min (handle, sw_params, frames_per_cycle * (*nperiodsp - user_nperiods + 1));
  541. else
  542. err = snd_pcm_sw_params_set_avail_min (handle, sw_params, frames_per_cycle);
  543. if (err < 0) {
  544. printf("ALSA: cannot set avail min for %s\n", stream_name);
  545. return -1;
  546. }
  547. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  548. printf("ALSA: cannot set software parameters for %s\n", stream_name);
  549. return -1;
  550. }
  551. return 0;
  552. }
  553. int AlsaDriver::set_parameters (nframes_t frames_per_interupt,
  554. nframes_t nperiods,
  555. nframes_t rate)
  556. {
  557. int dir;
  558. snd_pcm_uframes_t p_period_size = 0;
  559. snd_pcm_uframes_t c_period_size = 0;
  560. channel_t chn;
  561. unsigned int pr = 0;
  562. unsigned int cr = 0;
  563. int err;
  564. frame_rate = rate;
  565. frames_per_cycle = frames_per_interupt;
  566. user_nperiods = nperiods;
  567. fprintf (stderr, "configuring for %d Hz, period=%ld frames (%.1f ms), buffer=%ld periods\n",
  568. rate, (long)frames_per_cycle,(((float)frames_per_cycle / (float) rate) * 1000.0f), user_nperiods);
  569. if (capture_handle) {
  570. if (configure_stream (
  571. alsa_name_capture,
  572. "capture",
  573. capture_handle,
  574. capture_hw_params,
  575. capture_sw_params,
  576. &capture_nperiods,
  577. &capture_nchannels,
  578. capture_sample_bytes)) {
  579. PERROR ("ALSA: cannot configure capture channel");
  580. return -1;
  581. }
  582. }
  583. if (playback_handle) {
  584. if (configure_stream (
  585. alsa_name_playback,
  586. "playback",
  587. playback_handle,
  588. playback_hw_params,
  589. playback_sw_params,
  590. &playback_nperiods,
  591. &playback_nchannels,
  592. playback_sample_bytes)) {
  593. PERROR ("ALSA: cannot configure playback channel");
  594. return -1;
  595. }
  596. }
  597. /* check the rate, since thats rather important */
  598. if (playback_handle) {
  599. snd_pcm_hw_params_get_rate (playback_hw_params, &pr, &dir);
  600. }
  601. if (capture_handle) {
  602. snd_pcm_hw_params_get_rate (capture_hw_params, &cr, &dir);
  603. }
  604. if (capture_handle && playback_handle) {
  605. if (cr != pr) {
  606. PERROR ("ALSA Driver: playback and capture sample rates do not match (%d vs. %d)", pr, cr);
  607. }
  608. /* only change if *both* capture and playback rates
  609. * don't match requested certain hardware actually
  610. * still works properly in full-duplex with slightly
  611. * different rate values between adc and dac
  612. */
  613. if (cr != frame_rate && pr != frame_rate) {
  614. PERROR ("ALSA Driver: sample rate in use (%d Hz) does not match requested rate (%d Hz)", cr, frame_rate);
  615. frame_rate = cr;
  616. }
  617. } else if (capture_handle && cr != frame_rate) {
  618. PERROR ("ALSA Driver: capture sample rate in use (%d Hz) does not match requested rate (%d Hz)", cr, frame_rate);
  619. frame_rate = cr;
  620. } else if (playback_handle && pr != frame_rate) {
  621. PERROR ("ALSA Driver: playback sample rate in use (%d Hz) does not match requested rate (%d Hz)", pr, frame_rate);
  622. frame_rate = pr;
  623. }
  624. /* check the fragment size, since thats non-negotiable */
  625. if (playback_handle) {
  626. snd_pcm_access_t access;
  627. err = snd_pcm_hw_params_get_period_size (playback_hw_params, &p_period_size, &dir);
  628. err = snd_pcm_hw_params_get_format (playback_hw_params, &playback_sample_format);
  629. err = snd_pcm_hw_params_get_access (playback_hw_params, &access);
  630. playback_interleaved = (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  631. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  632. if (p_period_size != frames_per_cycle) {
  633. PERROR ("alsa_pcm: requested an interrupt every %ld frames but got %ld frames for playback", (long)frames_per_cycle, p_period_size);
  634. return -1;
  635. }
  636. }
  637. if (capture_handle) {
  638. snd_pcm_access_t access;
  639. err = snd_pcm_hw_params_get_period_size (capture_hw_params, &c_period_size, &dir);
  640. err = snd_pcm_hw_params_get_format (capture_hw_params, &(capture_sample_format));
  641. err = snd_pcm_hw_params_get_access (capture_hw_params, &access);
  642. capture_interleaved = (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  643. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  644. if (c_period_size != frames_per_cycle) {
  645. PERROR ("alsa_pcm: requested an interrupt every %ld frames but got %ld frames for capture", (long)frames_per_cycle, p_period_size);
  646. return -1;
  647. }
  648. }
  649. playback_sample_bytes = snd_pcm_format_physical_width(playback_sample_format) / 8;
  650. capture_sample_bytes = snd_pcm_format_physical_width(capture_sample_format) / 8;
  651. if (playback_handle) {
  652. switch (playback_sample_format) {
  653. case SND_PCM_FORMAT_S32_LE:
  654. case SND_PCM_FORMAT_S24_3LE:
  655. case SND_PCM_FORMAT_S24_3BE:
  656. case SND_PCM_FORMAT_S16_LE:
  657. case SND_PCM_FORMAT_S32_BE:
  658. case SND_PCM_FORMAT_S16_BE:
  659. break;
  660. default:
  661. PERROR ("ALSA Driver: programming error: unhandled format type for playback");
  662. return -1;
  663. }
  664. }
  665. if (capture_handle) {
  666. switch (capture_sample_format) {
  667. case SND_PCM_FORMAT_S32_LE:
  668. case SND_PCM_FORMAT_S24_3LE:
  669. case SND_PCM_FORMAT_S24_3BE:
  670. case SND_PCM_FORMAT_S16_LE:
  671. case SND_PCM_FORMAT_S32_BE:
  672. case SND_PCM_FORMAT_S16_BE:
  673. break;
  674. default:
  675. PERROR ("ALSA Driver: programming error: unhandled format type for capture");
  676. return -1;
  677. }
  678. }
  679. if (playback_interleaved) {
  680. const snd_pcm_channel_area_t *my_areas;
  681. snd_pcm_uframes_t offset, frames;
  682. if (snd_pcm_mmap_begin(playback_handle, &my_areas, &offset, &frames) < 0) {
  683. PERROR ("ALSA: %s: mmap areas info error", alsa_name_playback);
  684. return -1;
  685. }
  686. interleave_unit = snd_pcm_format_physical_width(playback_sample_format) / 8;
  687. } else {
  688. interleave_unit = 0; /* NOT USED */
  689. }
  690. if (capture_interleaved) {
  691. const snd_pcm_channel_area_t *my_areas;
  692. snd_pcm_uframes_t offset, frames;
  693. if (snd_pcm_mmap_begin(capture_handle, &my_areas, &offset, &frames) < 0) {
  694. PERROR ("ALSA: %s: mmap areas info error", alsa_name_capture);
  695. return -1;
  696. }
  697. }
  698. if (playback_nchannels > capture_nchannels) {
  699. max_nchannels = playback_nchannels;
  700. user_nchannels = capture_nchannels;
  701. } else {
  702. max_nchannels = capture_nchannels;
  703. user_nchannels = playback_nchannels;
  704. }
  705. setup_io_function_pointers ();
  706. /* Allocate and initialize structures that rely on the
  707. channels counts.
  708. Set up the bit pattern that is used to record which
  709. channels require action on every cycle. any bits that are
  710. not set after the engine's process() call indicate channels
  711. that potentially need to be silenced.
  712. */
  713. bitset_create (&channels_done, max_nchannels);
  714. bitset_create (&channels_not_done, max_nchannels);
  715. if (playback_handle) {
  716. playback_addr = (char**) malloc (sizeof (char *) * playback_nchannels);
  717. memset (playback_addr, 0, sizeof (char *) * playback_nchannels);
  718. playback_interleave_skip = (unsigned long *) malloc (sizeof (unsigned long *) * playback_nchannels);
  719. memset (playback_interleave_skip, 0, sizeof (unsigned long *) * playback_nchannels);
  720. silent = (unsigned long *) malloc (sizeof (unsigned long) * playback_nchannels);
  721. for (chn = 0; chn < playback_nchannels; chn++) {
  722. silent[chn] = 0;
  723. }
  724. for (chn = 0; chn < playback_nchannels; chn++) {
  725. bitset_add (channels_done, chn);
  726. }
  727. dither_state = (dither_state_t *) calloc ( playback_nchannels, sizeof (dither_state_t));
  728. }
  729. if (capture_handle) {
  730. capture_addr = (char **) malloc (sizeof (char *) * capture_nchannels);
  731. memset (capture_addr, 0, sizeof (char *) * capture_nchannels);
  732. capture_interleave_skip = (unsigned long *) malloc (sizeof (unsigned long *) * capture_nchannels);
  733. memset (capture_interleave_skip, 0, sizeof (unsigned long *) * capture_nchannels);
  734. }
  735. period_usecs = (trav_time_t) floor ((((float) frames_per_cycle) / frame_rate) * 1000000.0f);
  736. poll_timeout = (int) floor (1.5f * period_usecs);
  737. return 0;
  738. }
  739. int AlsaDriver::reset_parameters (nframes_t frames_per_cycle,
  740. nframes_t user_nperiods,
  741. nframes_t rate)
  742. {
  743. /* XXX unregister old ports ? */
  744. release_channel_dependent_memory ();
  745. return set_parameters (frames_per_cycle, user_nperiods, rate);
  746. }
  747. int AlsaDriver::get_channel_addresses (snd_pcm_uframes_t *capture_avail,
  748. snd_pcm_uframes_t *playback_avail,
  749. snd_pcm_uframes_t *capture_offset,
  750. snd_pcm_uframes_t *playback_offset)
  751. {
  752. int err;
  753. channel_t chn;
  754. if (capture_avail) {
  755. if ((err = snd_pcm_mmap_begin (capture_handle, &capture_areas, capture_offset, capture_avail)) < 0) {
  756. PERROR ("ALSA: %s: mmap areas info error", alsa_name_capture);
  757. return -1;
  758. }
  759. for (chn = 0; chn < capture_nchannels; chn++) {
  760. const snd_pcm_channel_area_t *a = &capture_areas[chn];
  761. capture_addr[chn] = (char *) a->addr + ((a->first + a->step * *capture_offset) / 8);
  762. capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  763. }
  764. }
  765. if (playback_avail) {
  766. if ((err = snd_pcm_mmap_begin (playback_handle, &playback_areas, playback_offset, playback_avail)) < 0) {
  767. PERROR ("ALSA: %s: mmap areas info error ", alsa_name_playback);
  768. return -1;
  769. }
  770. for (chn = 0; chn < playback_nchannels; chn++) {
  771. const snd_pcm_channel_area_t *a = &playback_areas[chn];
  772. playback_addr[chn] = (char *) a->addr + ((a->first + a->step * *playback_offset) / 8);
  773. playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  774. }
  775. }
  776. return 0;
  777. }
  778. int AlsaDriver::start()
  779. {
  780. int err;
  781. snd_pcm_uframes_t poffset, pavail;
  782. channel_t chn;
  783. poll_last = 0;
  784. poll_next = 0;
  785. if (playback_handle) {
  786. if ((err = snd_pcm_prepare (playback_handle)) < 0) {
  787. PERROR ("ALSA: prepare error for playback on \"%s\" (%s)", alsa_name_playback, snd_strerror(err));
  788. return -1;
  789. }
  790. }
  791. if ((capture_handle && capture_and_playback_not_synced) || !playback_handle) {
  792. if ((err = snd_pcm_prepare (capture_handle)) < 0) {
  793. PERROR ("ALSA: prepare error for capture on \"%s\" (%s)", alsa_name_capture, snd_strerror(err));
  794. return -1;
  795. }
  796. }
  797. if (playback_handle) {
  798. playback_nfds = snd_pcm_poll_descriptors_count (playback_handle);
  799. } else {
  800. playback_nfds = 0;
  801. }
  802. if (capture_handle) {
  803. capture_nfds = snd_pcm_poll_descriptors_count (capture_handle);
  804. } else {
  805. capture_nfds = 0;
  806. }
  807. if (pfd) {
  808. free (pfd);
  809. }
  810. pfd = (struct pollfd *) malloc (sizeof (struct pollfd) * (playback_nfds + capture_nfds + 2));
  811. if (playback_handle) {
  812. /* fill playback buffer with zeroes, and mark
  813. all fragments as having data.
  814. */
  815. pavail = snd_pcm_avail_update (playback_handle);
  816. if (pavail != frames_per_cycle * playback_nperiods) {
  817. PERROR ("ALSA: full buffer not available at start");
  818. return -1;
  819. }
  820. if (get_channel_addresses (0, &pavail, 0, &poffset)) {
  821. return -1;
  822. }
  823. /* XXX this is cheating. ALSA offers no guarantee that
  824. we can access the entire buffer at any one time. It
  825. works on most hardware tested so far, however, buts
  826. its a liability in the long run. I think that
  827. alsa-lib may have a better function for doing this
  828. here, where the goal is to silence the entire
  829. buffer.
  830. */
  831. for (chn = 0; chn < playback_nchannels; chn++) {
  832. silence_on_channel (chn, user_nperiods * frames_per_cycle);
  833. }
  834. snd_pcm_mmap_commit (playback_handle, poffset, user_nperiods * frames_per_cycle);
  835. if ((err = snd_pcm_start (playback_handle)) < 0) {
  836. PERROR ("ALSA: could not start playback (%s)", snd_strerror (err));
  837. return -1;
  838. }
  839. }
  840. if ((capture_handle && capture_and_playback_not_synced) || !playback_handle) {
  841. if ((err = snd_pcm_start (capture_handle)) < 0) {
  842. PERROR ("ALSA: could not start capture (%s)", snd_strerror (err));
  843. return -1;
  844. }
  845. }
  846. device->driverSetupMessage(tr("Succesfully started ALSA stream!"), AudioDevice::DRIVER_SETUP_SUCCESS);
  847. return 0;
  848. }
  849. int AlsaDriver::stop()
  850. {
  851. int err;
  852. audio_sample_t* buf;
  853. /* silence all capture port buffers, because we might
  854. be entering offline mode.
  855. */
  856. for (int i=0; i<m_captureChannels.size(); ++i) {
  857. AudioChannel* chan = m_captureChannels.at(i);
  858. buf = chan->get_buffer(frames_per_cycle);
  859. memset (buf, 0, sizeof (audio_sample_t) * frames_per_cycle);
  860. }
  861. if (playback_handle) {
  862. if ((err = snd_pcm_drop (playback_handle)) < 0) {
  863. PERROR ("ALSA: channel flush for playback failed (%s)", snd_strerror (err));
  864. return -1;
  865. }
  866. }
  867. if (!playback_handle || capture_and_playback_not_synced) {
  868. if (capture_handle) {
  869. if ((err = snd_pcm_drop (capture_handle)) < 0) {
  870. PERROR ("ALSA: channel flush for capture failed (%s)", snd_strerror (err));
  871. return -1;
  872. }
  873. }
  874. }
  875. return 0;
  876. }
  877. int AlsaDriver::restart()
  878. {
  879. if (stop())
  880. return -1;
  881. return start();
  882. }
  883. int AlsaDriver::xrun_recovery (float *delayed_usecs)
  884. {
  885. PWARN("xrun");
  886. snd_pcm_status_t *status;
  887. int res;
  888. snd_pcm_status_alloca(&status);
  889. if (capture_handle) {
  890. if ((res = snd_pcm_status(capture_handle, status)) < 0) {
  891. printf ("status error: %s", snd_strerror(res));
  892. }
  893. } else {
  894. if ((res = snd_pcm_status(playback_handle, status)) < 0) {
  895. printf ("status error: %s", snd_strerror(res));
  896. }
  897. }
  898. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN && process_count > XRUN_REPORT_DELAY) {
  899. struct timeval now, diff, tstamp;
  900. gettimeofday(&now, 0);
  901. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  902. timersub(&now, &tstamp, &diff);
  903. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  904. printf ("\n**** alsa_pcm: xrun of at least %.3f msecs\n\n", *delayed_usecs / 1000.0);
  905. device->xrun();
  906. }
  907. if (restart()) {
  908. return -1;
  909. }
  910. return 0;
  911. }
  912. void AlsaDriver::silence_untouched_channels (nframes_t nframes)
  913. {
  914. channel_t chn;
  915. nframes_t buffer_frames = frames_per_cycle * playback_nperiods;
  916. for (chn = 0; chn < playback_nchannels; chn++) {
  917. if (bitset_contains (channels_not_done, chn)) {
  918. if (silent[chn] < buffer_frames) {
  919. silence_on_channel_no_mark (chn, nframes);
  920. silent[chn] += nframes;
  921. }
  922. }
  923. }
  924. }
  925. static int under_gdb = false;
  926. int AlsaDriver::wait(int extra_fd, int *status, float *delayed_usecs)
  927. {
  928. snd_pcm_sframes_t avail = 0;
  929. snd_pcm_sframes_t capture_avail = 0;
  930. snd_pcm_sframes_t playback_avail = 0;
  931. int xrun_detected = false;
  932. int need_capture;
  933. int need_playback;
  934. unsigned int i;
  935. trav_time_t poll_enter;
  936. trav_time_t poll_ret = 0;
  937. *status = -1;
  938. *delayed_usecs = 0;
  939. need_capture = capture_handle ? 1 : 0;
  940. if (extra_fd >= 0) {
  941. need_playback = 0;
  942. } else {
  943. need_playback = playback_handle ? 1 : 0;
  944. }
  945. again:
  946. while (need_playback || need_capture) {
  947. int poll_result;
  948. unsigned int ci = 0;
  949. unsigned int nfds;
  950. unsigned short revents;
  951. nfds = 0;
  952. if (need_playback) {
  953. snd_pcm_poll_descriptors (playback_handle, &pfd[0], playback_nfds);
  954. nfds += playback_nfds;
  955. }
  956. if (need_capture) {
  957. snd_pcm_poll_descriptors (capture_handle, &pfd[nfds], capture_nfds);
  958. ci = nfds;
  959. nfds += capture_nfds;
  960. }
  961. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  962. for (i = 0; i < nfds; i++) {
  963. pfd[i].events |= POLLERR;
  964. }
  965. if (extra_fd >= 0) {
  966. pfd[nfds].fd = extra_fd;
  967. pfd[nfds].events =
  968. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  969. nfds++;
  970. }
  971. poll_enter = get_microseconds ();
  972. if (poll_enter > poll_next) {
  973. /*
  974. * This processing cycle was delayed past the
  975. * next due interrupt! Do not account this as
  976. * a wakeup delay:
  977. */
  978. poll_next = 0;
  979. }
  980. device->transport_cycle_end(poll_enter);
  981. poll_result = poll (pfd, nfds, poll_timeout);
  982. if (poll_result < 0) {
  983. if (errno == EINTR) {
  984. printf ("poll interrupt\n");
  985. // this happens mostly when run
  986. // under gdb, or when exiting due to a signal
  987. if (under_gdb) {
  988. goto again;
  989. }
  990. *status = -2;
  991. return 0;
  992. }
  993. PERROR ("ALSA: poll call failed (%s)",
  994. strerror (errno));
  995. *status = -3;
  996. return 0;
  997. }
  998. poll_ret = get_microseconds ();
  999. if (extra_fd < 0) {
  1000. if (poll_next && poll_ret > poll_next) {
  1001. *delayed_usecs = poll_ret - poll_next;
  1002. }
  1003. poll_last = poll_ret;
  1004. poll_next = poll_ret + period_usecs;
  1005. device->transport_cycle_start (poll_ret);
  1006. }
  1007. #ifdef DEBUG_WAKEUP
  1008. fprintf (stderr, "%" PRIu64 ": checked %d fds, %" PRIu64
  1009. " usecs since poll entered\n", poll_ret, nfds,
  1010. poll_ret - poll_enter);
  1011. #endif
  1012. /* check to see if it was the extra FD that caused us
  1013. * to return from poll */
  1014. if (extra_fd >= 0) {
  1015. if (pfd[nfds-1].revents == 0) {
  1016. /* we timed out on the extra fd */
  1017. *status = -4;
  1018. return -1;
  1019. }
  1020. /* if POLLIN was the only bit set, we're OK */
  1021. *status = 0;
  1022. return (pfd[nfds-1].revents == POLLIN) ? 0 : -1;
  1023. }
  1024. if (need_playback) {
  1025. if (snd_pcm_poll_descriptors_revents(playback_handle, &pfd[0], playback_nfds, &revents) < 0) {
  1026. qWarning("ALSA: playback revents failed");
  1027. *status = -6;
  1028. return 0;
  1029. }
  1030. if (revents & POLLERR) {
  1031. xrun_detected = TRUE;
  1032. }
  1033. if (revents & POLLOUT) {
  1034. need_playback = 0;
  1035. #ifdef DEBUG_WAKEUP
  1036. fprintf (stderr, "%" PRIu64
  1037. " playback stream ready\n",
  1038. poll_ret);
  1039. #endif
  1040. }
  1041. }
  1042. if (need_capture) {
  1043. if (snd_pcm_poll_descriptors_revents(capture_handle, &pfd[ci], capture_nfds, &revents) < 0) {
  1044. qWarning ("ALSA: capture revents failed");
  1045. *status = -6;
  1046. return 0;
  1047. }
  1048. if (revents & POLLERR) {
  1049. xrun_detected = TRUE;
  1050. }
  1051. if (revents & POLLIN) {
  1052. need_capture = 0;
  1053. #ifdef DEBUG_WAKEUP
  1054. fprintf (stderr, "%" PRIu64
  1055. " capture stream ready\n",
  1056. poll_ret);
  1057. #endif
  1058. }
  1059. }
  1060. if (poll_result == 0) {
  1061. qWarning ("ALSA: poll time out, polled for %ld usecs", (long)(poll_ret - poll_enter));
  1062. *status = -5;
  1063. return 0;
  1064. }
  1065. }
  1066. if (capture_handle) {
  1067. if ((capture_avail = snd_pcm_avail_update (capture_handle)) < 0) {
  1068. if (capture_avail == -EPIPE) {
  1069. xrun_detected = true;
  1070. } else {
  1071. PERROR ("ALSA Driver: unknown avail_update return value (%ld)", capture_avail);
  1072. }
  1073. }
  1074. } else {
  1075. /* odd, but see min() computation below */
  1076. capture_avail = INT_MAX;
  1077. }
  1078. if (playback_handle) {
  1079. if ((playback_avail = snd_pcm_avail_update (playback_handle)) < 0) {
  1080. if (playback_avail == -EPIPE) {
  1081. xrun_detected = true;
  1082. } else {
  1083. PERROR ("ALSA Driver: unknown avail_update return value (%ld)", playback_avail);
  1084. }
  1085. }
  1086. } else {
  1087. /* odd, but see min() computation below */
  1088. playback_avail = INT_MAX;
  1089. }
  1090. if (xrun_detected) {
  1091. *status = xrun_recovery (delayed_usecs);
  1092. return 0;
  1093. }
  1094. *status = 0;
  1095. last_wait_ust = poll_ret;
  1096. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  1097. #ifdef DEBUG_WAKEUP
  1098. fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
  1099. "cavail = %lu\n",
  1100. avail, playback_avail, capture_avail);
  1101. #endif
  1102. /* mark all channels not done for now. read/write will change this */
  1103. bitset_copy (channels_not_done, channels_done);
  1104. /* constrain the available count to the nearest (round down) number of
  1105. periods.
  1106. */
  1107. return avail - (avail % frames_per_cycle);
  1108. }
  1109. int AlsaDriver::_null_cycle(nframes_t nframes)
  1110. {
  1111. nframes_t nf;
  1112. snd_pcm_uframes_t offset;
  1113. snd_pcm_uframes_t contiguous;
  1114. uint chn;
  1115. if (nframes > frames_per_cycle) {
  1116. return -1;
  1117. }
  1118. if (capture_handle) {
  1119. nf = nframes;
  1120. offset = 0;
  1121. while (nf) {
  1122. contiguous = nf;
  1123. if (snd_pcm_mmap_begin (
  1124. capture_handle,
  1125. &capture_areas,
  1126. (snd_pcm_uframes_t *) &offset,
  1127. (snd_pcm_uframes_t *) &contiguous)) {
  1128. return -1;
  1129. }
  1130. if (snd_pcm_mmap_commit (capture_handle, offset, contiguous) < 0) {
  1131. return -1;
  1132. }
  1133. nf -= contiguous;
  1134. }
  1135. }
  1136. if (playback_handle) {
  1137. nf = nframes;
  1138. offset = 0;
  1139. while (nf) {
  1140. contiguous = nf;
  1141. if (snd_pcm_mmap_begin (
  1142. playback_handle,
  1143. &playback_areas,
  1144. (snd_pcm_uframes_t *) &offset,
  1145. (snd_pcm_uframes_t *) &contiguous)) {
  1146. return -1;
  1147. }
  1148. for (chn = 0; chn < playback_nchannels; chn++) {
  1149. silence_on_channel (chn, contiguous);
  1150. }
  1151. if (snd_pcm_mmap_commit (playback_handle, offset, contiguous) < 0) {
  1152. return -1;
  1153. }
  1154. nf -= contiguous;
  1155. }
  1156. }
  1157. return 0;
  1158. }
  1159. int AlsaDriver::bufsize(nframes_t nframes)
  1160. {
  1161. return reset_parameters (nframes, user_nperiods,frame_rate);
  1162. }
  1163. int AlsaDriver::_read(nframes_t nframes)
  1164. {
  1165. snd_pcm_uframes_t contiguous;
  1166. snd_pcm_uframes_t nread;
  1167. snd_pcm_uframes_t offset;
  1168. nframes_t orig_nframes;
  1169. audio_sample_t* buf;
  1170. int err;
  1171. if (nframes > frames_per_cycle) {
  1172. return -1;
  1173. }
  1174. if (!capture_handle) {
  1175. return 0;
  1176. }
  1177. nread = 0;
  1178. contiguous = 0;
  1179. orig_nframes = nframes;
  1180. while (nframes) {
  1181. contiguous = nframes;
  1182. if (get_channel_addresses (&contiguous, (snd_pcm_uframes_t *) 0, &offset, 0) < 0) {
  1183. return -1;
  1184. }
  1185. for (int i=0; i<m_captureChannels.size(); ++i) {
  1186. AudioChannel* channel = m_captureChannels.at(i);
  1187. buf = channel->get_buffer(nframes);
  1188. read_from_channel (channel->get_number(), buf + nread, contiguous);
  1189. // FIXME: should AudioChannel::read_from_hardware_port() be modified
  1190. // so that it also does partial buffer processing as we do here ?
  1191. channel->read_from_hardware_port(buf, nframes);
  1192. }
  1193. if ((err = snd_pcm_mmap_commit (capture_handle, offset, contiguous)) < 0) {
  1194. PERROR ("ALSA: could not complete read of %ld frames: error = %d\n", contiguous, err);
  1195. return -1;
  1196. }
  1197. nframes -= contiguous;
  1198. nread += contiguous;
  1199. }
  1200. return 0;
  1201. }
  1202. int AlsaDriver::_write(nframes_t nframes)
  1203. {
  1204. audio_sample_t* buf;
  1205. nframes_t orig_nframes;
  1206. snd_pcm_uframes_t nwritten;
  1207. snd_pcm_uframes_t contiguous;
  1208. snd_pcm_uframes_t offset;
  1209. int err;
  1210. process_count++;
  1211. if (! playback_handle) {
  1212. return 0;
  1213. }
  1214. if (nframes > frames_per_cycle) {
  1215. return -1;
  1216. }
  1217. nwritten = 0;
  1218. contiguous = 0;
  1219. orig_nframes = nframes;
  1220. while (nframes) {
  1221. contiguous = nframes;
  1222. if (get_channel_addresses ((snd_pcm_uframes_t *) 0, &contiguous, 0, &offset) < 0) {
  1223. return -1;
  1224. }
  1225. for (int i=0; i<m_playbackChannels.size(); ++i) {
  1226. AudioChannel* channel = m_playbackChannels.at(i);
  1227. buf = channel->get_buffer(nframes);
  1228. write_to_channel (channel->get_number(), buf + nwritten, contiguous);
  1229. channel->silence_buffer(nframes);
  1230. }
  1231. if (!bitset_empty (channels_not_done)) {
  1232. silence_untouched_channels (contiguous);
  1233. }
  1234. if ((err = snd_pcm_mmap_commit (playback_handle, offset, contiguous)) < 0) {
  1235. PERROR ("ALSA: could not complete playback of %ld frames: error = %d", contiguous, err);
  1236. if (err != EPIPE && err != ESTRPIPE)
  1237. return -1;
  1238. }
  1239. nframes -= contiguous;
  1240. nwritten += contiguous;
  1241. }
  1242. return 0;
  1243. }
  1244. int AlsaDriver::_run_cycle()
  1245. {
  1246. int wait_status;
  1247. float delayed_usecs;
  1248. nframes_t nframes;
  1249. nframes = wait (-1, &wait_status, &delayed_usecs);
  1250. if (wait_status < 0) {
  1251. return -1; /* driver failed */
  1252. }
  1253. if (nframes == 0) {
  1254. /* we detected an xrun and restarted: notify
  1255. * clients about the delay.
  1256. */
  1257. device->delay(delayed_usecs);
  1258. return 0;
  1259. }
  1260. return device->run_cycle (nframes, delayed_usecs);
  1261. }
  1262. int AlsaDriver::attach()
  1263. {
  1264. char buf[32];
  1265. channel_t chn;
  1266. AudioChannel* chan;
  1267. int port_flags;
  1268. device->set_buffer_size (frames_per_cycle);
  1269. device->set_sample_rate (frame_rate);
  1270. port_flags = PortIsOutput|PortIsPhysical|PortIsTerminal;
  1271. for (chn = 0; chn < capture_nchannels; chn++) {
  1272. snprintf (buf, sizeof(buf) - 1, "capture_%lu", chn+1);
  1273. chan = add_capture_channel(buf);
  1274. chan->set_latency( frames_per_cycle + capture_frame_latency );
  1275. }
  1276. port_flags = PortIsInput|PortIsPhysical|PortIsTerminal;
  1277. for (chn = 0; chn < playback_nchannels; chn++) {
  1278. snprintf (buf, sizeof(buf) - 1, "playback_%lu", chn+1);
  1279. chan = add_playback_channel(buf);
  1280. chan->set_latency( frames_per_cycle + capture_frame_latency );
  1281. }
  1282. return 1;
  1283. }
  1284. int AlsaDriver::detach ()
  1285. {
  1286. return 0;
  1287. }
  1288. QString AlsaDriver::get_device_name( )
  1289. {
  1290. return alsa_device_name(false);
  1291. }
  1292. QString AlsaDriver::get_device_longname( )
  1293. {
  1294. return alsa_device_name(true);
  1295. }
  1296. QString AlsaDriver::alsa_device_name(bool longname, int devicenumber)
  1297. {
  1298. snd_ctl_card_info_t *info;
  1299. snd_ctl_t *handle;
  1300. char name[32];
  1301. sprintf(name, "hw:%d", devicenumber);
  1302. int err = 0;
  1303. snd_ctl_card_info_alloca(&info);
  1304. if ((err = snd_ctl_open(&handle, name, devicenumber)) < 0) {
  1305. PMESG("AlsaDriver::alsa_device_name: Control open (device %i): %s", devicenumber, snd_strerror(err));
  1306. return "";
  1307. }
  1308. if ((err = snd_ctl_card_info(handle, info)) < 0) {
  1309. PMESG("Control hardware info (%i): %s", 0, snd_strerror(err));
  1310. }
  1311. snd_ctl_close(handle);
  1312. if (err < 0) {
  1313. return "Device name unknown";
  1314. }
  1315. if (longname) {
  1316. return snd_ctl_card_info_get_name(info);
  1317. }
  1318. return snd_ctl_card_info_get_id(info);
  1319. }
  1320. int AlsaDriver::get_device_id_by_name(const QString& name)
  1321. {
  1322. for (int i=0; i<6; i++) {
  1323. if (alsa_device_name(false, i) == name) {
  1324. return i;
  1325. }
  1326. }
  1327. return -1;
  1328. }
  1329. //eof