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.

1552 lines
41KB

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