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.

2978 lines
92KB

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