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.

1684 lines
44KB

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