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.

2299 lines
58KB

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