jack1 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.

2510 lines
61KB

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