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.

1280 lines
35KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <memory.h>
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <errno.h>
  22. #include <glib.h>
  23. #include <stdarg.h>
  24. #include <asm/msr.h>
  25. #include <jack/alsa_driver.h>
  26. #include <jack/types.h>
  27. #include <jack/internal.h>
  28. #include <jack/engine.h>
  29. #include <jack/hammerfall.h>
  30. #include <jack/generic.h>
  31. static int config_max_level = 0;
  32. static int config_min_level = 0;
  33. static void
  34. alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
  35. {
  36. if (driver->playback_addr) {
  37. free (driver->playback_addr);
  38. driver->playback_addr = 0;
  39. }
  40. if (driver->capture_addr) {
  41. free (driver->capture_addr);
  42. driver->capture_addr = 0;
  43. }
  44. if (driver->silent) {
  45. free (driver->silent);
  46. driver->silent = 0;
  47. }
  48. }
  49. static int
  50. alsa_driver_check_capabilities (alsa_driver_t *driver)
  51. {
  52. return 0;
  53. }
  54. static int
  55. alsa_driver_check_card_type (alsa_driver_t *driver)
  56. {
  57. int err;
  58. snd_ctl_card_info_t *card_info;
  59. snd_ctl_card_info_alloca (&card_info);
  60. if ((err = snd_ctl_open (&driver->ctl_handle, driver->alsa_name, 0)) < 0) {
  61. jack_error ("control open \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  62. return -1;
  63. }
  64. if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
  65. jack_error ("control hardware info \"%s\" (%s)", driver->alsa_name, snd_strerror (err));
  66. snd_ctl_close (driver->ctl_handle);
  67. return -1;
  68. }
  69. driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
  70. return alsa_driver_check_capabilities (driver);
  71. }
  72. static int
  73. alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
  74. {
  75. driver->hw = jack_alsa_hammerfall_hw_new (driver);
  76. return 0;
  77. }
  78. static int
  79. alsa_driver_generic_hardware (alsa_driver_t *driver)
  80. {
  81. driver->hw = jack_alsa_generic_hw_new (driver);
  82. return 0;
  83. }
  84. static int
  85. alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring)
  86. {
  87. int err;
  88. if (!strcmp(driver->alsa_driver, "RME9652")) {
  89. if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
  90. return err;
  91. }
  92. } else {
  93. if ((err = alsa_driver_generic_hardware (driver)) != 0) {
  94. return err;
  95. }
  96. }
  97. if (driver->hw->capabilities & Cap_HardwareMonitoring) {
  98. driver->has_hw_monitoring = TRUE;
  99. /* XXX need to ensure that this is really FALSE or TRUE or whatever*/
  100. driver->hw_monitoring = hw_monitoring;
  101. } else {
  102. driver->has_hw_monitoring = FALSE;
  103. driver->hw_monitoring = FALSE;
  104. }
  105. if (driver->hw->capabilities & Cap_ClockLockReporting) {
  106. driver->has_clock_sync_reporting = TRUE;
  107. } else {
  108. driver->has_clock_sync_reporting = FALSE;
  109. }
  110. return 0;
  111. }
  112. static void
  113. alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
  114. {
  115. switch (driver->sample_bytes) {
  116. case 2:
  117. if (driver->interleaved) {
  118. driver->channel_copy = memcpy_interleave_d16_s16;
  119. } else {
  120. driver->channel_copy = memcpy_fake;
  121. }
  122. driver->write_via_copy = sample_move_d16_sS;
  123. driver->read_via_copy = sample_move_dS_s16;
  124. break;
  125. case 4:
  126. if (driver->interleaved) {
  127. driver->channel_copy = memcpy_interleave_d32_s32;
  128. } else {
  129. driver->channel_copy = memcpy_fake;
  130. }
  131. driver->write_via_copy = sample_move_d32u24_sS;
  132. driver->read_via_copy = sample_move_dS_s32u24;
  133. break;
  134. }
  135. }
  136. static int
  137. alsa_driver_configure_stream (alsa_driver_t *driver,
  138. const char *stream_name,
  139. snd_pcm_t *handle,
  140. snd_pcm_hw_params_t *hw_params,
  141. snd_pcm_sw_params_t *sw_params,
  142. unsigned long *nchns)
  143. {
  144. int err;
  145. if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
  146. jack_error ("ALSA: no playback configurations available");
  147. return -1;
  148. }
  149. if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params)) < 0) {
  150. jack_error ("ALSA: cannot restrict period size to integral value.");
  151. return -1;
  152. }
  153. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
  154. if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
  155. jack_error ("ALSA: mmap-based access is not possible for the %s "
  156. "stream of this audio interface", stream_name);
  157. return -1;
  158. }
  159. }
  160. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S32_LE)) < 0) {
  161. if ((err = snd_pcm_hw_params_set_format (handle, hw_params, SND_PCM_FORMAT_S16_LE)) < 0) {
  162. jack_error ("Sorry. The audio interface \"%s\""
  163. "doesn't support either of the two hardware sample formats that ardour can use.",
  164. driver->alsa_name);
  165. return -1;
  166. }
  167. }
  168. if ((err = snd_pcm_hw_params_set_rate_near (handle, hw_params, driver->frame_rate, 0)) < 0) {
  169. jack_error ("ALSA: cannot set sample/frame rate to %u for %s", driver->frame_rate, stream_name);
  170. return -1;
  171. }
  172. *nchns = snd_pcm_hw_params_get_channels_max (hw_params);
  173. if (*nchns > 1024) {
  174. /* the hapless user is an unwitting victim of the "default"
  175. ALSA PCM device, which can support up to 16 million
  176. channels. since they can't be bothered to set up
  177. a proper default device, limit the number of channels
  178. for them to a sane default.
  179. */
  180. jack_error ("You appear to be using the ALSA software \"plug\" layer, probably\n"
  181. "a result of using the \"default\" ALSA device. This is less\n"
  182. "efficient than it could be. Consider using a ~/.asoundrc file\n"
  183. "to define a hardware audio device rather than using the plug layer\n");
  184. *nchns = 2;
  185. }
  186. if ((err = snd_pcm_hw_params_set_channels (handle, hw_params, *nchns)) < 0) {
  187. jack_error ("ALSA: cannot set channel count to %u for %s", *nchns, stream_name);
  188. return -1;
  189. }
  190. if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params, driver->frames_per_cycle, 0)) < 0) {
  191. jack_error ("ALSA: cannot set period size to %u frames for %s", driver->frames_per_cycle, stream_name);
  192. return -1;
  193. }
  194. if ((err = snd_pcm_hw_params_set_periods (handle, hw_params, 2, 0)) < 0) {
  195. jack_error ("ALSA: cannot set number of periods to 2 for %s", stream_name);
  196. return -1;
  197. }
  198. if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params, 2 * driver->frames_per_cycle)) < 0) {
  199. jack_error ("ALSA: cannot set buffer length to %u for %s", 2 * driver->frames_per_cycle, stream_name);
  200. return -1;
  201. }
  202. if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
  203. jack_error ("ALSA: cannot set hardware parameters for %s", stream_name);
  204. return -1;
  205. }
  206. snd_pcm_sw_params_current (handle, sw_params);
  207. if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params, ~0U)) < 0) {
  208. jack_error ("ALSA: cannot set start mode for %s", stream_name);
  209. return -1;
  210. }
  211. if ((err = snd_pcm_sw_params_set_stop_threshold (handle, sw_params, ~0U)) < 0) {
  212. jack_error ("ALSA: cannot set stop mode for %s", stream_name);
  213. return -1;
  214. }
  215. if ((err = snd_pcm_sw_params_set_silence_threshold (handle, sw_params, 0)) < 0) {
  216. jack_error ("ALSA: cannot set silence threshold for %s", stream_name);
  217. return -1;
  218. }
  219. if ((err = snd_pcm_sw_params_set_silence_size (handle, sw_params, driver->frames_per_cycle * driver->nfragments)) < 0) {
  220. jack_error ("ALSA: cannot set silence size for %s", stream_name);
  221. return -1;
  222. }
  223. if ((err = snd_pcm_sw_params_set_avail_min (handle, sw_params, driver->frames_per_cycle)) < 0) {
  224. jack_error ("ALSA: cannot set avail min for %s", stream_name);
  225. return -1;
  226. }
  227. if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
  228. jack_error ("ALSA: cannot set software parameters for %s", stream_name);
  229. return -1;
  230. }
  231. return 0;
  232. }
  233. static int
  234. alsa_driver_set_parameters (alsa_driver_t *driver, nframes_t frames_per_cycle, nframes_t rate)
  235. {
  236. int p_noninterleaved;
  237. int c_noninterleaved;
  238. snd_pcm_format_t c_format, p_format;
  239. int dir;
  240. unsigned int p_period_size, c_period_size;
  241. unsigned int p_nfragments, c_nfragments;
  242. channel_t chn;
  243. driver->frame_rate = rate;
  244. driver->frames_per_cycle = frames_per_cycle;
  245. if (alsa_driver_configure_stream (driver, "capture",
  246. driver->capture_handle,
  247. driver->capture_hw_params,
  248. driver->capture_sw_params,
  249. &driver->capture_nchannels)) {
  250. jack_error ("ALSA: cannot configure capture channel");
  251. return -1;
  252. }
  253. if (alsa_driver_configure_stream (driver, "playback",
  254. driver->playback_handle,
  255. driver->playback_hw_params,
  256. driver->playback_sw_params,
  257. &driver->playback_nchannels)) {
  258. jack_error ("ALSA: cannot configure playback channel");
  259. return -1;
  260. }
  261. /* check the fragment size, since thats non-negotiable */
  262. p_period_size = snd_pcm_hw_params_get_period_size (driver->playback_hw_params, &dir);
  263. c_period_size = snd_pcm_hw_params_get_period_size (driver->capture_hw_params, &dir);
  264. if (c_period_size != driver->frames_per_cycle || p_period_size != driver->frames_per_cycle) {
  265. jack_error ("alsa_pcm: requested an interrupt every %u frames but got %uc%up frames",
  266. driver->frames_per_cycle, c_period_size, p_period_size);
  267. return -1;
  268. }
  269. p_nfragments = snd_pcm_hw_params_get_periods (driver->playback_hw_params, &dir);
  270. c_nfragments = snd_pcm_hw_params_get_periods (driver->capture_hw_params, &dir);
  271. if (p_nfragments != c_nfragments) {
  272. jack_error ("alsa_pcm: different period counts for playback and capture!");
  273. return -1;
  274. }
  275. driver->nfragments = c_nfragments;
  276. driver->buffer_frames = driver->frames_per_cycle * driver->nfragments;
  277. /* Check that we are using the same sample format on both streams */
  278. p_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->playback_hw_params);
  279. c_format = (snd_pcm_format_t) snd_pcm_hw_params_get_format (driver->capture_hw_params);
  280. if (p_format != c_format) {
  281. jack_error ("Sorry. The audio interface \"%s\""
  282. "doesn't support the same sample format for capture and playback."
  283. "this JACK/ALSA driver cannot use this hardware.", driver->alsa_name);
  284. return -1;
  285. }
  286. driver->sample_format = p_format;
  287. driver->sample_bytes = snd_pcm_format_physical_width (driver->sample_format) / 8;
  288. driver->bytes_per_cycle = driver->sample_bytes * driver->frames_per_cycle;
  289. switch (driver->sample_format) {
  290. case SND_PCM_FORMAT_S32_LE:
  291. /* XXX must handle the n-bits of 24-in-32 problems here */
  292. if (config_max_level) {
  293. driver->max_level = config_max_level;
  294. } else {
  295. driver->max_level = INT_MAX;
  296. }
  297. if (config_min_level) {
  298. driver->min_level = config_min_level;
  299. } else {
  300. driver->min_level = INT_MIN;
  301. }
  302. break;
  303. case SND_PCM_FORMAT_S16_LE:
  304. if (config_max_level) {
  305. driver->max_level = config_max_level;
  306. } else {
  307. driver->max_level = SHRT_MAX;
  308. }
  309. if (config_min_level) {
  310. driver->min_level = config_min_level;
  311. } else {
  312. driver->min_level = SHRT_MIN;
  313. }
  314. break;
  315. default:
  316. jack_error ("programming error: unhandled format type");
  317. exit (1);
  318. }
  319. /* check interleave setup */
  320. p_noninterleaved = (snd_pcm_hw_params_get_access (driver->playback_hw_params) == SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
  321. c_noninterleaved = (snd_pcm_hw_params_get_access (driver->capture_hw_params) == SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
  322. if (c_noninterleaved != p_noninterleaved) {
  323. jack_error ("ALSA: the playback and capture components of this audio interface differ "
  324. "in their use of channel interleaving. Ardour cannot use this h/w.");
  325. return -1;
  326. }
  327. driver->interleaved = !c_noninterleaved;
  328. if (driver->interleaved) {
  329. driver->interleave_unit = snd_pcm_format_physical_width (driver->sample_format) / 8;
  330. driver->playback_interleave_skip = driver->interleave_unit * driver->playback_nchannels;
  331. driver->capture_interleave_skip = driver->interleave_unit * driver->capture_nchannels;
  332. } else {
  333. driver->interleave_unit = 0; /* NOT USED */
  334. driver->playback_interleave_skip = snd_pcm_format_physical_width (driver->sample_format) / 8;
  335. driver->capture_interleave_skip = driver->playback_interleave_skip;
  336. }
  337. if (driver->playback_nchannels > driver->capture_nchannels) {
  338. driver->max_nchannels = driver->playback_nchannels;
  339. driver->user_nchannels = driver->capture_nchannels;
  340. } else {
  341. driver->max_nchannels = driver->capture_nchannels;
  342. driver->user_nchannels = driver->playback_nchannels;
  343. }
  344. alsa_driver_setup_io_function_pointers (driver);
  345. /* Allocate and initialize structures that rely on
  346. the channels counts.
  347. */
  348. driver->playback_addr = (char **) malloc (sizeof (char *) * driver->playback_nchannels);
  349. driver->capture_addr = (char **) malloc (sizeof (char *) * driver->capture_nchannels);
  350. memset (driver->playback_addr, 0, sizeof (char *) * driver->playback_nchannels);
  351. memset (driver->capture_addr, 0, sizeof (char *) * driver->capture_nchannels);
  352. driver->silent = (unsigned long *) malloc (sizeof (unsigned long) * driver->playback_nchannels);
  353. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  354. driver->silent[chn] = 0;
  355. }
  356. driver->clock_sync_data = (ClockSyncStatus *) malloc (sizeof (ClockSyncStatus) *
  357. driver->capture_nchannels > driver->playback_nchannels ?
  358. driver->capture_nchannels : driver->playback_nchannels);
  359. /* set up the bit pattern that is used to record which
  360. channels require action on every cycle. any bits that are
  361. not set after the engine's process() call indicate channels
  362. that potentially need to be silenced.
  363. XXX this is limited to <wordsize> channels. Use a bitset
  364. type instead.
  365. */
  366. driver->channel_done_bits = 0;
  367. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  368. driver->channel_done_bits |= (1<<chn);
  369. }
  370. driver->period_interval = (unsigned long) floor ((((float) driver->frames_per_cycle) / driver->frame_rate) * 1000.0);
  371. if (driver->engine) {
  372. driver->engine->set_buffer_size (driver->engine, driver->frames_per_cycle);
  373. }
  374. return 0;
  375. }
  376. static int
  377. alsa_driver_reset_parameters (alsa_driver_t *driver, nframes_t frames_per_cycle, nframes_t rate)
  378. {
  379. /* XXX unregister old ports ? */
  380. alsa_driver_release_channel_dependent_memory (driver);
  381. return alsa_driver_set_parameters (driver, frames_per_cycle, rate);
  382. }
  383. static int
  384. alsa_driver_get_channel_addresses (alsa_driver_t *driver,
  385. snd_pcm_uframes_t *capture_avail,
  386. snd_pcm_uframes_t *playback_avail,
  387. snd_pcm_uframes_t *capture_offset,
  388. snd_pcm_uframes_t *playback_offset)
  389. {
  390. unsigned long err;
  391. channel_t chn;
  392. if (capture_avail) {
  393. if ((err = snd_pcm_mmap_begin (driver->capture_handle, &driver->capture_areas,
  394. (snd_pcm_uframes_t *) capture_offset,
  395. (snd_pcm_uframes_t *) capture_avail)) < 0) {
  396. jack_error ("ALSA-HW: %s: mmap areas info error", driver->alsa_name);
  397. return -1;
  398. }
  399. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  400. const snd_pcm_channel_area_t *a = &driver->capture_areas[chn];
  401. driver->capture_addr[chn] = (char *) a->addr + ((a->first + a->step * *capture_offset) / 8);
  402. }
  403. }
  404. if (playback_avail) {
  405. if ((err = snd_pcm_mmap_begin (driver->playback_handle, &driver->playback_areas,
  406. (snd_pcm_uframes_t *) playback_offset,
  407. (snd_pcm_uframes_t *) playback_avail)) < 0) {
  408. jack_error ("ALSA-HW: %s: mmap areas info error ", driver->alsa_name);
  409. return -1;
  410. }
  411. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  412. const snd_pcm_channel_area_t *a = &driver->playback_areas[chn];
  413. driver->playback_addr[chn] = (char *) a->addr + ((a->first + a->step * *playback_offset) / 8);
  414. }
  415. }
  416. return 0;
  417. }
  418. static int
  419. alsa_driver_audio_start (alsa_driver_t *driver)
  420. {
  421. int err;
  422. snd_pcm_uframes_t poffset, pavail;
  423. channel_t chn;
  424. if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
  425. jack_error ("ALSA-HW: prepare error for playback on \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  426. return -1;
  427. }
  428. if (driver->capture_and_playback_not_synced) {
  429. if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
  430. jack_error ("ALSA-HW: prepare error for capture on \"%s\" (%s)", driver->alsa_name, snd_strerror(err));
  431. return -1;
  432. }
  433. }
  434. if (driver->hw_monitoring) {
  435. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  436. }
  437. /* fill playback buffer with zeroes, and mark
  438. all fragments as having data.
  439. */
  440. pavail = snd_pcm_avail_update (driver->playback_handle);
  441. if (pavail != driver->buffer_frames) {
  442. jack_error ("ALSA-HW: full buffer not available at start");
  443. return -1;
  444. }
  445. if (alsa_driver_get_channel_addresses (driver, 0, &pavail, 0, &poffset)) {
  446. return -1;
  447. }
  448. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  449. alsa_driver_silence_on_channel (driver, chn, driver->buffer_frames);
  450. }
  451. snd_pcm_mmap_commit (driver->playback_handle, poffset, driver->buffer_frames);
  452. if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
  453. jack_error ("could not start playback (%s)", snd_strerror (err));
  454. return -1;
  455. }
  456. if (driver->capture_and_playback_not_synced) {
  457. if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
  458. jack_error ("could not start capture (%s)", snd_strerror (err));
  459. return -1;
  460. }
  461. }
  462. if (driver->hw_monitoring && (driver->input_monitor_mask || driver->all_monitor_in)) {
  463. if (driver->all_monitor_in) {
  464. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  465. } else {
  466. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  467. }
  468. }
  469. driver->playback_nfds = snd_pcm_poll_descriptors_count (driver->playback_handle);
  470. driver->capture_nfds = snd_pcm_poll_descriptors_count (driver->capture_handle);
  471. if (driver->pfd)
  472. free (driver->pfd);
  473. driver->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) *
  474. (driver->playback_nfds + driver->capture_nfds + 1));
  475. return 0;
  476. }
  477. static int
  478. alsa_driver_audio_stop (alsa_driver_t *driver)
  479. {
  480. int err;
  481. if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
  482. jack_error ("alsa_pcm: channel flush for playback failed (%s)", snd_strerror (err));
  483. return -1;
  484. }
  485. if (driver->capture_and_playback_not_synced) {
  486. if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
  487. jack_error ("alsa_pcm: channel flush for capture failed (%s)", snd_strerror (err));
  488. return -1;
  489. }
  490. }
  491. driver->hw->set_input_monitor_mask (driver->hw, 0);
  492. return 0;
  493. }
  494. static int
  495. alsa_driver_xrun_recovery (alsa_driver_t *driver)
  496. {
  497. snd_pcm_sframes_t capture_delay;
  498. int err;
  499. if ((err = snd_pcm_delay (driver->capture_handle, &capture_delay))) {
  500. jack_error ("alsa_pcm: cannot determine capture delay (%s)", snd_strerror (err));
  501. exit (1);
  502. }
  503. fprintf (stderr, "alsa_pcm: xrun of %lu frames, (%.3f msecs)\n", capture_delay,
  504. ((float) capture_delay / (float) driver->frame_rate) * 1000.0);
  505. #if ENGINE
  506. if (!engine->xrun_recoverable ()) {
  507. /* don't report an error here, its distracting */
  508. return -1;
  509. }
  510. #endif
  511. if (alsa_driver_audio_stop (driver) || alsa_driver_audio_start (driver)) {
  512. return -1;
  513. }
  514. return 0;
  515. }
  516. static void
  517. alsa_driver_silence_untouched_channels (alsa_driver_t *driver, nframes_t nframes)
  518. {
  519. channel_t chn;
  520. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  521. if ((driver->channels_not_done & (1<<chn))) {
  522. if (driver->silent[chn] < driver->buffer_frames) {
  523. alsa_driver_silence_on_channel_no_mark (driver, chn, nframes);
  524. driver->silent[chn] += nframes;
  525. }
  526. }
  527. }
  528. }
  529. void
  530. alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn, ClockSyncStatus status)
  531. {
  532. driver->clock_sync_data[chn] = status;
  533. jack_driver_clock_sync_notify ((jack_driver_t *) driver, chn, status);
  534. }
  535. static int under_gdb = FALSE;
  536. static int
  537. alsa_driver_wait (alsa_driver_t *driver)
  538. {
  539. snd_pcm_sframes_t avail = 0;
  540. snd_pcm_sframes_t contiguous = 0;
  541. snd_pcm_sframes_t capture_avail = 0;
  542. snd_pcm_sframes_t playback_avail = 0;
  543. snd_pcm_uframes_t capture_offset = 0;
  544. snd_pcm_uframes_t playback_offset = 0;
  545. int xrun_detected;
  546. channel_t chn;
  547. GSList *node;
  548. sample_t *buffer;
  549. int need_capture = 1;
  550. int need_playback = 1;
  551. int i;
  552. again:
  553. while (need_playback || need_capture) {
  554. int p_timed_out, c_timed_out;
  555. int ci = 0;
  556. int nfds;
  557. nfds = 0;
  558. if (need_playback) {
  559. snd_pcm_poll_descriptors (driver->playback_handle, &driver->pfd[0], driver->playback_nfds);
  560. nfds += driver->playback_nfds;
  561. }
  562. if (need_capture) {
  563. snd_pcm_poll_descriptors (driver->capture_handle, &driver->pfd[nfds], driver->capture_nfds);
  564. ci = nfds;
  565. nfds += driver->capture_nfds;
  566. }
  567. /* ALSA doesn't set POLLERR in some versions of 0.9.X */
  568. for (i = 0; i < nfds; i++) {
  569. driver->pfd[nfds].events |= POLLERR;
  570. }
  571. if (poll (driver->pfd, nfds, 1000) < 0) {
  572. if (errno == EINTR) {
  573. printf ("poll interrupt\n");
  574. // this happens mostly when run
  575. // under gdb, or when exiting due to a signal
  576. if (under_gdb) {
  577. goto again;
  578. }
  579. return 1;
  580. }
  581. jack_error ("ALSA::Device: poll call failed (%s)", strerror (errno));
  582. return -1;
  583. }
  584. if (driver->engine) {
  585. struct timeval tv;
  586. gettimeofday (&tv, NULL);
  587. driver->engine->control->time.microseconds = tv.tv_sec * 1000000 + tv.tv_usec;
  588. }
  589. p_timed_out = 0;
  590. if (need_playback) {
  591. for (i = 0; i < driver->playback_nfds; i++) {
  592. if (driver->pfd[i].revents & POLLERR) {
  593. jack_error ("ALSA: poll reports error on playback stream.");
  594. return -1;
  595. }
  596. if (driver->pfd[i].revents == 0) {
  597. p_timed_out++;
  598. }
  599. }
  600. if (p_timed_out == 0) {
  601. need_playback = 0;
  602. }
  603. }
  604. c_timed_out = 0;
  605. if (need_capture) {
  606. for (i = ci; i < nfds; i++) {
  607. if (driver->pfd[i].revents & POLLERR) {
  608. jack_error ("ALSA: poll reports error on capture stream.");
  609. return -1;
  610. }
  611. if (driver->pfd[i].revents == 0) {
  612. c_timed_out++;
  613. }
  614. }
  615. if (c_timed_out == 0) {
  616. need_capture = 0;
  617. }
  618. }
  619. if (p_timed_out == driver->playback_nfds && c_timed_out == driver->capture_nfds) {
  620. jack_error ("ALSA: poll time out");
  621. return -1;
  622. }
  623. }
  624. xrun_detected = FALSE;
  625. if ((capture_avail = snd_pcm_avail_update (driver->capture_handle)) < 0) {
  626. if (capture_avail == -EPIPE) {
  627. xrun_detected = TRUE;
  628. } else {
  629. jack_error ("unknown ALSA avail_update return value (%u)", capture_avail);
  630. }
  631. }
  632. if ((playback_avail = snd_pcm_avail_update (driver->playback_handle)) < 0) {
  633. if (playback_avail == -EPIPE) {
  634. xrun_detected = TRUE;
  635. } else {
  636. jack_error ("unknown ALSA avail_update return value (%u)", playback_avail);
  637. }
  638. }
  639. if (xrun_detected) {
  640. return alsa_driver_xrun_recovery (driver);
  641. }
  642. avail = capture_avail < playback_avail ? capture_avail : playback_avail;
  643. /* constrain the available count to the nearest (round down) number of
  644. periods.
  645. */
  646. avail = avail - (avail % driver->frames_per_cycle);
  647. while (avail) {
  648. capture_avail = (avail > driver->frames_per_cycle) ? driver->frames_per_cycle : avail;
  649. playback_avail = (avail > driver->frames_per_cycle) ? driver->frames_per_cycle : avail;
  650. if (alsa_driver_get_channel_addresses (driver,
  651. (snd_pcm_uframes_t *) &capture_avail,
  652. (snd_pcm_uframes_t *) &playback_avail,
  653. &capture_offset, &playback_offset) < 0) {
  654. return -1;
  655. }
  656. contiguous = capture_avail < playback_avail ? capture_avail : playback_avail;
  657. /* XXX possible race condition here with silence_pending */
  658. /* XXX this design is wrong. cf. ardour/audioengine *** FIX ME *** */
  659. if (driver->silence_pending) {
  660. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  661. if (driver->silence_pending & (1<<chn)) {
  662. alsa_driver_silence_on_channel (driver, chn, contiguous);
  663. }
  664. }
  665. driver->silence_pending = 0;
  666. }
  667. driver->channels_not_done = driver->channel_done_bits;
  668. /* XXX race condition on engine ptr */
  669. if (driver->engine && driver->engine->process (driver->engine, contiguous)) {
  670. jack_error ("alsa_pcm: engine processing error - stopping.");
  671. return -1;
  672. }
  673. /* now move data from ports to channels */
  674. for (chn = 0, node = driver->playback_ports; node; node = g_slist_next (node), chn++) {
  675. jack_port_t *port = (jack_port_t *) node->data;
  676. /* optimize needless data copying away */
  677. if (port->connections == 0) {
  678. continue;
  679. }
  680. buffer = (sample_t *) jack_port_get_buffer (port, contiguous);
  681. alsa_driver_write_to_channel (driver, chn, buffer, contiguous, 0, 1.0);
  682. }
  683. /* Now handle input monitoring */
  684. driver->input_monitor_mask = 0;
  685. for (chn = 0, node = driver->capture_ports; node; node = g_slist_next (node), chn++) {
  686. if (((jack_port_t *) node->data)->shared->monitor_requests) {
  687. driver->input_monitor_mask |= (1<<chn);
  688. }
  689. }
  690. if (!driver->hw_monitoring) {
  691. if (driver->all_monitor_in) {
  692. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  693. alsa_driver_copy_channel (driver, chn, chn, contiguous);
  694. }
  695. } else if (driver->input_monitor_mask) {
  696. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  697. if (driver->input_monitor_mask & (1<<chn)) {
  698. alsa_driver_copy_channel (driver, chn, chn, contiguous);
  699. }
  700. }
  701. }
  702. } else {
  703. if ((driver->hw->input_monitor_mask != driver->input_monitor_mask) && !driver->all_monitor_in) {
  704. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  705. }
  706. }
  707. if (driver->channels_not_done) {
  708. alsa_driver_silence_untouched_channels (driver, contiguous);
  709. }
  710. snd_pcm_mmap_commit (driver->capture_handle, capture_offset, contiguous);
  711. snd_pcm_mmap_commit (driver->playback_handle, playback_offset, contiguous);
  712. avail -= contiguous;
  713. }
  714. // rdtscl (now);
  715. // fprintf (stderr, "engine cycle took %.6f usecs\n", (((float) (now - start))/450.0f));
  716. return 0;
  717. }
  718. static int
  719. alsa_driver_process (nframes_t nframes, void *arg)
  720. {
  721. alsa_driver_t *driver = (alsa_driver_t *) arg;
  722. channel_t chn;
  723. jack_port_t *port;
  724. GSList *node;
  725. for (chn = 0, node = driver->capture_ports; node; node = g_slist_next (node), chn++) {
  726. port = (jack_port_t *) node->data;
  727. if (port->connections == 0) {
  728. continue;
  729. }
  730. alsa_driver_read_from_channel (driver, chn, jack_port_buffer (port), nframes, 0);
  731. }
  732. return 0;
  733. }
  734. static void
  735. alsa_driver_attach (alsa_driver_t *driver, jack_engine_t *engine)
  736. {
  737. char buf[32];
  738. channel_t chn;
  739. jack_port_t *port;
  740. driver->engine = engine;
  741. driver->engine->set_buffer_size (engine, driver->frames_per_cycle);
  742. driver->engine->set_sample_rate (engine, driver->frame_rate);
  743. /* Now become a client of the engine */
  744. if ((driver->client = jack_driver_become_client ("alsa_pcm")) == NULL) {
  745. jack_error ("ALSA: cannot become client");
  746. return;
  747. }
  748. jack_set_process_callback (driver->client, alsa_driver_process, driver);
  749. for (chn = 0; chn < driver->capture_nchannels; chn++) {
  750. snprintf (buf, sizeof(buf) - 1, "in_%lu", chn+1);
  751. port = jack_port_register (driver->client, buf,
  752. JACK_DEFAULT_AUDIO_TYPE,
  753. JackPortIsOutput|JackPortIsPhysical|JackPortCanMonitor, 0);
  754. if (port == 0) {
  755. jack_error ("ALSA: cannot register port for %s", buf);
  756. break;
  757. }
  758. /* XXX fix this so that it can handle: systemic (external) latency
  759. */
  760. jack_port_set_latency (port, driver->frames_per_cycle * driver->nfragments);
  761. driver->capture_ports = g_slist_append (driver->capture_ports, port);
  762. }
  763. for (chn = 0; chn < driver->playback_nchannels; chn++) {
  764. snprintf (buf, sizeof(buf) - 1, "out_%lu", chn+1);
  765. port = jack_port_register (driver->client, buf,
  766. JACK_DEFAULT_AUDIO_TYPE,
  767. JackPortIsInput|JackPortIsPhysical, 0);
  768. if (port == 0) {
  769. jack_error ("ALSA: cannot register port for %s", buf);
  770. break;
  771. }
  772. /* XXX fix this so that it can handle: systemic (external) latency
  773. */
  774. jack_port_set_latency (port, driver->frames_per_cycle * driver->nfragments);
  775. driver->playback_ports = g_slist_append (driver->playback_ports, port);
  776. }
  777. printf ("ALSA: ports registered, starting driver\n");
  778. jack_activate (driver->client);
  779. }
  780. static void
  781. alsa_driver_detach (alsa_driver_t *driver, jack_engine_t *engine)
  782. {
  783. GSList *node;
  784. if (driver->engine == 0) {
  785. return;
  786. }
  787. for (node = driver->capture_ports; node; node = g_slist_next (node)) {
  788. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  789. }
  790. g_slist_free (driver->capture_ports);
  791. driver->capture_ports = 0;
  792. for (node = driver->playback_ports; node; node = g_slist_next (node)) {
  793. jack_port_unregister (driver->client, ((jack_port_t *) node->data));
  794. }
  795. g_slist_free (driver->playback_ports);
  796. driver->playback_ports = 0;
  797. driver->engine = 0;
  798. }
  799. static int
  800. alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
  801. {
  802. return driver->hw->change_sample_clock (driver->hw, mode);
  803. }
  804. static void
  805. alsa_driver_mark_channel_silent (alsa_driver_t *driver, unsigned long chn)
  806. {
  807. driver->silence_pending |= (1<<chn);
  808. }
  809. static void
  810. alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
  811. {
  812. if (driver->hw_monitoring) {
  813. if (yn) {
  814. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  815. } else {
  816. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  817. }
  818. }
  819. driver->all_monitor_in = yn;
  820. }
  821. static void
  822. alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
  823. {
  824. if (yn) {
  825. driver->hw_monitoring = TRUE;
  826. if (driver->all_monitor_in) {
  827. driver->hw->set_input_monitor_mask (driver->hw, ~0U);
  828. } else {
  829. driver->hw->set_input_monitor_mask (driver->hw, driver->input_monitor_mask);
  830. }
  831. } else {
  832. driver->hw_monitoring = FALSE;
  833. driver->hw->set_input_monitor_mask (driver->hw, 0);
  834. }
  835. }
  836. static ClockSyncStatus
  837. alsa_driver_clock_sync_status (channel_t chn)
  838. {
  839. return Lock;
  840. }
  841. static void
  842. alsa_driver_delete (alsa_driver_t *driver)
  843. {
  844. if (driver->capture_handle) {
  845. snd_pcm_close (driver->capture_handle);
  846. driver->capture_handle = 0;
  847. }
  848. if (driver->playback_handle) {
  849. snd_pcm_close (driver->playback_handle);
  850. driver->capture_handle = 0;
  851. }
  852. if (driver->capture_hw_params) {
  853. snd_pcm_hw_params_free (driver->capture_hw_params);
  854. driver->capture_hw_params = 0;
  855. }
  856. if (driver->playback_hw_params) {
  857. snd_pcm_hw_params_free (driver->playback_hw_params);
  858. driver->playback_hw_params = 0;
  859. }
  860. if (driver->capture_sw_params) {
  861. snd_pcm_sw_params_free (driver->capture_sw_params);
  862. driver->capture_sw_params = 0;
  863. }
  864. if (driver->playback_sw_params) {
  865. snd_pcm_sw_params_free (driver->playback_sw_params);
  866. driver->playback_sw_params = 0;
  867. }
  868. if (driver->pfd) {
  869. free (driver->pfd);
  870. }
  871. if (driver->hw) {
  872. driver->hw->release (driver->hw);
  873. driver->hw = 0;
  874. }
  875. free(driver->alsa_name);
  876. free(driver->alsa_driver);
  877. alsa_driver_release_channel_dependent_memory (driver);
  878. jack_driver_release ((jack_driver_t *) driver);
  879. free (driver);
  880. }
  881. static jack_driver_t *
  882. alsa_driver_new (char *name, char *alsa_device,
  883. nframes_t frames_per_cycle,
  884. nframes_t rate,
  885. int hw_monitoring)
  886. {
  887. int err;
  888. alsa_driver_t *driver;
  889. printf ("creating alsa driver ... %s|%lu|%lu|%s\n",
  890. alsa_device, frames_per_cycle, rate,
  891. hw_monitoring ? "hwmon":"swmon");
  892. driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
  893. jack_driver_init ((jack_driver_t *) driver);
  894. driver->attach = (JackDriverAttachFunction) alsa_driver_attach;
  895. driver->detach = (JackDriverDetachFunction) alsa_driver_detach;
  896. driver->wait = (JackDriverWaitFunction) alsa_driver_wait;
  897. driver->audio_stop = (JackDriverAudioStopFunction) alsa_driver_audio_stop;
  898. driver->audio_start = (JackDriverAudioStartFunction) alsa_driver_audio_start;
  899. driver->set_hw_monitoring = (JackDriverSetHwMonitoringFunction) alsa_driver_set_hw_monitoring ;
  900. driver->reset_parameters = (JackDriverResetParametersFunction) alsa_driver_reset_parameters;
  901. driver->mark_channel_silent = (JackDriverMarkChannelSilentFunction) alsa_driver_mark_channel_silent;
  902. driver->request_all_monitor_input = (JackDriverRequestAllMonitorInputFunction) alsa_driver_request_all_monitor_input;
  903. driver->clock_sync_status = (JackDriverClockSyncStatusFunction) alsa_driver_clock_sync_status;
  904. driver->change_sample_clock = (JackDriverChangeSampleClockFunction) alsa_driver_change_sample_clock;
  905. driver->ctl_handle = 0;
  906. driver->hw = 0;
  907. driver->capture_and_playback_not_synced = FALSE;
  908. driver->nfragments = 0;
  909. driver->max_nchannels = 0;
  910. driver->user_nchannels = 0;
  911. driver->playback_nchannels = 0;
  912. driver->capture_nchannels = 0;
  913. driver->playback_addr = 0;
  914. driver->capture_addr = 0;
  915. driver->silence_pending = 0;
  916. driver->silent = 0;
  917. driver->all_monitor_in = FALSE;
  918. driver->clock_mode = ClockMaster; /* XXX is it? */
  919. driver->input_monitor_mask = 0; /* XXX is it? */
  920. driver->capture_ports = 0;
  921. driver->playback_ports = 0;
  922. driver->pfd = 0;
  923. driver->playback_nfds = 0;
  924. driver->capture_nfds = 0;
  925. if ((err = snd_pcm_open (&driver->playback_handle, alsa_device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
  926. jack_error ("ALSA: Cannot open PCM device %s/%s", name, alsa_device);
  927. free (driver);
  928. return 0;
  929. }
  930. driver->alsa_name = strdup (alsa_device);
  931. if ((err = snd_pcm_open (&driver->capture_handle, alsa_device, SND_PCM_STREAM_CAPTURE, 0)) < 0) {
  932. jack_error ("ALSA: Cannot open PCM device %s", name);
  933. free (driver);
  934. return 0;
  935. }
  936. if (alsa_driver_check_card_type (driver)) {
  937. free (driver);
  938. return 0;
  939. }
  940. driver->playback_hw_params = 0;
  941. driver->capture_hw_params = 0;
  942. driver->playback_sw_params = 0;
  943. driver->capture_sw_params = 0;
  944. if ((err = snd_pcm_hw_params_malloc (&driver->playback_hw_params)) < 0) {
  945. jack_error ("ALSA: could no allocate playback hw params structure");
  946. alsa_driver_delete (driver);
  947. return 0;
  948. }
  949. if ((err = snd_pcm_hw_params_malloc (&driver->capture_hw_params)) < 0) {
  950. jack_error ("ALSA: could no allocate capture hw params structure");
  951. alsa_driver_delete (driver);
  952. return 0;
  953. }
  954. if ((err = snd_pcm_sw_params_malloc (&driver->playback_sw_params)) < 0) {
  955. jack_error ("ALSA: could no allocate playback sw params structure");
  956. alsa_driver_delete (driver);
  957. return 0;
  958. }
  959. if ((err = snd_pcm_sw_params_malloc (&driver->capture_sw_params)) < 0) {
  960. jack_error ("ALSA: could no allocate capture sw params structure");
  961. alsa_driver_delete (driver);
  962. return 0;
  963. }
  964. if (alsa_driver_set_parameters (driver, frames_per_cycle, rate)) {
  965. alsa_driver_delete (driver);
  966. return 0;
  967. }
  968. if (snd_pcm_link (driver->capture_handle, driver->playback_handle) != 0) {
  969. driver->capture_and_playback_not_synced = TRUE;
  970. } else {
  971. driver->capture_and_playback_not_synced = FALSE;
  972. }
  973. alsa_driver_hw_specific (driver, hw_monitoring);
  974. return (jack_driver_t *) driver;
  975. }
  976. /* PLUGIN INTERFACE */
  977. jack_driver_t *
  978. driver_initialize (va_list ap)
  979. {
  980. nframes_t srate;
  981. nframes_t frames_per_interrupt;
  982. char *pcm_name;
  983. int hw_monitoring;
  984. pcm_name = va_arg (ap, char *);
  985. frames_per_interrupt = va_arg (ap, nframes_t);
  986. srate = va_arg (ap, nframes_t);
  987. hw_monitoring = va_arg (ap, int);
  988. return alsa_driver_new ("alsa_pcm", pcm_name, frames_per_interrupt, srate, hw_monitoring);
  989. }
  990. void
  991. driver_finish (jack_driver_t *driver)
  992. {
  993. alsa_driver_delete ((alsa_driver_t *) driver);
  994. }