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.

2683 lines
80KB

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