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

2948 lines
96KB

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