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.

2444 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. err = 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. if (err) {
  675. jack_error ("ALSA: cannot configure capture channel");
  676. return -1;
  677. }
  678. }
  679. if (driver->playback_handle) {
  680. err = alsa_driver_configure_stream (
  681. driver,
  682. driver->alsa_name_playback,
  683. "playback",
  684. driver->playback_handle,
  685. driver->playback_hw_params,
  686. driver->playback_sw_params,
  687. &driver->playback_nperiods,
  688. &driver->playback_nchannels,
  689. driver->playback_sample_bytes);
  690. if (err) {
  691. jack_error ("ALSA: cannot configure playback channel");
  692. return -1;
  693. }
  694. }
  695. /* check the rate, since that's rather important */
  696. if (driver->playback_handle) {
  697. snd_pcm_hw_params_get_rate (driver->playback_hw_params,
  698. &pr, &dir);
  699. }
  700. if (driver->capture_handle) {
  701. snd_pcm_hw_params_get_rate (driver->capture_hw_params,
  702. &cr, &dir);
  703. }
  704. if (driver->capture_handle && driver->playback_handle) {
  705. if (cr != pr) {
  706. jack_error ("playback and capture sample rates do "
  707. "not match (%d vs. %d)", pr, cr);
  708. }
  709. /* only change if *both* capture and playback rates
  710. * don't match requested certain hardware actually
  711. * still works properly in full-duplex with slightly
  712. * different rate values between adc and dac
  713. */
  714. if (cr != driver->frame_rate && pr != driver->frame_rate) {
  715. jack_error ("sample rate in use (%d Hz) does not "
  716. "match requested rate (%d Hz)",
  717. cr, driver->frame_rate);
  718. driver->frame_rate = cr;
  719. }
  720. }
  721. else if (driver->capture_handle && cr != driver->frame_rate) {
  722. jack_error ("capture sample rate in use (%d Hz) does not "
  723. "match requested rate (%d Hz)",
  724. cr, driver->frame_rate);
  725. driver->frame_rate = cr;
  726. }
  727. else if (driver->playback_handle && pr != driver->frame_rate) {
  728. jack_error ("playback sample rate in use (%d Hz) does not "
  729. "match requested rate (%d Hz)",
  730. pr, driver->frame_rate);
  731. driver->frame_rate = pr;
  732. }
  733. /* check the fragment size, since that's non-negotiable */
  734. if (driver->playback_handle) {
  735. snd_pcm_access_t access;
  736. err = snd_pcm_hw_params_get_period_size (
  737. driver->playback_hw_params, &p_period_size, &dir);
  738. err = snd_pcm_hw_params_get_format (
  739. driver->playback_hw_params,
  740. &(driver->playback_sample_format));
  741. err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
  742. &access);
  743. driver->playback_interleaved =
  744. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  745. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  746. if (p_period_size != driver->frames_per_cycle) {
  747. jack_error ("alsa_pcm: requested an interrupt every %"
  748. PRIu32
  749. " frames but got %u frames for playback",
  750. driver->frames_per_cycle, p_period_size);
  751. return -1;
  752. }
  753. }
  754. if (driver->capture_handle) {
  755. snd_pcm_access_t access;
  756. err = snd_pcm_hw_params_get_period_size (
  757. driver->capture_hw_params, &c_period_size, &dir);
  758. err = snd_pcm_hw_params_get_format (
  759. driver->capture_hw_params,
  760. &(driver->capture_sample_format));
  761. err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
  762. &access);
  763. driver->capture_interleaved =
  764. (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
  765. || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
  766. if (c_period_size != driver->frames_per_cycle) {
  767. jack_error ("alsa_pcm: requested an interrupt every %"
  768. PRIu32
  769. " frames but got %u frames for capture",
  770. driver->frames_per_cycle, c_period_size);
  771. return -1;
  772. }
  773. }
  774. alsa_driver_set_sample_bytes(driver);
  775. if (driver->playback_handle) {
  776. err = alsa_driver_check_format(driver->playback_sample_format);
  777. if(err < 0) {
  778. jack_error ("programming error: unhandled format "
  779. "type for playback");
  780. return -1;
  781. }
  782. }
  783. if (driver->capture_handle) {
  784. err = alsa_driver_check_format(driver->capture_sample_format);
  785. if(err < 0) {
  786. jack_error ("programming error: unhandled format "
  787. "type for capture");
  788. return -1;
  789. }
  790. }
  791. if (driver->playback_interleaved) {
  792. const snd_pcm_channel_area_t *my_areas;
  793. snd_pcm_uframes_t offset, frames;
  794. if (snd_pcm_mmap_begin(driver->playback_handle,
  795. &my_areas, &offset, &frames) < 0) {
  796. jack_error ("ALSA: %s: mmap areas info error",
  797. driver->alsa_name_playback);
  798. return -1;
  799. }
  800. driver->interleave_unit =
  801. snd_pcm_format_physical_width (
  802. driver->playback_sample_format) / 8;
  803. } else {
  804. driver->interleave_unit = 0; /* NOT USED */
  805. }
  806. if (driver->capture_interleaved) {
  807. const snd_pcm_channel_area_t *my_areas;
  808. snd_pcm_uframes_t offset, frames;
  809. if (snd_pcm_mmap_begin(driver->capture_handle,
  810. &my_areas, &offset, &frames) < 0) {
  811. jack_error ("ALSA: %s: mmap areas info error",
  812. driver->alsa_name_capture);
  813. return -1;
  814. }
  815. }
  816. if (driver->playback_nchannels > driver->capture_nchannels) {
  817. driver->max_nchannels = driver->playback_nchannels;
  818. } else {
  819. driver->max_nchannels = driver->capture_nchannels;
  820. }
  821. alsa_driver_setup_io_function_pointers (driver);
  822. /* Allocate and initialize structures that rely on the
  823. channels counts.
  824. Set up the bit pattern that is used to record which
  825. channels require action on every cycle. any bits that are
  826. not set after the engine's process() call indicate channels
  827. that potentially need to be silenced.
  828. */
  829. bitset_create (&driver->channels_done, driver->max_nchannels);
  830. bitset_create (&driver->channels_not_done, driver->max_nchannels);
  831. if (driver->playback_handle) {
  832. driver->playback_addr = (char **)
  833. malloc (sizeof (char *) * driver->playback_nchannels);
  834. memset (driver->playback_addr, 0,
  835. sizeof (char *) * driver->playback_nchannels);
  836. driver->playback_interleave_skip = (unsigned long *)
  837. malloc (sizeof (unsigned long *) * driver->playback_nchannels);
  838. memset (driver->playback_interleave_skip, 0,
  839. sizeof (unsigned long *) * driver->playback_nchannels);
  840. driver->silent = (unsigned long *)
  841. malloc (sizeof (unsigned long)
  842. * driver->playback_nchannels);
  843. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  844. driver->silent[chn] = 0;
  845. }
  846. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  847. bitset_add (driver->channels_done, chn);
  848. }
  849. driver->dither_state = (dither_state_t *)
  850. calloc ( driver->playback_nchannels,
  851. sizeof (dither_state_t));
  852. }
  853. if (driver->capture_handle) {
  854. driver->capture_addr = (char **)
  855. malloc (sizeof (char *) * driver->capture_nchannels);
  856. memset (driver->capture_addr, 0,
  857. sizeof (char *) * driver->capture_nchannels);
  858. driver->capture_interleave_skip = (unsigned long *)
  859. malloc (sizeof (unsigned long *) * driver->capture_nchannels);
  860. memset (driver->capture_interleave_skip, 0,
  861. sizeof (unsigned long *) * driver->capture_nchannels);
  862. }
  863. driver->clock_sync_data = (ClockSyncStatus *)
  864. malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
  865. driver->period_usecs =
  866. (jack_time_t) floor ((((float) driver->frames_per_cycle) /
  867. driver->frame_rate) * 1000000.0f);
  868. driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
  869. // JACK2
  870. /*
  871. if (driver->engine) {
  872. if (driver->engine->set_buffer_size (driver->engine,
  873. driver->frames_per_cycle)) {
  874. jack_error ("ALSA: Cannot set engine buffer size to %d (check MIDI)", driver->frames_per_cycle);
  875. return -1;
  876. }
  877. }
  878. */
  879. return 0;
  880. // may be unused
  881. (void)err;
  882. }
  883. int
  884. alsa_driver_reset_parameters (alsa_driver_t *driver,
  885. jack_nframes_t frames_per_cycle,
  886. jack_nframes_t user_nperiods,
  887. jack_nframes_t rate)
  888. {
  889. /* XXX unregister old ports ? */
  890. alsa_driver_release_channel_dependent_memory (driver);
  891. return alsa_driver_set_parameters (driver,
  892. frames_per_cycle,
  893. user_nperiods, rate);
  894. }
  895. static int
  896. alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  897. snd_pcm_uframes_t *capture_avail,
  898. snd_pcm_uframes_t *playback_avail,
  899. snd_pcm_uframes_t *capture_offset,
  900. snd_pcm_uframes_t *playback_offset)
  901. {
  902. int err;
  903. channel_t chn;
  904. if (capture_avail) {
  905. if ((err = snd_pcm_mmap_begin (
  906. driver->capture_handle, &driver->capture_areas,
  907. (snd_pcm_uframes_t *) capture_offset,
  908. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  909. jack_error ("ALSA: %s: mmap areas info error",
  910. driver->alsa_name_capture);
  911. return -1;
  912. }
  913. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  914. const snd_pcm_channel_area_t *a =
  915. &driver->capture_areas[chn];
  916. driver->capture_addr[chn] = (char *) a->addr
  917. + ((a->first + a->step * *capture_offset) / 8);
  918. driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  919. }
  920. }
  921. if (playback_avail) {
  922. if ((err = snd_pcm_mmap_begin (
  923. driver->playback_handle, &driver->playback_areas,
  924. (snd_pcm_uframes_t *) playback_offset,
  925. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  926. jack_error ("ALSA: %s: mmap areas info error ",
  927. driver->alsa_name_playback);
  928. return -1;
  929. }
  930. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  931. const snd_pcm_channel_area_t *a =
  932. &driver->playback_areas[chn];
  933. driver->playback_addr[chn] = (char *) a->addr
  934. + ((a->first + a->step * *playback_offset) / 8);
  935. driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
  936. }
  937. }
  938. return 0;
  939. }
  940. static int
  941. alsa_driver_stream_start(snd_pcm_t *pcm, bool is_capture)
  942. {
  943. return snd_pcm_start(pcm);
  944. }
  945. int
  946. alsa_driver_start (alsa_driver_t *driver)
  947. {
  948. int err;
  949. snd_pcm_uframes_t poffset, pavail;
  950. channel_t chn;
  951. driver->poll_last = 0;
  952. driver->poll_next = 0;
  953. if (driver->playback_handle) {
  954. if ((err = alsa_driver_prepare (driver->playback_handle, SND_PCM_STREAM_PLAYBACK)) < 0) {
  955. jack_error ("ALSA: prepare error for playback on "
  956. "\"%s\" (%s)", driver->alsa_name_playback,
  957. snd_strerror(err));
  958. return -1;
  959. }
  960. }
  961. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  962. || !driver->playback_handle) {
  963. if ((err = alsa_driver_prepare (driver->capture_handle, SND_PCM_STREAM_CAPTURE)) < 0) {
  964. jack_error ("ALSA: prepare error for capture on \"%s\""
  965. " (%s)", driver->alsa_name_capture,
  966. snd_strerror(err));
  967. return -1;
  968. }
  969. }
  970. if (driver->hw_monitoring) {
  971. if (driver->input_monitor_mask || driver->all_monitor_in) {
  972. if (driver->all_monitor_in) {
  973. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  974. } else {
  975. driver->hw->set_input_monitor_mask (
  976. driver->hw, driver->input_monitor_mask);
  977. }
  978. } else {
  979. driver->hw->set_input_monitor_mask (driver->hw,
  980. driver->input_monitor_mask);
  981. }
  982. }
  983. if (driver->playback_handle) {
  984. driver->playback_nfds =
  985. snd_pcm_poll_descriptors_count (driver->playback_handle);
  986. } else {
  987. driver->playback_nfds = 0;
  988. }
  989. if (driver->capture_handle) {
  990. driver->capture_nfds =
  991. snd_pcm_poll_descriptors_count (driver->capture_handle);
  992. } else {
  993. driver->capture_nfds = 0;
  994. }
  995. if (driver->pfd) {
  996. free (driver->pfd);
  997. }
  998. driver->pfd = (struct pollfd *)
  999. malloc (sizeof (struct pollfd) *
  1000. (driver->playback_nfds + driver->capture_nfds + 2));
  1001. if (driver->midi && !driver->xrun_recovery)
  1002. (driver->midi->start)(driver->midi);
  1003. if (driver->playback_handle) {
  1004. const jack_nframes_t silence_frames = driver->frames_per_cycle *
  1005. driver->playback_nperiods;
  1006. /* fill playback buffer with zeroes, and mark
  1007. all fragments as having data.
  1008. */
  1009. pavail = snd_pcm_avail_update (driver->playback_handle);
  1010. if (pavail != silence_frames) {
  1011. jack_error ("ALSA: full buffer not available at start");
  1012. return -1;
  1013. }
  1014. if (alsa_driver_get_channel_addresses (driver,
  1015. 0, &pavail, 0, &poffset)) {
  1016. return -1;
  1017. }
  1018. /* XXX this is cheating. ALSA offers no guarantee that
  1019. we can access the entire buffer at any one time. It
  1020. works on most hardware tested so far, however, buts
  1021. its a liability in the long run. I think that
  1022. alsa-lib may have a better function for doing this
  1023. here, where the goal is to silence the entire
  1024. buffer.
  1025. */
  1026. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  1027. alsa_driver_silence_on_channel (
  1028. driver, chn, silence_frames);
  1029. }
  1030. snd_pcm_mmap_commit (driver->playback_handle, poffset, silence_frames);
  1031. if ((err = alsa_driver_stream_start (driver->playback_handle, SND_PCM_STREAM_PLAYBACK)) < 0) {
  1032. jack_error ("ALSA: could not start playback (%s)",
  1033. snd_strerror (err));
  1034. return -1;
  1035. }
  1036. }
  1037. if ((driver->capture_handle && driver->capture_and_playback_not_synced)
  1038. || !driver->playback_handle) {
  1039. if ((err = alsa_driver_stream_start (driver->capture_handle, SND_PCM_STREAM_CAPTURE)) < 0) {
  1040. jack_error ("ALSA: could not start capture (%s)",
  1041. snd_strerror (err));
  1042. return -1;
  1043. }
  1044. }
  1045. return 0;
  1046. }
  1047. int
  1048. alsa_driver_stop (alsa_driver_t *driver)
  1049. {
  1050. int err;
  1051. // JSList* node;
  1052. // int chn;
  1053. /* silence all capture port buffers, because we might
  1054. be entering offline mode.
  1055. */
  1056. // JACK2
  1057. /*
  1058. for (chn = 0, node = driver->capture_ports; node;
  1059. node = jack_slist_next (node), chn++) {
  1060. jack_port_t* port;
  1061. char* buf;
  1062. jack_nframes_t nframes = driver->engine->control->buffer_size;
  1063. port = (jack_port_t *) node->data;
  1064. buf = jack_port_get_buffer (port, nframes);
  1065. memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
  1066. }
  1067. */
  1068. // JACK2
  1069. ClearOutput();
  1070. if (driver->playback_handle) {
  1071. err = snd_pcm_drop (driver->playback_handle);
  1072. if (err < 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. err = snd_pcm_drop (driver->capture_handle);
  1082. if (err < 0) {
  1083. jack_error ("ALSA: channel flush for "
  1084. "capture failed (%s)",
  1085. snd_strerror (err));
  1086. return -1;
  1087. }
  1088. }
  1089. }
  1090. if (driver->hw_monitoring) {
  1091. driver->hw->set_input_monitor_mask (driver->hw, 0);
  1092. }
  1093. if (driver->midi && !driver->xrun_recovery)
  1094. (driver->midi->stop)(driver->midi);
  1095. return 0;
  1096. }
  1097. static int
  1098. alsa_driver_restart (alsa_driver_t *driver)
  1099. {
  1100. int res;
  1101. driver->xrun_recovery = 1;
  1102. // JACK2
  1103. /*
  1104. if ((res = driver->nt_stop((struct _jack_driver_nt *) driver))==0)
  1105. res = driver->nt_start((struct _jack_driver_nt *) driver);
  1106. */
  1107. res = Restart();
  1108. driver->xrun_recovery = 0;
  1109. if (res && driver->midi)
  1110. (driver->midi->stop)(driver->midi);
  1111. return res;
  1112. }
  1113. static int
  1114. alsa_driver_get_status (alsa_driver_t *driver)
  1115. {
  1116. int res;
  1117. snd_pcm_t *pcm_handle;
  1118. snd_pcm_status_t *status;
  1119. snd_pcm_status_alloca(&status);
  1120. if (driver->capture_handle) {
  1121. pcm_handle = driver->capture_handle;
  1122. } else {
  1123. pcm_handle = driver->playback_handle;
  1124. }
  1125. res = snd_pcm_status(pcm_handle, status);
  1126. if (res < 0) {
  1127. jack_error("status error: %s", snd_strerror(res));
  1128. return -1;
  1129. }
  1130. return snd_pcm_status_get_state(status);
  1131. }
  1132. static int
  1133. alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
  1134. {
  1135. int status;
  1136. int res;
  1137. status = alsa_driver_get_status(driver);
  1138. if (status == SND_PCM_STATE_SUSPENDED) {
  1139. if (driver->capture_handle) {
  1140. if ((res = alsa_driver_prepare(driver->capture_handle, SND_PCM_STREAM_CAPTURE))
  1141. < 0) {
  1142. jack_error("error preparing after suspend: %s", snd_strerror(res));
  1143. }
  1144. }
  1145. if (driver->playback_handle) {
  1146. if ((res = alsa_driver_prepare(driver->playback_handle, SND_PCM_STREAM_PLAYBACK))
  1147. < 0) {
  1148. jack_error("error preparing after suspend: %s", snd_strerror(res));
  1149. }
  1150. }
  1151. }
  1152. if (status == SND_PCM_STATE_XRUN
  1153. && driver->process_count > XRUN_REPORT_DELAY) {
  1154. struct timeval now, diff, tstamp;
  1155. driver->xrun_count++;
  1156. snd_pcm_status_get_tstamp(status,&now);
  1157. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  1158. timersub(&now, &tstamp, &diff);
  1159. *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
  1160. jack_log("**** alsa_pcm: xrun of at least %.3f msecs",*delayed_usecs / 1000.0);
  1161. if (driver->capture_handle) {
  1162. jack_log("Repreparing capture");
  1163. if ((res = alsa_driver_prepare(driver->capture_handle,
  1164. SND_PCM_STREAM_CAPTURE)) < 0) {
  1165. jack_error("error preparing after xrun: %s", snd_strerror(res));
  1166. }
  1167. }
  1168. if (driver->playback_handle) {
  1169. jack_log("Repreparing playback");
  1170. if ((res = alsa_driver_prepare(driver->playback_handle,
  1171. SND_PCM_STREAM_PLAYBACK)) < 0) {
  1172. jack_error("error preparing after xrun: %s", snd_strerror(res));
  1173. }
  1174. }
  1175. }
  1176. if (alsa_driver_restart (driver)) {
  1177. return -1;
  1178. }
  1179. return 0;
  1180. }
  1181. static void
  1182. alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
  1183. jack_nframes_t nframes)
  1184. {
  1185. channel_t chn;
  1186. jack_nframes_t buffer_frames =
  1187. driver->frames_per_cycle * driver->playback_nperiods;
  1188. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  1189. if (bitset_contains (driver->channels_not_done, chn)) {
  1190. if (driver->silent[chn] < buffer_frames) {
  1191. alsa_driver_silence_on_channel_no_mark (
  1192. driver, chn, nframes);
  1193. driver->silent[chn] += nframes;
  1194. }
  1195. }
  1196. }
  1197. }
  1198. static int
  1199. alsa_driver_poll_descriptors(snd_pcm_t *pcm, struct pollfd *pfds, unsigned int space, bool is_capture)
  1200. {
  1201. return snd_pcm_poll_descriptors(pcm, pfds, space);
  1202. }
  1203. static snd_pcm_sframes_t
  1204. alsa_driver_avail(alsa_driver_t *driver, snd_pcm_t *pcm, bool is_capture)
  1205. {
  1206. return snd_pcm_avail_update(pcm);
  1207. }
  1208. static int under_gdb = FALSE;
  1209. jack_nframes_t
  1210. alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
  1211. *delayed_usecs)
  1212. {
  1213. snd_pcm_sframes_t avail = 0;
  1214. snd_pcm_sframes_t capture_avail = 0;
  1215. snd_pcm_sframes_t playback_avail = 0;
  1216. int xrun_detected = FALSE;
  1217. int need_capture;
  1218. int need_playback;
  1219. int retry_cnt = 0;
  1220. unsigned int i;
  1221. jack_time_t poll_enter;
  1222. jack_time_t poll_ret = 0;
  1223. *status = -1;
  1224. *delayed_usecs = 0;
  1225. need_capture = driver->capture_handle ? 1 : 0;
  1226. if (extra_fd >= 0) {
  1227. need_playback = 0;
  1228. } else {
  1229. need_playback = driver->playback_handle ? 1 : 0;
  1230. }
  1231. again:
  1232. while ((need_playback || need_capture) && !xrun_detected) {
  1233. int poll_result;
  1234. unsigned int ci = 0;
  1235. unsigned int nfds;
  1236. unsigned short revents;
  1237. nfds = 0;
  1238. if (need_playback) {
  1239. alsa_driver_poll_descriptors (driver->playback_handle,
  1240. &driver->pfd[0],
  1241. driver->playback_nfds, SND_PCM_STREAM_PLAYBACK);
  1242. nfds += driver->playback_nfds;
  1243. }
  1244. if (need_capture) {
  1245. alsa_driver_poll_descriptors (driver->capture_handle,
  1246. &driver->pfd[nfds],
  1247. driver->capture_nfds, SND_PCM_STREAM_CAPTURE);
  1248. ci = nfds;
  1249. nfds += driver->capture_nfds;
  1250. }
  1251. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  1252. for (i = 0; i < nfds; i++) {
  1253. driver->pfd[i].events |= POLLERR;
  1254. }
  1255. if (extra_fd >= 0) {
  1256. driver->pfd[nfds].fd = extra_fd;
  1257. driver->pfd[nfds].events =
  1258. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  1259. nfds++;
  1260. }
  1261. poll_enter = jack_get_microseconds ();
  1262. if (poll_enter > driver->poll_next) {
  1263. /*
  1264. * This processing cycle was delayed past the
  1265. * next due interrupt! Do not account this as
  1266. * a wakeup delay:
  1267. */
  1268. driver->poll_next = 0;
  1269. driver->poll_late++;
  1270. }
  1271. #ifdef __ANDROID__
  1272. poll_result = poll (driver->pfd, nfds, -1); //fix for sleep issue
  1273. #else
  1274. poll_result = poll (driver->pfd, nfds, driver->poll_timeout);
  1275. #endif
  1276. if (poll_result < 0) {
  1277. if (errno == EINTR) {
  1278. const char poll_log[] = "ALSA: poll interrupt";
  1279. // this happens mostly when run
  1280. // under gdb, or when exiting due to a signal
  1281. if (under_gdb) {
  1282. jack_info(poll_log);
  1283. goto again;
  1284. }
  1285. jack_error(poll_log);
  1286. *status = -2;
  1287. return 0;
  1288. }
  1289. jack_error ("ALSA: poll call failed (%s)",
  1290. strerror (errno));
  1291. *status = -3;
  1292. return 0;
  1293. }
  1294. poll_ret = jack_get_microseconds ();
  1295. if (poll_result == 0) {
  1296. retry_cnt++;
  1297. if(retry_cnt > MAX_RETRY_COUNT) {
  1298. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1299. " usecs, Reached max retry cnt = %d, Exiting",
  1300. poll_ret - poll_enter, MAX_RETRY_COUNT);
  1301. *status = -5;
  1302. return 0;
  1303. }
  1304. jack_error ("ALSA: poll time out, polled for %" PRIu64
  1305. " usecs, Retrying with a recovery, retry cnt = %d",
  1306. poll_ret - poll_enter, retry_cnt);
  1307. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1308. if(*status != 0) {
  1309. jack_error ("ALSA: poll time out, recovery failed with status = %d", *status);
  1310. return 0;
  1311. }
  1312. }
  1313. // JACK2
  1314. SetTime(poll_ret);
  1315. if (extra_fd < 0) {
  1316. if (driver->poll_next && poll_ret > driver->poll_next) {
  1317. *delayed_usecs = poll_ret - driver->poll_next;
  1318. }
  1319. driver->poll_last = poll_ret;
  1320. driver->poll_next = poll_ret + driver->period_usecs;
  1321. // JACK2
  1322. /*
  1323. driver->engine->transport_cycle_start (driver->engine,
  1324. poll_ret);
  1325. */
  1326. }
  1327. #ifdef DEBUG_WAKEUP
  1328. fprintf (stderr, "%" PRIu64 ": checked %d fds, started at %" PRIu64 " %" PRIu64 " usecs since poll entered\n",
  1329. poll_ret, nfds, poll_enter, poll_ret - poll_enter);
  1330. #endif
  1331. /* check to see if it was the extra FD that caused us
  1332. * to return from poll */
  1333. if (extra_fd >= 0) {
  1334. if (driver->pfd[nfds-1].revents == 0) {
  1335. /* we timed out on the extra fd */
  1336. *status = -4;
  1337. return -1;
  1338. }
  1339. /* if POLLIN was the only bit set, we're OK */
  1340. *status = 0;
  1341. return (driver->pfd[nfds-1].revents == POLLIN) ? 0 : -1;
  1342. }
  1343. if (need_playback) {
  1344. if (snd_pcm_poll_descriptors_revents
  1345. (driver->playback_handle, &driver->pfd[0],
  1346. driver->playback_nfds, &revents) < 0) {
  1347. jack_error ("ALSA: playback revents failed");
  1348. *status = -6;
  1349. return 0;
  1350. }
  1351. if (revents & POLLNVAL) {
  1352. jack_error ("ALSA: playback device disconnected");
  1353. *status = -7;
  1354. return 0;
  1355. }
  1356. if (revents & POLLERR) {
  1357. xrun_detected = TRUE;
  1358. }
  1359. if (revents & POLLOUT) {
  1360. need_playback = 0;
  1361. #ifdef DEBUG_WAKEUP
  1362. fprintf (stderr, "%" PRIu64
  1363. " playback stream ready\n",
  1364. poll_ret);
  1365. #endif
  1366. }
  1367. }
  1368. if (need_capture) {
  1369. if (snd_pcm_poll_descriptors_revents
  1370. (driver->capture_handle, &driver->pfd[ci],
  1371. driver->capture_nfds, &revents) < 0) {
  1372. jack_error ("ALSA: capture revents failed");
  1373. *status = -6;
  1374. return 0;
  1375. }
  1376. if (revents & POLLNVAL) {
  1377. jack_error ("ALSA: capture device disconnected");
  1378. *status = -7;
  1379. return 0;
  1380. }
  1381. if (revents & POLLERR) {
  1382. xrun_detected = TRUE;
  1383. }
  1384. if (revents & POLLIN) {
  1385. need_capture = 0;
  1386. #ifdef DEBUG_WAKEUP
  1387. fprintf (stderr, "%" PRIu64
  1388. " capture stream ready\n",
  1389. poll_ret);
  1390. #endif
  1391. }
  1392. }
  1393. }
  1394. if (driver->capture_handle) {
  1395. if ((capture_avail = alsa_driver_avail (driver,
  1396. driver->capture_handle, SND_PCM_STREAM_CAPTURE)) < 0) {
  1397. if (capture_avail == -EPIPE) {
  1398. xrun_detected = TRUE;
  1399. } else {
  1400. jack_error ("unknown ALSA avail_update return"
  1401. " value (%u)", capture_avail);
  1402. }
  1403. }
  1404. } else {
  1405. /* odd, but see min() computation below */
  1406. capture_avail = INT_MAX;
  1407. }
  1408. if (driver->playback_handle) {
  1409. if ((playback_avail = alsa_driver_avail (driver,
  1410. driver->playback_handle, SND_PCM_STREAM_PLAYBACK)) < 0) {
  1411. if (playback_avail == -EPIPE) {
  1412. xrun_detected = TRUE;
  1413. } else {
  1414. jack_error ("unknown ALSA avail_update return"
  1415. " value (%u)", playback_avail);
  1416. }
  1417. }
  1418. } else {
  1419. /* odd, but see min() computation below */
  1420. playback_avail = INT_MAX;
  1421. }
  1422. if (xrun_detected) {
  1423. *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
  1424. return 0;
  1425. }
  1426. *status = 0;
  1427. driver->last_wait_ust = poll_ret;
  1428. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  1429. #ifdef DEBUG_WAKEUP
  1430. fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
  1431. "cavail = %lu\n",
  1432. avail, playback_avail, capture_avail);
  1433. #endif
  1434. /* mark all channels not done for now. read/write will change this */
  1435. bitset_copy (driver->channels_not_done, driver->channels_done);
  1436. /* constrain the available count to the nearest (round down) number of
  1437. periods.
  1438. */
  1439. return avail - (avail % driver->frames_per_cycle);
  1440. }
  1441. int
  1442. alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
  1443. {
  1444. snd_pcm_sframes_t contiguous;
  1445. snd_pcm_sframes_t nread;
  1446. snd_pcm_uframes_t offset;
  1447. jack_nframes_t orig_nframes;
  1448. // jack_default_audio_sample_t* buf;
  1449. // channel_t chn;
  1450. // JSList *node;
  1451. // jack_port_t* port;
  1452. int err;
  1453. if (nframes > driver->frames_per_cycle) {
  1454. return -1;
  1455. }
  1456. // JACK2
  1457. /*
  1458. if (driver->engine->freewheeling) {
  1459. return 0;
  1460. }
  1461. */
  1462. if (driver->midi)
  1463. (driver->midi->read)(driver->midi, nframes);
  1464. if (!driver->capture_handle) {
  1465. return 0;
  1466. }
  1467. nread = 0;
  1468. contiguous = 0;
  1469. orig_nframes = nframes;
  1470. while (nframes) {
  1471. contiguous = nframes;
  1472. if (alsa_driver_get_channel_addresses (
  1473. driver,
  1474. (snd_pcm_uframes_t *) &contiguous,
  1475. (snd_pcm_uframes_t *) 0,
  1476. &offset, 0) < 0) {
  1477. return -1;
  1478. }
  1479. // JACK2
  1480. /*
  1481. for (chn = 0, node = driver->capture_ports; node;
  1482. node = jack_slist_next (node), chn++) {
  1483. port = (jack_port_t *) node->data;
  1484. if (!jack_port_connected (port)) {
  1485. // no-copy optimization
  1486. continue;
  1487. }
  1488. buf = jack_port_get_buffer (port, orig_nframes);
  1489. alsa_driver_read_from_channel (driver, chn,
  1490. buf + nread, contiguous);
  1491. }
  1492. */
  1493. ReadInput(orig_nframes, contiguous, nread);
  1494. if ((err = snd_pcm_mmap_commit (driver->capture_handle,
  1495. offset, contiguous)) < 0) {
  1496. jack_error ("ALSA: could not complete read of %"
  1497. PRIu32 " frames: error = %d", contiguous, err);
  1498. return -1;
  1499. }
  1500. nframes -= contiguous;
  1501. nread += contiguous;
  1502. }
  1503. return 0;
  1504. }
  1505. int
  1506. alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
  1507. {
  1508. // channel_t chn;
  1509. // JSList *node;
  1510. // JSList *mon_node;
  1511. // jack_default_audio_sample_t* buf;
  1512. // jack_default_audio_sample_t* monbuf;
  1513. jack_nframes_t orig_nframes;
  1514. snd_pcm_sframes_t nwritten;
  1515. snd_pcm_sframes_t contiguous;
  1516. snd_pcm_uframes_t offset;
  1517. // jack_port_t *port;
  1518. int err;
  1519. driver->process_count++;
  1520. // JACK2
  1521. /*
  1522. if (!driver->playback_handle || driver->engine->freewheeling) {
  1523. return 0;
  1524. }
  1525. */
  1526. if (!driver->playback_handle) {
  1527. return 0;
  1528. }
  1529. if (nframes > driver->frames_per_cycle) {
  1530. return -1;
  1531. }
  1532. if (driver->midi)
  1533. (driver->midi->write)(driver->midi, nframes);
  1534. nwritten = 0;
  1535. contiguous = 0;
  1536. orig_nframes = nframes;
  1537. /* check current input monitor request status */
  1538. driver->input_monitor_mask = 0;
  1539. // JACK2
  1540. /*
  1541. for (chn = 0, node = driver->capture_ports; node;
  1542. node = jack_slist_next (node), chn++) {
  1543. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  1544. driver->input_monitor_mask |= (1<<chn);
  1545. }
  1546. }
  1547. */
  1548. MonitorInput();
  1549. if (driver->hw_monitoring) {
  1550. if ((driver->hw->input_monitor_mask
  1551. != driver->input_monitor_mask)
  1552. && !driver->all_monitor_in) {
  1553. driver->hw->set_input_monitor_mask (
  1554. driver->hw, driver->input_monitor_mask);
  1555. }
  1556. }
  1557. while (nframes) {
  1558. contiguous = nframes;
  1559. if (alsa_driver_get_channel_addresses (
  1560. driver,
  1561. (snd_pcm_uframes_t *) 0,
  1562. (snd_pcm_uframes_t *) &contiguous,
  1563. 0, &offset) < 0) {
  1564. return -1;
  1565. }
  1566. // JACK2
  1567. /*
  1568. for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
  1569. node;
  1570. node = jack_slist_next (node), chn++) {
  1571. port = (jack_port_t *) node->data;
  1572. if (!jack_port_connected (port)) {
  1573. continue;
  1574. }
  1575. buf = jack_port_get_buffer (port, orig_nframes);
  1576. alsa_driver_write_to_channel (driver, chn,
  1577. buf + nwritten, contiguous);
  1578. if (mon_node) {
  1579. port = (jack_port_t *) mon_node->data;
  1580. if (!jack_port_connected (port)) {
  1581. continue;
  1582. }
  1583. monbuf = jack_port_get_buffer (port, orig_nframes);
  1584. memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
  1585. mon_node = jack_slist_next (mon_node);
  1586. }
  1587. }
  1588. */
  1589. // JACK2
  1590. WriteOutput(orig_nframes, contiguous, nwritten);
  1591. if (!bitset_empty (driver->channels_not_done)) {
  1592. alsa_driver_silence_untouched_channels (driver,
  1593. contiguous);
  1594. }
  1595. if ((err = snd_pcm_mmap_commit (driver->playback_handle,
  1596. offset, contiguous)) < 0) {
  1597. jack_error ("ALSA: could not complete playback of %"
  1598. PRIu32 " frames: error = %d", contiguous, err);
  1599. if (err != -EPIPE && err != -ESTRPIPE)
  1600. return -1;
  1601. }
  1602. nframes -= contiguous;
  1603. nwritten += contiguous;
  1604. }
  1605. return 0;
  1606. }
  1607. #if 0
  1608. static int /* UNUSED */
  1609. alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
  1610. {
  1611. return driver->hw->change_sample_clock (driver->hw, mode);
  1612. }
  1613. static void /* UNUSED */
  1614. alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
  1615. {
  1616. if (driver->hw_monitoring) {
  1617. if (yn) {
  1618. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1619. } else {
  1620. driver->hw->set_input_monitor_mask (
  1621. driver->hw, driver->input_monitor_mask);
  1622. }
  1623. }
  1624. driver->all_monitor_in = yn;
  1625. }
  1626. static void /* UNUSED */
  1627. alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
  1628. {
  1629. if (yn) {
  1630. driver->hw_monitoring = TRUE;
  1631. if (driver->all_monitor_in) {
  1632. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  1633. } else {
  1634. driver->hw->set_input_monitor_mask (
  1635. driver->hw, driver->input_monitor_mask);
  1636. }
  1637. } else {
  1638. driver->hw_monitoring = FALSE;
  1639. driver->hw->set_input_monitor_mask (driver->hw, 0);
  1640. }
  1641. }
  1642. static ClockSyncStatus /* UNUSED */
  1643. alsa_driver_clock_sync_status (channel_t chn)
  1644. {
  1645. return Lock;
  1646. }
  1647. #endif
  1648. void
  1649. alsa_driver_delete (alsa_driver_t *driver)
  1650. {
  1651. JSList *node;
  1652. if (driver->midi)
  1653. (driver->midi->destroy)(driver->midi);
  1654. for (node = driver->clock_sync_listeners; node;
  1655. node = jack_slist_next (node)) {
  1656. free (node->data);
  1657. }
  1658. jack_slist_free (driver->clock_sync_listeners);
  1659. if (driver->ctl_handle) {
  1660. snd_ctl_close (driver->ctl_handle);
  1661. driver->ctl_handle = 0;
  1662. }
  1663. if (driver->capture_handle) {
  1664. snd_pcm_close (driver->capture_handle);
  1665. driver->capture_handle = 0;
  1666. }
  1667. if (driver->playback_handle) {
  1668. snd_pcm_close (driver->playback_handle);
  1669. driver->capture_handle = 0;
  1670. }
  1671. if (driver->capture_hw_params) {
  1672. snd_pcm_hw_params_free (driver->capture_hw_params);
  1673. driver->capture_hw_params = 0;
  1674. }
  1675. if (driver->playback_hw_params) {
  1676. snd_pcm_hw_params_free (driver->playback_hw_params);
  1677. driver->playback_hw_params = 0;
  1678. }
  1679. if (driver->capture_sw_params) {
  1680. snd_pcm_sw_params_free (driver->capture_sw_params);
  1681. driver->capture_sw_params = 0;
  1682. }
  1683. if (driver->playback_sw_params) {
  1684. snd_pcm_sw_params_free (driver->playback_sw_params);
  1685. driver->playback_sw_params = 0;
  1686. }
  1687. if (driver->pfd) {
  1688. free (driver->pfd);
  1689. }
  1690. if (driver->hw) {
  1691. driver->hw->release (driver->hw);
  1692. driver->hw = 0;
  1693. }
  1694. free(driver->alsa_name_playback);
  1695. free(driver->alsa_name_capture);
  1696. free(driver->alsa_driver);
  1697. alsa_driver_release_channel_dependent_memory (driver);
  1698. //JACK2
  1699. //jack_driver_nt_finish ((jack_driver_nt_t *) driver);
  1700. free (driver);
  1701. }
  1702. static char*
  1703. discover_alsa_using_apps ()
  1704. {
  1705. char found[2048];
  1706. char command[5192];
  1707. char* path = getenv ("PATH");
  1708. char* dir;
  1709. size_t flen = 0;
  1710. int card;
  1711. int device;
  1712. size_t cmdlen = 0;
  1713. if (!path) {
  1714. return NULL;
  1715. }
  1716. /* look for lsof and give up if its not in PATH */
  1717. path = strdup (path);
  1718. dir = strtok (path, ":");
  1719. while (dir) {
  1720. char maybe[PATH_MAX+1];
  1721. snprintf (maybe, sizeof(maybe), "%s/lsof", dir);
  1722. if (access (maybe, X_OK) == 0) {
  1723. break;
  1724. }
  1725. dir = strtok (NULL, ":");
  1726. }
  1727. free (path);
  1728. if (!dir) {
  1729. return NULL;
  1730. }
  1731. snprintf (command, sizeof (command), "lsof -Fc0 ");
  1732. cmdlen = strlen (command);
  1733. for (card = 0; card < 8; ++card) {
  1734. for (device = 0; device < 8; ++device) {
  1735. char buf[32];
  1736. snprintf (buf, sizeof (buf), "/dev/snd/pcmC%dD%dp", card, device);
  1737. if (access (buf, F_OK) == 0) {
  1738. snprintf (command+cmdlen, sizeof(command)-cmdlen, "%s ", buf);
  1739. }
  1740. cmdlen = strlen (command);
  1741. snprintf (buf, sizeof (buf), "/dev/snd/pcmC%dD%dc", card, device);
  1742. if (access (buf, F_OK) == 0) {
  1743. snprintf (command+cmdlen, sizeof(command)-cmdlen, "%s ", buf);
  1744. }
  1745. cmdlen = strlen (command);
  1746. }
  1747. }
  1748. FILE* f = popen (command, "r");
  1749. if (!f) {
  1750. return NULL;
  1751. }
  1752. while (!feof (f)) {
  1753. char buf[1024]; /* lsof doesn't output much */
  1754. if (!fgets (buf, sizeof (buf), f)) {
  1755. break;
  1756. }
  1757. if (*buf != 'p') {
  1758. return NULL;
  1759. }
  1760. /* buf contains NULL as a separator between the process field and the command field */
  1761. char *pid = buf;
  1762. ++pid; /* skip leading 'p' */
  1763. char *cmd = pid;
  1764. /* skip to NULL */
  1765. while (*cmd) {
  1766. ++cmd;
  1767. }
  1768. ++cmd; /* skip to 'c' */
  1769. ++cmd; /* skip to first character of command */
  1770. snprintf (found+flen, sizeof (found)-flen, "%s (process ID %s)\n", cmd, pid);
  1771. flen = strlen (found);
  1772. if (flen >= sizeof (found)) {
  1773. break;
  1774. }
  1775. }
  1776. pclose (f);
  1777. if (flen) {
  1778. return strdup (found);
  1779. } else {
  1780. return NULL;
  1781. }
  1782. }
  1783. static int
  1784. alsa_driver_open (alsa_driver_t *driver, bool is_capture)
  1785. {
  1786. int err = 0;
  1787. char* current_apps;
  1788. if(is_capture) {
  1789. err = snd_pcm_open (&driver->capture_handle,
  1790. driver->alsa_name_capture,
  1791. SND_PCM_STREAM_CAPTURE,
  1792. SND_PCM_NONBLOCK);
  1793. } else {
  1794. err = snd_pcm_open (&driver->playback_handle,
  1795. driver->alsa_name_playback,
  1796. SND_PCM_STREAM_PLAYBACK,
  1797. SND_PCM_NONBLOCK);
  1798. }
  1799. if (err < 0) {
  1800. switch (errno) {
  1801. case EBUSY:
  1802. #ifdef __ANDROID__
  1803. jack_error ("\n\nATTENTION: The device \"%s\" is "
  1804. "already in use. Please stop the"
  1805. " application using it and "
  1806. "run JACK again",
  1807. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback);
  1808. #else
  1809. current_apps = discover_alsa_using_apps ();
  1810. if (current_apps) {
  1811. jack_error ("\n\nATTENTION: The device \"%s\" is "
  1812. "already in use. The following applications "
  1813. " are using your soundcard(s) so you should "
  1814. " check them and stop them as necessary before "
  1815. " trying to start JACK again:\n\n%s",
  1816. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback,
  1817. current_apps);
  1818. free (current_apps);
  1819. } else {
  1820. jack_error ("\n\nATTENTION: The device \"%s\" is "
  1821. "already in use. Please stop the"
  1822. " application using it and "
  1823. "run JACK again",
  1824. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback);
  1825. }
  1826. #endif
  1827. break;
  1828. case EPERM:
  1829. jack_error ("you do not have permission to open "
  1830. "the audio device \"%s\" for playback",
  1831. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback);
  1832. break;
  1833. case EINVAL:
  1834. jack_error ("the state of handle or the mode is invalid "
  1835. "or invalid state change occured \"%s\" for playback",
  1836. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback);
  1837. break;
  1838. case ENOENT:
  1839. jack_error ("device \"%s\" does not exist for playback",
  1840. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback);
  1841. break;
  1842. case ENOMEM:
  1843. jack_error ("Not enough memory available for allocation for \"%s\" for playback",
  1844. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback);
  1845. break;
  1846. case SND_ERROR_INCOMPATIBLE_VERSION:
  1847. jack_error ("Version mismatch \"%s\" for playback",
  1848. is_capture ? driver->alsa_name_capture : driver->alsa_name_playback);
  1849. break;
  1850. }
  1851. alsa_driver_delete (driver);
  1852. if(is_capture) {
  1853. driver->capture_handle = NULL;
  1854. } else {
  1855. driver->playback_handle = NULL;
  1856. }
  1857. }
  1858. if (is_capture && driver->capture_handle) {
  1859. snd_pcm_nonblock (driver->capture_handle, 0);
  1860. } else if(!is_capture && driver->playback_handle) {
  1861. snd_pcm_nonblock (driver->playback_handle, 0);
  1862. }
  1863. return err;
  1864. }
  1865. jack_driver_t *
  1866. alsa_driver_new (char *name, char *playback_alsa_device,
  1867. char *capture_alsa_device,
  1868. jack_client_t *client,
  1869. jack_nframes_t frames_per_cycle,
  1870. jack_nframes_t user_nperiods,
  1871. jack_nframes_t rate,
  1872. int hw_monitoring,
  1873. int hw_metering,
  1874. int capturing,
  1875. int playing,
  1876. DitherAlgorithm dither,
  1877. int soft_mode,
  1878. int monitor,
  1879. int user_capture_nchnls,
  1880. int user_playback_nchnls,
  1881. int shorts_first,
  1882. jack_nframes_t capture_latency,
  1883. jack_nframes_t playback_latency,
  1884. alsa_midi_t *midi_driver
  1885. )
  1886. {
  1887. int err;
  1888. alsa_driver_t *driver;
  1889. jack_info ("creating alsa driver ... %s|%s|%" PRIu32 "|%" PRIu32
  1890. "|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s",
  1891. playing ? playback_alsa_device : "-",
  1892. capturing ? capture_alsa_device : "-",
  1893. frames_per_cycle, user_nperiods, rate,
  1894. user_capture_nchnls,user_playback_nchnls,
  1895. hw_monitoring ? "hwmon": "nomon",
  1896. hw_metering ? "hwmeter":"swmeter",
  1897. soft_mode ? "soft-mode":"-",
  1898. shorts_first ? "16bit":"32bit");
  1899. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  1900. jack_driver_nt_init ((jack_driver_nt_t *) driver);
  1901. // JACK2
  1902. /*
  1903. driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
  1904. driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
  1905. driver->read = (JackDriverReadFunction) alsa_driver_read;
  1906. driver->write = (JackDriverReadFunction) alsa_driver_write;
  1907. driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
  1908. driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
  1909. driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
  1910. driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
  1911. driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
  1912. */
  1913. driver->playback_handle = NULL;
  1914. driver->capture_handle = NULL;
  1915. driver->ctl_handle = 0;
  1916. driver->hw = 0;
  1917. driver->capture_and_playback_not_synced = FALSE;
  1918. driver->max_nchannels = 0;
  1919. driver->playback_nchannels = user_playback_nchnls;
  1920. driver->capture_nchannels = user_capture_nchnls;
  1921. driver->playback_sample_bytes = (shorts_first ? 2:4);
  1922. driver->capture_sample_bytes = (shorts_first ? 2:4);
  1923. driver->capture_frame_latency = capture_latency;
  1924. driver->playback_frame_latency = playback_latency;
  1925. driver->playback_addr = 0;
  1926. driver->capture_addr = 0;
  1927. driver->playback_interleave_skip = NULL;
  1928. driver->capture_interleave_skip = NULL;
  1929. driver->silent = 0;
  1930. driver->all_monitor_in = FALSE;
  1931. driver->with_monitor_ports = monitor;
  1932. driver->clock_mode = ClockMaster; /* XXX is it? */
  1933. driver->input_monitor_mask = 0; /* XXX is it? */
  1934. driver->capture_ports = 0;
  1935. driver->playback_ports = 0;
  1936. driver->monitor_ports = 0;
  1937. driver->pfd = 0;
  1938. driver->playback_nfds = 0;
  1939. driver->capture_nfds = 0;
  1940. driver->dither = dither;
  1941. driver->soft_mode = soft_mode;
  1942. driver->quirk_bswap = 0;
  1943. pthread_mutex_init (&driver->clock_sync_lock, 0);
  1944. driver->clock_sync_listeners = 0;
  1945. driver->poll_late = 0;
  1946. driver->xrun_count = 0;
  1947. driver->process_count = 0;
  1948. driver->alsa_name_playback = strdup (playback_alsa_device);
  1949. driver->alsa_name_capture = strdup (capture_alsa_device);
  1950. driver->midi = midi_driver;
  1951. driver->xrun_recovery = 0;
  1952. if (alsa_driver_check_card_type (driver)) {
  1953. alsa_driver_delete (driver);
  1954. return NULL;
  1955. }
  1956. alsa_driver_hw_specific (driver, hw_monitoring, hw_metering);
  1957. if (playing) {
  1958. err = alsa_driver_open(driver, SND_PCM_STREAM_PLAYBACK);
  1959. if(err < 0) {
  1960. jack_error ("\n\nATTENTION: Opening of the playback device \"%s\" failed.",
  1961. playback_alsa_device);
  1962. return NULL;
  1963. }
  1964. }
  1965. if(capturing) {
  1966. err = alsa_driver_open(driver, SND_PCM_STREAM_CAPTURE);
  1967. if(err < 0) {
  1968. jack_error ("\n\nATTENTION: Opening of the capture device \"%s\" failed.",
  1969. capture_alsa_device);
  1970. return NULL;
  1971. }
  1972. }
  1973. if (driver->playback_handle == NULL) {
  1974. if (playing) {
  1975. /* they asked for playback, but we can't do it */
  1976. jack_error ("ALSA: Cannot open PCM device %s for "
  1977. "playback. Falling back to capture-only"
  1978. " mode", name);
  1979. if (driver->capture_handle == NULL) {
  1980. /* can't do anything */
  1981. alsa_driver_delete (driver);
  1982. return NULL;
  1983. }
  1984. playing = FALSE;
  1985. }
  1986. }
  1987. if (driver->capture_handle == NULL) {
  1988. if (capturing) {
  1989. /* they asked for capture, but we can't do it */
  1990. jack_error ("ALSA: Cannot open PCM device %s for "
  1991. "capture. Falling back to playback-only"
  1992. " mode", name);
  1993. if (driver->playback_handle == NULL) {
  1994. /* can't do anything */
  1995. alsa_driver_delete (driver);
  1996. return NULL;
  1997. }
  1998. capturing = FALSE;
  1999. }
  2000. }
  2001. driver->playback_hw_params = 0;
  2002. driver->capture_hw_params = 0;
  2003. driver->playback_sw_params = 0;
  2004. driver->capture_sw_params = 0;
  2005. if (driver->playback_handle) {
  2006. if ((err = snd_pcm_hw_params_malloc (
  2007. &driver->playback_hw_params)) < 0) {
  2008. jack_error ("ALSA: could not allocate playback hw"
  2009. " params structure");
  2010. alsa_driver_delete (driver);
  2011. return NULL;
  2012. }
  2013. if ((err = snd_pcm_sw_params_malloc (
  2014. &driver->playback_sw_params)) < 0) {
  2015. jack_error ("ALSA: could not allocate playback sw"
  2016. " params structure");
  2017. alsa_driver_delete (driver);
  2018. return NULL;
  2019. }
  2020. }
  2021. if (driver->capture_handle) {
  2022. if ((err = snd_pcm_hw_params_malloc (
  2023. &driver->capture_hw_params)) < 0) {
  2024. jack_error ("ALSA: could not allocate capture hw"
  2025. " params structure");
  2026. alsa_driver_delete (driver);
  2027. return NULL;
  2028. }
  2029. if ((err = snd_pcm_sw_params_malloc (
  2030. &driver->capture_sw_params)) < 0) {
  2031. jack_error ("ALSA: could not allocate capture sw"
  2032. " params structure");
  2033. alsa_driver_delete (driver);
  2034. return NULL;
  2035. }
  2036. }
  2037. if (alsa_driver_set_parameters (driver, frames_per_cycle,
  2038. user_nperiods, rate)) {
  2039. alsa_driver_delete (driver);
  2040. return NULL;
  2041. }
  2042. driver->capture_and_playback_not_synced = FALSE;
  2043. if (driver->capture_handle && driver->playback_handle) {
  2044. if (snd_pcm_link (driver->playback_handle,
  2045. driver->capture_handle) != 0) {
  2046. driver->capture_and_playback_not_synced = TRUE;
  2047. }
  2048. }
  2049. driver->client = client;
  2050. return (jack_driver_t *) driver;
  2051. }
  2052. int
  2053. alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver,
  2054. unsigned int which)
  2055. {
  2056. JSList *node;
  2057. int ret = -1;
  2058. pthread_mutex_lock (&driver->clock_sync_lock);
  2059. for (node = driver->clock_sync_listeners; node;
  2060. node = jack_slist_next (node)) {
  2061. if (((ClockSyncListener *) node->data)->id == which) {
  2062. driver->clock_sync_listeners =
  2063. jack_slist_remove_link (
  2064. driver->clock_sync_listeners, node);
  2065. free (node->data);
  2066. jack_slist_free_1 (node);
  2067. ret = 0;
  2068. break;
  2069. }
  2070. }
  2071. pthread_mutex_unlock (&driver->clock_sync_lock);
  2072. return ret;
  2073. }
  2074. /* DRIVER "PLUGIN" INTERFACE */
  2075. const char driver_client_name[] = "alsa_pcm";
  2076. void
  2077. driver_finish (jack_driver_t *driver)
  2078. {
  2079. alsa_driver_delete ((alsa_driver_t *) driver);
  2080. }