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.

2750 lines
88KB

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