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.

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