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.

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