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.

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