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.

2972 lines
90KB

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