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.

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