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.

2173 lines
54KB

  1. /* -*- mode: c; c-file-style: "linux"; -*- */
  2. /*
  3. Copyright (C) 2001 Paul Davis
  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. #define __STDC_FORMAT_MACROS // For inttypes.h to work in C++
  17. #include <math.h>
  18. #include <stdio.h>
  19. #include <memory.h>
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <errno.h>
  23. #include <stdarg.h>
  24. #include <signal.h>
  25. #include <sys/types.h>
  26. #include <sys/time.h>
  27. #include <regex.h>
  28. #include <string.h>
  29. #include "alsa_driver.h"
  30. #include "hammerfall.h"
  31. #include "hdsp.h"
  32. #include "ice1712.h"
  33. #include "usx2y.h"
  34. #include "generic.h"
  35. #include "memops.h"
  36. #include "JackError.h"
  37. #include "alsa_midi_impl.h"
  38. extern void store_work_time (int);
  39. extern void store_wait_time (int);
  40. extern void show_wait_times ();
  41. extern void show_work_times ();
  42. #undef DEBUG_WAKEUP
  43. /* Delay (in process calls) before jackd will report an xrun */
  44. #define XRUN_REPORT_DELAY 0
  45. void
  46. jack_driver_init (jack_driver_t *driver)
  47. {
  48. memset (driver, 0, sizeof (*driver));
  49. driver->attach = 0;
  50. driver->detach = 0;
  51. driver->write = 0;
  52. driver->read = 0;
  53. driver->null_cycle = 0;
  54. driver->bufsize = 0;
  55. driver->start = 0;
  56. driver->stop = 0;
  57. }
  58. void
  59. jack_driver_nt_init (jack_driver_nt_t * driver)
  60. {
  61. memset (driver, 0, sizeof (*driver));
  62. jack_driver_init ((jack_driver_t *) driver);
  63. driver->attach = 0;
  64. driver->detach = 0;
  65. driver->bufsize = 0;
  66. driver->stop = 0;
  67. driver->start = 0;
  68. driver->nt_bufsize = 0;
  69. driver->nt_start = 0;
  70. driver->nt_stop = 0;
  71. driver->nt_attach = 0;
  72. driver->nt_detach = 0;
  73. driver->nt_run_cycle = 0;
  74. }
  75. static void
  76. alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
  77. {
  78. bitset_destroy (&driver->channels_done);
  79. bitset_destroy (&driver->channels_not_done);
  80. if (driver->playback_addr) {
  81. free (driver->playback_addr);
  82. driver->playback_addr = 0;
  83. }
  84. if (driver->capture_addr) {
  85. free (driver->capture_addr);
  86. driver->capture_addr = 0;
  87. }
  88. if (driver->playback_interleave_skip) {
  89. free (driver->playback_interleave_skip);
  90. driver->playback_interleave_skip = NULL;
  91. }
  92. if (driver->capture_interleave_skip) {
  93. free (driver->capture_interleave_skip);
  94. driver->capture_interleave_skip = NULL;
  95. }
  96. if (driver->silent) {
  97. free (driver->silent);
  98. driver->silent = 0;
  99. }
  100. if (driver->dither_state) {
  101. free (driver->dither_state);
  102. driver->dither_state = 0;
  103. }
  104. }
  105. static int
  106. alsa_driver_check_capabilities (alsa_driver_t *driver)
  107. {
  108. return 0;
  109. }
  110. static int
  111. alsa_driver_check_card_type (alsa_driver_t *driver)
  112. {
  113. int err;
  114. snd_ctl_card_info_t *card_info;
  115. char * ctl_name;
  116. regex_t expression;
  117. snd_ctl_card_info_alloca (&card_info);
  118. regcomp(&expression,"(plug)?hw:[0-9](,[0-9])?",REG_ICASE|REG_EXTENDED);
  119. if (!regexec(&expression,driver->alsa_name_playback,0,NULL,0)) {
  120. /* the user wants a hw or plughw device, the ctl name
  121. * should be hw:x where x is the card number */
  122. char tmp[5];
  123. strncpy(tmp,strstr(driver->alsa_name_playback,"hw"),4);
  124. tmp[4]='\0';
  125. jack_info("control device %s",tmp);
  126. ctl_name = strdup(tmp);
  127. } else {
  128. ctl_name = strdup(driver->alsa_name_playback);
  129. }
  130. // XXX: I don't know the "right" way to do this. Which to use
  131. // driver->alsa_name_playback or driver->alsa_name_capture.
  132. if ((err = snd_ctl_open (&driver->ctl_handle, ctl_name, 0)) < 0) {
  133. jack_error ("control open \"%s\" (%s)", ctl_name,
  134. snd_strerror(err));
  135. } else if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  136. jack_error ("control hardware info \"%s\" (%s)",
  137. driver->alsa_name_playback, snd_strerror (err));
  138. snd_ctl_close (driver->ctl_handle);
  139. }
  140. driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  141. regfree(&expression);
  142. free(ctl_name);
  143. return alsa_driver_check_capabilities (driver);
  144. }
  145. static int
  146. alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
  147. {
  148. driver->hw = jack_alsa_hammerfall_hw_new (driver);
  149. return 0;
  150. }
  151. static int
  152. alsa_driver_hdsp_hardware (alsa_driver_t *driver)
  153. {
  154. driver->hw = jack_alsa_hdsp_hw_new (driver);
  155. return 0;
  156. }
  157. static int
  158. alsa_driver_ice1712_hardware (alsa_driver_t *driver)
  159. {
  160. driver->hw = jack_alsa_ice1712_hw_new (driver);
  161. return 0;
  162. }
  163. // JACK2
  164. /*
  165. static int
  166. alsa_driver_usx2y_hardware (alsa_driver_t *driver)
  167. {
  168. driver->hw = jack_alsa_usx2y_hw_new (driver);
  169. return 0;
  170. }
  171. */
  172. static int
  173. alsa_driver_generic_hardware (alsa_driver_t *driver)
  174. {
  175. driver->hw = jack_alsa_generic_hw_new (driver);
  176. return 0;
  177. }
  178. static int
  179. alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring,
  180. int hw_metering)
  181. {
  182. int err;
  183. if (!strcmp(driver->alsa_driver, "RME9652")) {
  184. if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
  185. return err;
  186. }
  187. } else if (!strcmp(driver->alsa_driver, "H-DSP")) {
  188. if ((err = alsa_driver_hdsp_hardware (driver)) !=0) {
  189. return err;
  190. }
  191. } else if (!strcmp(driver->alsa_driver, "ICE1712")) {
  192. if ((err = alsa_driver_ice1712_hardware (driver)) !=0) {
  193. return err;
  194. }
  195. }
  196. // JACK2
  197. /*
  198. else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
  199. if ((err = alsa_driver_usx2y_hardware (driver)) !=0) {
  200. return err;
  201. }
  202. }
  203. */
  204. else {
  205. if ((err = alsa_driver_generic_hardware (driver)) != 0) {
  206. return err;
  207. }
  208. }
  209. if (driver->hw->capabilities & Cap_HardwareMonitoring) {
  210. driver->has_hw_monitoring = TRUE;
  211. /* XXX need to ensure that this is really FALSE or
  212. * TRUE or whatever*/
  213. driver->hw_monitoring = hw_monitoring;
  214. } else {
  215. driver->has_hw_monitoring = FALSE;
  216. driver->hw_monitoring = FALSE;
  217. }
  218. if (driver->hw->capabilities & Cap_ClockLockReporting) {
  219. driver->has_clock_sync_reporting = TRUE;
  220. } else {
  221. driver->has_clock_sync_reporting = FALSE;
  222. }
  223. if (driver->hw->capabilities & Cap_HardwareMetering) {
  224. driver->has_hw_metering = TRUE;
  225. driver->hw_metering = hw_metering;
  226. } else {
  227. driver->has_hw_metering = FALSE;
  228. driver->hw_metering = FALSE;
  229. }
  230. return 0;
  231. }
  232. static void
  233. alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
  234. {
  235. if (driver->playback_handle) {
  236. if (SND_PCM_FORMAT_FLOAT_LE == driver->playback_sample_format) {
  237. driver->write_via_copy = sample_move_dS_floatLE;
  238. } else {
  239. switch (driver->playback_sample_bytes) {
  240. case 2:
  241. switch (driver->dither) {
  242. case Rectangular:
  243. jack_info("Rectangular dithering at 16 bits");
  244. driver->write_via_copy = driver->quirk_bswap?
  245. sample_move_dither_rect_d16_sSs:
  246. sample_move_dither_rect_d16_sS;
  247. break;
  248. case Triangular:
  249. jack_info("Triangular dithering at 16 bits");
  250. driver->write_via_copy = driver->quirk_bswap?
  251. sample_move_dither_tri_d16_sSs:
  252. sample_move_dither_tri_d16_sS;
  253. break;
  254. case Shaped:
  255. jack_info("Noise-shaped dithering at 16 bits");
  256. driver->write_via_copy = driver->quirk_bswap?
  257. sample_move_dither_shaped_d16_sSs:
  258. sample_move_dither_shaped_d16_sS;
  259. break;
  260. default:
  261. driver->write_via_copy = driver->quirk_bswap?
  262. sample_move_d16_sSs :
  263. sample_move_d16_sS;
  264. break;
  265. }
  266. break;
  267. case 3: /* NO DITHER */
  268. driver->write_via_copy = driver->quirk_bswap?
  269. sample_move_d24_sSs:
  270. sample_move_d24_sS;
  271. break;
  272. case 4: /* NO DITHER */
  273. driver->write_via_copy = driver->quirk_bswap?
  274. sample_move_d32u24_sSs:
  275. sample_move_d32u24_sS;
  276. break;
  277. default:
  278. jack_error ("impossible sample width (%d) discovered!",
  279. driver->playback_sample_bytes);
  280. exit (1);
  281. }
  282. }
  283. }
  284. if (driver->capture_handle) {
  285. if (SND_PCM_FORMAT_FLOAT_LE == driver->capture_sample_format) {
  286. driver->read_via_copy = sample_move_floatLE_sSs;
  287. } else {
  288. switch (driver->capture_sample_bytes) {
  289. case 2:
  290. driver->read_via_copy = driver->quirk_bswap?
  291. sample_move_dS_s16s:
  292. sample_move_dS_s16;
  293. break;
  294. case 3:
  295. driver->read_via_copy = driver->quirk_bswap?
  296. sample_move_dS_s24s:
  297. sample_move_dS_s24;
  298. break;
  299. case 4:
  300. driver->read_via_copy = driver->quirk_bswap?
  301. sample_move_dS_s32u24s:
  302. sample_move_dS_s32u24;
  303. break;
  304. }
  305. }
  306. }
  307. }
  308. static int
  309. alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
  310. const char *stream_name,
  311. snd_pcm_t *handle,
  312. snd_pcm_hw_params_t *hw_params,
  313. snd_pcm_sw_params_t *sw_params,
  314. unsigned int *nperiodsp,
  315. channel_t *nchns,
  316. unsigned long sample_width)
  317. {
  318. int err, format;
  319. unsigned int frame_rate;
  320. snd_pcm_uframes_t stop_th;
  321. static struct {
  322. char Name[32];
  323. snd_pcm_format_t format;
  324. int swapped;
  325. } formats[] = {
  326. {"32bit float little-endian", SND_PCM_FORMAT_FLOAT_LE},
  327. {"32bit integer little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
  328. {"32bit integer big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
  329. {"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE},
  330. {"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE},
  331. {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
  332. {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
  333. };
  334. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  335. #define FIRST_16BIT_FORMAT 5
  336. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  337. jack_error ("ALSA: no playback configurations available (%s)",
  338. snd_strerror (err));
  339. return -1;
  340. }
  341. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
  342. < 0) {
  343. jack_error ("ALSA: cannot restrict period size to integral"
  344. " value.");
  345. return -1;
  346. }
  347. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  348. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  349. if ((err = snd_pcm_hw_params_set_access (
  350. handle, hw_params,
  351. SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
  352. jack_error ("ALSA: mmap-based access is not possible"
  353. " for the %s "
  354. "stream of this audio interface",
  355. stream_name);
  356. return -1;
  357. }
  358. }
  359. }
  360. format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
  361. while (1) {
  362. if ((err = snd_pcm_hw_params_set_format (
  363. handle, hw_params, formats[format].format)) < 0) {
  364. if ((sample_width == 4
  365. ? format++ >= NUMFORMATS - 1
  366. : format-- <= 0)) {
  367. jack_error ("Sorry. The audio interface \"%s\""
  368. " doesn't support any of the"
  369. " hardware sample formats that"
  370. " JACK's alsa-driver can use.",
  371. device_name);
  372. return -1;
  373. }
  374. } else {
  375. if (formats[format].swapped) {
  376. driver->quirk_bswap = 1;
  377. } else {
  378. driver->quirk_bswap = 0;
  379. }
  380. jack_info ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
  381. break;
  382. }
  383. }
  384. frame_rate = driver->frame_rate ;
  385. err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
  386. &frame_rate, NULL) ;
  387. driver->frame_rate = frame_rate ;
  388. if (err < 0) {
  389. jack_error ("ALSA: cannot set sample/frame rate to %"
  390. PRIu32 " for %s", driver->frame_rate,
  391. stream_name);
  392. return -1;
  393. }
  394. if (!*nchns) {
  395. /*if not user-specified, try to find the maximum
  396. * number of channels */
  397. unsigned int channels_max ;
  398. err = snd_pcm_hw_params_get_channels_max (hw_params,
  399. &channels_max);
  400. *nchns = channels_max ;
  401. if (*nchns > 1024) {
  402. /* the hapless user is an unwitting victim of
  403. the "default" ALSA PCM device, which can
  404. support up to 16 million channels. since
  405. they can't be bothered to set up a proper
  406. default device, limit the number of
  407. channels for them to a sane default.
  408. */
  409. jack_error (
  410. "You appear to be using the ALSA software \"plug\" layer, probably\n"
  411. "a result of using the \"default\" ALSA device. This is less\n"
  412. "efficient than it could be. Consider using a hardware device\n"
  413. "instead rather than using the plug layer. Usually the name of the\n"
  414. "hardware device that corresponds to the first sound card is hw:0\n"
  415. );
  416. *nchns = 2;
  417. }
  418. }
  419. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
  420. *nchns)) < 0) {
  421. jack_error ("ALSA: cannot set channel count to %u for %s",
  422. *nchns, stream_name);
  423. return -1;
  424. }
  425. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
  426. driver->frames_per_cycle,
  427. 0))
  428. < 0) {
  429. jack_error ("ALSA: cannot set period size to %" PRIu32
  430. " frames for %s", driver->frames_per_cycle,
  431. stream_name);
  432. return -1;
  433. }
  434. *nperiodsp = driver->user_nperiods;
  435. snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
  436. if (*nperiodsp < driver->user_nperiods)
  437. *nperiodsp = driver->user_nperiods;
  438. if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
  439. nperiodsp, NULL) < 0) {
  440. jack_error ("ALSA: cannot set number of periods to %u for %s",
  441. *nperiodsp, stream_name);
  442. return -1;
  443. }
  444. if (*nperiodsp < driver->user_nperiods) {
  445. jack_error ("ALSA: got smaller periods %u than %u for %s",
  446. *nperiodsp, (unsigned int) driver->user_nperiods,
  447. stream_name);
  448. return -1;
  449. }
  450. jack_info ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
  451. #if 0
  452. if (!jack_power_of_two(driver->frames_per_cycle)) {
  453. jack_error("JACK: frames must be a power of two "
  454. "(64, 512, 1024, ...)\n");
  455. return -1;
  456. }
  457. #endif
  458. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
  459. *nperiodsp *
  460. driver->frames_per_cycle))
  461. < 0) {
  462. jack_error ("ALSA: cannot set buffer length to %" PRIu32
  463. " for %s",
  464. *nperiodsp * driver->frames_per_cycle,
  465. stream_name);
  466. return -1;
  467. }
  468. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  469. jack_error ("ALSA: cannot set hardware parameters for %s",
  470. stream_name);
  471. return -1;
  472. }
  473. snd_pcm_sw_params_current (handle, sw_params);
  474. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
  475. 0U)) < 0) {
  476. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  477. return -1;
  478. }
  479. stop_th = *nperiodsp * driver->frames_per_cycle;
  480. if (driver->soft_mode) {
  481. stop_th = (snd_pcm_uframes_t)-1;
  482. }
  483. if ((err = snd_pcm_sw_params_set_stop_threshold (
  484. handle, sw_params, stop_th)) < 0) {
  485. jack_error ("ALSA: cannot set stop mode for %s",
  486. stream_name);
  487. return -1;
  488. }
  489. if ((err = snd_pcm_sw_params_set_silence_threshold (
  490. handle, sw_params, 0)) < 0) {
  491. jack_error ("ALSA: cannot set silence threshold for %s",
  492. stream_name);
  493. return -1;
  494. }
  495. #if 0
  496. jack_info ("set silence size to %lu * %lu = %lu",
  497. driver->frames_per_cycle, *nperiodsp,
  498. driver->frames_per_cycle * *nperiodsp);
  499. if ((err = snd_pcm_sw_params_set_silence_size (
  500. handle, sw_params,
  501. driver->frames_per_cycle * *nperiodsp)) < 0) {
  502. jack_error ("ALSA: cannot set silence size for %s",
  503. stream_name);
  504. return -1;
  505. }
  506. #endif
  507. if (handle == driver->playback_handle)
  508. err = snd_pcm_sw_params_set_avail_min (
  509. handle, sw_params,
  510. driver->frames_per_cycle
  511. * (*nperiodsp - driver->user_nperiods + 1));
  512. else
  513. err = snd_pcm_sw_params_set_avail_min (
  514. handle, sw_params, driver->frames_per_cycle);
  515. if (err < 0) {
  516. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  517. return -1;
  518. }
  519. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  520. jack_error ("ALSA: cannot set software parameters for %s\n",
  521. stream_name);
  522. return -1;
  523. }
  524. return 0;
  525. }
  526. static int
  527. alsa_driver_set_parameters (alsa_driver_t *driver,
  528. jack_nframes_t frames_per_cycle,
  529. jack_nframes_t user_nperiods,
  530. jack_nframes_t rate)
  531. {
  532. int dir;
  533. snd_pcm_uframes_t p_period_size = 0;
  534. snd_pcm_uframes_t c_period_size = 0;
  535. channel_t chn;
  536. unsigned int pr = 0;
  537. unsigned int cr = 0;
  538. int err;
  539. driver->frame_rate = rate;
  540. driver->frames_per_cycle = frames_per_cycle;
  541. driver->user_nperiods = user_nperiods;
  542. jack_info ("configuring for %" PRIu32 "Hz, period = %"
  543. PRIu32 " frames (%.1f ms), buffer = %" PRIu32 " periods",
  544. rate, frames_per_cycle, (((float)frames_per_cycle / (float) rate) * 1000.0f), user_nperiods);
  545. if (driver->capture_handle) {
  546. if (alsa_driver_configure_stream (
  547. driver,
  548. driver->alsa_name_capture,
  549. "capture",
  550. driver->capture_handle,
  551. driver->capture_hw_params,
  552. driver->capture_sw_params,
  553. &driver->capture_nperiods,
  554. &driver->capture_nchannels,
  555. driver->capture_sample_bytes)) {
  556. jack_error ("ALSA: cannot configure capture channel");
  557. return -1;
  558. }
  559. }
  560. if (driver->playback_handle) {
  561. if (alsa_driver_configure_stream (
  562. driver,
  563. driver->alsa_name_playback,
  564. "playback",
  565. driver->playback_handle,
  566. driver->playback_hw_params,
  567. driver->playback_sw_params,
  568. &driver->playback_nperiods,
  569. &driver->playback_nchannels,
  570. driver->playback_sample_bytes)) {
  571. jack_error ("ALSA: cannot configure playback channel");
  572. return -1;
  573. }
  574. }
  575. /* check the rate, since thats rather important */
  576. if (driver->playback_handle) {
  577. snd_pcm_hw_params_get_rate (driver->playback_hw_params,
  578. &pr, &dir);
  579. }
  580. if (driver->capture_handle) {
  581. snd_pcm_hw_params_get_rate (driver->capture_hw_params,
  582. &cr, &dir);
  583. }
  584. if (driver->capture_handle && driver->playback_handle) {
  585. if (cr != pr) {
  586. jack_error ("playback and capture sample rates do "
  587. "not match (%d vs. %d)", pr, cr);
  588. }
  589. /* only change if *both* capture and playback rates
  590. * don't match requested certain hardware actually
  591. * still works properly in full-duplex with slightly
  592. * different rate values between adc and dac
  593. */
  594. if (cr != driver->frame_rate && pr != driver->frame_rate) {
  595. jack_error ("sample rate in use (%d Hz) does not "
  596. "match requested rate (%d Hz)",
  597. cr, driver->frame_rate);
  598. driver->frame_rate = cr;
  599. }
  600. }
  601. else if (driver->capture_handle && cr != driver->frame_rate) {
  602. jack_error ("capture sample rate in use (%d Hz) does not "
  603. "match requested rate (%d Hz)",
  604. cr, driver->frame_rate);
  605. driver->frame_rate = cr;
  606. }
  607. else if (driver->playback_handle && pr != driver->frame_rate) {
  608. jack_error ("playback sample rate in use (%d Hz) does not "
  609. "match requested rate (%d Hz)",
  610. pr, driver->frame_rate);
  611. driver->frame_rate = pr;
  612. }
  613. /* check the fragment size, since thats non-negotiable */
  614. if (driver->playback_handle) {
  615. snd_pcm_access_t access;
  616. err = snd_pcm_hw_params_get_period_size (
  617. driver->playback_hw_params, &p_period_size, &dir);
  618. err = snd_pcm_hw_params_get_format (
  619. driver->playback_hw_params,
  620. &(driver->playback_sample_format));
  621. err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
  622. &access);
  623. driver->playback_interleaved =
  624. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  625. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  626. if (p_period_size != driver->frames_per_cycle) {
  627. jack_error ("alsa_pcm: requested an interrupt every %"
  628. PRIu32
  629. " frames but got %u frames for playback",
  630. driver->frames_per_cycle, p_period_size);
  631. return -1;
  632. }
  633. }
  634. if (driver->capture_handle) {
  635. snd_pcm_access_t access;
  636. err = snd_pcm_hw_params_get_period_size (
  637. driver->capture_hw_params, &c_period_size, &dir);
  638. err = snd_pcm_hw_params_get_format (
  639. driver->capture_hw_params,
  640. &(driver->capture_sample_format));
  641. err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
  642. &access);
  643. driver->capture_interleaved =
  644. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  645. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  646. if (c_period_size != driver->frames_per_cycle) {
  647. jack_error ("alsa_pcm: requested an interrupt every %"
  648. PRIu32
  649. " frames but got %uc frames for capture",
  650. driver->frames_per_cycle, p_period_size);
  651. return -1;
  652. }
  653. }
  654. driver->playback_sample_bytes =
  655. snd_pcm_format_physical_width (driver->playback_sample_format)
  656. / 8;
  657. driver->capture_sample_bytes =
  658. snd_pcm_format_physical_width (driver->capture_sample_format)
  659. / 8;
  660. if (driver->playback_handle) {
  661. switch (driver->playback_sample_format) {
  662. case SND_PCM_FORMAT_FLOAT_LE:
  663. case SND_PCM_FORMAT_S32_LE:
  664. case SND_PCM_FORMAT_S24_3LE:
  665. case SND_PCM_FORMAT_S24_3BE:
  666. case SND_PCM_FORMAT_S16_LE:
  667. case SND_PCM_FORMAT_S32_BE:
  668. case SND_PCM_FORMAT_S16_BE:
  669. break;
  670. default:
  671. jack_error ("programming error: unhandled format "
  672. "type for playback");
  673. exit (1);
  674. }
  675. }
  676. if (driver->capture_handle) {
  677. switch (driver->capture_sample_format) {
  678. case SND_PCM_FORMAT_FLOAT_LE:
  679. case SND_PCM_FORMAT_S32_LE:
  680. case SND_PCM_FORMAT_S24_3LE:
  681. case SND_PCM_FORMAT_S24_3BE:
  682. case SND_PCM_FORMAT_S16_LE:
  683. case SND_PCM_FORMAT_S32_BE:
  684. case SND_PCM_FORMAT_S16_BE:
  685. break;
  686. default:
  687. jack_error ("programming error: unhandled format "
  688. "type for capture");
  689. exit (1);
  690. }
  691. }
  692. if (driver->playback_interleaved) {
  693. const snd_pcm_channel_area_t *my_areas;
  694. snd_pcm_uframes_t offset, frames;
  695. if (snd_pcm_mmap_begin(driver->playback_handle,
  696. &my_areas, &offset, &frames) < 0) {
  697. jack_error ("ALSA: %s: mmap areas info error",
  698. driver->alsa_name_playback);
  699. return -1;
  700. }
  701. driver->interleave_unit =
  702. snd_pcm_format_physical_width (
  703. driver->playback_sample_format) / 8;
  704. } else {
  705. driver->interleave_unit = 0; /* NOT USED */
  706. }
  707. if (driver->capture_interleaved) {
  708. const snd_pcm_channel_area_t *my_areas;
  709. snd_pcm_uframes_t offset, frames;
  710. if (snd_pcm_mmap_begin(driver->capture_handle,
  711. &my_areas, &offset, &frames) < 0) {
  712. jack_error ("ALSA: %s: mmap areas info error",
  713. driver->alsa_name_capture);
  714. return -1;
  715. }
  716. }
  717. if (driver->playback_nchannels > driver->capture_nchannels) {
  718. driver->max_nchannels = driver->playback_nchannels;
  719. driver->user_nchannels = driver->capture_nchannels;
  720. } else {
  721. driver->max_nchannels = driver->capture_nchannels;
  722. driver->user_nchannels = driver->playback_nchannels;
  723. }
  724. alsa_driver_setup_io_function_pointers (driver);
  725. /* Allocate and initialize structures that rely on the
  726. channels counts.
  727. Set up the bit pattern that is used to record which
  728. channels require action on every cycle. any bits that are
  729. not set after the engine's process() call indicate channels
  730. that potentially need to be silenced.
  731. */
  732. bitset_create (&driver->channels_done, driver->max_nchannels);
  733. bitset_create (&driver->channels_not_done, driver->max_nchannels);
  734. if (driver->playback_handle) {
  735. driver->playback_addr = (char **)
  736. malloc (sizeof (char *) * driver->playback_nchannels);
  737. memset (driver->playback_addr, 0,
  738. sizeof (char *) * driver->playback_nchannels);
  739. driver->playback_interleave_skip = (unsigned long *)
  740. malloc (sizeof (unsigned long *) * driver->playback_nchannels);
  741. memset (driver->playback_interleave_skip, 0,
  742. sizeof (unsigned long *) * driver->playback_nchannels);
  743. driver->silent = (unsigned long *)
  744. malloc (sizeof (unsigned long)
  745. * driver->playback_nchannels);
  746. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  747. driver->silent[chn] = 0;
  748. }
  749. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  750. bitset_add (driver->channels_done, chn);
  751. }
  752. driver->dither_state = (dither_state_t *)
  753. calloc ( driver->playback_nchannels,
  754. sizeof (dither_state_t));
  755. }
  756. if (driver->capture_handle) {
  757. driver->capture_addr = (char **)
  758. malloc (sizeof (char *) * driver->capture_nchannels);
  759. memset (driver->capture_addr, 0,
  760. sizeof (char *) * driver->capture_nchannels);
  761. driver->capture_interleave_skip = (unsigned long *)
  762. malloc (sizeof (unsigned long *) * driver->capture_nchannels);
  763. memset (driver->capture_interleave_skip, 0,
  764. sizeof (unsigned long *) * driver->capture_nchannels);
  765. }
  766. driver->clock_sync_data = (ClockSyncStatus *)
  767. malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
  768. driver->period_usecs =
  769. (jack_time_t) floor ((((float) driver->frames_per_cycle) /
  770. driver->frame_rate) * 1000000.0f);
  771. driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
  772. // JACK2
  773. /*
  774. if (driver->engine) {
  775. if (driver->engine->set_buffer_size (driver->engine,
  776. driver->frames_per_cycle)) {
  777. jack_error ("ALSA: Cannot set engine buffer size to %d (check MIDI)", driver->frames_per_cycle);
  778. return -1;
  779. }
  780. }
  781. */
  782. return 0;
  783. }
  784. int
  785. alsa_driver_reset_parameters (alsa_driver_t *driver,
  786. jack_nframes_t frames_per_cycle,
  787. jack_nframes_t user_nperiods,
  788. jack_nframes_t rate)
  789. {
  790. /* XXX unregister old ports ? */
  791. alsa_driver_release_channel_dependent_memory (driver);
  792. return alsa_driver_set_parameters (driver,
  793. frames_per_cycle,
  794. user_nperiods, rate);
  795. }
  796. static int
  797. alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  798. snd_pcm_uframes_t *capture_avail,
  799. snd_pcm_uframes_t *playback_avail,
  800. snd_pcm_uframes_t *capture_offset,
  801. snd_pcm_uframes_t *playback_offset)
  802. {
  803. unsigned long err;
  804. channel_t chn;
  805. if (capture_avail) {
  806. if ((err = snd_pcm_mmap_begin (
  807. driver->capture_handle, &driver->capture_areas,
  808. (snd_pcm_uframes_t *) capture_offset,
  809. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  810. jack_error ("ALSA: %s: mmap areas info error",
  811. driver->alsa_name_capture);
  812. return -1;
  813. }
  814. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  815. const snd_pcm_channel_area_t *a =
  816. &driver->capture_areas[chn];
  817. driver->capture_addr[chn] = (char *) a->addr
  818. + ((a->first + a->step * *capture_offset) / 8);
  819. driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  820. }
  821. }
  822. if (playback_avail) {
  823. if ((err = snd_pcm_mmap_begin (
  824. driver->playback_handle, &driver->playback_areas,
  825. (snd_pcm_uframes_t *) playback_offset,
  826. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  827. jack_error ("ALSA: %s: mmap areas info error ",
  828. driver->alsa_name_playback);
  829. return -1;
  830. }
  831. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  832. const snd_pcm_channel_area_t *a =
  833. &driver->playback_areas[chn];
  834. driver->playback_addr[chn] = (char *) a->addr
  835. + ((a->first + a->step * *playback_offset) / 8);
  836. driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  837. }
  838. }
  839. return 0;
  840. }
  841. int
  842. alsa_driver_start (alsa_driver_t *driver)
  843. {
  844. int err;
  845. snd_pcm_uframes_t poffset, pavail;
  846. channel_t chn;
  847. driver->poll_last = 0;
  848. driver->poll_next = 0;
  849. if (driver->playback_handle) {
  850. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  851. jack_error ("ALSA: prepare error for playback on "
  852. "\"%s\" (%s)", driver->alsa_name_playback,
  853. snd_strerror(err));
  854. return -1;
  855. }
  856. }
  857. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  858. || !driver->playback_handle) {
  859. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  860. jack_error ("ALSA: prepare error for capture on \"%s\""
  861. " (%s)", driver->alsa_name_capture,
  862. snd_strerror(err));
  863. return -1;
  864. }
  865. }
  866. if (driver->hw_monitoring) {
  867. if (driver->input_monitor_mask || driver->all_monitor_in) {
  868. if (driver->all_monitor_in) {
  869. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  870. } else {
  871. driver->hw->set_input_monitor_mask (
  872. driver->hw, driver->input_monitor_mask);
  873. }
  874. } else {
  875. driver->hw->set_input_monitor_mask (driver->hw,
  876. driver->input_monitor_mask);
  877. }
  878. }
  879. if (driver->playback_handle) {
  880. driver->playback_nfds =
  881. snd_pcm_poll_descriptors_count (driver->playback_handle);
  882. } else {
  883. driver->playback_nfds = 0;
  884. }
  885. if (driver->capture_handle) {
  886. driver->capture_nfds =
  887. snd_pcm_poll_descriptors_count (driver->capture_handle);
  888. } else {
  889. driver->capture_nfds = 0;
  890. }
  891. if (driver->pfd) {
  892. free (driver->pfd);
  893. }
  894. driver->pfd = (struct pollfd *)
  895. malloc (sizeof (struct pollfd) *
  896. (driver->playback_nfds + driver->capture_nfds + 2));
  897. if (driver->midi && !driver->xrun_recovery)
  898. (driver->midi->start)(driver->midi);
  899. if (driver->playback_handle) {
  900. /* fill playback buffer with zeroes, and mark
  901. all fragments as having data.
  902. */
  903. pavail = snd_pcm_avail_update (driver->playback_handle);
  904. if (pavail !=
  905. driver->frames_per_cycle * driver->playback_nperiods) {
  906. jack_error ("ALSA: full buffer not available at start");
  907. return -1;
  908. }
  909. if (alsa_driver_get_channel_addresses (driver,
  910. 0, &pavail, 0, &poffset)) {
  911. return -1;
  912. }
  913. /* XXX this is cheating. ALSA offers no guarantee that
  914. we can access the entire buffer at any one time. It
  915. works on most hardware tested so far, however, buts
  916. its a liability in the long run. I think that
  917. alsa-lib may have a better function for doing this
  918. here, where the goal is to silence the entire
  919. buffer.
  920. */
  921. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  922. alsa_driver_silence_on_channel (
  923. driver, chn,
  924. driver->user_nperiods
  925. * driver->frames_per_cycle);
  926. }
  927. snd_pcm_mmap_commit (driver->playback_handle, poffset,
  928. driver->user_nperiods
  929. * driver->frames_per_cycle);
  930. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  931. jack_error ("ALSA: could not start playback (%s)",
  932. snd_strerror (err));
  933. return -1;
  934. }
  935. }
  936. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  937. || !driver->playback_handle) {
  938. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  939. jack_error ("ALSA: could not start capture (%s)",
  940. snd_strerror (err));
  941. return -1;
  942. }
  943. }
  944. return 0;
  945. }
  946. int
  947. alsa_driver_stop (alsa_driver_t *driver)
  948. {
  949. int err;
  950. // JSList* node;
  951. // int chn;
  952. /* silence all capture port buffers, because we might
  953. be entering offline mode.
  954. */
  955. // JACK2
  956. /*
  957. for (chn = 0, node = driver->capture_ports; node;
  958. node = jack_slist_next (node), chn++) {
  959. jack_port_t* port;
  960. char* buf;
  961. jack_nframes_t nframes = driver->engine->control->buffer_size;
  962. port = (jack_port_t *) node->data;
  963. buf = jack_port_get_buffer (port, nframes);
  964. memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
  965. }
  966. */
  967. // JACK2
  968. ClearOutput();
  969. if (driver->playback_handle) {
  970. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  971. jack_error ("ALSA: channel flush for playback "
  972. "failed (%s)", snd_strerror (err));
  973. return -1;
  974. }
  975. }
  976. if (!driver->playback_handle
  977. || driver->capture_and_playback_not_synced) {
  978. if (driver->capture_handle) {
  979. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  980. jack_error ("ALSA: channel flush for "
  981. "capture failed (%s)",
  982. snd_strerror (err));
  983. return -1;
  984. }
  985. }
  986. }
  987. if (driver->hw_monitoring) {
  988. driver->hw->set_input_monitor_mask (driver->hw, 0);
  989. }
  990. if (driver->midi && !driver->xrun_recovery)
  991. (driver->midi->stop)(driver->midi);
  992. return 0;
  993. }
  994. static int
  995. alsa_driver_restart (alsa_driver_t *driver)
  996. {
  997. int res;
  998. driver->xrun_recovery = 1;
  999. // JACK2
  1000. /*
  1001. if ((res = driver->nt_stop((struct _jack_driver_nt *) driver))==0)
  1002. res = driver->nt_start((struct _jack_driver_nt *) driver);
  1003. */
  1004. res = Restart();
  1005. driver->xrun_recovery = 0;
  1006. if (res && driver->midi)
  1007. (driver->midi->stop)(driver->midi);
  1008. return res;
  1009. }
  1010. static int
  1011. alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
  1012. {
  1013. snd_pcm_status_t *status;
  1014. int res;
  1015. snd_pcm_status_alloca(&status);
  1016. if (driver->capture_handle) {
  1017. if ((res = snd_pcm_status(driver->capture_handle, status))
  1018. < 0) {
  1019. jack_error("status error: %s", snd_strerror(res));
  1020. }
  1021. } else {
  1022. if ((res = snd_pcm_status(driver->playback_handle, status))
  1023. < 0) {
  1024. jack_error("status error: %s", snd_strerror(res));
  1025. }
  1026. }
  1027. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_SUSPENDED)
  1028. {
  1029. jack_log("**** alsa_pcm: pcm in suspended state, resuming it" );
  1030. if (driver->capture_handle) {
  1031. if ((res = snd_pcm_prepare(driver->capture_handle))
  1032. < 0) {
  1033. jack_error("error preparing after suspend: %s", snd_strerror(res));
  1034. }
  1035. } else {
  1036. if ((res = snd_pcm_prepare(driver->playback_handle))
  1037. < 0) {
  1038. jack_error("error preparing after suspend: %s", snd_strerror(res));
  1039. }
  1040. }
  1041. }
  1042. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN
  1043. && driver->process_count > XRUN_REPORT_DELAY) {
  1044. struct timeval now, diff, tstamp;
  1045. driver->xrun_count++;
  1046. snd_pcm_status_get_tstamp(status,&now);
  1047. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  1048. timersub(&now, &tstamp, &diff);
  1049. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  1050. jack_log("**** alsa_pcm: xrun of at least %.3f msecs",*delayed_usecs / 1000.0);
  1051. }
  1052. if (alsa_driver_restart (driver)) {
  1053. return -1;
  1054. }
  1055. return 0;
  1056. }
  1057. void
  1058. alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
  1059. jack_nframes_t nframes)
  1060. {
  1061. channel_t chn;
  1062. jack_nframes_t buffer_frames =
  1063. driver->frames_per_cycle * driver->playback_nperiods;
  1064. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  1065. if (bitset_contains (driver->channels_not_done, chn)) {
  1066. if (driver->silent[chn] < buffer_frames) {
  1067. alsa_driver_silence_on_channel_no_mark (
  1068. driver, chn, nframes);
  1069. driver->silent[chn] += nframes;
  1070. }
  1071. }
  1072. }
  1073. }
  1074. void
  1075. alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn,
  1076. ClockSyncStatus status)
  1077. {
  1078. driver->clock_sync_data[chn] = status;
  1079. alsa_driver_clock_sync_notify (driver, chn, status);
  1080. }
  1081. static int under_gdb = FALSE;
  1082. jack_nframes_t
  1083. 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. again:
  1104. while (need_playback || need_capture) {
  1105. int poll_result;
  1106. unsigned int ci = 0;
  1107. unsigned int nfds;
  1108. unsigned short revents;
  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. poll_result = poll (driver->pfd, nfds, driver->poll_timeout);
  1144. if (poll_result < 0) {
  1145. if (errno == EINTR) {
  1146. jack_info ("poll interrupt");
  1147. // this happens mostly when run
  1148. // under gdb, or when exiting due to a signal
  1149. if (under_gdb) {
  1150. goto again;
  1151. }
  1152. *status = -2;
  1153. return 0;
  1154. }
  1155. jack_error ("ALSA: poll call failed (%s)",
  1156. strerror (errno));
  1157. *status = -3;
  1158. return 0;
  1159. }
  1160. poll_ret = jack_get_microseconds ();
  1161. // JACK2
  1162. SetTime(poll_ret);
  1163. if (extra_fd < 0) {
  1164. if (driver->poll_next && poll_ret > driver->poll_next) {
  1165. *delayed_usecs = poll_ret - driver->poll_next;
  1166. }
  1167. driver->poll_last = poll_ret;
  1168. driver->poll_next = poll_ret + driver->period_usecs;
  1169. // JACK2
  1170. /*
  1171. driver->engine->transport_cycle_start (driver->engine,
  1172. poll_ret);
  1173. */
  1174. }
  1175. #ifdef DEBUG_WAKEUP
  1176. fprintf (stderr, "%" PRIu64 ": checked %d fds, started at %" PRIu64 " %" PRIu64 " usecs since poll entered\n",
  1177. poll_ret, nfds, poll_enter, poll_ret - poll_enter);
  1178. #endif
  1179. /* check to see if it was the extra FD that caused us
  1180. * to return from poll */
  1181. if (extra_fd >= 0) {
  1182. if (driver->pfd[nfds-1].revents == 0) {
  1183. /* we timed out on the extra fd */
  1184. *status = -4;
  1185. return -1;
  1186. }
  1187. /* if POLLIN was the only bit set, we're OK */
  1188. *status = 0;
  1189. return (driver->pfd[nfds-1].revents == POLLIN) ? 0 : -1;
  1190. }
  1191. if (need_playback) {
  1192. if (snd_pcm_poll_descriptors_revents
  1193. (driver->playback_handle, &driver->pfd[0],
  1194. driver->playback_nfds, &revents) < 0) {
  1195. jack_error ("ALSA: playback revents failed");
  1196. *status = -6;
  1197. return 0;
  1198. }
  1199. if (revents & POLLERR) {
  1200. xrun_detected = TRUE;
  1201. }
  1202. if (revents & POLLOUT) {
  1203. need_playback = 0;
  1204. #ifdef DEBUG_WAKEUP
  1205. fprintf (stderr, "%" PRIu64
  1206. " playback stream ready\n",
  1207. poll_ret);
  1208. #endif
  1209. }
  1210. }
  1211. if (need_capture) {
  1212. if (snd_pcm_poll_descriptors_revents
  1213. (driver->capture_handle, &driver->pfd[ci],
  1214. driver->capture_nfds, &revents) < 0) {
  1215. jack_error ("ALSA: capture revents failed");
  1216. *status = -6;
  1217. return 0;
  1218. }
  1219. if (revents & POLLERR) {
  1220. xrun_detected = TRUE;
  1221. }
  1222. if (revents & POLLIN) {
  1223. need_capture = 0;
  1224. #ifdef DEBUG_WAKEUP
  1225. fprintf (stderr, "%" PRIu64
  1226. " capture stream ready\n",
  1227. poll_ret);
  1228. #endif
  1229. }
  1230. }
  1231. if (poll_result == 0) {
  1232. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1233. " usecs",
  1234. poll_ret - poll_enter);
  1235. *status = -5;
  1236. return 0;
  1237. }
  1238. }
  1239. if (driver->capture_handle) {
  1240. if ((capture_avail = snd_pcm_avail_update (
  1241. driver->capture_handle)) < 0) {
  1242. if (capture_avail == -EPIPE) {
  1243. xrun_detected = TRUE;
  1244. } else {
  1245. jack_error ("unknown ALSA avail_update return"
  1246. " value (%u)", capture_avail);
  1247. }
  1248. }
  1249. } else {
  1250. /* odd, but see min() computation below */
  1251. capture_avail = INT_MAX;
  1252. }
  1253. if (driver->playback_handle) {
  1254. if ((playback_avail = snd_pcm_avail_update (
  1255. driver->playback_handle)) < 0) {
  1256. if (playback_avail == -EPIPE) {
  1257. xrun_detected = TRUE;
  1258. } else {
  1259. jack_error ("unknown ALSA avail_update return"
  1260. " value (%u)", playback_avail);
  1261. }
  1262. }
  1263. } else {
  1264. /* odd, but see min() computation below */
  1265. playback_avail = INT_MAX;
  1266. }
  1267. if (xrun_detected) {
  1268. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1269. return 0;
  1270. }
  1271. *status = 0;
  1272. driver->last_wait_ust = poll_ret;
  1273. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  1274. #ifdef DEBUG_WAKEUP
  1275. fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
  1276. "cavail = %lu\n",
  1277. avail, playback_avail, capture_avail);
  1278. #endif
  1279. /* mark all channels not done for now. read/write will change this */
  1280. bitset_copy (driver->channels_not_done, driver->channels_done);
  1281. /* constrain the available count to the nearest (round down) number of
  1282. periods.
  1283. */
  1284. return avail - (avail % driver->frames_per_cycle);
  1285. }
  1286. int
  1287. alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
  1288. {
  1289. snd_pcm_sframes_t contiguous;
  1290. snd_pcm_sframes_t nread;
  1291. snd_pcm_uframes_t offset;
  1292. jack_nframes_t orig_nframes;
  1293. // jack_default_audio_sample_t* buf;
  1294. // channel_t chn;
  1295. // JSList *node;
  1296. // jack_port_t* port;
  1297. int err;
  1298. if (nframes > driver->frames_per_cycle) {
  1299. return -1;
  1300. }
  1301. // JACK2
  1302. /*
  1303. if (driver->engine->freewheeling) {
  1304. return 0;
  1305. }
  1306. */
  1307. if (driver->midi)
  1308. (driver->midi->read)(driver->midi, nframes);
  1309. if (!driver->capture_handle) {
  1310. return 0;
  1311. }
  1312. nread = 0;
  1313. contiguous = 0;
  1314. orig_nframes = nframes;
  1315. while (nframes) {
  1316. contiguous = nframes;
  1317. if (alsa_driver_get_channel_addresses (
  1318. driver,
  1319. (snd_pcm_uframes_t *) &contiguous,
  1320. (snd_pcm_uframes_t *) 0,
  1321. &offset, 0) < 0) {
  1322. return -1;
  1323. }
  1324. // JACK2
  1325. /*
  1326. for (chn = 0, node = driver->capture_ports; node;
  1327. node = jack_slist_next (node), chn++) {
  1328. port = (jack_port_t *) node->data;
  1329. if (!jack_port_connected (port)) {
  1330. // no-copy optimization
  1331. continue;
  1332. }
  1333. buf = jack_port_get_buffer (port, orig_nframes);
  1334. alsa_driver_read_from_channel (driver, chn,
  1335. buf + nread, contiguous);
  1336. }
  1337. */
  1338. ReadInput(orig_nframes, contiguous, nread);
  1339. if ((err = snd_pcm_mmap_commit (driver->capture_handle,
  1340. offset, contiguous)) < 0) {
  1341. jack_error ("ALSA: could not complete read of %"
  1342. PRIu32 " frames: error = %d", contiguous, err);
  1343. return -1;
  1344. }
  1345. nframes -= contiguous;
  1346. nread += contiguous;
  1347. }
  1348. return 0;
  1349. }
  1350. int
  1351. alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
  1352. {
  1353. // channel_t chn;
  1354. // JSList *node;
  1355. // JSList *mon_node;
  1356. // jack_default_audio_sample_t* buf;
  1357. // jack_default_audio_sample_t* monbuf;
  1358. jack_nframes_t orig_nframes;
  1359. snd_pcm_sframes_t nwritten;
  1360. snd_pcm_sframes_t contiguous;
  1361. snd_pcm_uframes_t offset;
  1362. // jack_port_t *port;
  1363. int err;
  1364. driver->process_count++;
  1365. // JACK2
  1366. /*
  1367. if (!driver->playback_handle || driver->engine->freewheeling) {
  1368. return 0;
  1369. }
  1370. */
  1371. if (!driver->playback_handle) {
  1372. return 0;
  1373. }
  1374. if (nframes > driver->frames_per_cycle) {
  1375. return -1;
  1376. }
  1377. if (driver->midi)
  1378. (driver->midi->write)(driver->midi, nframes);
  1379. nwritten = 0;
  1380. contiguous = 0;
  1381. orig_nframes = nframes;
  1382. /* check current input monitor request status */
  1383. driver->input_monitor_mask = 0;
  1384. // JACK2
  1385. /*
  1386. for (chn = 0, node = driver->capture_ports; node;
  1387. node = jack_slist_next (node), chn++) {
  1388. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  1389. driver->input_monitor_mask |= (1<<chn);
  1390. }
  1391. }
  1392. */
  1393. MonitorInput();
  1394. if (driver->hw_monitoring) {
  1395. if ((driver->hw->input_monitor_mask
  1396. != driver->input_monitor_mask)
  1397. && !driver->all_monitor_in) {
  1398. driver->hw->set_input_monitor_mask (
  1399. driver->hw, driver->input_monitor_mask);
  1400. }
  1401. }
  1402. while (nframes) {
  1403. contiguous = nframes;
  1404. if (alsa_driver_get_channel_addresses (
  1405. driver,
  1406. (snd_pcm_uframes_t *) 0,
  1407. (snd_pcm_uframes_t *) &contiguous,
  1408. 0, &offset) < 0) {
  1409. return -1;
  1410. }
  1411. // JACK2
  1412. /*
  1413. for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
  1414. node;
  1415. node = jack_slist_next (node), chn++) {
  1416. port = (jack_port_t *) node->data;
  1417. if (!jack_port_connected (port)) {
  1418. continue;
  1419. }
  1420. buf = jack_port_get_buffer (port, orig_nframes);
  1421. alsa_driver_write_to_channel (driver, chn,
  1422. buf + nwritten, contiguous);
  1423. if (mon_node) {
  1424. port = (jack_port_t *) mon_node->data;
  1425. if (!jack_port_connected (port)) {
  1426. continue;
  1427. }
  1428. monbuf = jack_port_get_buffer (port, orig_nframes);
  1429. memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1430. mon_node = jack_slist_next (mon_node);
  1431. }
  1432. }
  1433. */
  1434. // JACK2
  1435. WriteOutput(orig_nframes, contiguous, nwritten);
  1436. if (!bitset_empty (driver->channels_not_done)) {
  1437. alsa_driver_silence_untouched_channels (driver,
  1438. contiguous);
  1439. }
  1440. if ((err = snd_pcm_mmap_commit (driver->playback_handle,
  1441. offset, contiguous)) < 0) {
  1442. jack_error ("ALSA: could not complete playback of %"
  1443. PRIu32 " frames: error = %d", contiguous, err);
  1444. if (err != -EPIPE && err != -ESTRPIPE)
  1445. return -1;
  1446. }
  1447. nframes -= contiguous;
  1448. nwritten += contiguous;
  1449. }
  1450. return 0;
  1451. }
  1452. #if 0
  1453. static int /* UNUSED */
  1454. alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
  1455. {
  1456. return driver->hw->change_sample_clock (driver->hw, mode);
  1457. }
  1458. static void /* UNUSED */
  1459. alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
  1460. {
  1461. if (driver->hw_monitoring) {
  1462. if (yn) {
  1463. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1464. } else {
  1465. driver->hw->set_input_monitor_mask (
  1466. driver->hw, driver->input_monitor_mask);
  1467. }
  1468. }
  1469. driver->all_monitor_in = yn;
  1470. }
  1471. static void /* UNUSED */
  1472. alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
  1473. {
  1474. if (yn) {
  1475. driver->hw_monitoring = TRUE;
  1476. if (driver->all_monitor_in) {
  1477. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1478. } else {
  1479. driver->hw->set_input_monitor_mask (
  1480. driver->hw, driver->input_monitor_mask);
  1481. }
  1482. } else {
  1483. driver->hw_monitoring = FALSE;
  1484. driver->hw->set_input_monitor_mask (driver->hw, 0);
  1485. }
  1486. }
  1487. static ClockSyncStatus /* UNUSED */
  1488. alsa_driver_clock_sync_status (channel_t chn)
  1489. {
  1490. return Lock;
  1491. }
  1492. #endif
  1493. void
  1494. alsa_driver_delete (alsa_driver_t *driver)
  1495. {
  1496. JSList *node;
  1497. if (driver->midi)
  1498. (driver->midi->destroy)(driver->midi);
  1499. for (node = driver->clock_sync_listeners; node;
  1500. node = jack_slist_next (node)) {
  1501. free (node->data);
  1502. }
  1503. jack_slist_free (driver->clock_sync_listeners);
  1504. if (driver->ctl_handle) {
  1505. snd_ctl_close (driver->ctl_handle);
  1506. driver->ctl_handle = 0;
  1507. }
  1508. if (driver->capture_handle) {
  1509. snd_pcm_close (driver->capture_handle);
  1510. driver->capture_handle = 0;
  1511. }
  1512. if (driver->playback_handle) {
  1513. snd_pcm_close (driver->playback_handle);
  1514. driver->capture_handle = 0;
  1515. }
  1516. if (driver->capture_hw_params) {
  1517. snd_pcm_hw_params_free (driver->capture_hw_params);
  1518. driver->capture_hw_params = 0;
  1519. }
  1520. if (driver->playback_hw_params) {
  1521. snd_pcm_hw_params_free (driver->playback_hw_params);
  1522. driver->playback_hw_params = 0;
  1523. }
  1524. if (driver->capture_sw_params) {
  1525. snd_pcm_sw_params_free (driver->capture_sw_params);
  1526. driver->capture_sw_params = 0;
  1527. }
  1528. if (driver->playback_sw_params) {
  1529. snd_pcm_sw_params_free (driver->playback_sw_params);
  1530. driver->playback_sw_params = 0;
  1531. }
  1532. if (driver->pfd) {
  1533. free (driver->pfd);
  1534. }
  1535. if (driver->hw) {
  1536. driver->hw->release (driver->hw);
  1537. driver->hw = 0;
  1538. }
  1539. free(driver->alsa_name_playback);
  1540. free(driver->alsa_name_capture);
  1541. free(driver->alsa_driver);
  1542. alsa_driver_release_channel_dependent_memory (driver);
  1543. //JACK2
  1544. //jack_driver_nt_finish ((jack_driver_nt_t *) driver);
  1545. free (driver);
  1546. }
  1547. jack_driver_t *
  1548. alsa_driver_new (char *name, char *playback_alsa_device,
  1549. char *capture_alsa_device,
  1550. jack_client_t *client,
  1551. jack_nframes_t frames_per_cycle,
  1552. jack_nframes_t user_nperiods,
  1553. jack_nframes_t rate,
  1554. int hw_monitoring,
  1555. int hw_metering,
  1556. int capturing,
  1557. int playing,
  1558. DitherAlgorithm dither,
  1559. int soft_mode,
  1560. int monitor,
  1561. int user_capture_nchnls,
  1562. int user_playback_nchnls,
  1563. int shorts_first,
  1564. jack_nframes_t capture_latency,
  1565. jack_nframes_t playback_latency,
  1566. alsa_midi_t *midi_driver
  1567. )
  1568. {
  1569. int err;
  1570. alsa_driver_t *driver;
  1571. jack_info ("creating alsa driver ... %s|%s|%" PRIu32 "|%" PRIu32
  1572. "|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s",
  1573. playing ? playback_alsa_device : "-",
  1574. capturing ? capture_alsa_device : "-",
  1575. frames_per_cycle, user_nperiods, rate,
  1576. user_capture_nchnls,user_playback_nchnls,
  1577. hw_monitoring ? "hwmon": "nomon",
  1578. hw_metering ? "hwmeter":"swmeter",
  1579. soft_mode ? "soft-mode":"-",
  1580. shorts_first ? "16bit":"32bit");
  1581. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  1582. jack_driver_nt_init ((jack_driver_nt_t *) driver);
  1583. // JACK2
  1584. /*
  1585. driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
  1586. driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
  1587. driver->read = (JackDriverReadFunction) alsa_driver_read;
  1588. driver->write = (JackDriverReadFunction) alsa_driver_write;
  1589. driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
  1590. driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
  1591. driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
  1592. driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
  1593. driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
  1594. */
  1595. driver->playback_handle = NULL;
  1596. driver->capture_handle = NULL;
  1597. driver->ctl_handle = 0;
  1598. driver->hw = 0;
  1599. driver->capture_and_playback_not_synced = FALSE;
  1600. driver->max_nchannels = 0;
  1601. driver->user_nchannels = 0;
  1602. driver->playback_nchannels = user_playback_nchnls;
  1603. driver->capture_nchannels = user_capture_nchnls;
  1604. driver->playback_sample_bytes = (shorts_first ? 2:4);
  1605. driver->capture_sample_bytes = (shorts_first ? 2:4);
  1606. driver->capture_frame_latency = capture_latency;
  1607. driver->playback_frame_latency = playback_latency;
  1608. driver->playback_addr = 0;
  1609. driver->capture_addr = 0;
  1610. driver->playback_interleave_skip = NULL;
  1611. driver->capture_interleave_skip = NULL;
  1612. driver->silent = 0;
  1613. driver->all_monitor_in = FALSE;
  1614. driver->with_monitor_ports = monitor;
  1615. driver->clock_mode = ClockMaster; /* XXX is it? */
  1616. driver->input_monitor_mask = 0; /* XXX is it? */
  1617. driver->capture_ports = 0;
  1618. driver->playback_ports = 0;
  1619. driver->monitor_ports = 0;
  1620. driver->pfd = 0;
  1621. driver->playback_nfds = 0;
  1622. driver->capture_nfds = 0;
  1623. driver->dither = dither;
  1624. driver->soft_mode = soft_mode;
  1625. driver->quirk_bswap = 0;
  1626. pthread_mutex_init (&driver->clock_sync_lock, 0);
  1627. driver->clock_sync_listeners = 0;
  1628. driver->poll_late = 0;
  1629. driver->xrun_count = 0;
  1630. driver->process_count = 0;
  1631. driver->alsa_name_playback = strdup (playback_alsa_device);
  1632. driver->alsa_name_capture = strdup (capture_alsa_device);
  1633. driver->midi = midi_driver;
  1634. driver->xrun_recovery = 0;
  1635. if (alsa_driver_check_card_type (driver)) {
  1636. alsa_driver_delete (driver);
  1637. return NULL;
  1638. }
  1639. alsa_driver_hw_specific (driver, hw_monitoring, hw_metering);
  1640. if (playing) {
  1641. if (snd_pcm_open (&driver->playback_handle,
  1642. playback_alsa_device,
  1643. SND_PCM_STREAM_PLAYBACK,
  1644. SND_PCM_NONBLOCK) < 0) {
  1645. switch (errno) {
  1646. case EBUSY:
  1647. jack_error ("the playback device \"%s\" is "
  1648. "already in use. Please stop the"
  1649. " application using it and "
  1650. "run JACK again",
  1651. playback_alsa_device);
  1652. alsa_driver_delete (driver);
  1653. return NULL;
  1654. break;
  1655. case EPERM:
  1656. jack_error ("you do not have permission to open "
  1657. "the audio device \"%s\" for playback",
  1658. playback_alsa_device);
  1659. alsa_driver_delete (driver);
  1660. return NULL;
  1661. break;
  1662. }
  1663. driver->playback_handle = NULL;
  1664. }
  1665. if (driver->playback_handle) {
  1666. snd_pcm_nonblock (driver->playback_handle, 0);
  1667. }
  1668. }
  1669. if (capturing) {
  1670. if (snd_pcm_open (&driver->capture_handle,
  1671. capture_alsa_device,
  1672. SND_PCM_STREAM_CAPTURE,
  1673. SND_PCM_NONBLOCK) < 0) {
  1674. switch (errno) {
  1675. case EBUSY:
  1676. jack_error ("the capture device \"%s\" is "
  1677. "already in use. Please stop the"
  1678. " application using it and "
  1679. "run JACK again",
  1680. capture_alsa_device);
  1681. alsa_driver_delete (driver);
  1682. return NULL;
  1683. break;
  1684. case EPERM:
  1685. jack_error ("you do not have permission to open "
  1686. "the audio device \"%s\" for capture",
  1687. capture_alsa_device);
  1688. alsa_driver_delete (driver);
  1689. return NULL;
  1690. break;
  1691. }
  1692. driver->capture_handle = NULL;
  1693. }
  1694. if (driver->capture_handle) {
  1695. snd_pcm_nonblock (driver->capture_handle, 0);
  1696. }
  1697. }
  1698. if (driver->playback_handle == NULL) {
  1699. if (playing) {
  1700. /* they asked for playback, but we can't do it */
  1701. jack_error ("ALSA: Cannot open PCM device %s for "
  1702. "playback. Falling back to capture-only"
  1703. " mode", name);
  1704. if (driver->capture_handle == NULL) {
  1705. /* can't do anything */
  1706. alsa_driver_delete (driver);
  1707. return NULL;
  1708. }
  1709. playing = FALSE;
  1710. }
  1711. }
  1712. if (driver->capture_handle == NULL) {
  1713. if (capturing) {
  1714. /* they asked for capture, but we can't do it */
  1715. jack_error ("ALSA: Cannot open PCM device %s for "
  1716. "capture. Falling back to playback-only"
  1717. " mode", name);
  1718. if (driver->playback_handle == NULL) {
  1719. /* can't do anything */
  1720. alsa_driver_delete (driver);
  1721. return NULL;
  1722. }
  1723. capturing = FALSE;
  1724. }
  1725. }
  1726. driver->playback_hw_params = 0;
  1727. driver->capture_hw_params = 0;
  1728. driver->playback_sw_params = 0;
  1729. driver->capture_sw_params = 0;
  1730. if (driver->playback_handle) {
  1731. if ((err = snd_pcm_hw_params_malloc (
  1732. &driver->playback_hw_params)) < 0) {
  1733. jack_error ("ALSA: could not allocate playback hw"
  1734. " params structure");
  1735. alsa_driver_delete (driver);
  1736. return NULL;
  1737. }
  1738. if ((err = snd_pcm_sw_params_malloc (
  1739. &driver->playback_sw_params)) < 0) {
  1740. jack_error ("ALSA: could not allocate playback sw"
  1741. " params structure");
  1742. alsa_driver_delete (driver);
  1743. return NULL;
  1744. }
  1745. }
  1746. if (driver->capture_handle) {
  1747. if ((err = snd_pcm_hw_params_malloc (
  1748. &driver->capture_hw_params)) < 0) {
  1749. jack_error ("ALSA: could not allocate capture hw"
  1750. " params structure");
  1751. alsa_driver_delete (driver);
  1752. return NULL;
  1753. }
  1754. if ((err = snd_pcm_sw_params_malloc (
  1755. &driver->capture_sw_params)) < 0) {
  1756. jack_error ("ALSA: could not allocate capture sw"
  1757. " params structure");
  1758. alsa_driver_delete (driver);
  1759. return NULL;
  1760. }
  1761. }
  1762. if (alsa_driver_set_parameters (driver, frames_per_cycle,
  1763. user_nperiods, rate)) {
  1764. alsa_driver_delete (driver);
  1765. return NULL;
  1766. }
  1767. driver->capture_and_playback_not_synced = FALSE;
  1768. if (driver->capture_handle && driver->playback_handle) {
  1769. if (snd_pcm_link (driver->playback_handle,
  1770. driver->capture_handle) != 0) {
  1771. driver->capture_and_playback_not_synced = TRUE;
  1772. }
  1773. }
  1774. driver->client = client;
  1775. return (jack_driver_t *) driver;
  1776. }
  1777. int
  1778. alsa_driver_listen_for_clock_sync_status (alsa_driver_t *driver,
  1779. ClockSyncListenerFunction func,
  1780. void *arg)
  1781. {
  1782. ClockSyncListener *csl;
  1783. csl = (ClockSyncListener *) malloc (sizeof (ClockSyncListener));
  1784. csl->function = func;
  1785. csl->arg = arg;
  1786. csl->id = driver->next_clock_sync_listener_id++;
  1787. pthread_mutex_lock (&driver->clock_sync_lock);
  1788. driver->clock_sync_listeners =
  1789. jack_slist_prepend (driver->clock_sync_listeners, csl);
  1790. pthread_mutex_unlock (&driver->clock_sync_lock);
  1791. return csl->id;
  1792. }
  1793. int
  1794. alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver,
  1795. unsigned int which)
  1796. {
  1797. JSList *node;
  1798. int ret = -1;
  1799. pthread_mutex_lock (&driver->clock_sync_lock);
  1800. for (node = driver->clock_sync_listeners; node;
  1801. node = jack_slist_next (node)) {
  1802. if (((ClockSyncListener *) node->data)->id == which) {
  1803. driver->clock_sync_listeners =
  1804. jack_slist_remove_link (
  1805. driver->clock_sync_listeners, node);
  1806. free (node->data);
  1807. jack_slist_free_1 (node);
  1808. ret = 0;
  1809. break;
  1810. }
  1811. }
  1812. pthread_mutex_unlock (&driver->clock_sync_lock);
  1813. return ret;
  1814. }
  1815. void
  1816. alsa_driver_clock_sync_notify (alsa_driver_t *driver, channel_t chn,
  1817. ClockSyncStatus status)
  1818. {
  1819. JSList *node;
  1820. pthread_mutex_lock (&driver->clock_sync_lock);
  1821. for (node = driver->clock_sync_listeners; node;
  1822. node = jack_slist_next (node)) {
  1823. ClockSyncListener *csl = (ClockSyncListener *) node->data;
  1824. csl->function (chn, status, csl->arg);
  1825. }
  1826. pthread_mutex_unlock (&driver->clock_sync_lock);
  1827. }
  1828. /* DRIVER "PLUGIN" INTERFACE */
  1829. const char driver_client_name[] = "alsa_pcm";
  1830. void
  1831. driver_finish (jack_driver_t *driver)
  1832. {
  1833. alsa_driver_delete ((alsa_driver_t *) driver);
  1834. }