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.

1615 lines
42KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <memory.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <glib.h>
  23. #include <stdarg.h>
  24. #include <getopt.h>
  25. #include <asm/msr.h>
  26. #include <jack/alsa_driver.h>
  27. #include <jack/types.h>
  28. #include <jack/internal.h>
  29. #include <jack/engine.h>
  30. #include <jack/hammerfall.h>
  31. #include <jack/generic.h>
  32. static void
  33. alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
  34. {
  35. if (driver->playback_addr) {
  36. free (driver->playback_addr);
  37. driver->playback_addr = 0;
  38. }
  39. if (driver->capture_addr) {
  40. free (driver->capture_addr);
  41. driver->capture_addr = 0;
  42. }
  43. if (driver->silent) {
  44. free (driver->silent);
  45. driver->silent = 0;
  46. }
  47. if (driver->dither_state) {
  48. free (driver->dither_state);
  49. driver->dither_state = 0;
  50. }
  51. }
  52. static int
  53. alsa_driver_check_capabilities (alsa_driver_t *driver)
  54. {
  55. return 0;
  56. }
  57. static int
  58. alsa_driver_check_card_type (alsa_driver_t *driver)
  59. {
  60. int err;
  61. snd_ctl_card_info_t *card_info;
  62. snd_ctl_card_info_alloca (&card_info);
  63. if ((err = snd_ctl_open (&driver->ctl_handle, driver->alsa_name, 0)) < 0) {
  64. jack_error ("control open \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  65. return -1;
  66. }
  67. if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  68. jack_error ("control hardware info \"%s\" (%s)", driver->alsa_name, snd_strerror (err));
  69. snd_ctl_close (driver->ctl_handle);
  70. return -1;
  71. }
  72. driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  73. return alsa_driver_check_capabilities (driver);
  74. }
  75. static int
  76. alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
  77. {
  78. driver->hw = jack_alsa_hammerfall_hw_new (driver);
  79. return 0;
  80. }
  81. static int
  82. alsa_driver_generic_hardware (alsa_driver_t *driver)
  83. {
  84. driver->hw = jack_alsa_generic_hw_new (driver);
  85. return 0;
  86. }
  87. static int
  88. alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring)
  89. {
  90. int err;
  91. if (!strcmp(driver->alsa_driver, "RME9652")) {
  92. if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
  93. return err;
  94. }
  95. } else {
  96. if ((err = alsa_driver_generic_hardware (driver)) != 0) {
  97. return err;
  98. }
  99. }
  100. if (driver->hw->capabilities & Cap_HardwareMonitoring) {
  101. driver->has_hw_monitoring = TRUE;
  102. /* XXX need to ensure that this is really FALSE or TRUE or whatever*/
  103. driver->hw_monitoring = hw_monitoring;
  104. } else {
  105. driver->has_hw_monitoring = FALSE;
  106. driver->hw_monitoring = FALSE;
  107. }
  108. if (driver->hw->capabilities & Cap_ClockLockReporting) {
  109. driver->has_clock_sync_reporting = TRUE;
  110. } else {
  111. driver->has_clock_sync_reporting = FALSE;
  112. }
  113. return 0;
  114. }
  115. static void
  116. alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
  117. {
  118. switch (driver->sample_bytes) {
  119. case 2:
  120. if (driver->interleaved) {
  121. driver->channel_copy = memcpy_interleave_d16_s16;
  122. } else {
  123. driver->channel_copy = memcpy_fake;
  124. }
  125. switch (driver->dither) {
  126. case Rectangular:
  127. printf("Rectangular dithering at 16 bits\n");
  128. driver->write_via_copy = sample_move_dither_rect_d16_sS;
  129. break;
  130. case Triangular:
  131. printf("Triangular dithering at 16 bits\n");
  132. driver->write_via_copy = sample_move_dither_tri_d16_sS;
  133. break;
  134. case Shaped:
  135. printf("Noise-shaped dithering at 16 bits\n");
  136. driver->write_via_copy = sample_move_dither_shaped_d16_sS;
  137. break;
  138. default:
  139. driver->write_via_copy = sample_move_d16_sS;
  140. break;
  141. }
  142. driver->read_via_copy = sample_move_dS_s16;
  143. break;
  144. case 4:
  145. if (driver->interleaved) {
  146. driver->channel_copy = memcpy_interleave_d32_s32;
  147. } else {
  148. driver->channel_copy = memcpy_fake;
  149. }
  150. switch (driver->dither) {
  151. case Rectangular:
  152. printf("Rectangular dithering at 16 bits\n");
  153. driver->write_via_copy = sample_move_dither_rect_d32u24_sS;
  154. break;
  155. case Triangular:
  156. printf("Triangular dithering at 16 bits\n");
  157. driver->write_via_copy = sample_move_dither_tri_d32u24_sS;
  158. break;
  159. case Shaped:
  160. printf("Noise-shaped dithering at 16 bits\n");
  161. driver->write_via_copy = sample_move_dither_shaped_d32u24_sS;
  162. break;
  163. default:
  164. driver->write_via_copy = sample_move_d32u24_sS;
  165. break;
  166. }
  167. driver->read_via_copy = sample_move_dS_s32u24;
  168. break;
  169. }
  170. }
  171. static int
  172. alsa_driver_configure_stream (alsa_driver_t *driver,
  173. const char *stream_name,
  174. snd_pcm_t *handle,
  175. snd_pcm_hw_params_t *hw_params,
  176. snd_pcm_sw_params_t *sw_params,
  177. unsigned long *nchns)
  178. {
  179. int err;
  180. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  181. jack_error ("ALSA: no playback configurations available");
  182. return -1;
  183. }
  184. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params)) < 0) {
  185. jack_error ("ALSA: cannot restrict period size to integral value.");
  186. return -1;
  187. }
  188. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  189. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  190. jack_error ("ALSA: mmap-based access is not possible for the %s "
  191. "stream of this audio interface", stream_name);
  192. return -1;
  193. }
  194. }
  195. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S32_LE)) < 0) {
  196. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
  197. jack_error ("Sorry. The audio interface \"%s\""
  198. "doesn't support either of the two hardware sample formats that ardour can use.",
  199. driver->alsa_name);
  200. return -1;
  201. }
  202. }
  203. if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, driver->frame_rate, 0)) < 0) {
  204. jack_error ("ALSA: cannot set sample/frame rate to %u for %s", driver->frame_rate, stream_name);
  205. return -1;
  206. }
  207. *nchns = snd_pcm_hw_params_get_channels_max (hw_params);
  208. if (*nchns > 1024) {
  209. /* the hapless user is an unwitting victim of the "default"
  210. ALSA PCM device, which can support up to 16 million
  211. channels. since they can't be bothered to set up
  212. a proper default device, limit the number of channels
  213. for them to a sane default.
  214. */
  215. jack_error ("You appear to be using the ALSA software \"plug\" layer, probably\n"
  216. "a result of using the \"default\" ALSA device. This is less\n"
  217. "efficient than it could be. Consider using a ~/.asoundrc file\n"
  218. "to define a hardware audio device rather than using the plug layer\n");
  219. *nchns = 2;
  220. }
  221. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, *nchns)) < 0) {
  222. jack_error ("ALSA: cannot set channel count to %u for %s", *nchns, stream_name);
  223. return -1;
  224. }
  225. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params, driver->frames_per_cycle, 0)) < 0) {
  226. jack_error ("ALSA: cannot set period size to %u frames for %s", driver->frames_per_cycle, stream_name);
  227. return -1;
  228. }
  229. if ((err = snd_pcm_hw_params_set_periods (handle, hw_params, driver->user_nperiods, 0)) < 0) {
  230. jack_error ("ALSA: cannot set number of periods to %u for %s", driver->user_nperiods, stream_name);
  231. return -1;
  232. }
  233. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params, driver->user_nperiods * driver->frames_per_cycle)) < 0) {
  234. jack_error ("ALSA: cannot set buffer length to %u for %s", driver->user_nperiods * driver->frames_per_cycle, stream_name);
  235. return -1;
  236. }
  237. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  238. jack_error ("ALSA: cannot set hardware parameters for %s", stream_name);
  239. return -1;
  240. }
  241. snd_pcm_sw_params_current (handle, sw_params);
  242. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, ~0U)) < 0) {
  243. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  244. return -1;
  245. }
  246. if ((err = snd_pcm_sw_params_set_stop_threshold (handle, sw_params, driver->user_nperiods * driver->frames_per_cycle)) < 0) {
  247. jack_error ("ALSA: cannot set stop mode for %s", stream_name);
  248. return -1;
  249. }
  250. if ((err = snd_pcm_sw_params_set_silence_threshold (handle, sw_params, 0)) < 0) {
  251. jack_error ("ALSA: cannot set silence threshold for %s", stream_name);
  252. return -1;
  253. }
  254. if ((err = snd_pcm_sw_params_set_silence_size (handle, sw_params, driver->frames_per_cycle * driver->nfragments)) < 0) {
  255. jack_error ("ALSA: cannot set silence size for %s", stream_name);
  256. return -1;
  257. }
  258. if ((err = snd_pcm_sw_params_set_avail_min (handle, sw_params, driver->frames_per_cycle)) < 0) {
  259. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  260. return -1;
  261. }
  262. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  263. jack_error ("ALSA: cannot set software parameters for %s", stream_name);
  264. return -1;
  265. }
  266. return 0;
  267. }
  268. static int
  269. alsa_driver_set_parameters (alsa_driver_t *driver, nframes_t frames_per_cycle, nframes_t user_nperiods, nframes_t rate)
  270. {
  271. int p_noninterleaved = 0;
  272. int c_noninterleaved = 0;
  273. snd_pcm_format_t c_format = 0;
  274. snd_pcm_format_t p_format = 0;
  275. int dir;
  276. unsigned int p_period_size = 0;
  277. unsigned int c_period_size = 0;
  278. unsigned int p_nfragments = 0;
  279. unsigned int c_nfragments = 0;
  280. channel_t chn;
  281. driver->frame_rate = rate;
  282. driver->frames_per_cycle = frames_per_cycle;
  283. driver->user_nperiods = user_nperiods;
  284. if (driver->capture_handle) {
  285. if (alsa_driver_configure_stream (driver, "capture",
  286. driver->capture_handle,
  287. driver->capture_hw_params,
  288. driver->capture_sw_params,
  289. &driver->capture_nchannels)) {
  290. jack_error ("ALSA: cannot configure capture channel");
  291. return -1;
  292. }
  293. }
  294. if (driver->playback_handle) {
  295. if (alsa_driver_configure_stream (driver, "playback",
  296. driver->playback_handle,
  297. driver->playback_hw_params,
  298. driver->playback_sw_params,
  299. &driver->playback_nchannels)) {
  300. jack_error ("ALSA: cannot configure playback channel");
  301. return -1;
  302. }
  303. }
  304. /* check the fragment size, since thats non-negotiable */
  305. if (driver->playback_handle) {
  306. p_period_size = snd_pcm_hw_params_get_period_size (driver->playback_hw_params, &dir);
  307. p_nfragments = snd_pcm_hw_params_get_periods (driver->playback_hw_params, &dir);
  308. p_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->playback_hw_params);
  309. p_noninterleaved = (snd_pcm_hw_params_get_access (driver->playback_hw_params) == SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
  310. if (p_period_size != driver->frames_per_cycle) {
  311. jack_error ("alsa_pcm: requested an interrupt every %u frames but got %uc frames for playback",
  312. driver->frames_per_cycle,p_period_size);
  313. return -1;
  314. }
  315. }
  316. if (driver->capture_handle) {
  317. c_period_size = snd_pcm_hw_params_get_period_size (driver->capture_hw_params, &dir);
  318. c_nfragments = snd_pcm_hw_params_get_periods (driver->capture_hw_params, &dir);
  319. c_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->capture_hw_params);
  320. c_noninterleaved = (snd_pcm_hw_params_get_access (driver->capture_hw_params) == SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
  321. if (c_period_size != driver->frames_per_cycle) {
  322. jack_error ("alsa_pcm: requested an interrupt every %u frames but got %uc frames for capture",
  323. driver->frames_per_cycle,p_period_size);
  324. return -1;
  325. }
  326. }
  327. if (driver->capture_handle && driver->playback_handle) {
  328. if (p_nfragments != c_nfragments) {
  329. jack_error ("alsa_pcm: different period counts for playback and capture!");
  330. return -1;
  331. }
  332. /* Check that we are using the same sample format on both streams */
  333. if (p_format != c_format) {
  334. jack_error ("Sorry. The PCM device \"%s\""
  335. "doesn't support the same sample format for capture and playback."
  336. "We cannot use this PCM device.", driver->alsa_name);
  337. return -1;
  338. }
  339. /* check interleave setup */
  340. if (c_noninterleaved != p_noninterleaved) {
  341. jack_error ("ALSA: the playback and capture components for this PCM device differ "
  342. "in their use of channel interleaving. We cannot use this PCM device.");
  343. return -1;
  344. }
  345. driver->nfragments = c_nfragments;
  346. driver->interleaved = !c_noninterleaved;
  347. driver->sample_format = c_format;
  348. } else if (driver->capture_handle) {
  349. driver->nfragments = c_nfragments;
  350. driver->interleaved = !c_noninterleaved;
  351. driver->sample_format = c_format;
  352. } else {
  353. driver->nfragments = p_nfragments;
  354. driver->interleaved = !p_noninterleaved;
  355. driver->sample_format = p_format;
  356. }
  357. driver->buffer_frames = driver->frames_per_cycle * driver->nfragments;
  358. driver->sample_bytes = snd_pcm_format_physical_width (driver->sample_format) / 8;
  359. driver->bytes_per_cycle = driver->sample_bytes * driver->frames_per_cycle;
  360. switch (driver->sample_format) {
  361. case SND_PCM_FORMAT_S32_LE:
  362. case SND_PCM_FORMAT_S16_LE:
  363. break;
  364. default:
  365. jack_error ("programming error: unhandled format type");
  366. exit (1);
  367. }
  368. if (driver->interleaved) {
  369. driver->interleave_unit = snd_pcm_format_physical_width (driver->sample_format) / 8;
  370. driver->playback_interleave_skip = driver->interleave_unit * driver->playback_nchannels;
  371. driver->capture_interleave_skip = driver->interleave_unit * driver->capture_nchannels;
  372. } else {
  373. driver->interleave_unit = 0; /* NOT USED */
  374. driver->playback_interleave_skip = snd_pcm_format_physical_width (driver->sample_format) / 8;
  375. driver->capture_interleave_skip = driver->playback_interleave_skip;
  376. }
  377. if (driver->playback_nchannels > driver->capture_nchannels) {
  378. driver->max_nchannels = driver->playback_nchannels;
  379. driver->user_nchannels = driver->capture_nchannels;
  380. } else {
  381. driver->max_nchannels = driver->capture_nchannels;
  382. driver->user_nchannels = driver->playback_nchannels;
  383. }
  384. alsa_driver_setup_io_function_pointers (driver);
  385. /* Allocate and initialize structures that rely on
  386. the channels counts.
  387. */
  388. /* set up the bit pattern that is used to record which
  389. channels require action on every cycle. any bits that are
  390. not set after the engine's process() call indicate channels
  391. that potentially need to be silenced.
  392. XXX this is limited to <wordsize> channels. Use a bitset
  393. type instead.
  394. */
  395. driver->channel_done_bits = 0;
  396. if (driver->playback_handle) {
  397. driver->playback_addr = (char **) malloc (sizeof (char *) * driver->playback_nchannels);
  398. memset (driver->playback_addr, 0, sizeof (char *) * driver->playback_nchannels);
  399. driver->silent = (unsigned long *) malloc (sizeof (unsigned long) * driver->playback_nchannels);
  400. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  401. driver->silent[chn] = 0;
  402. }
  403. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  404. driver->channel_done_bits |= (1<<chn);
  405. }
  406. driver->dither_state = (dither_state_t *) calloc ( driver->playback_nchannels, sizeof (dither_state_t));
  407. }
  408. if (driver->capture_handle) {
  409. driver->capture_addr = (char **) malloc (sizeof (char *) * driver->capture_nchannels);
  410. memset (driver->capture_addr, 0, sizeof (char *) * driver->capture_nchannels);
  411. }
  412. driver->clock_sync_data = (ClockSyncStatus *) malloc (sizeof (ClockSyncStatus) *
  413. driver->capture_nchannels > driver->playback_nchannels ?
  414. driver->capture_nchannels : driver->playback_nchannels);
  415. driver->period_interval = (unsigned long) floor ((((float) driver->frames_per_cycle) / driver->frame_rate) * 1000.0);
  416. if (driver->engine) {
  417. driver->engine->set_buffer_size (driver->engine, driver->frames_per_cycle);
  418. }
  419. return 0;
  420. }
  421. static int
  422. alsa_driver_reset_parameters (alsa_driver_t *driver, nframes_t frames_per_cycle, nframes_t user_nperiods, nframes_t rate)
  423. {
  424. /* XXX unregister old ports ? */
  425. alsa_driver_release_channel_dependent_memory (driver);
  426. return alsa_driver_set_parameters (driver, frames_per_cycle, user_nperiods, rate);
  427. }
  428. static int
  429. alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  430. snd_pcm_uframes_t *capture_avail,
  431. snd_pcm_uframes_t *playback_avail,
  432. snd_pcm_uframes_t *capture_offset,
  433. snd_pcm_uframes_t *playback_offset)
  434. {
  435. unsigned long err;
  436. channel_t chn;
  437. if (capture_avail) {
  438. if ((err = snd_pcm_mmap_begin (driver->capture_handle, &driver->capture_areas,
  439. (snd_pcm_uframes_t *) capture_offset,
  440. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  441. jack_error ("ALSA-HW: %s: mmap areas info error", driver->alsa_name);
  442. return -1;
  443. }
  444. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  445. const snd_pcm_channel_area_t *a = &driver->capture_areas[chn];
  446. driver->capture_addr[chn] = (char *) a->addr + ((a->first + a->step * *capture_offset) / 8);
  447. }
  448. }
  449. if (playback_avail) {
  450. if ((err = snd_pcm_mmap_begin (driver->playback_handle, &driver->playback_areas,
  451. (snd_pcm_uframes_t *) playback_offset,
  452. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  453. jack_error ("ALSA-HW: %s: mmap areas info error ", driver->alsa_name);
  454. return -1;
  455. }
  456. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  457. const snd_pcm_channel_area_t *a = &driver->playback_areas[chn];
  458. driver->playback_addr[chn] = (char *) a->addr + ((a->first + a->step * *playback_offset) / 8);
  459. }
  460. }
  461. return 0;
  462. }
  463. static int
  464. alsa_driver_audio_start (alsa_driver_t *driver)
  465. {
  466. int err;
  467. snd_pcm_uframes_t poffset, pavail;
  468. channel_t chn;
  469. if (driver->playback_handle) {
  470. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  471. jack_error ("ALSA-HW: prepare error for playback on \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  472. return -1;
  473. }
  474. }
  475. if (driver->capture_handle && driver->capture_and_playback_not_synced) {
  476. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  477. jack_error ("ALSA-HW: prepare error for capture on \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  478. return -1;
  479. }
  480. }
  481. if (driver->hw_monitoring) {
  482. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  483. }
  484. if (driver->playback_handle) {
  485. /* fill playback buffer with zeroes, and mark
  486. all fragments as having data.
  487. */
  488. pavail = snd_pcm_avail_update (driver->playback_handle);
  489. if (pavail != driver->buffer_frames) {
  490. jack_error ("ALSA-HW: full buffer not available at start");
  491. return -1;
  492. }
  493. if (alsa_driver_get_channel_addresses (driver, 0, &pavail, 0, &poffset)) {
  494. return -1;
  495. }
  496. /* XXX this is cheating. ALSA offers no guarantee that we can access
  497. the entire buffer at any one time. It works on most hardware
  498. tested so far, however, buts its a liability in the long run. I think
  499. that alsa-lib may have a better function for doing this here, where
  500. the goal is to silence the entire buffer.
  501. */
  502. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  503. alsa_driver_silence_on_channel (driver, chn, driver->buffer_frames);
  504. }
  505. snd_pcm_mmap_commit (driver->playback_handle, poffset, driver->buffer_frames);
  506. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  507. jack_error ("could not start playback (%s)", snd_strerror (err));
  508. return -1;
  509. }
  510. }
  511. if (driver->capture_handle && driver->capture_and_playback_not_synced) {
  512. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  513. jack_error ("could not start capture (%s)", snd_strerror (err));
  514. return -1;
  515. }
  516. }
  517. if (driver->hw_monitoring && (driver->input_monitor_mask || driver->all_monitor_in)) {
  518. if (driver->all_monitor_in) {
  519. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  520. } else {
  521. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  522. }
  523. }
  524. if (driver->playback_handle) {
  525. driver->playback_nfds = snd_pcm_poll_descriptors_count (driver->playback_handle);
  526. } else {
  527. driver->playback_nfds = 0;
  528. }
  529. if (driver->capture_handle) {
  530. driver->capture_nfds = snd_pcm_poll_descriptors_count (driver->capture_handle);
  531. } else {
  532. driver->capture_nfds = 0;
  533. }
  534. if (driver->pfd)
  535. free (driver->pfd);
  536. driver->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) *
  537. (driver->playback_nfds + driver->capture_nfds + 2));
  538. return 0;
  539. }
  540. static int
  541. alsa_driver_audio_stop (alsa_driver_t *driver)
  542. {
  543. int err;
  544. if (driver->playback_handle) {
  545. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  546. jack_error ("alsa_pcm: channel flush for playback failed (%s)", snd_strerror (err));
  547. return -1;
  548. }
  549. }
  550. if (!driver->playback_handle || driver->capture_and_playback_not_synced) {
  551. if (driver->capture_handle) {
  552. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  553. jack_error ("alsa_pcm: channel flush for capture failed (%s)", snd_strerror (err));
  554. return -1;
  555. }
  556. }
  557. }
  558. driver->hw->set_input_monitor_mask (driver->hw, 0);
  559. return 0;
  560. }
  561. static int
  562. alsa_driver_xrun_recovery (alsa_driver_t *driver)
  563. {
  564. snd_pcm_status_t *status;
  565. int res;
  566. snd_pcm_status_alloca(&status);
  567. if (driver->capture_handle) {
  568. if ((res = snd_pcm_status(driver->capture_handle, status)) < 0) {
  569. jack_error("status error: %s", snd_strerror(res));
  570. }
  571. } else {
  572. if ((res = snd_pcm_status(driver->playback_handle, status)) < 0) {
  573. jack_error("status error: %s", snd_strerror(res));
  574. }
  575. }
  576. if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
  577. struct timeval now, diff, tstamp;
  578. gettimeofday(&now, 0);
  579. snd_pcm_status_get_trigger_tstamp(status, &tstamp);
  580. timersub(&now, &tstamp, &diff);
  581. fprintf(stderr, "alsa_pcm: xrun of at least %.3f msecs\n", diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
  582. }
  583. if (alsa_driver_audio_stop (driver) || alsa_driver_audio_start (driver)) {
  584. return -1;
  585. }
  586. return 0;
  587. }
  588. static void
  589. alsa_driver_silence_untouched_channels (alsa_driver_t *driver, nframes_t nframes)
  590. {
  591. channel_t chn;
  592. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  593. if ((driver->channels_not_done & (1<<chn))) {
  594. if (driver->silent[chn] < driver->buffer_frames) {
  595. alsa_driver_silence_on_channel_no_mark (driver, chn, nframes);
  596. driver->silent[chn] += nframes;
  597. }
  598. }
  599. }
  600. }
  601. void
  602. alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn, ClockSyncStatus status)
  603. {
  604. driver->clock_sync_data[chn] = status;
  605. alsa_driver_clock_sync_notify (driver, chn, status);
  606. }
  607. static int under_gdb = FALSE;
  608. static nframes_t
  609. alsa_driver_wait (alsa_driver_t *driver, int fd, int *status)
  610. {
  611. snd_pcm_sframes_t avail = 0;
  612. snd_pcm_sframes_t capture_avail = 0;
  613. snd_pcm_sframes_t playback_avail = 0;
  614. int xrun_detected = FALSE;
  615. int need_capture;
  616. int need_playback;
  617. int i;
  618. *status = -1;
  619. need_capture = driver->capture_handle ? 1 : 0;
  620. if (fd >= 0) {
  621. need_playback = 0;
  622. } else {
  623. need_playback = driver->playback_handle ? 1 : 0;
  624. }
  625. again:
  626. while (need_playback || need_capture) {
  627. int p_timed_out, c_timed_out;
  628. int ci = 0;
  629. int nfds;
  630. nfds = 0;
  631. if (need_playback) {
  632. snd_pcm_poll_descriptors (driver->playback_handle, &driver->pfd[0], driver->playback_nfds);
  633. nfds += driver->playback_nfds;
  634. }
  635. if (need_capture) {
  636. snd_pcm_poll_descriptors (driver->capture_handle, &driver->pfd[nfds], driver->capture_nfds);
  637. ci = nfds;
  638. nfds += driver->capture_nfds;
  639. }
  640. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  641. for (i = 0; i < nfds; i++) {
  642. driver->pfd[nfds].events |= POLLERR;
  643. }
  644. if (fd >= 0) {
  645. driver->pfd[nfds].fd = fd;
  646. driver->pfd[nfds].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  647. nfds++;
  648. }
  649. if (poll (driver->pfd, nfds, 1000) < 0) {
  650. if (errno == EINTR) {
  651. printf ("poll interrupt\n");
  652. // this happens mostly when run
  653. // under gdb, or when exiting due to a signal
  654. if (under_gdb) {
  655. goto again;
  656. }
  657. return 0;
  658. }
  659. jack_error ("ALSA::Device: poll call failed (%s)", strerror (errno));
  660. return 0;
  661. }
  662. /* check to see if it was the extra FD that caused us to return from poll
  663. */
  664. if (fd >= 0) {
  665. if (driver->pfd[nfds-1].revents == 0) {
  666. /* we timed out on the extra fd */
  667. return -1;
  668. } else {
  669. /* if POLLIN was the only bit set, we're OK */
  670. *status = 0;
  671. return (driver->pfd[nfds-1].revents == POLLIN) ? 0 : -1;
  672. }
  673. }
  674. if (driver->engine) {
  675. struct timeval tv;
  676. gettimeofday (&tv, NULL);
  677. driver->engine->control->time.microseconds = tv.tv_sec * 1000000 + tv.tv_usec;
  678. }
  679. p_timed_out = 0;
  680. if (need_playback) {
  681. for (i = 0; i < driver->playback_nfds; i++) {
  682. if (driver->pfd[i].revents & POLLERR) {
  683. xrun_detected = TRUE;
  684. }
  685. if (driver->pfd[i].revents == 0) {
  686. p_timed_out++;
  687. }
  688. }
  689. if (p_timed_out == 0) {
  690. need_playback = 0;
  691. }
  692. }
  693. c_timed_out = 0;
  694. if (need_capture) {
  695. for (i = ci; i < nfds; i++) {
  696. if (driver->pfd[i].revents & POLLERR) {
  697. xrun_detected = TRUE;
  698. }
  699. if (driver->pfd[i].revents == 0) {
  700. c_timed_out++;
  701. }
  702. }
  703. if (c_timed_out == 0) {
  704. need_capture = 0;
  705. }
  706. }
  707. if ((p_timed_out && (p_timed_out == driver->playback_nfds)) &&
  708. (c_timed_out && (c_timed_out == driver->capture_nfds))){
  709. jack_error ("ALSA: poll time out");
  710. return 0;
  711. }
  712. }
  713. if (driver->capture_handle) {
  714. if ((capture_avail = snd_pcm_avail_update (driver->capture_handle)) < 0) {
  715. if (capture_avail == -EPIPE) {
  716. xrun_detected = TRUE;
  717. } else {
  718. jack_error ("unknown ALSA avail_update return value (%u)", capture_avail);
  719. }
  720. }
  721. } else {
  722. capture_avail = INT_MAX; /* odd, but see min() computation below */
  723. }
  724. if (driver->playback_handle) {
  725. if ((playback_avail = snd_pcm_avail_update (driver->playback_handle)) < 0) {
  726. if (playback_avail == -EPIPE) {
  727. xrun_detected = TRUE;
  728. } else {
  729. jack_error ("unknown ALSA avail_update return value (%u)", playback_avail);
  730. }
  731. }
  732. } else {
  733. playback_avail = INT_MAX; /* odd, but see min() computation below */
  734. }
  735. if (xrun_detected) {
  736. *status = alsa_driver_xrun_recovery (driver);
  737. return 0;
  738. }
  739. *status = 0;
  740. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  741. /* constrain the available count to the nearest (round down) number of
  742. periods.
  743. */
  744. return avail - (avail % driver->frames_per_cycle);
  745. }
  746. static int
  747. alsa_driver_process (alsa_driver_t *driver, nframes_t nframes)
  748. {
  749. snd_pcm_sframes_t contiguous = 0;
  750. snd_pcm_sframes_t capture_avail = 0;
  751. snd_pcm_sframes_t playback_avail = 0;
  752. snd_pcm_uframes_t capture_offset = 0;
  753. snd_pcm_uframes_t playback_offset = 0;
  754. channel_t chn;
  755. GSList *node;
  756. jack_engine_t *engine = driver->engine;
  757. static int cnt = 0;
  758. cnt++;
  759. while (nframes) {
  760. if (driver->capture_handle) {
  761. if (driver->playback_handle) {
  762. /* DUPLEX */
  763. capture_avail = (nframes > driver->frames_per_cycle) ? driver->frames_per_cycle : nframes;
  764. playback_avail = (nframes > driver->frames_per_cycle) ? driver->frames_per_cycle : nframes;
  765. if (alsa_driver_get_channel_addresses (driver,
  766. (snd_pcm_uframes_t *) &capture_avail,
  767. (snd_pcm_uframes_t *) &playback_avail,
  768. &capture_offset, &playback_offset) < 0) {
  769. return -1;
  770. }
  771. contiguous = capture_avail < playback_avail ? capture_avail : playback_avail;
  772. } else {
  773. /* CAPTURE ONLY */
  774. capture_avail = (nframes > driver->frames_per_cycle) ? driver->frames_per_cycle : nframes;
  775. if (alsa_driver_get_channel_addresses (driver,
  776. (snd_pcm_uframes_t *) &capture_avail,
  777. (snd_pcm_uframes_t *) 0,
  778. &capture_offset, 0) < 0) {
  779. return -1;
  780. }
  781. contiguous = capture_avail;
  782. }
  783. } else {
  784. /* PLAYBACK ONLY */
  785. playback_avail = (nframes > driver->frames_per_cycle) ? driver->frames_per_cycle : nframes;
  786. if (alsa_driver_get_channel_addresses (driver,
  787. (snd_pcm_uframes_t *) 0,
  788. (snd_pcm_uframes_t *) &playback_avail,
  789. 0, &playback_offset) < 0) {
  790. return -1;
  791. }
  792. contiguous = playback_avail;
  793. }
  794. driver->channels_not_done = driver->channel_done_bits;
  795. if (engine->process_lock (engine) == 0) {
  796. channel_t chn;
  797. jack_port_t *port;
  798. GSList *node;
  799. int ret;
  800. if (driver->capture_handle) {
  801. for (chn = 0, node = driver->capture_ports; node; node = g_slist_next (node), chn++) {
  802. port = (jack_port_t *) node->data;
  803. if (!jack_port_connected (port)) {
  804. continue;
  805. }
  806. alsa_driver_read_from_channel (driver, chn, jack_port_get_buffer (port, nframes), nframes);
  807. }
  808. snd_pcm_mmap_commit (driver->capture_handle, capture_offset, contiguous);
  809. }
  810. if ((ret = engine->process (engine, contiguous)) != 0) {
  811. engine->process_unlock (engine);
  812. alsa_driver_audio_stop (driver);
  813. if (ret > 0) {
  814. engine->post_process (engine);
  815. }
  816. return ret;
  817. }
  818. if (driver->playback_handle) {
  819. /* now move data from ports to channels */
  820. for (chn = 0, node = driver->playback_ports; node; node = g_slist_next (node), chn++) {
  821. sample_t *buf;
  822. jack_port_t *port = (jack_port_t *) node->data;
  823. if (!jack_port_connected (port)) {
  824. continue;
  825. }
  826. buf = jack_port_get_buffer (port, contiguous);
  827. alsa_driver_write_to_channel (driver, chn, buf, contiguous);
  828. }
  829. }
  830. engine->process_unlock (engine);
  831. }
  832. /* Now handle input monitoring */
  833. driver->input_monitor_mask = 0;
  834. for (chn = 0, node = driver->capture_ports; node; node = g_slist_next (node), chn++) {
  835. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  836. driver->input_monitor_mask |= (1<<chn);
  837. }
  838. }
  839. if (!driver->hw_monitoring) {
  840. if (driver->playback_handle) {
  841. if (driver->all_monitor_in) {
  842. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  843. alsa_driver_copy_channel (driver, chn, chn, contiguous);
  844. }
  845. } else if (driver->input_monitor_mask) {
  846. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  847. if (driver->input_monitor_mask & (1<<chn)) {
  848. alsa_driver_copy_channel (driver, chn, chn, contiguous);
  849. }
  850. }
  851. }
  852. }
  853. } else {
  854. if ((driver->hw->input_monitor_mask != driver->input_monitor_mask) && !driver->all_monitor_in) {
  855. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  856. }
  857. }
  858. if (driver->playback_handle) {
  859. if (driver->channels_not_done) {
  860. alsa_driver_silence_untouched_channels (driver, contiguous);
  861. }
  862. snd_pcm_mmap_commit (driver->playback_handle, playback_offset, contiguous);
  863. }
  864. nframes -= contiguous;
  865. }
  866. engine->post_process (engine);
  867. return 0;
  868. }
  869. static void
  870. alsa_driver_attach (alsa_driver_t *driver, jack_engine_t *engine)
  871. {
  872. char buf[32];
  873. channel_t chn;
  874. jack_port_t *port;
  875. driver->engine = engine;
  876. driver->engine->set_buffer_size (engine, driver->frames_per_cycle);
  877. driver->engine->set_sample_rate (engine, driver->frame_rate);
  878. /* Now become a client of the engine */
  879. if ((driver->client = jack_driver_become_client ("alsa_pcm")) == NULL) {
  880. jack_error ("ALSA: cannot become client");
  881. return;
  882. }
  883. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  884. snprintf (buf, sizeof(buf) - 1, "in_%lu", chn+1);
  885. port = jack_port_register (driver->client, buf,
  886. JACK_DEFAULT_AUDIO_TYPE,
  887. JackPortIsOutput|JackPortIsPhysical|JackPortIsTerminal|JackPortCanMonitor, 0);
  888. if (port == 0) {
  889. jack_error ("ALSA: cannot register port for %s", buf);
  890. break;
  891. }
  892. /* XXX fix this so that it can handle: systemic (external) latency
  893. */
  894. jack_port_set_latency (port, driver->frames_per_cycle);
  895. driver->capture_ports = g_slist_append (driver->capture_ports, port);
  896. }
  897. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  898. snprintf (buf, sizeof(buf) - 1, "out_%lu", chn+1);
  899. port = jack_port_register (driver->client, buf,
  900. JACK_DEFAULT_AUDIO_TYPE,
  901. JackPortIsInput|JackPortIsTerminal|JackPortIsPhysical, 0);
  902. if (port == 0) {
  903. jack_error ("ALSA: cannot register port for %s", buf);
  904. break;
  905. }
  906. /* XXX fix this so that it can handle: systemic (external) latency
  907. */
  908. jack_port_set_latency (port, driver->frames_per_cycle * driver->nfragments);
  909. driver->playback_ports = g_slist_append (driver->playback_ports, port);
  910. }
  911. printf ("ALSA: ports registered, starting driver\n");
  912. jack_activate (driver->client);
  913. }
  914. static void
  915. alsa_driver_detach (alsa_driver_t *driver, jack_engine_t *engine)
  916. {
  917. GSList *node;
  918. if (driver->engine == 0) {
  919. return;
  920. }
  921. for (node = driver->capture_ports; node; node = g_slist_next (node)) {
  922. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  923. }
  924. g_slist_free (driver->capture_ports);
  925. driver->capture_ports = 0;
  926. for (node = driver->playback_ports; node; node = g_slist_next (node)) {
  927. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  928. }
  929. g_slist_free (driver->playback_ports);
  930. driver->playback_ports = 0;
  931. driver->engine = 0;
  932. }
  933. static int
  934. alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
  935. {
  936. return driver->hw->change_sample_clock (driver->hw, mode);
  937. }
  938. static void
  939. alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
  940. {
  941. if (driver->hw_monitoring) {
  942. if (yn) {
  943. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  944. } else {
  945. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  946. }
  947. }
  948. driver->all_monitor_in = yn;
  949. }
  950. static void
  951. alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
  952. {
  953. if (yn) {
  954. driver->hw_monitoring = TRUE;
  955. if (driver->all_monitor_in) {
  956. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  957. } else {
  958. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  959. }
  960. } else {
  961. driver->hw_monitoring = FALSE;
  962. driver->hw->set_input_monitor_mask (driver->hw, 0);
  963. }
  964. }
  965. static ClockSyncStatus
  966. alsa_driver_clock_sync_status (channel_t chn)
  967. {
  968. return Lock;
  969. }
  970. static void
  971. alsa_driver_delete (alsa_driver_t *driver)
  972. {
  973. GSList *node;
  974. for (node = driver->clock_sync_listeners; node; node = g_slist_next (node)) {
  975. free (node->data);
  976. }
  977. g_slist_free (driver->clock_sync_listeners);
  978. if (driver->capture_handle) {
  979. snd_pcm_close (driver->capture_handle);
  980. driver->capture_handle = 0;
  981. }
  982. if (driver->playback_handle) {
  983. snd_pcm_close (driver->playback_handle);
  984. driver->capture_handle = 0;
  985. }
  986. if (driver->capture_hw_params) {
  987. snd_pcm_hw_params_free (driver->capture_hw_params);
  988. driver->capture_hw_params = 0;
  989. }
  990. if (driver->playback_hw_params) {
  991. snd_pcm_hw_params_free (driver->playback_hw_params);
  992. driver->playback_hw_params = 0;
  993. }
  994. if (driver->capture_sw_params) {
  995. snd_pcm_sw_params_free (driver->capture_sw_params);
  996. driver->capture_sw_params = 0;
  997. }
  998. if (driver->playback_sw_params) {
  999. snd_pcm_sw_params_free (driver->playback_sw_params);
  1000. driver->playback_sw_params = 0;
  1001. }
  1002. if (driver->pfd) {
  1003. free (driver->pfd);
  1004. }
  1005. if (driver->hw) {
  1006. driver->hw->release (driver->hw);
  1007. driver->hw = 0;
  1008. }
  1009. free(driver->alsa_name);
  1010. free(driver->alsa_driver);
  1011. alsa_driver_release_channel_dependent_memory (driver);
  1012. free (driver);
  1013. }
  1014. static jack_driver_t *
  1015. alsa_driver_new (char *name, char *alsa_device,
  1016. nframes_t frames_per_cycle,
  1017. nframes_t user_nperiods,
  1018. nframes_t rate,
  1019. int hw_monitoring,
  1020. int capturing,
  1021. int playing,
  1022. DitherAlgorithm dither)
  1023. {
  1024. int err;
  1025. alsa_driver_t *driver;
  1026. printf ("creating alsa driver ... %s|%lu|%lu|%lu|%s\n",
  1027. alsa_device, frames_per_cycle, user_nperiods, rate,
  1028. hw_monitoring ? "hwmon":"swmon");
  1029. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  1030. jack_driver_init ((jack_driver_t *) driver);
  1031. driver->attach = (JackDriverAttachFunction) alsa_driver_attach;
  1032. driver->detach = (JackDriverDetachFunction) alsa_driver_detach;
  1033. driver->wait = (JackDriverWaitFunction) alsa_driver_wait;
  1034. driver->process = (JackDriverProcessFunction) alsa_driver_process;
  1035. driver->start = (JackDriverStartFunction) alsa_driver_audio_start;
  1036. driver->stop = (JackDriverStopFunction) alsa_driver_audio_stop;
  1037. driver->playback_handle = NULL;
  1038. driver->capture_handle = NULL;
  1039. driver->ctl_handle = 0;
  1040. driver->hw = 0;
  1041. driver->capture_and_playback_not_synced = FALSE;
  1042. driver->nfragments = 0;
  1043. driver->max_nchannels = 0;
  1044. driver->user_nchannels = 0;
  1045. driver->playback_nchannels = 0;
  1046. driver->capture_nchannels = 0;
  1047. driver->playback_addr = 0;
  1048. driver->capture_addr = 0;
  1049. driver->silent = 0;
  1050. driver->all_monitor_in = FALSE;
  1051. driver->clock_mode = ClockMaster; /* XXX is it? */
  1052. driver->input_monitor_mask = 0; /* XXX is it? */
  1053. driver->capture_ports = 0;
  1054. driver->playback_ports = 0;
  1055. driver->pfd = 0;
  1056. driver->playback_nfds = 0;
  1057. driver->capture_nfds = 0;
  1058. driver->dither = dither;
  1059. pthread_mutex_init (&driver->clock_sync_lock, 0);
  1060. driver->clock_sync_listeners = 0;
  1061. if (playing) {
  1062. if ((err = snd_pcm_open (&driver->playback_handle, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  1063. jack_error ("ALSA: Cannot open PCM device %s/%s", name, alsa_device);
  1064. free (driver);
  1065. return 0;
  1066. }
  1067. }
  1068. if (capturing) {
  1069. if ((err = snd_pcm_open (&driver->capture_handle, alsa_device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
  1070. snd_pcm_close (driver->playback_handle);
  1071. jack_error ("ALSA: Cannot open PCM device %s", name);
  1072. free (driver);
  1073. return 0;
  1074. }
  1075. }
  1076. driver->alsa_name = strdup (alsa_device);
  1077. if (alsa_driver_check_card_type (driver)) {
  1078. if (driver->capture_handle) {
  1079. snd_pcm_close (driver->capture_handle);
  1080. }
  1081. if (driver->playback_handle) {
  1082. snd_pcm_close (driver->playback_handle);
  1083. }
  1084. free (driver);
  1085. return 0;
  1086. }
  1087. driver->playback_hw_params = 0;
  1088. driver->capture_hw_params = 0;
  1089. driver->playback_sw_params = 0;
  1090. driver->capture_sw_params = 0;
  1091. if (driver->playback_handle) {
  1092. if ((err = snd_pcm_hw_params_malloc (&driver->playback_hw_params)) < 0) {
  1093. jack_error ("ALSA: could no allocate playback hw params structure");
  1094. alsa_driver_delete (driver);
  1095. return 0;
  1096. }
  1097. if ((err = snd_pcm_sw_params_malloc (&driver->playback_sw_params)) < 0) {
  1098. jack_error ("ALSA: could no allocate playback sw params structure");
  1099. alsa_driver_delete (driver);
  1100. return 0;
  1101. }
  1102. }
  1103. if (driver->capture_handle) {
  1104. if ((err = snd_pcm_hw_params_malloc (&driver->capture_hw_params)) < 0) {
  1105. jack_error ("ALSA: could no allocate capture hw params structure");
  1106. alsa_driver_delete (driver);
  1107. return 0;
  1108. }
  1109. if ((err = snd_pcm_sw_params_malloc (&driver->capture_sw_params)) < 0) {
  1110. jack_error ("ALSA: could no allocate capture sw params structure");
  1111. alsa_driver_delete (driver);
  1112. return 0;
  1113. }
  1114. }
  1115. if (alsa_driver_set_parameters (driver, frames_per_cycle, user_nperiods, rate)) {
  1116. alsa_driver_delete (driver);
  1117. return 0;
  1118. }
  1119. driver->capture_and_playback_not_synced = FALSE;
  1120. if (driver->capture_handle && driver->playback_handle) {
  1121. if (snd_pcm_link (driver->capture_handle, driver->playback_handle) != 0) {
  1122. driver->capture_and_playback_not_synced = TRUE;
  1123. }
  1124. }
  1125. alsa_driver_hw_specific (driver, hw_monitoring);
  1126. return (jack_driver_t *) driver;
  1127. }
  1128. int
  1129. alsa_driver_listen_for_clock_sync_status (alsa_driver_t *driver,
  1130. ClockSyncListenerFunction func,
  1131. void *arg)
  1132. {
  1133. ClockSyncListener *csl;
  1134. csl = (ClockSyncListener *) malloc (sizeof (ClockSyncListener));
  1135. csl->function = func;
  1136. csl->arg = arg;
  1137. csl->id = driver->next_clock_sync_listener_id++;
  1138. pthread_mutex_lock (&driver->clock_sync_lock);
  1139. driver->clock_sync_listeners = g_slist_prepend (driver->clock_sync_listeners, csl);
  1140. pthread_mutex_unlock (&driver->clock_sync_lock);
  1141. return csl->id;
  1142. }
  1143. int
  1144. alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver, int which)
  1145. {
  1146. GSList *node;
  1147. int ret = -1;
  1148. pthread_mutex_lock (&driver->clock_sync_lock);
  1149. for (node = driver->clock_sync_listeners; node; node = g_slist_next (node)) {
  1150. if (((ClockSyncListener *) node->data)->id == which) {
  1151. driver->clock_sync_listeners = g_slist_remove_link (driver->clock_sync_listeners, node);
  1152. free (node->data);
  1153. g_slist_free_1 (node);
  1154. ret = 0;
  1155. break;
  1156. }
  1157. }
  1158. pthread_mutex_unlock (&driver->clock_sync_lock);
  1159. return ret;
  1160. }
  1161. void
  1162. alsa_driver_clock_sync_notify (alsa_driver_t *driver, channel_t chn, ClockSyncStatus status)
  1163. {
  1164. GSList *node;
  1165. pthread_mutex_lock (&driver->clock_sync_lock);
  1166. for (node = driver->clock_sync_listeners; node; node = g_slist_next (node)) {
  1167. ClockSyncListener *csl = (ClockSyncListener *) node->data;
  1168. csl->function (chn, status, csl->arg);
  1169. }
  1170. pthread_mutex_unlock (&driver->clock_sync_lock);
  1171. }
  1172. /* DRIVER "PLUGIN" INTERFACE */
  1173. static void
  1174. alsa_usage ()
  1175. {
  1176. fprintf (stderr, "\
  1177. alsa PCM driver args:
  1178. -d alsa-pcm-name (default: default)
  1179. -r sample-rate (default: 48kHz)
  1180. -p frames-per-period (default: 1024)
  1181. -n periods-per-hardware-buffer (default: 2)
  1182. -H (use hardware monitoring if available, default: no)
  1183. -D (duplex, default: yes)
  1184. -C (capture, default: duplex)
  1185. -P (playback, default: duplex)
  1186. -z[r|t|s|-] (dither, rect|tri|shaped|off, default: off)
  1187. ");
  1188. }
  1189. jack_driver_t *
  1190. driver_initialize (int argc, char **argv)
  1191. {
  1192. nframes_t srate = 48000;
  1193. nframes_t frames_per_interrupt = 1024;
  1194. unsigned long user_nperiods = 2;
  1195. char *pcm_name = "default";
  1196. int hw_monitoring = FALSE;
  1197. int capture = FALSE;
  1198. int playback = FALSE;
  1199. DitherAlgorithm dither = None;
  1200. int i;
  1201. /* grrrr ... getopt() cannot be called in more than one "loop"
  1202. per process instance. ridiculous, but true. why isn't there
  1203. a getopt_reinitialize() function?
  1204. */
  1205. for (i = 1; i < argc; i++) {
  1206. if (argv[i][0] == '-') {
  1207. switch (argv[i][1]) {
  1208. case 'D':
  1209. capture = TRUE;
  1210. playback = TRUE;
  1211. break;
  1212. case 'C':
  1213. capture = TRUE;
  1214. break;
  1215. case 'P':
  1216. playback = TRUE;
  1217. break;
  1218. case 'd':
  1219. pcm_name = argv[i+1];
  1220. i++;
  1221. break;
  1222. case 'n':
  1223. user_nperiods = atoi (argv[i+1]);
  1224. i++;
  1225. break;
  1226. case 'r':
  1227. srate = atoi (argv[i+1]);
  1228. i++;
  1229. break;
  1230. case 'p':
  1231. frames_per_interrupt = atoi (argv[i+1]);
  1232. i++;
  1233. break;
  1234. case 'H':
  1235. hw_monitoring = 1;
  1236. break;
  1237. case 'z':
  1238. switch (argv[i][2]) {
  1239. case '-':
  1240. dither = None;
  1241. break;
  1242. case 'r':
  1243. dither = Rectangular;
  1244. break;
  1245. case 's':
  1246. dither = Shaped;
  1247. break;
  1248. case 't':
  1249. default:
  1250. dither = Triangular;
  1251. break;
  1252. }
  1253. break;
  1254. default:
  1255. alsa_usage ();
  1256. return NULL;
  1257. }
  1258. } else {
  1259. alsa_usage ();
  1260. return NULL;
  1261. }
  1262. }
  1263. /* duplex is the default */
  1264. if (!capture && !playback) {
  1265. capture = TRUE;
  1266. playback = TRUE;
  1267. }
  1268. return alsa_driver_new ("alsa_pcm", pcm_name, frames_per_interrupt,
  1269. user_nperiods, srate, hw_monitoring, capture,
  1270. playback, dither);
  1271. }
  1272. void
  1273. driver_finish (jack_driver_t *driver)
  1274. {
  1275. alsa_driver_delete ((alsa_driver_t *) driver);
  1276. }