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.

1237 lines
34KB

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