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.

2632 lines
79KB

  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. int
  312. JackAlsaDriver::alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
  313. const char *stream_name,
  314. snd_pcm_t *handle,
  315. snd_pcm_hw_params_t *hw_params,
  316. snd_pcm_sw_params_t *sw_params,
  317. unsigned int *nperiodsp,
  318. unsigned long *nchns,
  319. unsigned long sample_width)
  320. {
  321. int err, format;
  322. unsigned int frame_rate;
  323. snd_pcm_uframes_t stop_th;
  324. static struct {
  325. char Name[32];
  326. snd_pcm_format_t format;
  327. int swapped;
  328. } formats[] = {
  329. {"32bit little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
  330. {"32bit big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
  331. {"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE},
  332. {"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE},
  333. {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
  334. {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
  335. };
  336. #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
  337. #define FIRST_16BIT_FORMAT 4
  338. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  339. jack_error ("ALSA: no playback configurations available (%s)",
  340. snd_strerror (err));
  341. return -1;
  342. }
  343. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
  344. < 0) {
  345. jack_error ("ALSA: cannot restrict period size to integral"
  346. " value.");
  347. return -1;
  348. }
  349. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  350. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  351. if ((err = snd_pcm_hw_params_set_access (
  352. handle, hw_params,
  353. SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
  354. jack_error ("ALSA: mmap-based access is not possible"
  355. " for the %s "
  356. "stream of this audio interface",
  357. stream_name);
  358. return -1;
  359. }
  360. }
  361. }
  362. format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
  363. while (1) {
  364. if ((err = snd_pcm_hw_params_set_format (
  365. handle, hw_params, formats[format].format)) < 0) {
  366. if ((sample_width == 4
  367. ? format++ >= NUMFORMATS - 1
  368. : format-- <= 0)) {
  369. jack_error ("Sorry. The audio interface \"%s\""
  370. " doesn't support any of the"
  371. " hardware sample formats that"
  372. " JACK's alsa-driver can use.",
  373. device_name);
  374. return -1;
  375. }
  376. } else {
  377. if (formats[format].swapped) {
  378. driver->quirk_bswap = 1;
  379. } else {
  380. driver->quirk_bswap = 0;
  381. }
  382. jack_error ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
  383. break;
  384. }
  385. }
  386. frame_rate = driver->frame_rate ;
  387. err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
  388. &frame_rate, NULL) ;
  389. driver->frame_rate = frame_rate ;
  390. if (err < 0) {
  391. jack_error ("ALSA: cannot set sample/frame rate to %ld for %s", driver->frame_rate, stream_name);
  392. return -1;
  393. }
  394. if (!*nchns) {
  395. /*if not user-specified, try to find the maximum
  396. * number of channels */
  397. unsigned int channels_max ;
  398. err = snd_pcm_hw_params_get_channels_max (hw_params,
  399. &channels_max);
  400. *nchns = channels_max ;
  401. if (*nchns > 1024) {
  402. /* the hapless user is an unwitting victim of
  403. the "default" ALSA PCM device, which can
  404. support up to 16 million channels. since
  405. they can't be bothered to set up a proper
  406. default device, limit the number of
  407. channels for them to a sane default.
  408. */
  409. jack_error (
  410. "You appear to be using the ALSA software \"plug\" layer, probably\n"
  411. "a result of using the \"default\" ALSA device. This is less\n"
  412. "efficient than it could be. Consider using a hardware device\n"
  413. "instead rather than using the plug layer. Usually the name of the\n"
  414. "hardware device that corresponds to the first sound card is hw:0\n"
  415. );
  416. *nchns = 2;
  417. }
  418. }
  419. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
  420. *nchns)) < 0) {
  421. jack_error ("ALSA: cannot set channel count to %u for %s",
  422. *nchns, stream_name);
  423. return -1;
  424. }
  425. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
  426. driver->frames_per_cycle,
  427. 0))
  428. < 0) {
  429. jack_error ("ALSA: cannot set period size to %ld frames for %s", driver->frames_per_cycle, stream_name);
  430. return -1;
  431. }
  432. *nperiodsp = driver->user_nperiods;
  433. snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
  434. if (*nperiodsp < driver->user_nperiods)
  435. *nperiodsp = driver->user_nperiods;
  436. if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
  437. nperiodsp, NULL) < 0) {
  438. jack_error ("ALSA: cannot set number of periods to %u for %s",
  439. *nperiodsp, stream_name);
  440. return -1;
  441. }
  442. if (*nperiodsp < driver->user_nperiods) {
  443. jack_error ("ALSA: got smaller periods %u than %u for %s",
  444. *nperiodsp, (unsigned int) driver->user_nperiods,
  445. stream_name);
  446. return -1;
  447. }
  448. jack_error ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
  449. /*
  450. if (!jack_power_of_two(driver->frames_per_cycle)) {
  451. jack_error("JACK: frames must be a power of two "
  452. "(64, 512, 1024, ...)\n");
  453. return -1;
  454. }
  455. */
  456. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
  457. *nperiodsp *
  458. driver->frames_per_cycle))
  459. < 0) {
  460. jack_error ("ALSA: cannot set buffer length to %ld for %s", *nperiodsp * driver->frames_per_cycle,
  461. stream_name);
  462. return -1;
  463. }
  464. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  465. jack_error ("ALSA: cannot set hardware parameters for %s",
  466. stream_name);
  467. return -1;
  468. }
  469. snd_pcm_sw_params_current (handle, sw_params);
  470. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
  471. 0U)) < 0) {
  472. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  473. return -1;
  474. }
  475. stop_th = *nperiodsp * driver->frames_per_cycle;
  476. if (driver->soft_mode) {
  477. stop_th = (snd_pcm_uframes_t)-1;
  478. }
  479. if ((err = snd_pcm_sw_params_set_stop_threshold (
  480. handle, sw_params, stop_th)) < 0) {
  481. jack_error ("ALSA: cannot set stop mode for %s",
  482. stream_name);
  483. return -1;
  484. }
  485. if ((err = snd_pcm_sw_params_set_silence_threshold (
  486. handle, sw_params, 0)) < 0) {
  487. jack_error ("ALSA: cannot set silence threshold for %s",
  488. stream_name);
  489. return -1;
  490. }
  491. #if 0
  492. fprintf (stderr, "set silence size to %lu * %lu = %lu\n",
  493. driver->frames_per_cycle, *nperiodsp,
  494. driver->frames_per_cycle * *nperiodsp);
  495. if ((err = snd_pcm_sw_params_set_silence_size (
  496. handle, sw_params,
  497. driver->frames_per_cycle * *nperiodsp)) < 0) {
  498. jack_error ("ALSA: cannot set silence size for %s",
  499. stream_name);
  500. return -1;
  501. }
  502. #endif
  503. if (handle == driver->playback_handle)
  504. err = snd_pcm_sw_params_set_avail_min (
  505. handle, sw_params,
  506. driver->frames_per_cycle
  507. * (*nperiodsp - driver->user_nperiods + 1));
  508. else
  509. err = snd_pcm_sw_params_set_avail_min (
  510. handle, sw_params, driver->frames_per_cycle);
  511. if (err < 0) {
  512. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  513. return -1;
  514. }
  515. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  516. jack_error ("ALSA: cannot set software parameters for %s\n",
  517. stream_name);
  518. return -1;
  519. }
  520. return 0;
  521. }
  522. int
  523. JackAlsaDriver::alsa_driver_set_parameters (alsa_driver_t *driver,
  524. jack_nframes_t frames_per_cycle,
  525. jack_nframes_t user_nperiods,
  526. jack_nframes_t rate)
  527. {
  528. int dir;
  529. snd_pcm_uframes_t p_period_size = 0;
  530. snd_pcm_uframes_t c_period_size = 0;
  531. channel_t chn;
  532. unsigned int pr = 0;
  533. unsigned int cr = 0;
  534. int err;
  535. driver->frame_rate = rate;
  536. driver->frames_per_cycle = frames_per_cycle;
  537. driver->user_nperiods = user_nperiods;
  538. /* // steph
  539. fprintf (stderr, "configuring for %" PRIu32 "Hz, period = %"
  540. PRIu32 " frames, buffer = %" PRIu32 " periods\n",
  541. rate, frames_per_cycle, user_nperiods);
  542. */
  543. if (driver->capture_handle) {
  544. if (alsa_driver_configure_stream (
  545. driver,
  546. driver->alsa_name_capture,
  547. "capture",
  548. driver->capture_handle,
  549. driver->capture_hw_params,
  550. driver->capture_sw_params,
  551. &driver->capture_nperiods,
  552. (long unsigned int*)&driver->capture_nchannels,
  553. driver->capture_sample_bytes)) {
  554. jack_error ("ALSA: cannot configure capture channel");
  555. return -1;
  556. }
  557. }
  558. if (driver->playback_handle) {
  559. if (alsa_driver_configure_stream (
  560. driver,
  561. driver->alsa_name_playback,
  562. "playback",
  563. driver->playback_handle,
  564. driver->playback_hw_params,
  565. driver->playback_sw_params,
  566. &driver->playback_nperiods,
  567. (long unsigned int*)&driver->playback_nchannels,
  568. driver->playback_sample_bytes)) {
  569. jack_error ("ALSA: cannot configure playback channel");
  570. return -1;
  571. }
  572. }
  573. /* check the rate, since thats rather important */
  574. if (driver->playback_handle) {
  575. snd_pcm_hw_params_get_rate (driver->playback_hw_params,
  576. &pr, &dir);
  577. }
  578. if (driver->capture_handle) {
  579. snd_pcm_hw_params_get_rate (driver->capture_hw_params,
  580. &cr, &dir);
  581. }
  582. if (driver->capture_handle && driver->playback_handle) {
  583. if (cr != pr) {
  584. jack_error ("playback and capture sample rates do "
  585. "not match (%d vs. %d)", pr, cr);
  586. }
  587. /* only change if *both* capture and playback rates
  588. * don't match requested certain hardware actually
  589. * still works properly in full-duplex with slightly
  590. * different rate values between adc and dac
  591. */
  592. if (cr != driver->frame_rate && pr != driver->frame_rate) {
  593. jack_error ("sample rate in use (%d Hz) does not "
  594. "match requested rate (%d Hz)",
  595. cr, driver->frame_rate);
  596. driver->frame_rate = cr;
  597. }
  598. } else if (driver->capture_handle && cr != driver->frame_rate) {
  599. jack_error ("capture sample rate in use (%d Hz) does not "
  600. "match requested rate (%d Hz)",
  601. cr, driver->frame_rate);
  602. driver->frame_rate = cr;
  603. } else if (driver->playback_handle && pr != driver->frame_rate) {
  604. jack_error ("playback sample rate in use (%d Hz) does not "
  605. "match requested rate (%d Hz)",
  606. pr, driver->frame_rate);
  607. driver->frame_rate = pr;
  608. }
  609. /* check the fragment size, since thats non-negotiable */
  610. if (driver->playback_handle) {
  611. snd_pcm_access_t access;
  612. err = snd_pcm_hw_params_get_period_size (
  613. driver->playback_hw_params, &p_period_size, &dir);
  614. err = snd_pcm_hw_params_get_format (
  615. driver->playback_hw_params,
  616. &(driver->playback_sample_format));
  617. err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
  618. &access);
  619. driver->playback_interleaved =
  620. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  621. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  622. if (p_period_size != driver->frames_per_cycle) {
  623. /*
  624. jack_error ("alsa_pcm: requested an interrupt every %"
  625. PRIu32
  626. " frames but got %u frames for playback",
  627. driver->frames_per_cycle, p_period_size);
  628. */
  629. return -1;
  630. }
  631. }
  632. if (driver->capture_handle) {
  633. snd_pcm_access_t access;
  634. err = snd_pcm_hw_params_get_period_size (
  635. driver->capture_hw_params, &c_period_size, &dir);
  636. err = snd_pcm_hw_params_get_format (
  637. driver->capture_hw_params,
  638. &(driver->capture_sample_format));
  639. err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
  640. &access);
  641. driver->capture_interleaved =
  642. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  643. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  644. if (c_period_size != driver->frames_per_cycle) {
  645. /* // steph
  646. jack_error ("alsa_pcm: requested an interrupt every %"
  647. PRIu32
  648. " frames but got %uc frames for capture",
  649. driver->frames_per_cycle, p_period_size);
  650. */
  651. return -1;
  652. }
  653. }
  654. driver->playback_sample_bytes =
  655. snd_pcm_format_physical_width (driver->playback_sample_format)
  656. / 8;
  657. driver->capture_sample_bytes =
  658. snd_pcm_format_physical_width (driver->capture_sample_format)
  659. / 8;
  660. if (driver->playback_handle) {
  661. switch (driver->playback_sample_format) {
  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 playback");
  672. exit (1);
  673. }
  674. }
  675. if (driver->capture_handle) {
  676. switch (driver->capture_sample_format) {
  677. case SND_PCM_FORMAT_S32_LE:
  678. case SND_PCM_FORMAT_S24_3LE:
  679. case SND_PCM_FORMAT_S24_3BE:
  680. case SND_PCM_FORMAT_S16_LE:
  681. case SND_PCM_FORMAT_S32_BE:
  682. case SND_PCM_FORMAT_S16_BE:
  683. break;
  684. default:
  685. jack_error ("programming error: unhandled format "
  686. "type for capture");
  687. exit (1);
  688. }
  689. }
  690. if (driver->playback_interleaved) {
  691. const snd_pcm_channel_area_t *my_areas;
  692. snd_pcm_uframes_t offset, frames;
  693. if (snd_pcm_mmap_begin(driver->playback_handle,
  694. &my_areas, &offset, &frames) < 0) {
  695. jack_error ("ALSA: %s: mmap areas info error",
  696. driver->alsa_name_playback);
  697. return -1;
  698. }
  699. driver->interleave_unit =
  700. snd_pcm_format_physical_width (
  701. driver->playback_sample_format) / 8;
  702. } else {
  703. driver->interleave_unit = 0; /* NOT USED */
  704. }
  705. if (driver->capture_interleaved) {
  706. const snd_pcm_channel_area_t *my_areas;
  707. snd_pcm_uframes_t offset, frames;
  708. if (snd_pcm_mmap_begin(driver->capture_handle,
  709. &my_areas, &offset, &frames) < 0) {
  710. jack_error ("ALSA: %s: mmap areas info error",
  711. driver->alsa_name_capture);
  712. return -1;
  713. }
  714. }
  715. if (driver->playback_nchannels > driver->capture_nchannels) {
  716. driver->max_nchannels = driver->playback_nchannels;
  717. driver->user_nchannels = driver->capture_nchannels;
  718. } else {
  719. driver->max_nchannels = driver->capture_nchannels;
  720. driver->user_nchannels = driver->playback_nchannels;
  721. }
  722. alsa_driver_setup_io_function_pointers (driver);
  723. /* Allocate and initialize structures that rely on the
  724. channels counts.
  725. Set up the bit pattern that is used to record which
  726. channels require action on every cycle. any bits that are
  727. not set after the engine's process() call indicate channels
  728. that potentially need to be silenced.
  729. */
  730. bitset_create (&driver->channels_done, driver->max_nchannels);
  731. bitset_create (&driver->channels_not_done, driver->max_nchannels);
  732. if (driver->playback_handle) {
  733. driver->playback_addr = (char **)
  734. malloc (sizeof (char *) * driver->playback_nchannels);
  735. memset (driver->playback_addr, 0,
  736. sizeof (char *) * driver->playback_nchannels);
  737. driver->playback_interleave_skip = (unsigned long *)
  738. malloc (sizeof (unsigned long *) * driver->playback_nchannels);
  739. memset (driver->playback_interleave_skip, 0,
  740. sizeof (unsigned long *) * driver->playback_nchannels);
  741. driver->silent = (unsigned long *)
  742. malloc (sizeof (unsigned long)
  743. * driver->playback_nchannels);
  744. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  745. driver->silent[chn] = 0;
  746. }
  747. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  748. bitset_add (driver->channels_done, chn);
  749. }
  750. driver->dither_state = (dither_state_t *)
  751. calloc ( driver->playback_nchannels,
  752. sizeof (dither_state_t));
  753. }
  754. if (driver->capture_handle) {
  755. driver->capture_addr = (char **)
  756. malloc (sizeof (char *) * driver->capture_nchannels);
  757. memset (driver->capture_addr, 0,
  758. sizeof (char *) * driver->capture_nchannels);
  759. driver->capture_interleave_skip = (unsigned long *)
  760. malloc (sizeof (unsigned long *) * driver->capture_nchannels);
  761. memset (driver->capture_interleave_skip, 0,
  762. sizeof (unsigned long *) * driver->capture_nchannels);
  763. }
  764. driver->clock_sync_data = (ClockSyncStatus *)
  765. malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
  766. driver->period_usecs =
  767. (jack_time_t) floor ((((float) driver->frames_per_cycle) /
  768. driver->frame_rate) * 1000000.0f);
  769. driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
  770. // steph
  771. /*
  772. if (driver->engine) {
  773. driver->engine->set_buffer_size (driver->engine,
  774. driver->frames_per_cycle);
  775. }
  776. */
  777. return 0;
  778. }
  779. int
  780. JackAlsaDriver::alsa_driver_reset_parameters (alsa_driver_t *driver,
  781. jack_nframes_t frames_per_cycle,
  782. jack_nframes_t user_nperiods,
  783. jack_nframes_t rate)
  784. {
  785. /* XXX unregister old ports ? */
  786. alsa_driver_release_channel_dependent_memory (driver);
  787. return alsa_driver_set_parameters (driver,
  788. frames_per_cycle,
  789. user_nperiods, rate);
  790. }
  791. int
  792. JackAlsaDriver::alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  793. snd_pcm_uframes_t *capture_avail,
  794. snd_pcm_uframes_t *playback_avail,
  795. snd_pcm_uframes_t *capture_offset,
  796. snd_pcm_uframes_t *playback_offset)
  797. {
  798. unsigned long err;
  799. channel_t chn;
  800. if (capture_avail) {
  801. if ((err = snd_pcm_mmap_begin (
  802. driver->capture_handle, &driver->capture_areas,
  803. (snd_pcm_uframes_t *) capture_offset,
  804. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  805. jack_error ("ALSA: %s: mmap areas info error",
  806. driver->alsa_name_capture);
  807. return -1;
  808. }
  809. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  810. const snd_pcm_channel_area_t *a =
  811. &driver->capture_areas[chn];
  812. driver->capture_addr[chn] = (char *) a->addr
  813. + ((a->first + a->step * *capture_offset) / 8);
  814. driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  815. }
  816. }
  817. if (playback_avail) {
  818. if ((err = snd_pcm_mmap_begin (
  819. driver->playback_handle, &driver->playback_areas,
  820. (snd_pcm_uframes_t *) playback_offset,
  821. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  822. jack_error ("ALSA: %s: mmap areas info error ",
  823. driver->alsa_name_playback);
  824. return -1;
  825. }
  826. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  827. const snd_pcm_channel_area_t *a =
  828. &driver->playback_areas[chn];
  829. driver->playback_addr[chn] = (char *) a->addr
  830. + ((a->first + a->step * *playback_offset) / 8);
  831. driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  832. }
  833. }
  834. return 0;
  835. }
  836. int
  837. JackAlsaDriver::alsa_driver_start (alsa_driver_t *driver)
  838. {
  839. int err;
  840. snd_pcm_uframes_t poffset, pavail;
  841. channel_t chn;
  842. driver->poll_last = 0;
  843. driver->poll_next = 0;
  844. if (driver->playback_handle) {
  845. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  846. jack_error ("ALSA: prepare error for playback on "
  847. "\"%s\" (%s)", driver->alsa_name_playback,
  848. snd_strerror(err));
  849. return -1;
  850. }
  851. }
  852. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  853. || !driver->playback_handle) {
  854. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  855. jack_error ("ALSA: prepare error for capture on \"%s\""
  856. " (%s)", driver->alsa_name_capture,
  857. snd_strerror(err));
  858. return -1;
  859. }
  860. }
  861. if (driver->hw_monitoring) {
  862. if (driver->input_monitor_mask || driver->all_monitor_in) {
  863. if (driver->all_monitor_in) {
  864. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  865. } else {
  866. driver->hw->set_input_monitor_mask (
  867. driver->hw, driver->input_monitor_mask);
  868. }
  869. } else {
  870. driver->hw->set_input_monitor_mask (driver->hw,
  871. driver->input_monitor_mask);
  872. }
  873. }
  874. if (driver->playback_handle) {
  875. driver->playback_nfds =
  876. snd_pcm_poll_descriptors_count (driver->playback_handle);
  877. } else {
  878. driver->playback_nfds = 0;
  879. }
  880. if (driver->capture_handle) {
  881. driver->capture_nfds =
  882. snd_pcm_poll_descriptors_count (driver->capture_handle);
  883. } else {
  884. driver->capture_nfds = 0;
  885. }
  886. if (driver->pfd) {
  887. free (driver->pfd);
  888. }
  889. driver->pfd = (struct pollfd *)
  890. malloc (sizeof (struct pollfd) *
  891. (driver->playback_nfds + driver->capture_nfds + 2));
  892. if (driver->playback_handle) {
  893. /* fill playback buffer with zeroes, and mark
  894. all fragments as having data.
  895. */
  896. pavail = snd_pcm_avail_update (driver->playback_handle);
  897. if (pavail !=
  898. driver->frames_per_cycle * driver->playback_nperiods) {
  899. jack_error ("ALSA: full buffer not available at start");
  900. return -1;
  901. }
  902. if (alsa_driver_get_channel_addresses (driver,
  903. 0, &pavail, 0, &poffset)) {
  904. return -1;
  905. }
  906. /* XXX this is cheating. ALSA offers no guarantee that
  907. we can access the entire buffer at any one time. It
  908. works on most hardware tested so far, however, buts
  909. its a liability in the long run. I think that
  910. alsa-lib may have a better function for doing this
  911. here, where the goal is to silence the entire
  912. buffer.
  913. */
  914. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  915. alsa_driver_silence_on_channel (
  916. driver, chn,
  917. driver->user_nperiods
  918. * driver->frames_per_cycle);
  919. }
  920. snd_pcm_mmap_commit (driver->playback_handle, poffset,
  921. driver->user_nperiods
  922. * driver->frames_per_cycle);
  923. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  924. jack_error ("ALSA: could not start playback (%s)",
  925. snd_strerror (err));
  926. return -1;
  927. }
  928. }
  929. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  930. || !driver->playback_handle) {
  931. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  932. jack_error ("ALSA: could not start capture (%s)",
  933. snd_strerror (err));
  934. return -1;
  935. }
  936. }
  937. return 0;
  938. }
  939. int
  940. JackAlsaDriver::alsa_driver_stop (alsa_driver_t *driver)
  941. {
  942. int err;
  943. //JSList* node;
  944. //int chn;
  945. /* silence all capture port buffers, because we might
  946. be entering offline mode.
  947. */
  948. // steph
  949. /*
  950. for (chn = 0, node = driver->capture_ports; node;
  951. node = jack_slist_next (node), chn++) {
  952. jack_port_t* port;
  953. char* buf;
  954. jack_nframes_t nframes = driver->engine->control->buffer_size;
  955. port = (jack_port_t *) node->data;
  956. buf = jack_port_get_buffer (port, nframes);
  957. memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
  958. }
  959. */
  960. for (int i = 0; i < fPlaybackChannels; i++) {
  961. jack_default_audio_sample_t* buf =
  962. (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[i], fEngineControl->fBufferSize);
  963. memset (buf, 0, sizeof (jack_default_audio_sample_t) * fEngineControl->fBufferSize);
  964. }
  965. if (driver->playback_handle) {
  966. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  967. jack_error ("ALSA: channel flush for playback "
  968. "failed (%s)", snd_strerror (err));
  969. return -1;
  970. }
  971. }
  972. if (!driver->playback_handle
  973. || driver->capture_and_playback_not_synced) {
  974. if (driver->capture_handle) {
  975. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  976. jack_error ("ALSA: channel flush for "
  977. "capture failed (%s)",
  978. snd_strerror (err));
  979. return -1;
  980. }
  981. }
  982. }
  983. if (driver->hw_monitoring) {
  984. driver->hw->set_input_monitor_mask (driver->hw, 0);
  985. }
  986. return 0;
  987. }
  988. int
  989. JackAlsaDriver::alsa_driver_restart (alsa_driver_t *driver)
  990. {
  991. // steph
  992. /*
  993. if (driver->nt_stop((struct _jack_driver_nt *) driver))
  994. return -1;
  995. return driver->nt_start((struct _jack_driver_nt *) driver);
  996. */
  997. if (Stop())
  998. return -1;
  999. return Start();
  1000. }
  1001. int
  1002. JackAlsaDriver::alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
  1003. {
  1004. snd_pcm_status_t *status;
  1005. int res;
  1006. jack_error("alsa_driver_xrun_recovery ");
  1007. snd_pcm_status_alloca(&status);
  1008. if (driver->capture_handle) {
  1009. if ((res = snd_pcm_status(driver->capture_handle, status))
  1010. < 0) {
  1011. jack_error("status error: %s", snd_strerror(res));
  1012. }
  1013. } else {
  1014. if ((res = snd_pcm_status(driver->playback_handle, status))
  1015. < 0) {
  1016. jack_error("status error: %s", snd_strerror(res));
  1017. }
  1018. }
  1019. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN
  1020. // && driver->process_count > XRUN_REPORT_DELAY) { // steph
  1021. && driver->process_count > 10) {
  1022. struct timeval now, diff, tstamp;
  1023. driver->xrun_count++;
  1024. gettimeofday(&now, 0);
  1025. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  1026. timersub(&now, &tstamp, &diff);
  1027. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  1028. // steph: utiliser une version TR
  1029. jack_error("\n\n**** alsa_pcm: xrun of at least %.3f msecs\n\n", *delayed_usecs / 1000.0);
  1030. }
  1031. if (alsa_driver_restart (driver)) {
  1032. return -1;
  1033. }
  1034. return 0;
  1035. }
  1036. void
  1037. JackAlsaDriver::alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
  1038. jack_nframes_t nframes)
  1039. {
  1040. channel_t chn;
  1041. jack_nframes_t buffer_frames =
  1042. driver->frames_per_cycle * driver->playback_nperiods;
  1043. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  1044. if (bitset_contains (driver->channels_not_done, chn)) {
  1045. if (driver->silent[chn] < buffer_frames) {
  1046. alsa_driver_silence_on_channel_no_mark (
  1047. driver, chn, nframes);
  1048. driver->silent[chn] += nframes;
  1049. }
  1050. }
  1051. }
  1052. }
  1053. jack_nframes_t
  1054. JackAlsaDriver::alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
  1055. *delayed_usecs)
  1056. {
  1057. snd_pcm_sframes_t avail = 0;
  1058. snd_pcm_sframes_t capture_avail = 0;
  1059. snd_pcm_sframes_t playback_avail = 0;
  1060. int xrun_detected = FALSE;
  1061. int need_capture;
  1062. int need_playback;
  1063. unsigned int i;
  1064. jack_time_t poll_enter;
  1065. jack_time_t poll_ret = 0;
  1066. *status = -1;
  1067. *delayed_usecs = 0;
  1068. need_capture = driver->capture_handle ? 1 : 0;
  1069. if (extra_fd >= 0) {
  1070. need_playback = 0;
  1071. } else {
  1072. need_playback = driver->playback_handle ? 1 : 0;
  1073. }
  1074. // steph
  1075. // again:
  1076. while (need_playback || need_capture) {
  1077. unsigned int p_timed_out, c_timed_out;
  1078. unsigned int ci = 0;
  1079. unsigned int nfds;
  1080. nfds = 0;
  1081. if (need_playback) {
  1082. snd_pcm_poll_descriptors (driver->playback_handle,
  1083. &driver->pfd[0],
  1084. driver->playback_nfds);
  1085. nfds += driver->playback_nfds;
  1086. }
  1087. if (need_capture) {
  1088. snd_pcm_poll_descriptors (driver->capture_handle,
  1089. &driver->pfd[nfds],
  1090. driver->capture_nfds);
  1091. ci = nfds;
  1092. nfds += driver->capture_nfds;
  1093. }
  1094. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  1095. for (i = 0; i < nfds; i++) {
  1096. driver->pfd[i].events |= POLLERR;
  1097. }
  1098. if (extra_fd >= 0) {
  1099. driver->pfd[nfds].fd = extra_fd;
  1100. driver->pfd[nfds].events =
  1101. POLLIN | POLLERR | POLLHUP | POLLNVAL;
  1102. nfds++;
  1103. }
  1104. poll_enter = jack_get_microseconds ();
  1105. if (poll_enter > driver->poll_next) {
  1106. /*
  1107. * This processing cycle was delayed past the
  1108. * next due interrupt! Do not account this as
  1109. * a wakeup delay:
  1110. */
  1111. driver->poll_next = 0;
  1112. driver->poll_late++;
  1113. }
  1114. if (poll (driver->pfd, nfds, driver->poll_timeout) < 0) {
  1115. if (errno == EINTR) {
  1116. printf ("poll interrupt\n");
  1117. // this happens mostly when run
  1118. // under gdb, or when exiting due to a signal
  1119. // steph
  1120. /*
  1121. if (under_gdb) {
  1122. goto again;
  1123. }
  1124. */
  1125. *status = -2;
  1126. return 0;
  1127. }
  1128. jack_error ("ALSA: poll call failed (%s)",
  1129. strerror (errno));
  1130. *status = -3;
  1131. return 0;
  1132. }
  1133. poll_ret = jack_get_microseconds ();
  1134. // steph
  1135. fLastWaitUst = poll_ret;
  1136. if (extra_fd < 0) {
  1137. if (driver->poll_next && poll_ret > driver->poll_next) {
  1138. *delayed_usecs = poll_ret - driver->poll_next;
  1139. }
  1140. driver->poll_last = poll_ret;
  1141. driver->poll_next = poll_ret + driver->period_usecs;
  1142. // steph
  1143. /*
  1144. driver->engine->transport_cycle_start (driver->engine,
  1145. poll_ret);
  1146. */
  1147. }
  1148. #ifdef DEBUG_WAKEUP
  1149. fprintf (stderr, "%" PRIu64 ": checked %d fds, %" PRIu64
  1150. " usecs since poll entered\n", poll_ret, nfds,
  1151. poll_ret - poll_enter);
  1152. #endif
  1153. /* check to see if it was the extra FD that caused us
  1154. * to return from poll */
  1155. if (extra_fd >= 0) {
  1156. if (driver->pfd[nfds - 1].revents == 0) {
  1157. /* we timed out on the extra fd */
  1158. *status = -4;
  1159. // steph (cannot return negative value....)
  1160. // return -1;
  1161. return 0;
  1162. }
  1163. /* if POLLIN was the only bit set, we're OK */
  1164. *status = 0;
  1165. return (driver->pfd[nfds - 1].revents == POLLIN) ? 0 : -1;
  1166. }
  1167. p_timed_out = 0;
  1168. if (need_playback) {
  1169. for (i = 0; i < driver->playback_nfds; i++) {
  1170. if (driver->pfd[i].revents & POLLERR) {
  1171. xrun_detected = TRUE;
  1172. }
  1173. if (driver->pfd[i].revents == 0) {
  1174. p_timed_out++;
  1175. #ifdef DEBUG_WAKEUP
  1176. fprintf (stderr, "%" PRIu64
  1177. " playback stream timed out\n",
  1178. poll_ret);
  1179. #endif
  1180. }
  1181. }
  1182. if (p_timed_out == 0) {
  1183. need_playback = 0;
  1184. #ifdef DEBUG_WAKEUP
  1185. fprintf (stderr, "%" PRIu64
  1186. " playback stream ready\n",
  1187. poll_ret);
  1188. #endif
  1189. }
  1190. }
  1191. c_timed_out = 0;
  1192. if (need_capture) {
  1193. for (i = ci; i < nfds; i++) {
  1194. if (driver->pfd[i].revents & POLLERR) {
  1195. xrun_detected = TRUE;
  1196. }
  1197. if (driver->pfd[i].revents == 0) {
  1198. c_timed_out++;
  1199. #ifdef DEBUG_WAKEUP
  1200. fprintf (stderr, "%" PRIu64
  1201. " capture stream timed out\n",
  1202. poll_ret);
  1203. #endif
  1204. }
  1205. }
  1206. if (c_timed_out == 0) {
  1207. need_capture = 0;
  1208. #ifdef DEBUG_WAKEUP
  1209. fprintf (stderr, "%" PRIu64
  1210. " capture stream ready\n",
  1211. poll_ret);
  1212. #endif
  1213. }
  1214. }
  1215. if ((p_timed_out && (p_timed_out == driver->playback_nfds)) &&
  1216. (c_timed_out && (c_timed_out == driver->capture_nfds))) {
  1217. // steph
  1218. /*
  1219. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1220. " usecs",
  1221. poll_ret - poll_enter);
  1222. */
  1223. *status = -5;
  1224. return 0;
  1225. }
  1226. }
  1227. if (driver->capture_handle) {
  1228. if ((capture_avail = snd_pcm_avail_update (
  1229. driver->capture_handle)) < 0) {
  1230. if (capture_avail == -EPIPE) {
  1231. xrun_detected = TRUE;
  1232. } else {
  1233. jack_error ("unknown ALSA avail_update return"
  1234. " value (%u)", capture_avail);
  1235. }
  1236. }
  1237. } else {
  1238. /* odd, but see min() computation below */
  1239. capture_avail = INT_MAX;
  1240. }
  1241. if (driver->playback_handle) {
  1242. if ((playback_avail = snd_pcm_avail_update (
  1243. driver->playback_handle)) < 0) {
  1244. if (playback_avail == -EPIPE) {
  1245. xrun_detected = TRUE;
  1246. } else {
  1247. jack_error ("unknown ALSA avail_update return"
  1248. " value (%u)", playback_avail);
  1249. }
  1250. }
  1251. } else {
  1252. /* odd, but see min() computation below */
  1253. playback_avail = INT_MAX;
  1254. }
  1255. if (xrun_detected) {
  1256. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1257. return 0;
  1258. }
  1259. *status = 0;
  1260. driver->last_wait_ust = poll_ret;
  1261. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  1262. #ifdef DEBUG_WAKEUP
  1263. fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
  1264. "cavail = %lu\n",
  1265. avail, playback_avail, capture_avail);
  1266. #endif
  1267. /* mark all channels not done for now. read/write will change this */
  1268. bitset_copy (driver->channels_not_done, driver->channels_done);
  1269. /* constrain the available count to the nearest (round down) number of
  1270. periods.
  1271. */
  1272. return avail - (avail % driver->frames_per_cycle);
  1273. }
  1274. int JackAlsaDriver::SetBufferSize(jack_nframes_t buffer_size)
  1275. {
  1276. JackLog("JackAlsaDriver::SetBufferSize %ld\n", buffer_size);
  1277. int res = alsa_driver_reset_parameters((alsa_driver_t *)fDriver, buffer_size,
  1278. ((alsa_driver_t *)fDriver)->user_nperiods,
  1279. ((alsa_driver_t *)fDriver)->frame_rate);
  1280. if (res == 0) { // update fEngineControl and fGraphManager
  1281. JackAudioDriver::SetBufferSize(buffer_size); // never fails
  1282. } else {
  1283. alsa_driver_reset_parameters((alsa_driver_t *)fDriver, fEngineControl->fBufferSize,
  1284. ((alsa_driver_t *)fDriver)->user_nperiods,
  1285. ((alsa_driver_t *)fDriver)->frame_rate);
  1286. }
  1287. return res;
  1288. }
  1289. int
  1290. JackAlsaDriver::alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
  1291. {
  1292. snd_pcm_sframes_t contiguous;
  1293. snd_pcm_sframes_t nread;
  1294. snd_pcm_sframes_t offset;
  1295. jack_nframes_t orig_nframes;
  1296. jack_default_audio_sample_t* buf;
  1297. //channel_t chn;
  1298. //JSList *node;
  1299. //jack_port_t* port;
  1300. int err;
  1301. // steph
  1302. /*
  1303. if (!driver->capture_handle || driver->engine->freewheeling) {
  1304. return 0;
  1305. }
  1306. */
  1307. if (!driver->capture_handle) {
  1308. return 0;
  1309. }
  1310. if (nframes > driver->frames_per_cycle) {
  1311. return -1;
  1312. }
  1313. nread = 0;
  1314. contiguous = 0;
  1315. orig_nframes = nframes;
  1316. while (nframes) {
  1317. contiguous = nframes;
  1318. if (alsa_driver_get_channel_addresses (
  1319. driver,
  1320. (snd_pcm_uframes_t *) &contiguous,
  1321. (snd_pcm_uframes_t *) 0,
  1322. (snd_pcm_uframes_t *)&offset, 0) < 0) {
  1323. return -1;
  1324. }
  1325. // steph
  1326. for (int i = 0; i < fCaptureChannels; i++) {
  1327. if (fGraphManager->GetConnectionsNum(fCapturePortList[i]) > 0) {
  1328. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[i], orig_nframes);
  1329. alsa_driver_read_from_channel (driver, i, buf + nread, contiguous);
  1330. }
  1331. }
  1332. /* // steph
  1333. for (chn = 0, node = driver->capture_ports; node;
  1334. node = jack_slist_next (node), chn++) {
  1335. port = (jack_port_t *) node->data;
  1336. if (!jack_port_connected (port)) {
  1337. // no-copy optimization
  1338. continue;
  1339. }
  1340. buf = jack_port_get_buffer (port, orig_nframes);
  1341. alsa_driver_read_from_channel (driver, chn,
  1342. buf + nread, contiguous);
  1343. }
  1344. */
  1345. if ((err = snd_pcm_mmap_commit (driver->capture_handle,
  1346. offset, contiguous)) < 0) {
  1347. // steph
  1348. // jack_error ("ALSA: could not complete read of %"
  1349. // PRIu32 " frames: error = %d\n", contiguous, err);
  1350. jack_error ("ALSA: could not complete read of %d frames: error = %d", contiguous, err);
  1351. return -1;
  1352. }
  1353. nframes -= contiguous;
  1354. nread += contiguous;
  1355. }
  1356. return 0;
  1357. }
  1358. int
  1359. JackAlsaDriver::alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
  1360. {
  1361. //channel_t chn;
  1362. //JSList *node;
  1363. //JSList *mon_node;
  1364. jack_default_audio_sample_t* buf;
  1365. jack_default_audio_sample_t* monbuf;
  1366. jack_nframes_t orig_nframes;
  1367. snd_pcm_sframes_t nwritten;
  1368. snd_pcm_sframes_t contiguous;
  1369. snd_pcm_sframes_t offset;
  1370. JackPort* port;
  1371. //jack_port_t *port;
  1372. int err;
  1373. driver->process_count++;
  1374. // steph
  1375. /*
  1376. if (!driver->playback_handle || driver->engine->freewheeling) {
  1377. return 0;
  1378. }
  1379. */
  1380. if (!driver->playback_handle) {
  1381. return 0;
  1382. }
  1383. if (nframes > driver->frames_per_cycle) {
  1384. return -1;
  1385. }
  1386. nwritten = 0;
  1387. contiguous = 0;
  1388. orig_nframes = nframes;
  1389. /* check current input monitor request status */
  1390. driver->input_monitor_mask = 0;
  1391. // steph
  1392. /*
  1393. for (chn = 0, node = driver->capture_ports; node;
  1394. node = jack_slist_next (node), chn++) {
  1395. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  1396. driver->input_monitor_mask |= (1<<chn);
  1397. }
  1398. }
  1399. */
  1400. for (int i = 0; i < fCaptureChannels; i++) {
  1401. port = fGraphManager->GetPort(fCapturePortList[i]);
  1402. if (port->MonitoringInput()) {
  1403. driver->input_monitor_mask |= (1<<i);
  1404. }
  1405. }
  1406. if (driver->hw_monitoring) {
  1407. if ((driver->hw->input_monitor_mask
  1408. != driver->input_monitor_mask)
  1409. && !driver->all_monitor_in) {
  1410. driver->hw->set_input_monitor_mask (
  1411. driver->hw, driver->input_monitor_mask);
  1412. }
  1413. }
  1414. while (nframes) {
  1415. contiguous = nframes;
  1416. if (alsa_driver_get_channel_addresses (
  1417. driver,
  1418. (snd_pcm_uframes_t *) 0,
  1419. (snd_pcm_uframes_t *) &contiguous,
  1420. 0, (snd_pcm_uframes_t *)&offset) < 0) {
  1421. return -1;
  1422. }
  1423. // steph
  1424. for (int i = 0; i < fPlaybackChannels; i++) {
  1425. // Ouput ports
  1426. if (fGraphManager->GetConnectionsNum(fPlaybackPortList[i]) > 0) {
  1427. buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[i], orig_nframes);
  1428. alsa_driver_write_to_channel (driver, i, buf + nwritten, contiguous);
  1429. // Monitor ports
  1430. if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[i]) > 0) {
  1431. monbuf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[i], orig_nframes);
  1432. memcpy(monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1433. }
  1434. }
  1435. }
  1436. /*
  1437. for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
  1438. node;
  1439. node = jack_slist_next (node), chn++) {
  1440. port = (jack_port_t *) node->data;
  1441. if (!jack_port_connected (port)) {
  1442. continue;
  1443. }
  1444. buf = jack_port_get_buffer (port, orig_nframes);
  1445. alsa_driver_write_to_channel (driver, chn,
  1446. buf + nwritten, contiguous);
  1447. if (mon_node) {
  1448. port = (jack_port_t *) mon_node->data;
  1449. if (!jack_port_connected (port)) {
  1450. continue;
  1451. }
  1452. monbuf = jack_port_get_buffer (port, orig_nframes);
  1453. memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1454. mon_node = jack_slist_next (mon_node);
  1455. }
  1456. }
  1457. */
  1458. if (!bitset_empty (driver->channels_not_done)) {
  1459. alsa_driver_silence_untouched_channels (driver,
  1460. contiguous);
  1461. }
  1462. if ((err = snd_pcm_mmap_commit (driver->playback_handle,
  1463. offset, contiguous)) < 0) {
  1464. // steph
  1465. // jack_error ("ALSA: could not complete playback of %"
  1466. // PRIu32 " frames: error = %d", contiguous, err);
  1467. jack_error ("ALSA: could not complete playback of %d frames: error = %d", contiguous, err);
  1468. if (err != EPIPE && err != ESTRPIPE)
  1469. return -1;
  1470. }
  1471. nframes -= contiguous;
  1472. nwritten += contiguous;
  1473. }
  1474. return 0;
  1475. }
  1476. void
  1477. JackAlsaDriver::alsa_driver_delete (alsa_driver_t *driver)
  1478. {
  1479. JSList *node;
  1480. for (node = driver->clock_sync_listeners; node;
  1481. node = jack_slist_next (node)) {
  1482. free (node->data);
  1483. }
  1484. jack_slist_free (driver->clock_sync_listeners);
  1485. if (driver->capture_handle) {
  1486. snd_pcm_close (driver->capture_handle);
  1487. driver->capture_handle = 0;
  1488. }
  1489. if (driver->playback_handle) {
  1490. snd_pcm_close (driver->playback_handle);
  1491. driver->capture_handle = 0;
  1492. }
  1493. if (driver->capture_hw_params) {
  1494. snd_pcm_hw_params_free (driver->capture_hw_params);
  1495. driver->capture_hw_params = 0;
  1496. }
  1497. if (driver->playback_hw_params) {
  1498. snd_pcm_hw_params_free (driver->playback_hw_params);
  1499. driver->playback_hw_params = 0;
  1500. }
  1501. if (driver->capture_sw_params) {
  1502. snd_pcm_sw_params_free (driver->capture_sw_params);
  1503. driver->capture_sw_params = 0;
  1504. }
  1505. if (driver->playback_sw_params) {
  1506. snd_pcm_sw_params_free (driver->playback_sw_params);
  1507. driver->playback_sw_params = 0;
  1508. }
  1509. if (driver->pfd) {
  1510. free (driver->pfd);
  1511. }
  1512. if (driver->hw) {
  1513. driver->hw->release (driver->hw);
  1514. driver->hw = 0;
  1515. }
  1516. free(driver->alsa_name_playback);
  1517. free(driver->alsa_name_capture);
  1518. free(driver->alsa_driver);
  1519. alsa_driver_release_channel_dependent_memory (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_CLIENT_NAME_SIZE + JACK_PORT_NAME_SIZE];
  1753. assert(fCaptureChannels < PORT_NUM);
  1754. assert(fPlaybackChannels < PORT_NUM);
  1755. port_flags = JackPortIsOutput | JackPortIsPhysical | JackPortIsTerminal;
  1756. alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
  1757. if (alsa_driver->has_hw_monitoring)
  1758. port_flags |= JackPortCanMonitor;
  1759. // ALSA driver may have changed the values
  1760. JackAudioDriver::SetBufferSize(alsa_driver->frames_per_cycle);
  1761. JackAudioDriver::SetSampleRate(alsa_driver->frame_rate);
  1762. JackLog("JackAudioDriver::Attach fBufferSize %ld fSampleRate %ld\n", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
  1763. for (int i = 0; i < fCaptureChannels; i++) {
  1764. snprintf(buf, sizeof(buf) - 1, "%s:capture_%u", fClientControl->fName, i + 1);
  1765. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  1766. jack_error("driver: cannot register port for %s", buf);
  1767. return -1;
  1768. }
  1769. port = fGraphManager->GetPort(port_index);
  1770. port->Rename("system:capture_%d", i + 1);
  1771. port->SetLatency(alsa_driver->frames_per_cycle + alsa_driver->capture_frame_latency);
  1772. fCapturePortList[i] = port_index;
  1773. JackLog("JackAudioDriver::Attach fCapturePortList[i] %ld \n", port_index);
  1774. }
  1775. port_flags = JackPortIsInput | JackPortIsPhysical | JackPortIsTerminal;
  1776. for (int i = 0; i < fPlaybackChannels; i++) {
  1777. snprintf(buf, sizeof(buf) - 1, "%s:playback_%u", fClientControl->fName, i + 1);
  1778. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, (JackPortFlags)port_flags)) == NO_PORT) {
  1779. jack_error("driver: cannot register port for %s", buf);
  1780. return -1;
  1781. }
  1782. port = fGraphManager->GetPort(port_index);
  1783. port->Rename("system:playback_%d", i + 1);
  1784. port->SetLatency((alsa_driver->frames_per_cycle * (alsa_driver->user_nperiods - 1)) + alsa_driver->playback_frame_latency);
  1785. fPlaybackPortList[i] = port_index;
  1786. JackLog("JackAudioDriver::Attach fPlaybackPortList[i] %ld \n", port_index);
  1787. // Monitor ports
  1788. if (fWithMonitorPorts) {
  1789. JackLog("Create monitor port \n");
  1790. snprintf(buf, sizeof(buf) - 1, "%s:monitor_%lu",fClientControl->fName, i + 1);
  1791. if ((port_index = fGraphManager->AllocatePort(fClientControl->fRefNum, buf, JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput)) == NO_PORT) {
  1792. jack_error ("ALSA: cannot register monitor port for %s", buf);
  1793. } else {
  1794. port = fGraphManager->GetPort(port_index);
  1795. port->SetLatency(alsa_driver->frames_per_cycle);
  1796. fMonitorPortList[i] = port_index;
  1797. }
  1798. }
  1799. }
  1800. return 0;
  1801. }
  1802. int JackAlsaDriver::Open(jack_nframes_t nframes,
  1803. jack_nframes_t user_nperiods,
  1804. jack_nframes_t samplerate,
  1805. int hw_monitoring,
  1806. int hw_metering,
  1807. int capturing,
  1808. int playing,
  1809. DitherAlgorithm dither,
  1810. int soft_mode,
  1811. int monitor,
  1812. int inchannels,
  1813. int outchannels,
  1814. int shorts_first,
  1815. const char* capture_driver_name,
  1816. const char* playback_driver_name,
  1817. jack_nframes_t capture_latency,
  1818. jack_nframes_t playback_latency)
  1819. {
  1820. // Generic JackAudioDriver Open
  1821. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing,
  1822. inchannels, outchannels, monitor, capture_driver_name, playback_driver_name,
  1823. capture_latency, playback_latency) != 0) {
  1824. return -1;
  1825. }
  1826. fDriver = alsa_driver_new ("alsa_pcm", (char*)playback_driver_name, (char*)capture_driver_name,
  1827. NULL,
  1828. nframes,
  1829. user_nperiods,
  1830. samplerate,
  1831. hw_monitoring,
  1832. hw_metering,
  1833. capturing,
  1834. playing,
  1835. dither,
  1836. soft_mode,
  1837. monitor,
  1838. inchannels,
  1839. outchannels,
  1840. shorts_first,
  1841. capture_latency,
  1842. playback_latency);
  1843. if (fDriver) {
  1844. // ALSA driver may have changed the in/out values
  1845. fCaptureChannels = ((alsa_driver_t *)fDriver)->capture_nchannels;
  1846. fPlaybackChannels = ((alsa_driver_t *)fDriver)->playback_nchannels;
  1847. return 0;
  1848. } else {
  1849. return -1;
  1850. }
  1851. }
  1852. int JackAlsaDriver::Close()
  1853. {
  1854. JackAudioDriver::Close();
  1855. alsa_driver_delete((alsa_driver_t*)fDriver);
  1856. return 0;
  1857. }
  1858. int JackAlsaDriver::Start()
  1859. {
  1860. return alsa_driver_start((alsa_driver_t *)fDriver);
  1861. }
  1862. int JackAlsaDriver::Stop()
  1863. {
  1864. return alsa_driver_stop((alsa_driver_t *)fDriver);
  1865. }
  1866. int JackAlsaDriver::Read()
  1867. {
  1868. /* Taken from alsa_driver_run_cycle */
  1869. //jack_engine_t *engine = driver->engine;
  1870. int wait_status;
  1871. float delayed_usecs;
  1872. jack_nframes_t nframes;
  1873. //DEBUG ("alsa run cycle wait\n");
  1874. nframes = alsa_driver_wait((alsa_driver_t *)fDriver, -1, &wait_status, &delayed_usecs);
  1875. //DEBUG ("alsaback from wait, nframes = %lu", nframes);
  1876. if (wait_status < 0)
  1877. return -1; /* driver failed */
  1878. if (nframes == 0) {
  1879. /* we detected an xrun and restarted: notify
  1880. * clients about the delay.
  1881. */
  1882. //engine->delay (engine, delayed_usecs);
  1883. JackLog("ALSA XRun \n");
  1884. //NotifyXRun(jack_get_microseconds());
  1885. NotifyXRun(fLastWaitUst);
  1886. //return 0;
  1887. return -1;
  1888. }
  1889. //fLastWaitUst = GetMicroSeconds(); // Take callback date here
  1890. if (nframes != fEngineControl->fBufferSize)
  1891. JackLog("JackAlsaDriver::Read nframes = %ld\n", nframes);
  1892. //return engine->run_cycle (engine, nframes, delayed_usecs);
  1893. fDelayedUst = (jack_time_t)delayed_usecs;
  1894. return alsa_driver_read((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
  1895. }
  1896. int JackAlsaDriver::Write()
  1897. {
  1898. //JackLog("write\n");
  1899. int res = alsa_driver_write((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
  1900. jack_time_t write_time = GetMicroSeconds();
  1901. /*
  1902. if (write_time > (fLastWaitUst - fDelayedUst) + fEngineControl->fPeriodUsecs) {
  1903. JackLog("ALSA write XRun \n");
  1904. NotifyXRun(write_time);
  1905. }
  1906. */
  1907. return res;
  1908. }
  1909. void
  1910. JackAlsaDriver::jack_driver_init (jack_driver_t *driver)
  1911. {
  1912. memset (driver, 0, sizeof (*driver));
  1913. driver->attach = 0;
  1914. driver->detach = 0;
  1915. driver->write = 0;
  1916. driver->read = 0;
  1917. driver->null_cycle = 0;
  1918. driver->bufsize = 0;
  1919. driver->start = 0;
  1920. driver->stop = 0;
  1921. }
  1922. void
  1923. JackAlsaDriver::jack_driver_nt_init (jack_driver_nt_t * driver)
  1924. {
  1925. memset (driver, 0, sizeof (*driver));
  1926. jack_driver_init ((jack_driver_t *) driver);
  1927. driver->attach = 0;
  1928. driver->detach = 0;
  1929. driver->bufsize = 0;
  1930. driver->stop = 0;
  1931. driver->start = 0;
  1932. driver->nt_bufsize = 0;
  1933. driver->nt_start = 0;
  1934. driver->nt_stop = 0;
  1935. driver->nt_attach = 0;
  1936. driver->nt_detach = 0;
  1937. driver->nt_run_cycle = 0;
  1938. }
  1939. } // end of namespace
  1940. #ifdef __cplusplus
  1941. extern "C"
  1942. {
  1943. #endif
  1944. static int
  1945. dither_opt (char c, DitherAlgorithm* dither) {
  1946. switch (c) {
  1947. case '-':
  1948. case 'n':
  1949. *dither = None;
  1950. break;
  1951. case 'r':
  1952. *dither = Rectangular;
  1953. break;
  1954. case 's':
  1955. *dither = Shaped;
  1956. break;
  1957. case 't':
  1958. *dither = Triangular;
  1959. break;
  1960. default:
  1961. fprintf (stderr, "ALSA driver: illegal dithering mode %c\n", c);
  1962. return -1;
  1963. }
  1964. return 0;
  1965. }
  1966. const jack_driver_desc_t* driver_get_descriptor () {
  1967. jack_driver_desc_t * desc;
  1968. jack_driver_param_desc_t * params;
  1969. unsigned int i;
  1970. desc = (jack_driver_desc_t*)calloc (1, sizeof (jack_driver_desc_t));
  1971. strcpy (desc->name, "alsa");
  1972. desc->nparams = 17;
  1973. params = (jack_driver_param_desc_t*)calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
  1974. i = 0;
  1975. strcpy (params[i].name, "capture");
  1976. params[i].character = 'C';
  1977. params[i].type = JackDriverParamString;
  1978. strcpy (params[i].value.str, "none");
  1979. strcpy (params[i].short_desc,
  1980. "Provide capture ports. Optionally set device");
  1981. strcpy (params[i].long_desc, params[i].short_desc);
  1982. i++;
  1983. strcpy (params[i].name, "playback");
  1984. params[i].character = 'P';
  1985. params[i].type = JackDriverParamString;
  1986. strcpy (params[i].value.str, "none");
  1987. strcpy (params[i].short_desc,
  1988. "Provide playback ports. Optionally set device");
  1989. strcpy (params[i].long_desc, params[i].short_desc);
  1990. i++;
  1991. strcpy (params[i].name, "device");
  1992. params[i].character = 'd';
  1993. params[i].type = JackDriverParamString;
  1994. strcpy (params[i].value.str, "hw:0");
  1995. strcpy (params[i].short_desc, "ALSA device name");
  1996. strcpy (params[i].long_desc, params[i].short_desc);
  1997. i++;
  1998. strcpy (params[i].name, "rate");
  1999. params[i].character = 'r';
  2000. params[i].type = JackDriverParamUInt;
  2001. params[i].value.ui = 48000U;
  2002. strcpy (params[i].short_desc, "Sample rate");
  2003. strcpy (params[i].long_desc, params[i].short_desc);
  2004. i++;
  2005. strcpy (params[i].name, "period");
  2006. params[i].character = 'p';
  2007. params[i].type = JackDriverParamUInt;
  2008. params[i].value.ui = 1024U;
  2009. strcpy (params[i].short_desc, "Frames per period");
  2010. strcpy (params[i].long_desc, params[i].short_desc);
  2011. i++;
  2012. strcpy (params[i].name, "nperiods");
  2013. params[i].character = 'n';
  2014. params[i].type = JackDriverParamUInt;
  2015. params[i].value.ui = 2U;
  2016. strcpy (params[i].short_desc, "Number of periods of playback latency");
  2017. strcpy (params[i].long_desc, params[i].short_desc);
  2018. i++;
  2019. strcpy (params[i].name, "hwmon");
  2020. params[i].character = 'H';
  2021. params[i].type = JackDriverParamBool;
  2022. params[i].value.i = 0;
  2023. strcpy (params[i].short_desc, "Hardware monitoring, if available");
  2024. strcpy (params[i].long_desc, params[i].short_desc);
  2025. i++;
  2026. strcpy (params[i].name, "hwmeter");
  2027. params[i].character = 'M';
  2028. params[i].type = JackDriverParamBool;
  2029. params[i].value.i = 0;
  2030. strcpy (params[i].short_desc, "Hardware metering, if available");
  2031. strcpy (params[i].long_desc, params[i].short_desc);
  2032. i++;
  2033. strcpy (params[i].name, "duplex");
  2034. params[i].character = 'D';
  2035. params[i].type = JackDriverParamBool;
  2036. params[i].value.i = 1;
  2037. strcpy (params[i].short_desc,
  2038. "Provide both capture and playback ports");
  2039. strcpy (params[i].long_desc, params[i].short_desc);
  2040. i++;
  2041. strcpy (params[i].name, "softmode");
  2042. params[i].character = 's';
  2043. params[i].type = JackDriverParamBool;
  2044. params[i].value.i = 0;
  2045. strcpy (params[i].short_desc, "Soft-mode, no xrun handling");
  2046. strcpy (params[i].long_desc, params[i].short_desc);
  2047. i++;
  2048. strcpy (params[i].name, "monitor");
  2049. params[i].character = 'm';
  2050. params[i].type = JackDriverParamBool;
  2051. params[i].value.i = 0;
  2052. strcpy (params[i].short_desc, "Provide monitor ports for the output");
  2053. strcpy (params[i].long_desc, params[i].short_desc);
  2054. i++;
  2055. strcpy (params[i].name, "dither");
  2056. params[i].character = 'z';
  2057. params[i].type = JackDriverParamChar;
  2058. params[i].value.c = 'n';
  2059. strcpy (params[i].short_desc, "Dithering mode");
  2060. strcpy (params[i].long_desc,
  2061. "Dithering mode:\n"
  2062. " n - none\n"
  2063. " r - rectangular\n"
  2064. " s - shaped\n"
  2065. " t - triangular");
  2066. i++;
  2067. strcpy (params[i].name, "inchannels");
  2068. params[i].character = 'i';
  2069. params[i].type = JackDriverParamUInt;
  2070. params[i].value.i = 0;
  2071. strcpy (params[i].short_desc,
  2072. "Number of capture channels (defaults to hardware max)");
  2073. strcpy (params[i].long_desc, params[i].short_desc);
  2074. i++;
  2075. strcpy (params[i].name, "outchannels");
  2076. params[i].character = 'o';
  2077. params[i].type = JackDriverParamUInt;
  2078. params[i].value.i = 0;
  2079. strcpy (params[i].short_desc,
  2080. "Number of playback channels (defaults to hardware max)");
  2081. strcpy (params[i].long_desc, params[i].short_desc);
  2082. i++;
  2083. strcpy (params[i].name, "shorts");
  2084. params[i].character = 'S';
  2085. params[i].type = JackDriverParamBool;
  2086. params[i].value.i = FALSE;
  2087. strcpy (params[i].short_desc, "Try 16-bit samples before 32-bit");
  2088. strcpy (params[i].long_desc, params[i].short_desc);
  2089. i++;
  2090. strcpy (params[i].name, "input-latency");
  2091. params[i].character = 'I';
  2092. params[i].type = JackDriverParamUInt;
  2093. params[i].value.i = 0;
  2094. strcpy (params[i].short_desc, "Extra input latency");
  2095. strcpy (params[i].long_desc, params[i].short_desc);
  2096. i++;
  2097. strcpy (params[i].name, "output-latency");
  2098. params[i].character = 'O';
  2099. params[i].type = JackDriverParamUInt;
  2100. params[i].value.i = 0;
  2101. strcpy (params[i].short_desc, "Extra output latency");
  2102. strcpy (params[i].long_desc, params[i].short_desc);
  2103. desc->params = params;
  2104. return desc;
  2105. }
  2106. Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngine* engine, Jack::JackSynchro** table, const JSList* params) {
  2107. jack_nframes_t srate = 48000;
  2108. jack_nframes_t frames_per_interrupt = 1024;
  2109. unsigned long user_nperiods = 2;
  2110. const char *playback_pcm_name = "hw:0";
  2111. const char *capture_pcm_name = "hw:0";
  2112. int hw_monitoring = FALSE;
  2113. int hw_metering = FALSE;
  2114. int capture = FALSE;
  2115. int playback = FALSE;
  2116. int soft_mode = FALSE;
  2117. int monitor = FALSE;
  2118. DitherAlgorithm dither = None;
  2119. int user_capture_nchnls = 0;
  2120. int user_playback_nchnls = 0;
  2121. int shorts_first = FALSE;
  2122. jack_nframes_t systemic_input_latency = 0;
  2123. jack_nframes_t systemic_output_latency = 0;
  2124. const JSList * node;
  2125. const jack_driver_param_t * param;
  2126. for (node = params; node; node = jack_slist_next (node)) {
  2127. param = (const jack_driver_param_t *) node->data;
  2128. switch (param->character) {
  2129. case 'C':
  2130. capture = TRUE;
  2131. if (strcmp (param->value.str, "none") != 0) {
  2132. capture_pcm_name = strdup (param->value.str);
  2133. printf("capture device %s\n", capture_pcm_name);
  2134. }
  2135. break;
  2136. case 'P':
  2137. playback = TRUE;
  2138. if (strcmp (param->value.str, "none") != 0) {
  2139. playback_pcm_name = strdup (param->value.str);
  2140. printf("playback device %s\n", playback_pcm_name);
  2141. }
  2142. break;
  2143. case 'D':
  2144. playback = TRUE;
  2145. capture = TRUE;
  2146. break;
  2147. case 'd':
  2148. playback_pcm_name = strdup (param->value.str);
  2149. capture_pcm_name = strdup (param->value.str);
  2150. printf("playback device %s\n", playback_pcm_name);
  2151. printf("capture device %s\n", capture_pcm_name);
  2152. break;
  2153. case 'H':
  2154. hw_monitoring = param->value.i;
  2155. break;
  2156. case 'm':
  2157. monitor = param->value.i;
  2158. break;
  2159. case 'M':
  2160. hw_metering = param->value.i;
  2161. break;
  2162. case 'r':
  2163. srate = param->value.ui;
  2164. printf("apparent rate = %d\n", srate);
  2165. break;
  2166. case 'p':
  2167. frames_per_interrupt = param->value.ui;
  2168. printf("frames per period = %d\n", frames_per_interrupt);
  2169. break;
  2170. case 'n':
  2171. user_nperiods = param->value.ui;
  2172. if (user_nperiods < 2) /* enforce minimum value */
  2173. user_nperiods = 2;
  2174. break;
  2175. case 's':
  2176. soft_mode = param->value.i;
  2177. break;
  2178. case 'z':
  2179. if (dither_opt (param->value.c, &dither)) {
  2180. return NULL;
  2181. }
  2182. break;
  2183. case 'i':
  2184. user_capture_nchnls = param->value.ui;
  2185. break;
  2186. case 'o':
  2187. user_playback_nchnls = param->value.ui;
  2188. break;
  2189. case 'S':
  2190. shorts_first = param->value.i;
  2191. break;
  2192. case 'I':
  2193. systemic_input_latency = param->value.ui;
  2194. break;
  2195. case 'O':
  2196. systemic_output_latency = param->value.ui;
  2197. break;
  2198. }
  2199. }
  2200. /* duplex is the default */
  2201. if (!capture && !playback) {
  2202. capture = TRUE;
  2203. playback = TRUE;
  2204. }
  2205. Jack::JackAlsaDriver* alsa_driver = new Jack::JackAlsaDriver("alsa_pcm", engine, table);
  2206. Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(alsa_driver);
  2207. // Special open for ALSA driver...
  2208. if (alsa_driver->Open(frames_per_interrupt, user_nperiods, srate, hw_monitoring, hw_metering, capture, playback, dither, soft_mode, monitor,
  2209. user_capture_nchnls, user_playback_nchnls, shorts_first, capture_pcm_name, playback_pcm_name,
  2210. systemic_input_latency, systemic_output_latency) == 0) {
  2211. return threaded_driver;
  2212. } else {
  2213. delete threaded_driver; // Delete the decorated driver
  2214. return NULL;
  2215. }
  2216. }
  2217. #ifdef __cplusplus
  2218. }
  2219. #endif