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.

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