jack1 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.

2626 lines
66KB

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