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.

1877 lines
49KB

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