jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2768 lines
89KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004 Grame
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program 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
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #include <iostream>
  20. #include <unistd.h>
  21. #include <math.h>
  22. #include <stdio.h>
  23. #include <memory.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <errno.h>
  27. #include <stdarg.h>
  28. #include <signal.h>
  29. #include <sys/types.h>
  30. #include <sys/time.h>
  31. #include <regex.h>
  32. #include <string.h>
  33. #include "JackAlsaDriver.h"
  34. #include "JackEngineControl.h"
  35. #include "JackClientControl.h"
  36. #include "JackPort.h"
  37. #include "JackGraphManager.h"
  38. #include "JackLockedEngine.h"
  39. #include "hammerfall.h"
  40. #include "hdsp.h"
  41. #include "ice1712.h"
  42. #include "usx2y.h"
  43. #include "generic.h"
  44. #include "memops.h"
  45. #include "JackPosixThread.h"
  46. #include "JackExports.h"
  47. namespace Jack
  48. {
  49. typedef long AudioDeviceID;
  50. #define jack_get_microseconds GetMicroSeconds
  51. /* Delay (in process calls) before jackd will report an xrun */
  52. #define XRUN_REPORT_DELAY 0
  53. void
  54. JackAlsaDriver::alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
  55. {
  56. bitset_destroy (&driver->channels_done);
  57. bitset_destroy (&driver->channels_not_done);
  58. if (driver->playback_addr) {
  59. free (driver->playback_addr);
  60. driver->playback_addr = 0;
  61. }
  62. if (driver->capture_addr) {
  63. free (driver->capture_addr);
  64. driver->capture_addr = 0;
  65. }
  66. if (driver->playback_interleave_skip) {
  67. free (driver->playback_interleave_skip);
  68. driver->playback_interleave_skip = NULL;
  69. }
  70. if (driver->capture_interleave_skip) {
  71. free (driver->capture_interleave_skip);
  72. driver->capture_interleave_skip = NULL;
  73. }
  74. if (driver->silent) {
  75. free (driver->silent);
  76. driver->silent = 0;
  77. }
  78. if (driver->dither_state) {
  79. free (driver->dither_state);
  80. driver->dither_state = 0;
  81. }
  82. }
  83. int
  84. JackAlsaDriver::alsa_driver_check_capabilities (alsa_driver_t *driver)
  85. {
  86. return 0;
  87. }
  88. int
  89. JackAlsaDriver::alsa_driver_check_card_type (alsa_driver_t *driver)
  90. {
  91. int err;
  92. snd_ctl_card_info_t *card_info;
  93. char * ctl_name;
  94. regex_t expression;
  95. snd_ctl_card_info_alloca (&card_info);
  96. regcomp(&expression, "(plug)?hw:[0-9](,[0-9])?", REG_ICASE | REG_EXTENDED);
  97. if (!regexec(&expression, driver->alsa_name_playback, 0, NULL, 0)) {
  98. /* the user wants a hw or plughw device, the ctl name
  99. * should be hw:x where x is the card number */
  100. char tmp[5];
  101. strncpy(tmp, strstr(driver->alsa_name_playback, "hw"), 4);
  102. tmp[4] = '\0';
  103. jack_log("control device %s", tmp);
  104. ctl_name = strdup(tmp);
  105. } else {
  106. ctl_name = strdup(driver->alsa_name_playback);
  107. }
  108. // XXX: I don't know the "right" way to do this. Which to use
  109. // driver->alsa_name_playback or driver->alsa_name_capture.
  110. if ((err = snd_ctl_open (&driver->ctl_handle, ctl_name, 0)) < 0) {
  111. jack_error ("control open \"%s\" (%s)", ctl_name,
  112. snd_strerror(err));
  113. return -1;
  114. }
  115. if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  116. jack_error ("control hardware info \"%s\" (%s)",
  117. driver->alsa_name_playback, snd_strerror (err));
  118. snd_ctl_close (driver->ctl_handle);
  119. return -1;
  120. }
  121. driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  122. jack_info("Using ALSA driver %s running on %s", driver->alsa_driver, snd_ctl_card_info_get_longname(card_info));
  123. regfree(&expression);
  124. free(ctl_name);
  125. return alsa_driver_check_capabilities (driver);
  126. }
  127. int
  128. JackAlsaDriver::alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
  129. {
  130. driver->hw = jack_alsa_hammerfall_hw_new (driver);
  131. return 0;
  132. }
  133. int
  134. JackAlsaDriver::alsa_driver_hdsp_hardware (alsa_driver_t *driver)
  135. {
  136. driver->hw = jack_alsa_hdsp_hw_new (driver);
  137. return 0;
  138. }
  139. int
  140. JackAlsaDriver::alsa_driver_ice1712_hardware (alsa_driver_t *driver)
  141. {
  142. driver->hw = jack_alsa_ice1712_hw_new (driver);
  143. return 0;
  144. }
  145. int
  146. JackAlsaDriver::alsa_driver_usx2y_hardware (alsa_driver_t *driver)
  147. {
  148. // TODO : will need so deeped redesign
  149. // driver->hw = jack_alsa_usx2y_hw_new (driver);
  150. return 0;
  151. }
  152. int
  153. JackAlsaDriver::alsa_driver_generic_hardware (alsa_driver_t *driver)
  154. {
  155. driver->hw = jack_alsa_generic_hw_new (driver);
  156. return 0;
  157. }
  158. int
  159. JackAlsaDriver::alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring,
  160. int hw_metering)
  161. {
  162. int err;
  163. if (!strcmp(driver->alsa_driver, "RME9652")) {
  164. if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
  165. return err;
  166. }
  167. } else if (!strcmp(driver->alsa_driver, "H-DSP")) {
  168. if ((err = alsa_driver_hdsp_hardware (driver)) != 0) {
  169. return err;
  170. }
  171. } else if (!strcmp(driver->alsa_driver, "ICE1712")) {
  172. if ((err = alsa_driver_ice1712_hardware (driver)) != 0) {
  173. return err;
  174. }
  175. } else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
  176. if ((err = alsa_driver_usx2y_hardware (driver)) != 0) {
  177. return err;
  178. }
  179. } else {
  180. if ((err = alsa_driver_generic_hardware (driver)) != 0) {
  181. return err;
  182. }
  183. }
  184. if (driver->hw->capabilities & Cap_HardwareMonitoring) {
  185. driver->has_hw_monitoring = TRUE;
  186. /* XXX need to ensure that this is really FALSE or
  187. * TRUE or whatever*/
  188. driver->hw_monitoring = hw_monitoring;
  189. } else {
  190. driver->has_hw_monitoring = FALSE;
  191. driver->hw_monitoring = FALSE;
  192. }
  193. if (driver->hw->capabilities & Cap_ClockLockReporting) {
  194. driver->has_clock_sync_reporting = TRUE;
  195. } else {
  196. driver->has_clock_sync_reporting = FALSE;
  197. }
  198. if (driver->hw->capabilities & Cap_HardwareMetering) {
  199. driver->has_hw_metering = TRUE;
  200. driver->hw_metering = hw_metering;
  201. } else {
  202. driver->has_hw_metering = FALSE;
  203. driver->hw_metering = FALSE;
  204. }
  205. return 0;
  206. }
  207. void
  208. JackAlsaDriver::alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
  209. {
  210. if (SND_PCM_FORMAT_FLOAT_LE == driver->playback_sample_format) {
  211. if (driver->playback_interleaved) {
  212. driver->channel_copy = memcpy_interleave_d32_s32;
  213. } else {
  214. driver->channel_copy = memcpy_fake;
  215. }
  216. driver->read_via_copy = sample_move_floatLE_sSs;
  217. driver->write_via_copy = sample_move_dS_floatLE;
  218. return;
  219. }
  220. switch (driver->playback_sample_bytes) {
  221. case 2:
  222. if (driver->playback_interleaved) {
  223. driver->channel_copy = memcpy_interleave_d16_s16;
  224. } else {
  225. driver->channel_copy = memcpy_fake;
  226. }
  227. switch (driver->dither) {
  228. case Rectangular:
  229. jack_log("Rectangular dithering at 16 bits");
  230. driver->write_via_copy = driver->quirk_bswap ?
  231. sample_move_dither_rect_d16_sSs :
  232. sample_move_dither_rect_d16_sS;
  233. break;
  234. case Triangular:
  235. jack_log("Triangular dithering at 16 bits");
  236. driver->write_via_copy = driver->quirk_bswap ?
  237. sample_move_dither_tri_d16_sSs :
  238. sample_move_dither_tri_d16_sS;
  239. break;
  240. case Shaped:
  241. jack_log("Noise-shaped dithering at 16 bits");
  242. driver->write_via_copy = driver->quirk_bswap ?
  243. sample_move_dither_shaped_d16_sSs :
  244. sample_move_dither_shaped_d16_sS;
  245. break;
  246. default:
  247. driver->write_via_copy = driver->quirk_bswap ?
  248. sample_move_d16_sSs : sample_move_d16_sS;
  249. break;
  250. }
  251. break;
  252. case 3:
  253. if (driver->playback_interleaved) {
  254. driver->channel_copy = memcpy_interleave_d24_s24;
  255. } else {
  256. driver->channel_copy = memcpy_fake;
  257. }
  258. switch (driver->dither) {
  259. case Rectangular:
  260. jack_log("Rectangular dithering at 16 bits");
  261. driver->write_via_copy = driver->quirk_bswap ?
  262. sample_move_dither_rect_d24_sSs :
  263. sample_move_dither_rect_d24_sS;
  264. break;
  265. case Triangular:
  266. jack_log("Triangular dithering at 16 bits");
  267. driver->write_via_copy = driver->quirk_bswap ?
  268. sample_move_dither_tri_d24_sSs :
  269. sample_move_dither_tri_d24_sS;
  270. break;
  271. case Shaped:
  272. jack_log("Noise-shaped dithering at 16 bits");
  273. driver->write_via_copy = driver->quirk_bswap ?
  274. sample_move_dither_shaped_d24_sSs :
  275. sample_move_dither_shaped_d24_sS;
  276. break;
  277. default:
  278. driver->write_via_copy = driver->quirk_bswap ?
  279. sample_move_d24_sSs : sample_move_d24_sS;
  280. break;
  281. }
  282. break;
  283. case 4:
  284. if (driver->playback_interleaved) {
  285. driver->channel_copy = memcpy_interleave_d32_s32;
  286. } else {
  287. driver->channel_copy = memcpy_fake;
  288. }
  289. switch (driver->dither) {
  290. case Rectangular:
  291. jack_log("Rectangular dithering at 16 bits");
  292. driver->write_via_copy = driver->quirk_bswap ?
  293. sample_move_dither_rect_d32u24_sSs :
  294. sample_move_dither_rect_d32u24_sS;
  295. break;
  296. case Triangular:
  297. jack_log("Triangular dithering at 16 bits");
  298. driver->write_via_copy = driver->quirk_bswap ?
  299. sample_move_dither_tri_d32u24_sSs :
  300. sample_move_dither_tri_d32u24_sS;
  301. break;
  302. case Shaped:
  303. jack_log("Noise-shaped dithering at 16 bits");
  304. driver->write_via_copy = driver->quirk_bswap ?
  305. sample_move_dither_shaped_d32u24_sSs :
  306. sample_move_dither_shaped_d32u24_sS;
  307. break;
  308. default:
  309. driver->write_via_copy = driver->quirk_bswap ?
  310. sample_move_d32u24_sSs : sample_move_d32u24_sS;
  311. break;
  312. }
  313. break;
  314. }
  315. switch (driver->capture_sample_bytes) {
  316. case 2:
  317. driver->read_via_copy = driver->quirk_bswap ?
  318. sample_move_dS_s16s : sample_move_dS_s16;
  319. break;
  320. case 3:
  321. driver->read_via_copy = driver->quirk_bswap ?
  322. sample_move_dS_s24s : sample_move_dS_s24;
  323. break;
  324. case 4:
  325. driver->read_via_copy = driver->quirk_bswap ?
  326. sample_move_dS_s32u24s : sample_move_dS_s32u24;
  327. break;
  328. }
  329. }
  330. int
  331. JackAlsaDriver::alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
  332. const char *stream_name,
  333. snd_pcm_t *handle,
  334. snd_pcm_hw_params_t *hw_params,
  335. snd_pcm_sw_params_t *sw_params,
  336. unsigned int *nperiodsp,
  337. unsigned long *nchns,
  338. unsigned long sample_width)
  339. {
  340. int err, format;
  341. unsigned int frame_rate;
  342. snd_pcm_uframes_t stop_th;
  343. static struct {
  344. char Name[32];
  345. snd_pcm_format_t format;
  346. int swapped;
  347. }
  348. formats[] = {
  349. {"32bit float little-endian", SND_PCM_FORMAT_FLOAT_LE},
  350. {"32bit little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
  351. {"32bit big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
  352. {"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE},
  353. {"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE},
  354. {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
  355. {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
  356. };
  357. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  358. #define FIRST_16BIT_FORMAT 5
  359. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  360. jack_error ("ALSA: no playback configurations available (%s)",
  361. snd_strerror (err));
  362. return -1;
  363. }
  364. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
  365. < 0) {
  366. jack_error ("ALSA: cannot restrict period size to integral"
  367. " value.");
  368. return -1;
  369. }
  370. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  371. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  372. if ((err = snd_pcm_hw_params_set_access (
  373. handle, hw_params,
  374. SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
  375. jack_error ("ALSA: mmap-based access is not possible"
  376. " for the %s "
  377. "stream of this audio interface",
  378. stream_name);
  379. return -1;
  380. }
  381. }
  382. }
  383. format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
  384. while (1) {
  385. if ((err = snd_pcm_hw_params_set_format (
  386. handle, hw_params, formats[format].format)) < 0) {
  387. if ((sample_width == 4
  388. ? format++ >= (int)NUMFORMATS - 1
  389. : format-- <= 0)) {
  390. jack_error ("Sorry. The audio interface \"%s\""
  391. " doesn't support any of the"
  392. " hardware sample formats that"
  393. " JACK's alsa-driver can use.",
  394. device_name);
  395. return -1;
  396. }
  397. } else {
  398. if (formats[format].swapped) {
  399. driver->quirk_bswap = 1;
  400. } else {
  401. driver->quirk_bswap = 0;
  402. }
  403. jack_info ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
  404. break;
  405. }
  406. }
  407. frame_rate = driver->frame_rate ;
  408. err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
  409. &frame_rate, NULL) ;
  410. driver->frame_rate = frame_rate ;
  411. if (err < 0) {
  412. jack_error ("ALSA: cannot set sample/frame rate to %ld for %s", driver->frame_rate, stream_name);
  413. return -1;
  414. }
  415. if (!*nchns) {
  416. /*if not user-specified, try to find the maximum
  417. * number of channels */
  418. unsigned int channels_max ;
  419. err = snd_pcm_hw_params_get_channels_max (hw_params,
  420. &channels_max);
  421. *nchns = channels_max ;
  422. if (*nchns > 1024) {
  423. /* the hapless user is an unwitting victim of
  424. the "default" ALSA PCM device, which can
  425. support up to 16 million channels. since
  426. they can't be bothered to set up a proper
  427. default device, limit the number of
  428. channels for them to a sane default.
  429. */
  430. jack_error (
  431. "You appear to be using the ALSA software \"plug\" layer, probably\n"
  432. "a result of using the \"default\" ALSA device. This is less\n"
  433. "efficient than it could be. Consider using a hardware device\n"
  434. "instead rather than using the plug layer. Usually the name of the\n"
  435. "hardware device that corresponds to the first sound card is hw:0\n"
  436. );
  437. *nchns = 2;
  438. }
  439. }
  440. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
  441. *nchns)) < 0) {
  442. jack_error ("ALSA: cannot set channel count to %u for %s",
  443. *nchns, stream_name);
  444. return -1;
  445. }
  446. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
  447. driver->frames_per_cycle,
  448. 0))
  449. < 0) {
  450. jack_error ("ALSA: cannot set period size to %ld frames for %s", driver->frames_per_cycle, stream_name);
  451. return -1;
  452. }
  453. *nperiodsp = driver->user_nperiods;
  454. snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
  455. if (*nperiodsp < driver->user_nperiods)
  456. *nperiodsp = driver->user_nperiods;
  457. if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
  458. nperiodsp, NULL) < 0) {
  459. jack_error ("ALSA: cannot set number of periods to %u for %s",
  460. *nperiodsp, stream_name);
  461. return -1;
  462. }
  463. if (*nperiodsp < driver->user_nperiods) {
  464. jack_error ("ALSA: got smaller periods %u than %u for %s",
  465. *nperiodsp, (unsigned int) driver->user_nperiods,
  466. stream_name);
  467. return -1;
  468. }
  469. jack_info ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
  470. /*
  471. if (!jack_power_of_two(driver->frames_per_cycle)) {
  472. jack_error("JACK: frames must be a power of two "
  473. "(64, 512, 1024, ...)\n");
  474. return -1;
  475. }
  476. */
  477. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
  478. *nperiodsp *
  479. driver->frames_per_cycle))
  480. < 0) {
  481. jack_error ("ALSA: cannot set buffer length to %ld for %s", *nperiodsp * driver->frames_per_cycle,
  482. stream_name);
  483. return -1;
  484. }
  485. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  486. jack_error ("ALSA: cannot set hardware parameters for %s",
  487. stream_name);
  488. return -1;
  489. }
  490. snd_pcm_sw_params_current (handle, sw_params);
  491. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
  492. 0U)) < 0) {
  493. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  494. return -1;
  495. }
  496. stop_th = *nperiodsp * driver->frames_per_cycle;
  497. if (driver->soft_mode) {
  498. stop_th = (snd_pcm_uframes_t) - 1;
  499. }
  500. if ((err = snd_pcm_sw_params_set_stop_threshold (
  501. handle, sw_params, stop_th)) < 0) {
  502. jack_error ("ALSA: cannot set stop mode for %s",
  503. stream_name);
  504. return -1;
  505. }
  506. if ((err = snd_pcm_sw_params_set_silence_threshold (
  507. handle, sw_params, 0)) < 0) {
  508. jack_error ("ALSA: cannot set silence threshold for %s",
  509. stream_name);
  510. return -1;
  511. }
  512. #if 0
  513. fprintf (stderr, "set silence size to %lu * %lu = %lu\n",
  514. driver->frames_per_cycle, *nperiodsp,
  515. driver->frames_per_cycle * *nperiodsp);
  516. if ((err = snd_pcm_sw_params_set_silence_size (
  517. handle, sw_params,
  518. driver->frames_per_cycle * *nperiodsp)) < 0) {
  519. jack_error ("ALSA: cannot set silence size for %s",
  520. stream_name);
  521. return -1;
  522. }
  523. #endif
  524. if (handle == driver->playback_handle)
  525. err = snd_pcm_sw_params_set_avail_min (
  526. handle, sw_params,
  527. driver->frames_per_cycle
  528. * (*nperiodsp - driver->user_nperiods + 1));
  529. else
  530. err = snd_pcm_sw_params_set_avail_min (
  531. handle, sw_params, driver->frames_per_cycle);
  532. if (err < 0) {
  533. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  534. return -1;
  535. }
  536. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  537. jack_error ("ALSA: cannot set software parameters for %s\n",
  538. stream_name);
  539. return -1;
  540. }
  541. return 0;
  542. }
  543. int
  544. JackAlsaDriver::alsa_driver_set_parameters (alsa_driver_t *driver,
  545. jack_nframes_t frames_per_cycle,
  546. jack_nframes_t user_nperiods,
  547. jack_nframes_t rate)
  548. {
  549. int dir;
  550. snd_pcm_uframes_t p_period_size = 0;
  551. snd_pcm_uframes_t c_period_size = 0;
  552. channel_t chn;
  553. unsigned int pr = 0;
  554. unsigned int cr = 0;
  555. int err;
  556. driver->frame_rate = rate;
  557. driver->frames_per_cycle = frames_per_cycle;
  558. driver->user_nperiods = user_nperiods;
  559. /* // steph
  560. fprintf (stderr, "configuring for %" PRIu32 "Hz, period = %"
  561. PRIu32 " frames, buffer = %" PRIu32 " periods\n",
  562. rate, frames_per_cycle, user_nperiods);
  563. */
  564. if (driver->capture_handle) {
  565. if (alsa_driver_configure_stream (
  566. driver,
  567. driver->alsa_name_capture,
  568. "capture",
  569. driver->capture_handle,
  570. driver->capture_hw_params,
  571. driver->capture_sw_params,
  572. &driver->capture_nperiods,
  573. (long unsigned int*)&driver->capture_nchannels,
  574. driver->capture_sample_bytes)) {
  575. jack_error ("ALSA: cannot configure capture channel");
  576. return -1;
  577. }
  578. }
  579. if (driver->playback_handle) {
  580. if (alsa_driver_configure_stream (
  581. driver,
  582. driver->alsa_name_playback,
  583. "playback",
  584. driver->playback_handle,
  585. driver->playback_hw_params,
  586. driver->playback_sw_params,
  587. &driver->playback_nperiods,
  588. (long unsigned int*)&driver->playback_nchannels,
  589. driver->playback_sample_bytes)) {
  590. jack_error ("ALSA: cannot configure playback channel");
  591. return -1;
  592. }
  593. }
  594. /* check the rate, since thats rather important */
  595. if (driver->playback_handle) {
  596. snd_pcm_hw_params_get_rate (driver->playback_hw_params,
  597. &pr, &dir);
  598. }
  599. if (driver->capture_handle) {
  600. snd_pcm_hw_params_get_rate (driver->capture_hw_params,
  601. &cr, &dir);
  602. }
  603. if (driver->capture_handle && driver->playback_handle) {
  604. if (cr != pr) {
  605. jack_error ("playback and capture sample rates do "
  606. "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 != driver->frame_rate && pr != driver->frame_rate) {
  614. jack_error ("sample rate in use (%d Hz) does not "
  615. "match requested rate (%d Hz)",
  616. cr, driver->frame_rate);
  617. driver->frame_rate = cr;
  618. }
  619. } else if (driver->capture_handle && cr != driver->frame_rate) {
  620. jack_error ("capture sample rate in use (%d Hz) does not "
  621. "match requested rate (%d Hz)",
  622. cr, driver->frame_rate);
  623. driver->frame_rate = cr;
  624. } else if (driver->playback_handle && pr != driver->frame_rate) {
  625. jack_error ("playback sample rate in use (%d Hz) does not "
  626. "match requested rate (%d Hz)",
  627. pr, driver->frame_rate);
  628. driver->frame_rate = pr;
  629. }
  630. /* check the fragment size, since thats non-negotiable */
  631. if (driver->playback_handle) {
  632. snd_pcm_access_t access;
  633. err = snd_pcm_hw_params_get_period_size (
  634. driver->playback_hw_params, &p_period_size, &dir);
  635. err = snd_pcm_hw_params_get_format (
  636. driver->playback_hw_params,
  637. &(driver->playback_sample_format));
  638. err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
  639. &access);
  640. driver->playback_interleaved =
  641. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  642. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  643. if (p_period_size != driver->frames_per_cycle) {
  644. /*
  645. jack_error ("alsa_pcm: requested an interrupt every %"
  646. PRIu32
  647. " frames but got %u frames for playback",
  648. driver->frames_per_cycle, p_period_size);
  649. */
  650. return -1;
  651. }
  652. }
  653. if (driver->capture_handle) {
  654. snd_pcm_access_t access;
  655. err = snd_pcm_hw_params_get_period_size (
  656. driver->capture_hw_params, &c_period_size, &dir);
  657. err = snd_pcm_hw_params_get_format (
  658. driver->capture_hw_params,
  659. &(driver->capture_sample_format));
  660. err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
  661. &access);
  662. driver->capture_interleaved =
  663. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  664. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  665. if (c_period_size != driver->frames_per_cycle) {
  666. /* // steph
  667. jack_error ("alsa_pcm: requested an interrupt every %"
  668. PRIu32
  669. " frames but got %uc frames for capture",
  670. driver->frames_per_cycle, p_period_size);
  671. */
  672. return -1;
  673. }
  674. }
  675. driver->playback_sample_bytes =
  676. snd_pcm_format_physical_width (driver->playback_sample_format)
  677. / 8;
  678. driver->capture_sample_bytes =
  679. snd_pcm_format_physical_width (driver->capture_sample_format)
  680. / 8;
  681. if (driver->playback_handle) {
  682. switch (driver->playback_sample_format) {
  683. case SND_PCM_FORMAT_FLOAT_LE:
  684. case SND_PCM_FORMAT_S32_LE:
  685. case SND_PCM_FORMAT_S24_3LE:
  686. case SND_PCM_FORMAT_S24_3BE:
  687. case SND_PCM_FORMAT_S16_LE:
  688. case SND_PCM_FORMAT_S32_BE:
  689. case SND_PCM_FORMAT_S16_BE:
  690. break;
  691. default:
  692. jack_error ("programming error: unhandled format "
  693. "type for playback");
  694. exit (1);
  695. }
  696. }
  697. if (driver->capture_handle) {
  698. switch (driver->capture_sample_format) {
  699. case SND_PCM_FORMAT_FLOAT_LE:
  700. case SND_PCM_FORMAT_S32_LE:
  701. case SND_PCM_FORMAT_S24_3LE:
  702. case SND_PCM_FORMAT_S24_3BE:
  703. case SND_PCM_FORMAT_S16_LE:
  704. case SND_PCM_FORMAT_S32_BE:
  705. case SND_PCM_FORMAT_S16_BE:
  706. break;
  707. default:
  708. jack_error ("programming error: unhandled format "
  709. "type for capture");
  710. exit (1);
  711. }
  712. }
  713. if (driver->playback_interleaved) {
  714. const snd_pcm_channel_area_t *my_areas;
  715. snd_pcm_uframes_t offset, frames;
  716. if (snd_pcm_mmap_begin(driver->playback_handle,
  717. &my_areas, &offset, &frames) < 0) {
  718. jack_error ("ALSA: %s: mmap areas info error",
  719. driver->alsa_name_playback);
  720. return -1;
  721. }
  722. driver->interleave_unit =
  723. snd_pcm_format_physical_width (
  724. driver->playback_sample_format) / 8;
  725. } else {
  726. driver->interleave_unit = 0; /* NOT USED */
  727. }
  728. if (driver->capture_interleaved) {
  729. const snd_pcm_channel_area_t *my_areas;
  730. snd_pcm_uframes_t offset, frames;
  731. if (snd_pcm_mmap_begin(driver->capture_handle,
  732. &my_areas, &offset, &frames) < 0) {
  733. jack_error ("ALSA: %s: mmap areas info error",
  734. driver->alsa_name_capture);
  735. return -1;
  736. }
  737. }
  738. if (driver->playback_nchannels > driver->capture_nchannels) {
  739. driver->max_nchannels = driver->playback_nchannels;
  740. driver->user_nchannels = driver->capture_nchannels;
  741. } else {
  742. driver->max_nchannels = driver->capture_nchannels;
  743. driver->user_nchannels = driver->playback_nchannels;
  744. }
  745. alsa_driver_setup_io_function_pointers (driver);
  746. /* Allocate and initialize structures that rely on the
  747. channels counts.
  748. Set up the bit pattern that is used to record which
  749. channels require action on every cycle. any bits that are
  750. not set after the engine's process() call indicate channels
  751. that potentially need to be silenced.
  752. */
  753. bitset_create (&driver->channels_done, driver->max_nchannels);
  754. bitset_create (&driver->channels_not_done, driver->max_nchannels);
  755. if (driver->playback_handle) {
  756. driver->playback_addr = (char **)
  757. malloc (sizeof (char *) * driver->playback_nchannels);
  758. memset (driver->playback_addr, 0,
  759. sizeof (char *) * driver->playback_nchannels);
  760. driver->playback_interleave_skip = (unsigned long *)
  761. malloc (sizeof (unsigned long *) * driver->playback_nchannels);
  762. memset (driver->playback_interleave_skip, 0,
  763. sizeof (unsigned long *) * driver->playback_nchannels);
  764. driver->silent = (unsigned long *)
  765. malloc (sizeof (unsigned long)
  766. * driver->playback_nchannels);
  767. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  768. driver->silent[chn] = 0;
  769. }
  770. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  771. bitset_add (driver->channels_done, chn);
  772. }
  773. driver->dither_state = (dither_state_t *)
  774. calloc ( driver->playback_nchannels,
  775. sizeof (dither_state_t));
  776. }
  777. if (driver->capture_handle) {
  778. driver->capture_addr = (char **)
  779. malloc (sizeof (char *) * driver->capture_nchannels);
  780. memset (driver->capture_addr, 0,
  781. sizeof (char *) * driver->capture_nchannels);
  782. driver->capture_interleave_skip = (unsigned long *)
  783. malloc (sizeof (unsigned long *) * driver->capture_nchannels);
  784. memset (driver->capture_interleave_skip, 0,
  785. sizeof (unsigned long *) * driver->capture_nchannels);
  786. }
  787. driver->clock_sync_data = (ClockSyncStatus *)
  788. malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
  789. driver->period_usecs =
  790. (jack_time_t) floor ((((float) driver->frames_per_cycle) /
  791. driver->frame_rate) * 1000000.0f);
  792. driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
  793. // steph
  794. /*
  795. if (driver->engine) {
  796. driver->engine->set_buffer_size (driver->engine,
  797. driver->frames_per_cycle);
  798. }
  799. */
  800. return 0;
  801. }
  802. int
  803. JackAlsaDriver::alsa_driver_reset_parameters (alsa_driver_t *driver,
  804. jack_nframes_t frames_per_cycle,
  805. jack_nframes_t user_nperiods,
  806. jack_nframes_t rate)
  807. {
  808. /* XXX unregister old ports ? */
  809. alsa_driver_release_channel_dependent_memory (driver);
  810. return alsa_driver_set_parameters (driver,
  811. frames_per_cycle,
  812. user_nperiods, rate);
  813. }
  814. int
  815. JackAlsaDriver::alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  816. snd_pcm_uframes_t *capture_avail,
  817. snd_pcm_uframes_t *playback_avail,
  818. snd_pcm_uframes_t *capture_offset,
  819. snd_pcm_uframes_t *playback_offset)
  820. {
  821. unsigned long err;
  822. channel_t chn;
  823. if (capture_avail) {
  824. if ((err = snd_pcm_mmap_begin (
  825. driver->capture_handle, &driver->capture_areas,
  826. (snd_pcm_uframes_t *) capture_offset,
  827. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  828. jack_error ("ALSA: %s: mmap areas info error",
  829. driver->alsa_name_capture);
  830. return -1;
  831. }
  832. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  833. const snd_pcm_channel_area_t *a =
  834. &driver->capture_areas[chn];
  835. driver->capture_addr[chn] = (char *) a->addr
  836. + ((a->first + a->step * *capture_offset) / 8);
  837. driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  838. }
  839. }
  840. if (playback_avail) {
  841. if ((err = snd_pcm_mmap_begin (
  842. driver->playback_handle, &driver->playback_areas,
  843. (snd_pcm_uframes_t *) playback_offset,
  844. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  845. jack_error ("ALSA: %s: mmap areas info error ",
  846. driver->alsa_name_playback);
  847. return -1;
  848. }
  849. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  850. const snd_pcm_channel_area_t *a =
  851. &driver->playback_areas[chn];
  852. driver->playback_addr[chn] = (char *) a->addr
  853. + ((a->first + a->step * *playback_offset) / 8);
  854. driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  855. }
  856. }
  857. return 0;
  858. }
  859. int
  860. JackAlsaDriver::alsa_driver_start (alsa_driver_t *driver)
  861. {
  862. int err;
  863. snd_pcm_uframes_t poffset, pavail;
  864. channel_t chn;
  865. driver->poll_last = 0;
  866. driver->poll_next = 0;
  867. if (driver->playback_handle) {
  868. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  869. jack_error ("ALSA: prepare error for playback on "
  870. "\"%s\" (%s)", driver->alsa_name_playback,
  871. snd_strerror(err));
  872. return -1;
  873. }
  874. }
  875. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  876. || !driver->playback_handle) {
  877. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  878. jack_error ("ALSA: prepare error for capture on \"%s\""
  879. " (%s)", driver->alsa_name_capture,
  880. snd_strerror(err));
  881. return -1;
  882. }
  883. }
  884. if (driver->hw_monitoring) {
  885. if (driver->input_monitor_mask || driver->all_monitor_in) {
  886. if (driver->all_monitor_in) {
  887. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  888. } else {
  889. driver->hw->set_input_monitor_mask (
  890. driver->hw, driver->input_monitor_mask);
  891. }
  892. } else {
  893. driver->hw->set_input_monitor_mask (driver->hw,
  894. driver->input_monitor_mask);
  895. }
  896. }
  897. if (driver->playback_handle) {
  898. driver->playback_nfds =
  899. snd_pcm_poll_descriptors_count (driver->playback_handle);
  900. } else {
  901. driver->playback_nfds = 0;
  902. }
  903. if (driver->capture_handle) {
  904. driver->capture_nfds =
  905. snd_pcm_poll_descriptors_count (driver->capture_handle);
  906. } else {
  907. driver->capture_nfds = 0;
  908. }
  909. if (driver->pfd) {
  910. free (driver->pfd);
  911. }
  912. driver->pfd = (struct pollfd *)
  913. malloc (sizeof (struct pollfd) *
  914. (driver->playback_nfds + driver->capture_nfds + 2));
  915. if (driver->midi && !driver->xrun_recovery)
  916. (driver->midi->start)(driver->midi);
  917. if (driver->playback_handle) {
  918. /* fill playback buffer with zeroes, and mark
  919. all fragments as having data.
  920. */
  921. pavail = snd_pcm_avail_update (driver->playback_handle);
  922. if (pavail !=
  923. driver->frames_per_cycle * driver->playback_nperiods) {
  924. jack_error ("ALSA: full buffer not available at start");
  925. return -1;
  926. }
  927. if (alsa_driver_get_channel_addresses (driver,
  928. 0, &pavail, 0, &poffset)) {
  929. return -1;
  930. }
  931. /* XXX this is cheating. ALSA offers no guarantee that
  932. we can access the entire buffer at any one time. It
  933. works on most hardware tested so far, however, buts
  934. its a liability in the long run. I think that
  935. alsa-lib may have a better function for doing this
  936. here, where the goal is to silence the entire
  937. buffer.
  938. */
  939. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  940. alsa_driver_silence_on_channel (
  941. driver, chn,
  942. driver->user_nperiods
  943. * driver->frames_per_cycle);
  944. }
  945. snd_pcm_mmap_commit (driver->playback_handle, poffset,
  946. driver->user_nperiods
  947. * driver->frames_per_cycle);
  948. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  949. jack_error ("ALSA: could not start playback (%s)",
  950. snd_strerror (err));
  951. return -1;
  952. }
  953. }
  954. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  955. || !driver->playback_handle) {
  956. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  957. jack_error ("ALSA: could not start capture (%s)",
  958. snd_strerror (err));
  959. return -1;
  960. }
  961. }
  962. return 0;
  963. }
  964. int
  965. JackAlsaDriver::alsa_driver_stop (alsa_driver_t *driver)
  966. {
  967. int err;
  968. //JSList* node;
  969. //int chn;
  970. /* silence all capture port buffers, because we might
  971. be entering offline mode.
  972. */
  973. // steph
  974. /*
  975. for (chn = 0, node = driver->capture_ports; node;
  976. node = jack_slist_next (node), chn++) {
  977. jack_port_t* port;
  978. char* buf;
  979. jack_nframes_t nframes = driver->engine->control->buffer_size;
  980. port = (jack_port_t *) node->data;
  981. buf = jack_port_get_buffer (port, nframes);
  982. memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
  983. }
  984. */
  985. for (int i = 0; i < fPlaybackChannels; i++) {
  986. jack_default_audio_sample_t* buf =
  987. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[i], fEngineControl->fBufferSize);
  988. memset (buf, 0, sizeof (jack_default_audio_sample_t) * fEngineControl->fBufferSize);
  989. }
  990. if (driver->playback_handle) {
  991. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  992. jack_error ("ALSA: channel flush for playback "
  993. "failed (%s)", snd_strerror (err));
  994. return -1;
  995. }
  996. }
  997. if (!driver->playback_handle
  998. || driver->capture_and_playback_not_synced) {
  999. if (driver->capture_handle) {
  1000. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  1001. jack_error ("ALSA: channel flush for "
  1002. "capture failed (%s)",
  1003. snd_strerror (err));
  1004. return -1;
  1005. }
  1006. }
  1007. }
  1008. if (driver->hw_monitoring) {
  1009. driver->hw->set_input_monitor_mask (driver->hw, 0);
  1010. }
  1011. if (driver->midi && !driver->xrun_recovery)
  1012. (driver->midi->stop)(driver->midi);
  1013. return 0;
  1014. }
  1015. int
  1016. JackAlsaDriver::alsa_driver_restart (alsa_driver_t *driver)
  1017. {
  1018. // steph
  1019. /*
  1020. if (driver->nt_stop((struct _jack_driver_nt *) driver))
  1021. return -1;
  1022. return driver->nt_start((struct _jack_driver_nt *) driver);
  1023. */
  1024. driver->xrun_recovery = 1;
  1025. int res = Stop();
  1026. if (!res)
  1027. res = Start();
  1028. driver->xrun_recovery = 0;
  1029. return res;
  1030. }
  1031. int
  1032. JackAlsaDriver::alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
  1033. {
  1034. snd_pcm_status_t *status;
  1035. int res;
  1036. jack_error("alsa_driver_xrun_recovery");
  1037. snd_pcm_status_alloca(&status);
  1038. if (driver->capture_handle) {
  1039. if ((res = snd_pcm_status(driver->capture_handle, status))
  1040. < 0) {
  1041. jack_error("status error: %s", snd_strerror(res));
  1042. }
  1043. } else {
  1044. if ((res = snd_pcm_status(driver->playback_handle, status))
  1045. < 0) {
  1046. jack_error("status error: %s", snd_strerror(res));
  1047. }
  1048. }
  1049. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN
  1050. && driver->process_count > XRUN_REPORT_DELAY) {
  1051. struct timeval now, diff, tstamp;
  1052. driver->xrun_count++;
  1053. snd_pcm_status_get_tstamp(status,&now);
  1054. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  1055. timersub(&now, &tstamp, &diff);
  1056. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  1057. // steph: TODO use a RT version
  1058. jack_error("\n\n**** alsa_pcm: xrun of at least %.3f msecs\n\n", *delayed_usecs / 1000.0);
  1059. }
  1060. if (alsa_driver_restart (driver)) {
  1061. return -1;
  1062. }
  1063. return 0;
  1064. }
  1065. void
  1066. JackAlsaDriver::alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
  1067. jack_nframes_t nframes)
  1068. {
  1069. channel_t chn;
  1070. jack_nframes_t buffer_frames =
  1071. driver->frames_per_cycle * driver->playback_nperiods;
  1072. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  1073. if (bitset_contains (driver->channels_not_done, chn)) {
  1074. if (driver->silent[chn] < buffer_frames) {
  1075. alsa_driver_silence_on_channel_no_mark (
  1076. driver, chn, nframes);
  1077. driver->silent[chn] += nframes;
  1078. }
  1079. }
  1080. }
  1081. }
  1082. jack_nframes_t
  1083. JackAlsaDriver::alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
  1084. *delayed_usecs)
  1085. {
  1086. snd_pcm_sframes_t avail = 0;
  1087. snd_pcm_sframes_t capture_avail = 0;
  1088. snd_pcm_sframes_t playback_avail = 0;
  1089. int xrun_detected = FALSE;
  1090. int need_capture;
  1091. int need_playback;
  1092. unsigned int i;
  1093. jack_time_t poll_enter;
  1094. jack_time_t poll_ret = 0;
  1095. *status = -1;
  1096. *delayed_usecs = 0;
  1097. need_capture = driver->capture_handle ? 1 : 0;
  1098. if (extra_fd >= 0) {
  1099. need_playback = 0;
  1100. } else {
  1101. need_playback = driver->playback_handle ? 1 : 0;
  1102. }
  1103. // steph
  1104. // again:
  1105. while (need_playback || need_capture) {
  1106. unsigned int p_timed_out, c_timed_out;
  1107. unsigned int ci = 0;
  1108. unsigned int nfds;
  1109. nfds = 0;
  1110. if (need_playback) {
  1111. snd_pcm_poll_descriptors (driver->playback_handle,
  1112. &driver->pfd[0],
  1113. driver->playback_nfds);
  1114. nfds += driver->playback_nfds;
  1115. }
  1116. if (need_capture) {
  1117. snd_pcm_poll_descriptors (driver->capture_handle,
  1118. &driver->pfd[nfds],
  1119. driver->capture_nfds);
  1120. ci = nfds;
  1121. nfds += driver->capture_nfds;
  1122. }
  1123. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  1124. for (i = 0; i < nfds; i++) {
  1125. driver->pfd[i].events |= POLLERR;
  1126. }
  1127. if (extra_fd >= 0) {
  1128. driver->pfd[nfds].fd = extra_fd;
  1129. driver->pfd[nfds].events =
  1130. POLLIN | POLLERR | POLLHUP | POLLNVAL;
  1131. nfds++;
  1132. }
  1133. poll_enter = jack_get_microseconds ();
  1134. if (poll_enter > driver->poll_next) {
  1135. /*
  1136. * This processing cycle was delayed past the
  1137. * next due interrupt! Do not account this as
  1138. * a wakeup delay:
  1139. */
  1140. driver->poll_next = 0;
  1141. driver->poll_late++;
  1142. }
  1143. if (poll (driver->pfd, nfds, driver->poll_timeout) < 0) {
  1144. if (errno == EINTR) {
  1145. jack_log("poll interrupt");
  1146. // this happens mostly when run
  1147. // under gdb, or when exiting due to a signal
  1148. // steph
  1149. /*
  1150. if (under_gdb) {
  1151. goto again;
  1152. }
  1153. */
  1154. *status = -2;
  1155. return 0;
  1156. }
  1157. jack_error ("ALSA: poll call failed (%s)",
  1158. strerror (errno));
  1159. *status = -3;
  1160. return 0;
  1161. }
  1162. poll_ret = jack_get_microseconds ();
  1163. // steph
  1164. fBeginDateUst = poll_ret;
  1165. if (extra_fd < 0) {
  1166. if (driver->poll_next && poll_ret > driver->poll_next) {
  1167. *delayed_usecs = poll_ret - driver->poll_next;
  1168. }
  1169. driver->poll_last = poll_ret;
  1170. driver->poll_next = poll_ret + driver->period_usecs;
  1171. // steph
  1172. /*
  1173. driver->engine->transport_cycle_start (driver->engine,
  1174. poll_ret);
  1175. */
  1176. }
  1177. #ifdef DEBUG_WAKEUP
  1178. fprintf (stderr, "%" PRIu64 ": checked %d fds, %" PRIu64
  1179. " usecs since poll entered\n", poll_ret, nfds,
  1180. poll_ret - poll_enter);
  1181. #endif
  1182. /* check to see if it was the extra FD that caused us
  1183. * to return from poll */
  1184. if (extra_fd >= 0) {
  1185. if (driver->pfd[nfds - 1].revents == 0) {
  1186. /* we timed out on the extra fd */
  1187. *status = -4;
  1188. // steph (cannot return negative value....)
  1189. // return -1;
  1190. return 0;
  1191. }
  1192. /* if POLLIN was the only bit set, we're OK */
  1193. *status = 0;
  1194. return (driver->pfd[nfds - 1].revents == POLLIN) ? 0 : -1;
  1195. }
  1196. p_timed_out = 0;
  1197. if (need_playback) {
  1198. for (i = 0; i < driver->playback_nfds; i++) {
  1199. if (driver->pfd[i].revents & POLLERR) {
  1200. xrun_detected = TRUE;
  1201. }
  1202. if (driver->pfd[i].revents == 0) {
  1203. p_timed_out++;
  1204. #ifdef DEBUG_WAKEUP
  1205. fprintf (stderr, "%" PRIu64
  1206. " playback stream timed out\n",
  1207. poll_ret);
  1208. #endif
  1209. }
  1210. }
  1211. if (p_timed_out == 0) {
  1212. need_playback = 0;
  1213. #ifdef DEBUG_WAKEUP
  1214. fprintf (stderr, "%" PRIu64
  1215. " playback stream ready\n",
  1216. poll_ret);
  1217. #endif
  1218. }
  1219. }
  1220. c_timed_out = 0;
  1221. if (need_capture) {
  1222. for (i = ci; i < nfds; i++) {
  1223. if (driver->pfd[i].revents & POLLERR) {
  1224. xrun_detected = TRUE;
  1225. }
  1226. if (driver->pfd[i].revents == 0) {
  1227. c_timed_out++;
  1228. #ifdef DEBUG_WAKEUP
  1229. fprintf (stderr, "%" PRIu64
  1230. " capture stream timed out\n",
  1231. poll_ret);
  1232. #endif
  1233. }
  1234. }
  1235. if (c_timed_out == 0) {
  1236. need_capture = 0;
  1237. #ifdef DEBUG_WAKEUP
  1238. fprintf (stderr, "%" PRIu64
  1239. " capture stream ready\n",
  1240. poll_ret);
  1241. #endif
  1242. }
  1243. }
  1244. if ((p_timed_out && (p_timed_out == driver->playback_nfds)) &&
  1245. (c_timed_out && (c_timed_out == driver->capture_nfds))) {
  1246. // steph
  1247. /*
  1248. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1249. " usecs",
  1250. poll_ret - poll_enter);
  1251. */
  1252. *status = -5;
  1253. return 0;
  1254. }
  1255. }
  1256. if (driver->capture_handle) {
  1257. if ((capture_avail = snd_pcm_avail_update (
  1258. driver->capture_handle)) < 0) {
  1259. if (capture_avail == -EPIPE) {
  1260. xrun_detected = TRUE;
  1261. } else {
  1262. jack_error ("unknown ALSA avail_update return"
  1263. " value (%u)", capture_avail);
  1264. }
  1265. }
  1266. } else {
  1267. /* odd, but see min() computation below */
  1268. capture_avail = INT_MAX;
  1269. }
  1270. if (driver->playback_handle) {
  1271. if ((playback_avail = snd_pcm_avail_update (
  1272. driver->playback_handle)) < 0) {
  1273. if (playback_avail == -EPIPE) {
  1274. xrun_detected = TRUE;
  1275. } else {
  1276. jack_error ("unknown ALSA avail_update return"
  1277. " value (%u)", playback_avail);
  1278. }
  1279. }
  1280. } else {
  1281. /* odd, but see min() computation below */
  1282. playback_avail = INT_MAX;
  1283. }
  1284. if (xrun_detected) {
  1285. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1286. return 0;
  1287. }
  1288. *status = 0;
  1289. driver->last_wait_ust = poll_ret;
  1290. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  1291. #ifdef DEBUG_WAKEUP
  1292. fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
  1293. "cavail = %lu\n",
  1294. avail, playback_avail, capture_avail);
  1295. #endif
  1296. /* mark all channels not done for now. read/write will change this */
  1297. bitset_copy (driver->channels_not_done, driver->channels_done);
  1298. /* constrain the available count to the nearest (round down) number of
  1299. periods.
  1300. */
  1301. return avail - (avail % driver->frames_per_cycle);
  1302. }
  1303. int JackAlsaDriver::SetBufferSize(jack_nframes_t buffer_size)
  1304. {
  1305. jack_log("JackAlsaDriver::SetBufferSize %ld", buffer_size);
  1306. int res = alsa_driver_reset_parameters((alsa_driver_t *)fDriver, buffer_size,
  1307. ((alsa_driver_t *)fDriver)->user_nperiods,
  1308. ((alsa_driver_t *)fDriver)->frame_rate);
  1309. if (res == 0) { // update fEngineControl and fGraphManager
  1310. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  1311. } else {
  1312. alsa_driver_reset_parameters((alsa_driver_t *)fDriver, fEngineControl->fBufferSize,
  1313. ((alsa_driver_t *)fDriver)->user_nperiods,
  1314. ((alsa_driver_t *)fDriver)->frame_rate);
  1315. }
  1316. return res;
  1317. }
  1318. int
  1319. JackAlsaDriver::alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
  1320. {
  1321. snd_pcm_sframes_t contiguous;
  1322. snd_pcm_sframes_t nread;
  1323. snd_pcm_sframes_t offset;
  1324. jack_nframes_t orig_nframes;
  1325. jack_default_audio_sample_t* buf;
  1326. //channel_t chn;
  1327. //JSList *node;
  1328. //jack_port_t* port;
  1329. int err;
  1330. // steph
  1331. /*
  1332. if (!driver->capture_handle || driver->engine->freewheeling) {
  1333. return 0;
  1334. }
  1335. */
  1336. if (!driver->capture_handle) {
  1337. return 0;
  1338. }
  1339. if (nframes > driver->frames_per_cycle) {
  1340. return -1;
  1341. }
  1342. if (driver->midi)
  1343. (driver->midi->read)(driver->midi, nframes);
  1344. nread = 0;
  1345. contiguous = 0;
  1346. orig_nframes = nframes;
  1347. while (nframes) {
  1348. contiguous = nframes;
  1349. if (alsa_driver_get_channel_addresses (
  1350. driver,
  1351. (snd_pcm_uframes_t *) &contiguous,
  1352. (snd_pcm_uframes_t *) 0,
  1353. (snd_pcm_uframes_t *)&offset, 0) < 0) {
  1354. return -1;
  1355. }
  1356. // steph
  1357. for (int i = 0; i < fCaptureChannels; i++) {
  1358. if (fGraphManager->GetConnectionsNum(fCapturePortList[i]) > 0) {
  1359. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[i], orig_nframes);
  1360. alsa_driver_read_from_channel (driver, i, buf + nread, contiguous);
  1361. }
  1362. }
  1363. /* // steph
  1364. for (chn = 0, node = driver->capture_ports; node;
  1365. node = jack_slist_next (node), chn++) {
  1366. port = (jack_port_t *) node->data;
  1367. if (!jack_port_connected (port)) {
  1368. // no-copy optimization
  1369. continue;
  1370. }
  1371. buf = jack_port_get_buffer (port, orig_nframes);
  1372. alsa_driver_read_from_channel (driver, chn,
  1373. buf + nread, contiguous);
  1374. }
  1375. */
  1376. if ((err = snd_pcm_mmap_commit (driver->capture_handle,
  1377. offset, contiguous)) < 0) {
  1378. // steph
  1379. // jack_error ("ALSA: could not complete read of %"
  1380. // PRIu32 " frames: error = %d\n", contiguous, err);
  1381. jack_error ("ALSA: could not complete read of %d frames: error = %d", contiguous, err);
  1382. return -1;
  1383. }
  1384. nframes -= contiguous;
  1385. nread += contiguous;
  1386. }
  1387. return 0;
  1388. }
  1389. int
  1390. JackAlsaDriver::alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
  1391. {
  1392. //channel_t chn;
  1393. //JSList *node;
  1394. //JSList *mon_node;
  1395. jack_default_audio_sample_t* buf;
  1396. jack_default_audio_sample_t* monbuf;
  1397. jack_nframes_t orig_nframes;
  1398. snd_pcm_sframes_t nwritten;
  1399. snd_pcm_sframes_t contiguous;
  1400. snd_pcm_sframes_t offset;
  1401. JackPort* port;
  1402. //jack_port_t *port;
  1403. int err;
  1404. driver->process_count++;
  1405. // steph
  1406. /*
  1407. if (!driver->playback_handle || driver->engine->freewheeling) {
  1408. return 0;
  1409. }
  1410. */
  1411. if (!driver->playback_handle) {
  1412. return 0;
  1413. }
  1414. if (nframes > driver->frames_per_cycle) {
  1415. return -1;
  1416. }
  1417. if (driver->midi)
  1418. (driver->midi->write)(driver->midi, nframes);
  1419. nwritten = 0;
  1420. contiguous = 0;
  1421. orig_nframes = nframes;
  1422. /* check current input monitor request status */
  1423. driver->input_monitor_mask = 0;
  1424. // steph
  1425. /*
  1426. for (chn = 0, node = driver->capture_ports; node;
  1427. node = jack_slist_next (node), chn++) {
  1428. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  1429. driver->input_monitor_mask |= (1<<chn);
  1430. }
  1431. }
  1432. */
  1433. for (int i = 0; i < fCaptureChannels; i++) {
  1434. port = fGraphManager->GetPort(fCapturePortList[i]);
  1435. if (port->MonitoringInput()) {
  1436. driver->input_monitor_mask |= (1 << i);
  1437. }
  1438. }
  1439. if (driver->hw_monitoring) {
  1440. if ((driver->hw->input_monitor_mask
  1441. != driver->input_monitor_mask)
  1442. && !driver->all_monitor_in) {
  1443. driver->hw->set_input_monitor_mask (
  1444. driver->hw, driver->input_monitor_mask);
  1445. }
  1446. }
  1447. while (nframes) {
  1448. contiguous = nframes;
  1449. if (alsa_driver_get_channel_addresses (
  1450. driver,
  1451. (snd_pcm_uframes_t *) 0,
  1452. (snd_pcm_uframes_t *) &contiguous,
  1453. 0, (snd_pcm_uframes_t *)&offset) < 0) {
  1454. return -1;
  1455. }
  1456. // steph
  1457. for (int i = 0; i < fPlaybackChannels; i++) {
  1458. // Ouput ports
  1459. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  1460. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[i], orig_nframes);
  1461. alsa_driver_write_to_channel (driver, i, buf + nwritten, contiguous);
  1462. // Monitor ports
  1463. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
  1464. monbuf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[i], orig_nframes);
  1465. memcpy(monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1466. }
  1467. }
  1468. }
  1469. /*
  1470. for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
  1471. node;
  1472. node = jack_slist_next (node), chn++) {
  1473. port = (jack_port_t *) node->data;
  1474. if (!jack_port_connected (port)) {
  1475. continue;
  1476. }
  1477. buf = jack_port_get_buffer (port, orig_nframes);
  1478. alsa_driver_write_to_channel (driver, chn,
  1479. buf + nwritten, contiguous);
  1480. if (mon_node) {
  1481. port = (jack_port_t *) mon_node->data;
  1482. if (!jack_port_connected (port)) {
  1483. continue;
  1484. }
  1485. monbuf = jack_port_get_buffer (port, orig_nframes);
  1486. memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1487. mon_node = jack_slist_next (mon_node);
  1488. }
  1489. }
  1490. */
  1491. if (!bitset_empty (driver->channels_not_done)) {
  1492. alsa_driver_silence_untouched_channels (driver,
  1493. contiguous);
  1494. }
  1495. if ((err = snd_pcm_mmap_commit (driver->playback_handle,
  1496. offset, contiguous)) < 0) {
  1497. // steph
  1498. // jack_error ("ALSA: could not complete playback of %"
  1499. // PRIu32 " frames: error = %d", contiguous, err);
  1500. jack_error ("ALSA: could not complete playback of %d frames: error = %d", contiguous, err);
  1501. if (err != EPIPE && err != ESTRPIPE)
  1502. return -1;
  1503. }
  1504. nframes -= contiguous;
  1505. nwritten += contiguous;
  1506. }
  1507. return 0;
  1508. }
  1509. void
  1510. JackAlsaDriver::alsa_driver_delete (alsa_driver_t *driver)
  1511. {
  1512. JSList *node;
  1513. if (driver->midi)
  1514. (driver->midi->destroy)(driver->midi);
  1515. for (node = driver->clock_sync_listeners; node;
  1516. node = jack_slist_next (node)) {
  1517. free (node->data);
  1518. }
  1519. jack_slist_free (driver->clock_sync_listeners);
  1520. if (driver->ctl_handle) {
  1521. snd_ctl_close (driver->ctl_handle);
  1522. driver->ctl_handle = 0;
  1523. }
  1524. if (driver->ctl_handle) {
  1525. snd_ctl_close (driver->ctl_handle);
  1526. driver->ctl_handle = 0;
  1527. }
  1528. if (driver->capture_handle) {
  1529. snd_pcm_close (driver->capture_handle);
  1530. driver->capture_handle = 0;
  1531. }
  1532. if (driver->playback_handle) {
  1533. snd_pcm_close (driver->playback_handle);
  1534. driver->capture_handle = 0;
  1535. }
  1536. if (driver->capture_hw_params) {
  1537. snd_pcm_hw_params_free (driver->capture_hw_params);
  1538. driver->capture_hw_params = 0;
  1539. }
  1540. if (driver->playback_hw_params) {
  1541. snd_pcm_hw_params_free (driver->playback_hw_params);
  1542. driver->playback_hw_params = 0;
  1543. }
  1544. if (driver->capture_sw_params) {
  1545. snd_pcm_sw_params_free (driver->capture_sw_params);
  1546. driver->capture_sw_params = 0;
  1547. }
  1548. if (driver->playback_sw_params) {
  1549. snd_pcm_sw_params_free (driver->playback_sw_params);
  1550. driver->playback_sw_params = 0;
  1551. }
  1552. if (driver->pfd) {
  1553. free (driver->pfd);
  1554. }
  1555. if (driver->hw) {
  1556. driver->hw->release (driver->hw);
  1557. driver->hw = 0;
  1558. }
  1559. free(driver->alsa_name_playback);
  1560. free(driver->alsa_name_capture);
  1561. free(driver->alsa_driver);
  1562. alsa_driver_release_channel_dependent_memory (driver);
  1563. free (driver);
  1564. }
  1565. jack_driver_t *
  1566. JackAlsaDriver::alsa_driver_new (const char *name, char *playback_alsa_device,
  1567. char *capture_alsa_device,
  1568. jack_client_t *client,
  1569. jack_nframes_t frames_per_cycle,
  1570. jack_nframes_t user_nperiods,
  1571. jack_nframes_t rate,
  1572. int hw_monitoring,
  1573. int hw_metering,
  1574. int capturing,
  1575. int playing,
  1576. DitherAlgorithm dither,
  1577. int soft_mode,
  1578. int monitor,
  1579. int user_capture_nchnls,
  1580. int user_playback_nchnls,
  1581. int shorts_first,
  1582. jack_nframes_t capture_latency,
  1583. jack_nframes_t playback_latency,
  1584. alsa_midi_t *midi)
  1585. {
  1586. int err;
  1587. alsa_driver_t *driver;
  1588. /*
  1589. printf ("creating alsa driver ... %s|%s|%" PRIu32 "|%" PRIu32
  1590. "|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s\n",
  1591. playing ? playback_alsa_device : "-",
  1592. capturing ? capture_alsa_device : "-",
  1593. frames_per_cycle, user_nperiods, rate,
  1594. user_capture_nchnls,user_playback_nchnls,
  1595. hw_monitoring ? "hwmon": "nomon",
  1596. hw_metering ? "hwmeter":"swmeter",
  1597. soft_mode ? "soft-mode":"-",
  1598. shorts_first ? "16bit":"32bit");
  1599. */
  1600. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  1601. jack_driver_nt_init ((jack_driver_nt_t *) driver);
  1602. driver->midi = midi;
  1603. driver->xrun_recovery = 0;
  1604. //driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
  1605. //driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
  1606. //driver->read = (JackDriverReadFunction) alsa_driver_read;
  1607. //driver->write = (JackDriverReadFunction) alsa_driver_write;
  1608. //driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
  1609. //driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
  1610. //driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
  1611. //driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
  1612. //driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
  1613. driver->playback_handle = NULL;
  1614. driver->capture_handle = NULL;
  1615. driver->ctl_handle = 0;
  1616. driver->hw = 0;
  1617. driver->capture_and_playback_not_synced = FALSE;
  1618. driver->max_nchannels = 0;
  1619. driver->user_nchannels = 0;
  1620. driver->playback_nchannels = user_playback_nchnls;
  1621. driver->capture_nchannels = user_capture_nchnls;
  1622. driver->playback_sample_bytes = (shorts_first ? 2 : 4);
  1623. driver->capture_sample_bytes = (shorts_first ? 2 : 4);
  1624. driver->capture_frame_latency = capture_latency;
  1625. driver->playback_frame_latency = playback_latency;
  1626. driver->playback_addr = 0;
  1627. driver->capture_addr = 0;
  1628. driver->playback_interleave_skip = NULL; // steph
  1629. driver->capture_interleave_skip = NULL; // steph
  1630. driver->silent = 0;
  1631. driver->all_monitor_in = FALSE;
  1632. driver->with_monitor_ports = monitor;
  1633. driver->clock_mode = ClockMaster; /* XXX is it? */
  1634. driver->input_monitor_mask = 0; /* XXX is it? */
  1635. driver->capture_ports = 0;
  1636. driver->playback_ports = 0;
  1637. driver->monitor_ports = 0;
  1638. driver->pfd = 0;
  1639. driver->playback_nfds = 0;
  1640. driver->capture_nfds = 0;
  1641. driver->dither = dither;
  1642. driver->soft_mode = soft_mode;
  1643. pthread_mutex_init (&driver->clock_sync_lock, 0);
  1644. driver->clock_sync_listeners = 0;
  1645. driver->poll_late = 0;
  1646. driver->xrun_count = 0;
  1647. driver->process_count = 0;
  1648. driver->alsa_name_playback = strdup (playback_alsa_device);
  1649. driver->alsa_name_capture = strdup (capture_alsa_device);
  1650. if (alsa_driver_check_card_type (driver)) {
  1651. alsa_driver_delete (driver);
  1652. return NULL;
  1653. }
  1654. alsa_driver_hw_specific (driver, hw_monitoring, hw_metering);
  1655. if (playing) {
  1656. if (snd_pcm_open (&driver->playback_handle,
  1657. playback_alsa_device,
  1658. SND_PCM_STREAM_PLAYBACK,
  1659. SND_PCM_NONBLOCK) < 0) {
  1660. switch (errno) {
  1661. case EBUSY:
  1662. jack_error ("the playback device \"%s\" is "
  1663. "already in use. Please stop the"
  1664. " application using it and "
  1665. "run JACK again",
  1666. playback_alsa_device);
  1667. alsa_driver_delete (driver);
  1668. return NULL;
  1669. break;
  1670. case EPERM:
  1671. jack_error ("you do not have permission to open "
  1672. "the audio device \"%s\" for playback",
  1673. playback_alsa_device);
  1674. alsa_driver_delete (driver);
  1675. return NULL;
  1676. break;
  1677. }
  1678. driver->playback_handle = NULL;
  1679. }
  1680. if (driver->playback_handle) {
  1681. snd_pcm_nonblock (driver->playback_handle, 0);
  1682. }
  1683. }
  1684. if (capturing) {
  1685. if (snd_pcm_open (&driver->capture_handle,
  1686. capture_alsa_device,
  1687. SND_PCM_STREAM_CAPTURE,
  1688. SND_PCM_NONBLOCK) < 0) {
  1689. switch (errno) {
  1690. case EBUSY:
  1691. jack_error ("the capture device \"%s\" is "
  1692. "already in use. Please stop the"
  1693. " application using it and "
  1694. "run JACK again",
  1695. capture_alsa_device);
  1696. alsa_driver_delete (driver);
  1697. return NULL;
  1698. break;
  1699. case EPERM:
  1700. jack_error ("you do not have permission to open "
  1701. "the audio device \"%s\" for capture",
  1702. capture_alsa_device);
  1703. alsa_driver_delete (driver);
  1704. return NULL;
  1705. break;
  1706. }
  1707. driver->capture_handle = NULL;
  1708. }
  1709. if (driver->capture_handle) {
  1710. snd_pcm_nonblock (driver->capture_handle, 0);
  1711. }
  1712. }
  1713. if (driver->playback_handle == NULL) {
  1714. if (playing) {
  1715. /* they asked for playback, but we can't do it */
  1716. jack_error ("ALSA: Cannot open PCM device %s for "
  1717. "playback. Falling back to capture-only"
  1718. " mode", name);
  1719. if (driver->capture_handle == NULL) {
  1720. /* can't do anything */
  1721. alsa_driver_delete (driver);
  1722. return NULL;
  1723. }
  1724. playing = FALSE;
  1725. }
  1726. }
  1727. if (driver->capture_handle == NULL) {
  1728. if (capturing) {
  1729. /* they asked for capture, but we can't do it */
  1730. jack_error ("ALSA: Cannot open PCM device %s for "
  1731. "capture. Falling back to playback-only"
  1732. " mode", name);
  1733. if (driver->playback_handle == NULL) {
  1734. /* can't do anything */
  1735. alsa_driver_delete (driver);
  1736. return NULL;
  1737. }
  1738. capturing = FALSE;
  1739. }
  1740. }
  1741. driver->playback_hw_params = 0;
  1742. driver->capture_hw_params = 0;
  1743. driver->playback_sw_params = 0;
  1744. driver->capture_sw_params = 0;
  1745. if (driver->playback_handle) {
  1746. if ((err = snd_pcm_hw_params_malloc (
  1747. &driver->playback_hw_params)) < 0) {
  1748. jack_error ("ALSA: could not allocate playback hw"
  1749. " params structure");
  1750. alsa_driver_delete (driver);
  1751. return NULL;
  1752. }
  1753. if ((err = snd_pcm_sw_params_malloc (
  1754. &driver->playback_sw_params)) < 0) {
  1755. jack_error ("ALSA: could not allocate playback sw"
  1756. " params structure");
  1757. alsa_driver_delete (driver);
  1758. return NULL;
  1759. }
  1760. }
  1761. if (driver->capture_handle) {
  1762. if ((err = snd_pcm_hw_params_malloc (
  1763. &driver->capture_hw_params)) < 0) {
  1764. jack_error ("ALSA: could not allocate capture hw"
  1765. " params structure");
  1766. alsa_driver_delete (driver);
  1767. return NULL;
  1768. }
  1769. if ((err = snd_pcm_sw_params_malloc (
  1770. &driver->capture_sw_params)) < 0) {
  1771. jack_error ("ALSA: could not allocate capture sw"
  1772. " params structure");
  1773. alsa_driver_delete (driver);
  1774. return NULL;
  1775. }
  1776. }
  1777. if (alsa_driver_set_parameters (driver, frames_per_cycle,
  1778. user_nperiods, rate)) {
  1779. alsa_driver_delete (driver);
  1780. return NULL;
  1781. }
  1782. driver->capture_and_playback_not_synced = FALSE;
  1783. if (driver->capture_handle && driver->playback_handle) {
  1784. if (snd_pcm_link (driver->capture_handle,
  1785. driver->playback_handle) != 0) {
  1786. driver->capture_and_playback_not_synced = TRUE;
  1787. }
  1788. }
  1789. driver->client = client;
  1790. return (jack_driver_t *) driver;
  1791. }
  1792. int JackAlsaDriver::Attach()
  1793. {
  1794. JackPort* port;
  1795. int port_index;
  1796. unsigned long port_flags;
  1797. char name[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1798. char alias[JACK_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1799. assert(fCaptureChannels < DRIVER_PORT_NUM);
  1800. assert(fPlaybackChannels < DRIVER_PORT_NUM);
  1801. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  1802. alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
  1803. if (alsa_driver->has_hw_monitoring)
  1804. port_flags |= JackPortCanMonitor;
  1805. // ALSA driver may have changed the values
  1806. JackAudioDriver::SetBufferSize(alsa_driver->frames_per_cycle);
  1807. JackAudioDriver::SetSampleRate(alsa_driver->frame_rate);
  1808. jack_log("JackAudioDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  1809. for (int i = 0; i < fCaptureChannels; i++) {
  1810. snprintf(alias, sizeof(alias) - 1, "%s:capture_%u", fAliasName, i + 1);
  1811. snprintf(name, sizeof(name) - 1, "%s:capture_%d", fClientControl.fName, i + 1);
  1812. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  1813. jack_error("driver: cannot register port for %s", name);
  1814. return -1;
  1815. }
  1816. port = fGraphManager->GetPort(port_index);
  1817. port->SetAlias(alias);
  1818. port->SetLatency(alsa_driver->frames_per_cycle + alsa_driver->capture_frame_latency);
  1819. fCapturePortList[i] = port_index;
  1820. jack_log("JackAudioDriver::Attach fCapturePortList[i] %ld ", port_index);
  1821. }
  1822. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  1823. for (int i = 0; i < fPlaybackChannels; i++) {
  1824. snprintf(alias, sizeof(alias) - 1, "%s:playback_%u", fAliasName, i + 1);
  1825. snprintf(name, sizeof(name) - 1, "%s:playback_%d", fClientControl.fName, i + 1);
  1826. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags, fEngineControl->fBufferSize)) == NO_PORT) {
  1827. jack_error("driver: cannot register port for %s", name);
  1828. return -1;
  1829. }
  1830. port = fGraphManager->GetPort(port_index);
  1831. port->SetAlias(alias);
  1832. // Add one buffer more latency if "async" mode is used...
  1833. port->SetLatency((alsa_driver->frames_per_cycle * (alsa_driver->user_nperiods - 1)) +
  1834. ((fEngineControl->fSyncMode) ? 0 : fEngineControl->fBufferSize) + alsa_driver->playback_frame_latency);
  1835. fPlaybackPortList[i] = port_index;
  1836. jack_log("JackAudioDriver::Attach fPlaybackPortList[i] %ld ", port_index);
  1837. // Monitor ports
  1838. if (fWithMonitorPorts) {
  1839. jack_log("Create monitor port ");
  1840. snprintf(name, sizeof(name) - 1, "%s:monitor_%d", fClientControl.fName, i + 1);
  1841. if ((port_index = fGraphManager->AllocatePort(fClientControl.fRefNum, name, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, fEngineControl->fBufferSize)) == NO_PORT) {
  1842. jack_error ("ALSA: cannot register monitor port for %s", name);
  1843. } else {
  1844. port = fGraphManager->GetPort(port_index);
  1845. port->SetLatency(alsa_driver->frames_per_cycle);
  1846. fMonitorPortList[i] = port_index;
  1847. }
  1848. }
  1849. }
  1850. if (alsa_driver->midi) {
  1851. int err = (alsa_driver->midi->attach)(alsa_driver->midi);
  1852. if (err)
  1853. jack_error ("ALSA: cannot attach MIDI: %d", err);
  1854. }
  1855. return 0;
  1856. }
  1857. int JackAlsaDriver::Detach()
  1858. {
  1859. alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
  1860. if (alsa_driver->midi)
  1861. (alsa_driver->midi->detach)(alsa_driver->midi);
  1862. return JackAudioDriver::Detach();
  1863. }
  1864. int JackAlsaDriver::Open(jack_nframes_t nframes,
  1865. jack_nframes_t user_nperiods,
  1866. jack_nframes_t samplerate,
  1867. bool hw_monitoring,
  1868. bool hw_metering,
  1869. bool capturing,
  1870. bool playing,
  1871. DitherAlgorithm dither,
  1872. bool soft_mode,
  1873. bool monitor,
  1874. int inchannels,
  1875. int outchannels,
  1876. bool shorts_first,
  1877. const char* capture_driver_name,
  1878. const char* playback_driver_name,
  1879. jack_nframes_t capture_latency,
  1880. jack_nframes_t playback_latency,
  1881. const char* midi_driver_name)
  1882. {
  1883. // Generic JackAudioDriver Open
  1884. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing,
  1885. inchannels, outchannels, monitor, capture_driver_name, playback_driver_name,
  1886. capture_latency, playback_latency) != 0) {
  1887. return -1;
  1888. }
  1889. alsa_midi_t *midi = 0;
  1890. if (strcmp(midi_driver_name, "seq") == 0)
  1891. midi = alsa_seqmidi_new((jack_client_t*)this, 0);
  1892. else if (strcmp(midi_driver_name, "raw") == 0)
  1893. midi = alsa_rawmidi_new((jack_client_t*)this);
  1894. fDriver = alsa_driver_new ("alsa_pcm", (char*)playback_driver_name, (char*)capture_driver_name,
  1895. NULL,
  1896. nframes,
  1897. user_nperiods,
  1898. samplerate,
  1899. hw_monitoring,
  1900. hw_metering,
  1901. capturing,
  1902. playing,
  1903. dither,
  1904. soft_mode,
  1905. monitor,
  1906. inchannels,
  1907. outchannels,
  1908. shorts_first,
  1909. capture_latency,
  1910. playback_latency,
  1911. midi);
  1912. if (fDriver) {
  1913. // ALSA driver may have changed the in/out values
  1914. fCaptureChannels = ((alsa_driver_t *)fDriver)->capture_nchannels;
  1915. fPlaybackChannels = ((alsa_driver_t *)fDriver)->playback_nchannels;
  1916. return 0;
  1917. } else {
  1918. return -1;
  1919. }
  1920. }
  1921. int JackAlsaDriver::Close()
  1922. {
  1923. JackAudioDriver::Close();
  1924. alsa_driver_delete((alsa_driver_t*)fDriver);
  1925. return 0;
  1926. }
  1927. int JackAlsaDriver::Start()
  1928. {
  1929. return alsa_driver_start((alsa_driver_t *)fDriver);
  1930. }
  1931. int JackAlsaDriver::Stop()
  1932. {
  1933. return alsa_driver_stop((alsa_driver_t *)fDriver);
  1934. }
  1935. int JackAlsaDriver::Read()
  1936. {
  1937. /* Taken from alsa_driver_run_cycle */
  1938. int wait_status;
  1939. jack_nframes_t nframes;
  1940. fDelayedUsecs = 0.f;
  1941. nframes = alsa_driver_wait((alsa_driver_t *)fDriver, -1, &wait_status, &fDelayedUsecs);
  1942. if (wait_status < 0)
  1943. return -1; /* driver failed */
  1944. if (nframes == 0) {
  1945. /* we detected an xrun and restarted: notify
  1946. * clients about the delay.
  1947. */
  1948. jack_log("ALSA XRun");
  1949. NotifyXRun(fBeginDateUst, fDelayedUsecs);
  1950. return -1;
  1951. }
  1952. if (nframes != fEngineControl->fBufferSize)
  1953. jack_log("JackAlsaDriver::Read nframes = %ld", nframes);
  1954. // Has to be done before read
  1955. JackDriver::CycleIncTime();
  1956. return alsa_driver_read((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
  1957. }
  1958. int JackAlsaDriver::Write()
  1959. {
  1960. return alsa_driver_write((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
  1961. }
  1962. void
  1963. JackAlsaDriver::jack_driver_init (jack_driver_t *driver)
  1964. {
  1965. memset (driver, 0, sizeof (*driver));
  1966. driver->attach = 0;
  1967. driver->detach = 0;
  1968. driver->write = 0;
  1969. driver->read = 0;
  1970. driver->null_cycle = 0;
  1971. driver->bufsize = 0;
  1972. driver->start = 0;
  1973. driver->stop = 0;
  1974. }
  1975. void
  1976. JackAlsaDriver::jack_driver_nt_init (jack_driver_nt_t * driver)
  1977. {
  1978. memset (driver, 0, sizeof (*driver));
  1979. jack_driver_init ((jack_driver_t *) driver);
  1980. driver->attach = 0;
  1981. driver->detach = 0;
  1982. driver->bufsize = 0;
  1983. driver->stop = 0;
  1984. driver->start = 0;
  1985. driver->nt_bufsize = 0;
  1986. driver->nt_start = 0;
  1987. driver->nt_stop = 0;
  1988. driver->nt_attach = 0;
  1989. driver->nt_detach = 0;
  1990. driver->nt_run_cycle = 0;
  1991. }
  1992. int JackAlsaDriver::is_realtime() const
  1993. {
  1994. return fEngineControl->fRealTime;
  1995. }
  1996. int JackAlsaDriver::create_thread(pthread_t *thread, int priority, int realtime, void *(*start_routine)(void*), void *arg)
  1997. {
  1998. return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
  1999. }
  2000. jack_port_id_t JackAlsaDriver::port_register(const char *port_name, const char *port_type, unsigned long flags, unsigned long buffer_size)
  2001. {
  2002. unsigned int port_index;
  2003. int res = fEngine->PortRegister(fClientControl.fRefNum, port_name, port_type, flags, buffer_size, &port_index);
  2004. return (res == 0) ? port_index : 0;
  2005. }
  2006. int JackAlsaDriver::port_unregister(jack_port_id_t port_index)
  2007. {
  2008. return fEngine->PortUnRegister(fClientControl.fRefNum, port_index);
  2009. }
  2010. void* JackAlsaDriver::port_get_buffer(int port, jack_nframes_t nframes)
  2011. {
  2012. return fGraphManager->GetBuffer(port, nframes);
  2013. }
  2014. int JackAlsaDriver::port_set_alias(int port, const char* name)
  2015. {
  2016. return fGraphManager->GetPort(port)->SetAlias(name);
  2017. }
  2018. jack_nframes_t JackAlsaDriver::get_sample_rate() const
  2019. {
  2020. return fEngineControl->fSampleRate;
  2021. }
  2022. jack_nframes_t JackAlsaDriver::frame_time() const
  2023. {
  2024. JackTimer timer;
  2025. fEngineControl->ReadFrameTime(&timer);
  2026. return timer.Time2Frames(GetMicroSeconds(), fEngineControl->fBufferSize);
  2027. }
  2028. jack_nframes_t JackAlsaDriver::last_frame_time() const
  2029. {
  2030. JackTimer timer;
  2031. fEngineControl->ReadFrameTime(&timer);
  2032. return timer.CurFrame();
  2033. }
  2034. } // end of namespace
  2035. #ifdef __cplusplus
  2036. extern "C"
  2037. {
  2038. #endif
  2039. static int
  2040. dither_opt (char c, DitherAlgorithm* dither)
  2041. {
  2042. switch (c) {
  2043. case '-':
  2044. case 'n':
  2045. *dither = None;
  2046. break;
  2047. case 'r':
  2048. *dither = Rectangular;
  2049. break;
  2050. case 's':
  2051. *dither = Shaped;
  2052. break;
  2053. case 't':
  2054. *dither = Triangular;
  2055. break;
  2056. default:
  2057. fprintf (stderr, "ALSA driver: illegal dithering mode %c\n", c);
  2058. return -1;
  2059. }
  2060. return 0;
  2061. }
  2062. EXPORT const jack_driver_desc_t* driver_get_descriptor ()
  2063. {
  2064. jack_driver_desc_t * desc;
  2065. jack_driver_param_desc_t * params;
  2066. unsigned int i;
  2067. desc = (jack_driver_desc_t*)calloc (1, sizeof (jack_driver_desc_t));
  2068. strcpy (desc->name, "alsa");
  2069. desc->nparams = 18;
  2070. params = (jack_driver_param_desc_t*)calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
  2071. i = 0;
  2072. strcpy (params[i].name, "capture");
  2073. params[i].character = 'C';
  2074. params[i].type = JackDriverParamString;
  2075. strcpy (params[i].value.str, "none");
  2076. strcpy (params[i].short_desc,
  2077. "Provide capture ports. Optionally set device");
  2078. strcpy (params[i].long_desc, params[i].short_desc);
  2079. i++;
  2080. strcpy (params[i].name, "playback");
  2081. params[i].character = 'P';
  2082. params[i].type = JackDriverParamString;
  2083. strcpy (params[i].value.str, "none");
  2084. strcpy (params[i].short_desc,
  2085. "Provide playback ports. Optionally set device");
  2086. strcpy (params[i].long_desc, params[i].short_desc);
  2087. i++;
  2088. strcpy (params[i].name, "device");
  2089. params[i].character = 'd';
  2090. params[i].type = JackDriverParamString;
  2091. strcpy (params[i].value.str, "hw:0");
  2092. strcpy (params[i].short_desc, "ALSA device name");
  2093. strcpy (params[i].long_desc, params[i].short_desc);
  2094. i++;
  2095. strcpy (params[i].name, "rate");
  2096. params[i].character = 'r';
  2097. params[i].type = JackDriverParamUInt;
  2098. params[i].value.ui = 48000U;
  2099. strcpy (params[i].short_desc, "Sample rate");
  2100. strcpy (params[i].long_desc, params[i].short_desc);
  2101. i++;
  2102. strcpy (params[i].name, "period");
  2103. params[i].character = 'p';
  2104. params[i].type = JackDriverParamUInt;
  2105. params[i].value.ui = 1024U;
  2106. strcpy (params[i].short_desc, "Frames per period");
  2107. strcpy (params[i].long_desc, params[i].short_desc);
  2108. i++;
  2109. strcpy (params[i].name, "nperiods");
  2110. params[i].character = 'n';
  2111. params[i].type = JackDriverParamUInt;
  2112. params[i].value.ui = 2U;
  2113. strcpy (params[i].short_desc, "Number of periods of playback latency");
  2114. strcpy (params[i].long_desc, params[i].short_desc);
  2115. i++;
  2116. strcpy (params[i].name, "hwmon");
  2117. params[i].character = 'H';
  2118. params[i].type = JackDriverParamBool;
  2119. params[i].value.i = 0;
  2120. strcpy (params[i].short_desc, "Hardware monitoring, if available");
  2121. strcpy (params[i].long_desc, params[i].short_desc);
  2122. i++;
  2123. strcpy (params[i].name, "hwmeter");
  2124. params[i].character = 'M';
  2125. params[i].type = JackDriverParamBool;
  2126. params[i].value.i = 0;
  2127. strcpy (params[i].short_desc, "Hardware metering, if available");
  2128. strcpy (params[i].long_desc, params[i].short_desc);
  2129. i++;
  2130. strcpy (params[i].name, "duplex");
  2131. params[i].character = 'D';
  2132. params[i].type = JackDriverParamBool;
  2133. params[i].value.i = 1;
  2134. strcpy (params[i].short_desc,
  2135. "Provide both capture and playback ports");
  2136. strcpy (params[i].long_desc, params[i].short_desc);
  2137. i++;
  2138. strcpy (params[i].name, "softmode");
  2139. params[i].character = 's';
  2140. params[i].type = JackDriverParamBool;
  2141. params[i].value.i = 0;
  2142. strcpy (params[i].short_desc, "Soft-mode, no xrun handling");
  2143. strcpy (params[i].long_desc, params[i].short_desc);
  2144. i++;
  2145. strcpy (params[i].name, "monitor");
  2146. params[i].character = 'm';
  2147. params[i].type = JackDriverParamBool;
  2148. params[i].value.i = 0;
  2149. strcpy (params[i].short_desc, "Provide monitor ports for the output");
  2150. strcpy (params[i].long_desc, params[i].short_desc);
  2151. i++;
  2152. strcpy (params[i].name, "dither");
  2153. params[i].character = 'z';
  2154. params[i].type = JackDriverParamChar;
  2155. params[i].value.c = 'n';
  2156. strcpy (params[i].short_desc, "Dithering mode");
  2157. strcpy (params[i].long_desc,
  2158. "Dithering mode:\n"
  2159. " n - none\n"
  2160. " r - rectangular\n"
  2161. " s - shaped\n"
  2162. " t - triangular");
  2163. i++;
  2164. strcpy (params[i].name, "inchannels");
  2165. params[i].character = 'i';
  2166. params[i].type = JackDriverParamUInt;
  2167. params[i].value.i = 0;
  2168. strcpy (params[i].short_desc,
  2169. "Number of capture channels (defaults to hardware max)");
  2170. strcpy (params[i].long_desc, params[i].short_desc);
  2171. i++;
  2172. strcpy (params[i].name, "outchannels");
  2173. params[i].character = 'o';
  2174. params[i].type = JackDriverParamUInt;
  2175. params[i].value.i = 0;
  2176. strcpy (params[i].short_desc,
  2177. "Number of playback channels (defaults to hardware max)");
  2178. strcpy (params[i].long_desc, params[i].short_desc);
  2179. i++;
  2180. strcpy (params[i].name, "shorts");
  2181. params[i].character = 'S';
  2182. params[i].type = JackDriverParamBool;
  2183. params[i].value.i = FALSE;
  2184. strcpy (params[i].short_desc, "Try 16-bit samples before 32-bit");
  2185. strcpy (params[i].long_desc, params[i].short_desc);
  2186. i++;
  2187. strcpy (params[i].name, "input-latency");
  2188. params[i].character = 'I';
  2189. params[i].type = JackDriverParamUInt;
  2190. params[i].value.i = 0;
  2191. strcpy (params[i].short_desc, "Extra input latency (frames)");
  2192. strcpy (params[i].long_desc, params[i].short_desc);
  2193. i++;
  2194. strcpy (params[i].name, "output-latency");
  2195. params[i].character = 'O';
  2196. params[i].type = JackDriverParamUInt;
  2197. params[i].value.i = 0;
  2198. strcpy (params[i].short_desc, "Extra output latency (frames)");
  2199. strcpy (params[i].long_desc, params[i].short_desc);
  2200. i++;
  2201. strcpy (params[i].name, "midi-driver");
  2202. params[i].character = 'X';
  2203. params[i].type = JackDriverParamString;
  2204. strcpy (params[i].value.str, "none");
  2205. strcpy (params[i].short_desc, "ALSA MIDI driver name (seq|raw)");
  2206. strcpy (params[i].long_desc,
  2207. "ALSA MIDI driver:\n"
  2208. " none - no MIDI driver\n"
  2209. " seq - ALSA Sequencer driver\n"
  2210. " raw - ALSA RawMIDI driver\n");
  2211. desc->params = params;
  2212. return desc;
  2213. }
  2214. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  2215. {
  2216. jack_nframes_t srate = 48000;
  2217. jack_nframes_t frames_per_interrupt = 1024;
  2218. unsigned long user_nperiods = 2;
  2219. const char *playback_pcm_name = "hw:0";
  2220. const char *capture_pcm_name = "hw:0";
  2221. int hw_monitoring = FALSE;
  2222. int hw_metering = FALSE;
  2223. int capture = FALSE;
  2224. int playback = FALSE;
  2225. int soft_mode = FALSE;
  2226. int monitor = FALSE;
  2227. DitherAlgorithm dither = None;
  2228. int user_capture_nchnls = 0;
  2229. int user_playback_nchnls = 0;
  2230. int shorts_first = FALSE;
  2231. jack_nframes_t systemic_input_latency = 0;
  2232. jack_nframes_t systemic_output_latency = 0;
  2233. const JSList * node;
  2234. const jack_driver_param_t * param;
  2235. const char *midi_driver = "none";
  2236. for (node = params; node; node = jack_slist_next (node)) {
  2237. param = (const jack_driver_param_t *) node->data;
  2238. switch (param->character) {
  2239. case 'C':
  2240. capture = TRUE;
  2241. if (strcmp (param->value.str, "none") != 0) {
  2242. capture_pcm_name = strdup (param->value.str);
  2243. jack_log("capture device %s", capture_pcm_name);
  2244. }
  2245. break;
  2246. case 'P':
  2247. playback = TRUE;
  2248. if (strcmp (param->value.str, "none") != 0) {
  2249. playback_pcm_name = strdup (param->value.str);
  2250. jack_log("playback device %s", playback_pcm_name);
  2251. }
  2252. break;
  2253. case 'D':
  2254. playback = TRUE;
  2255. capture = TRUE;
  2256. break;
  2257. case 'd':
  2258. playback_pcm_name = strdup (param->value.str);
  2259. capture_pcm_name = strdup (param->value.str);
  2260. jack_log("playback device %s", playback_pcm_name);
  2261. jack_log("capture device %s", capture_pcm_name);
  2262. break;
  2263. case 'H':
  2264. hw_monitoring = param->value.i;
  2265. break;
  2266. case 'm':
  2267. monitor = param->value.i;
  2268. break;
  2269. case 'M':
  2270. hw_metering = param->value.i;
  2271. break;
  2272. case 'r':
  2273. srate = param->value.ui;
  2274. jack_log("apparent rate = %d", srate);
  2275. break;
  2276. case 'p':
  2277. frames_per_interrupt = param->value.ui;
  2278. jack_log("frames per period = %d", frames_per_interrupt);
  2279. break;
  2280. case 'n':
  2281. user_nperiods = param->value.ui;
  2282. if (user_nperiods < 2) /* enforce minimum value */
  2283. user_nperiods = 2;
  2284. break;
  2285. case 's':
  2286. soft_mode = param->value.i;
  2287. break;
  2288. case 'z':
  2289. if (dither_opt (param->value.c, &dither)) {
  2290. return NULL;
  2291. }
  2292. break;
  2293. case 'i':
  2294. user_capture_nchnls = param->value.ui;
  2295. break;
  2296. case 'o':
  2297. user_playback_nchnls = param->value.ui;
  2298. break;
  2299. case 'S':
  2300. shorts_first = param->value.i;
  2301. break;
  2302. case 'I':
  2303. systemic_input_latency = param->value.ui;
  2304. break;
  2305. case 'O':
  2306. systemic_output_latency = param->value.ui;
  2307. break;
  2308. case 'X':
  2309. midi_driver = strdup(param->value.str);
  2310. break;
  2311. }
  2312. }
  2313. /* duplex is the default */
  2314. if (!capture && !playback) {
  2315. capture = TRUE;
  2316. playback = TRUE;
  2317. }
  2318. Jack::JackAlsaDriver* alsa_driver = new Jack::JackAlsaDriver("system", "alsa_pcm", engine, table);
  2319. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(alsa_driver);
  2320. // Special open for ALSA driver...
  2321. if (alsa_driver->Open(frames_per_interrupt, user_nperiods, srate, hw_monitoring, hw_metering, capture, playback, dither, soft_mode, monitor,
  2322. user_capture_nchnls, user_playback_nchnls, shorts_first, capture_pcm_name, playback_pcm_name,
  2323. systemic_input_latency, systemic_output_latency, midi_driver) == 0) {
  2324. return threaded_driver;
  2325. } else {
  2326. delete threaded_driver; // Delete the decorated driver
  2327. return NULL;
  2328. }
  2329. }
  2330. #ifdef __cplusplus
  2331. }
  2332. #endif