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.

1708 lines
45KB

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