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.

2423 lines
62KB

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