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.

1340 lines
36KB

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