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.

2879 lines
89KB

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