remove port_(un)lock functions; add dmitry's new time APIs (jack_get_time, jack_frame...
[jack.git] / drivers / alsa / alsa_driver.c
blob484d2dad1d3f9ec794db8af898bde8fe3227e3f1
1 /* -*- mode: c; c-file-style: "linux"; -*- */
2 /*
3 Copyright (C) 2001 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 $Id$
22 #include <math.h>
23 #include <stdio.h>
24 #include <memory.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <signal.h>
30 #include <sys/types.h>
31 #include <regex.h>
32 #include <string.h>
34 #include <jack/internal.h>
35 #include <jack/engine.h>
36 #include <jack/messagebuffer.h>
38 #include <sysdeps/time.h>
40 #include "alsa_driver.h"
41 #include "hammerfall.h"
42 #include "hdsp.h"
43 #include "ice1712.h"
44 #include "usx2y.h"
45 #include "generic.h"
47 extern void store_work_time (int);
48 extern void store_wait_time (int);
49 extern void show_wait_times ();
50 extern void show_work_times ();
52 #undef DEBUG_WAKEUP
54 /* Delay (in process calls) before jackd will report an xrun */
55 #define XRUN_REPORT_DELAY 0
57 static void
58 alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
60 bitset_destroy (&driver->channels_done);
61 bitset_destroy (&driver->channels_not_done);
63 if (driver->playback_addr) {
64 free (driver->playback_addr);
65 driver->playback_addr = 0;
68 if (driver->capture_addr) {
69 free (driver->capture_addr);
70 driver->capture_addr = 0;
73 if (driver->playback_interleave_skip) {
74 free (driver->playback_interleave_skip);
75 driver->playback_interleave_skip = NULL;
78 if (driver->capture_interleave_skip) {
79 free (driver->capture_interleave_skip);
80 driver->capture_interleave_skip = NULL;
83 if (driver->silent) {
84 free (driver->silent);
85 driver->silent = 0;
88 if (driver->dither_state) {
89 free (driver->dither_state);
90 driver->dither_state = 0;
94 static int
95 alsa_driver_check_capabilities (alsa_driver_t *driver)
97 return 0;
100 static int
101 alsa_driver_check_card_type (alsa_driver_t *driver)
103 int err;
104 snd_ctl_card_info_t *card_info;
105 char * ctl_name;
106 regex_t expression;
108 snd_ctl_card_info_alloca (&card_info);
110 regcomp(&expression,"(plug)?hw:[0-9](,[0-9])?",REG_ICASE|REG_EXTENDED);
112 if (!regexec(&expression,driver->alsa_name_playback,0,NULL,0)) {
113 /* the user wants a hw or plughw device, the ctl name
114 * should be hw:x where x is the card number */
116 char tmp[5];
117 strncpy(tmp,strstr(driver->alsa_name_playback,"hw"),4);
118 tmp[4]='\0';
119 printf("control device %s\n",tmp);
120 ctl_name = strdup(tmp);
121 } else {
122 ctl_name = strdup(driver->alsa_name_playback);
125 // XXX: I don't know the "right" way to do this. Which to use
126 // driver->alsa_name_playback or driver->alsa_name_capture.
127 if ((err = snd_ctl_open (&driver->ctl_handle, ctl_name, 0)) < 0) {
128 jack_error ("control open \"%s\" (%s)", ctl_name,
129 snd_strerror(err));
130 } else if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
131 jack_error ("control hardware info \"%s\" (%s)",
132 driver->alsa_name_playback, snd_strerror (err));
133 snd_ctl_close (driver->ctl_handle);
136 driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
138 regfree(&expression);
139 free(ctl_name);
141 return alsa_driver_check_capabilities (driver);
144 static int
145 alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
147 driver->hw = jack_alsa_hammerfall_hw_new (driver);
148 return 0;
151 static int
152 alsa_driver_hdsp_hardware (alsa_driver_t *driver)
154 driver->hw = jack_alsa_hdsp_hw_new (driver);
155 return 0;
158 static int
159 alsa_driver_ice1712_hardware (alsa_driver_t *driver)
161 driver->hw = jack_alsa_ice1712_hw_new (driver);
162 return 0;
165 static int
166 alsa_driver_usx2y_hardware (alsa_driver_t *driver)
168 driver->hw = jack_alsa_usx2y_hw_new (driver);
169 return 0;
172 static int
173 alsa_driver_generic_hardware (alsa_driver_t *driver)
175 driver->hw = jack_alsa_generic_hw_new (driver);
176 return 0;
179 static int
180 alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring,
181 int hw_metering)
183 int err;
185 if (!strcmp(driver->alsa_driver, "RME9652")) {
186 if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
187 return err;
189 } else if (!strcmp(driver->alsa_driver, "H-DSP")) {
190 if ((err = alsa_driver_hdsp_hardware (driver)) !=0) {
191 return err;
193 } else if (!strcmp(driver->alsa_driver, "ICE1712")) {
194 if ((err = alsa_driver_ice1712_hardware (driver)) !=0) {
195 return err;
197 } else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
198 if ((err = alsa_driver_usx2y_hardware (driver)) !=0) {
199 return err;
201 } else {
202 if ((err = alsa_driver_generic_hardware (driver)) != 0) {
203 return err;
207 if (driver->hw->capabilities & Cap_HardwareMonitoring) {
208 driver->has_hw_monitoring = TRUE;
209 /* XXX need to ensure that this is really FALSE or
210 * TRUE or whatever*/
211 driver->hw_monitoring = hw_monitoring;
212 } else {
213 driver->has_hw_monitoring = FALSE;
214 driver->hw_monitoring = FALSE;
217 if (driver->hw->capabilities & Cap_ClockLockReporting) {
218 driver->has_clock_sync_reporting = TRUE;
219 } else {
220 driver->has_clock_sync_reporting = FALSE;
223 if (driver->hw->capabilities & Cap_HardwareMetering) {
224 driver->has_hw_metering = TRUE;
225 driver->hw_metering = hw_metering;
226 } else {
227 driver->has_hw_metering = FALSE;
228 driver->hw_metering = FALSE;
231 return 0;
234 static void
235 alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
237 switch (driver->playback_sample_bytes) {
238 case 2:
239 if (driver->playback_interleaved) {
240 driver->channel_copy = memcpy_interleave_d16_s16;
241 } else {
242 driver->channel_copy = memcpy_fake;
245 switch (driver->dither) {
246 case Rectangular:
247 printf("Rectangular dithering at 16 bits\n");
248 driver->write_via_copy = driver->quirk_bswap?
249 sample_move_dither_rect_d16_sSs:
250 sample_move_dither_rect_d16_sS;
251 break;
253 case Triangular:
254 printf("Triangular dithering at 16 bits\n");
255 driver->write_via_copy = driver->quirk_bswap?
256 sample_move_dither_tri_d16_sSs:
257 sample_move_dither_tri_d16_sS;
258 break;
260 case Shaped:
261 printf("Noise-shaped dithering at 16 bits\n");
262 driver->write_via_copy = driver->quirk_bswap?
263 sample_move_dither_shaped_d16_sSs:
264 sample_move_dither_shaped_d16_sS;
265 break;
267 default:
268 driver->write_via_copy = driver->quirk_bswap?
269 sample_move_d16_sSs : sample_move_d16_sS;
270 break;
272 break;
274 case 3:
275 if (driver->playback_interleaved) {
276 driver->channel_copy = memcpy_interleave_d24_s24;
277 } else {
278 driver->channel_copy = memcpy_fake;
281 switch (driver->dither) {
282 case Rectangular:
283 printf("Rectangular dithering at 16 bits\n");
284 driver->write_via_copy = driver->quirk_bswap?
285 sample_move_dither_rect_d24_sSs:
286 sample_move_dither_rect_d24_sS;
287 break;
289 case Triangular:
290 printf("Triangular dithering at 16 bits\n");
291 driver->write_via_copy = driver->quirk_bswap?
292 sample_move_dither_tri_d24_sSs:
293 sample_move_dither_tri_d24_sS;
294 break;
296 case Shaped:
297 printf("Noise-shaped dithering at 16 bits\n");
298 driver->write_via_copy = driver->quirk_bswap?
299 sample_move_dither_shaped_d24_sSs:
300 sample_move_dither_shaped_d24_sS;
301 break;
303 default:
304 driver->write_via_copy = driver->quirk_bswap?
305 sample_move_d24_sSs : sample_move_d24_sS;
306 break;
308 break;
310 case 4:
311 if (driver->playback_interleaved) {
312 driver->channel_copy = memcpy_interleave_d32_s32;
313 } else {
314 driver->channel_copy = memcpy_fake;
317 switch (driver->dither) {
318 case Rectangular:
319 printf("Rectangular dithering at 16 bits\n");
320 driver->write_via_copy = driver->quirk_bswap?
321 sample_move_dither_rect_d32u24_sSs:
322 sample_move_dither_rect_d32u24_sS;
323 break;
325 case Triangular:
326 printf("Triangular dithering at 16 bits\n");
327 driver->write_via_copy = driver->quirk_bswap?
328 sample_move_dither_tri_d32u24_sSs:
329 sample_move_dither_tri_d32u24_sS;
330 break;
332 case Shaped:
333 printf("Noise-shaped dithering at 16 bits\n");
334 driver->write_via_copy = driver->quirk_bswap?
335 sample_move_dither_shaped_d32u24_sSs:
336 sample_move_dither_shaped_d32u24_sS;
337 break;
339 default:
340 driver->write_via_copy = driver->quirk_bswap?
341 sample_move_d32u24_sSs : sample_move_d32u24_sS;
342 break;
344 break;
347 switch (driver->capture_sample_bytes) {
348 case 2:
349 driver->read_via_copy = driver->quirk_bswap?
350 sample_move_dS_s16s : sample_move_dS_s16;
351 break;
352 case 3:
353 driver->read_via_copy = driver->quirk_bswap?
354 sample_move_dS_s24s : sample_move_dS_s24;
355 break;
356 case 4:
357 driver->read_via_copy = driver->quirk_bswap?
358 sample_move_dS_s32u24s : sample_move_dS_s32u24;
359 break;
363 static int
364 alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
365 const char *stream_name,
366 snd_pcm_t *handle,
367 snd_pcm_hw_params_t *hw_params,
368 snd_pcm_sw_params_t *sw_params,
369 unsigned int *nperiodsp,
370 channel_t *nchns,
371 unsigned long sample_width)
373 int err, format;
374 unsigned int frame_rate;
375 snd_pcm_uframes_t stop_th;
376 static struct {
377 char Name[32];
378 snd_pcm_format_t format;
379 int swapped;
380 } formats[] = {
381 {"32bit little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
382 {"32bit big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
383 {"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE},
384 {"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE},
385 {"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
386 {"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
388 #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
389 #define FIRST_16BIT_FORMAT 4
391 if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
392 jack_error ("ALSA: no playback configurations available (%s)",
393 snd_strerror (err));
394 return -1;
397 if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
398 < 0) {
399 jack_error ("ALSA: cannot restrict period size to integral"
400 " value.");
401 return -1;
404 if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
405 if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
406 if ((err = snd_pcm_hw_params_set_access (
407 handle, hw_params,
408 SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
409 jack_error ("ALSA: mmap-based access is not possible"
410 " for the %s "
411 "stream of this audio interface",
412 stream_name);
413 return -1;
418 format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
420 while (1) {
421 if ((err = snd_pcm_hw_params_set_format (
422 handle, hw_params, formats[format].format)) < 0) {
424 if ((sample_width == 4
425 ? format++ >= NUMFORMATS - 1
426 : format-- <= 0)) {
427 jack_error ("Sorry. The audio interface \"%s\""
428 " doesn't support any of the"
429 " hardware sample formats that"
430 " JACK's alsa-driver can use.",
431 device_name);
432 return -1;
434 } else {
435 if (formats[format].swapped) {
436 driver->quirk_bswap = 1;
437 } else {
438 driver->quirk_bswap = 0;
440 jack_error ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
441 break;
445 frame_rate = driver->frame_rate ;
446 err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
447 &frame_rate, NULL) ;
448 driver->frame_rate = frame_rate ;
449 if (err < 0) {
450 jack_error ("ALSA: cannot set sample/frame rate to %"
451 PRIu32 " for %s", driver->frame_rate,
452 stream_name);
453 return -1;
455 if (!*nchns) {
456 /*if not user-specified, try to find the maximum
457 * number of channels */
458 unsigned int channels_max ;
459 err = snd_pcm_hw_params_get_channels_max (hw_params,
460 &channels_max);
461 *nchns = channels_max ;
463 if (*nchns > 1024) {
465 /* the hapless user is an unwitting victim of
466 the "default" ALSA PCM device, which can
467 support up to 16 million channels. since
468 they can't be bothered to set up a proper
469 default device, limit the number of
470 channels for them to a sane default.
473 jack_error (
474 "You appear to be using the ALSA software \"plug\" layer, probably\n"
475 "a result of using the \"default\" ALSA device. This is less\n"
476 "efficient than it could be. Consider using a hardware device\n"
477 "instead rather than using the plug layer. Usually the name of the\n"
478 "hardware device that corresponds to the first sound card is hw:0\n"
480 *nchns = 2;
484 if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
485 *nchns)) < 0) {
486 jack_error ("ALSA: cannot set channel count to %u for %s",
487 *nchns, stream_name);
488 return -1;
491 if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
492 driver->frames_per_cycle,
494 < 0) {
495 jack_error ("ALSA: cannot set period size to %" PRIu32
496 " frames for %s", driver->frames_per_cycle,
497 stream_name);
498 return -1;
501 *nperiodsp = driver->user_nperiods;
502 snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
503 if (*nperiodsp < driver->user_nperiods)
504 *nperiodsp = driver->user_nperiods;
505 if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
506 nperiodsp, NULL) < 0) {
507 jack_error ("ALSA: cannot set number of periods to %u for %s",
508 *nperiodsp, stream_name);
509 return -1;
512 if (*nperiodsp < driver->user_nperiods) {
513 jack_error ("ALSA: got smaller periods %u than %u for %s",
514 *nperiodsp, (unsigned int) driver->user_nperiods,
515 stream_name);
516 return -1;
518 jack_error ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
520 if (!jack_power_of_two(driver->frames_per_cycle)) {
521 jack_error("JACK: frames must be a power of two "
522 "(64, 512, 1024, ...)\n");
523 return -1;
526 if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
527 *nperiodsp *
528 driver->frames_per_cycle))
529 < 0) {
530 jack_error ("ALSA: cannot set buffer length to %" PRIu32
531 " for %s",
532 *nperiodsp * driver->frames_per_cycle,
533 stream_name);
534 return -1;
537 if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
538 jack_error ("ALSA: cannot set hardware parameters for %s",
539 stream_name);
540 return -1;
543 snd_pcm_sw_params_current (handle, sw_params);
545 if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
546 0U)) < 0) {
547 jack_error ("ALSA: cannot set start mode for %s", stream_name);
548 return -1;
551 stop_th = *nperiodsp * driver->frames_per_cycle;
552 if (driver->soft_mode) {
553 stop_th = (snd_pcm_uframes_t)-1;
556 if ((err = snd_pcm_sw_params_set_stop_threshold (
557 handle, sw_params, stop_th)) < 0) {
558 jack_error ("ALSA: cannot set stop mode for %s",
559 stream_name);
560 return -1;
563 if ((err = snd_pcm_sw_params_set_silence_threshold (
564 handle, sw_params, 0)) < 0) {
565 jack_error ("ALSA: cannot set silence threshold for %s",
566 stream_name);
567 return -1;
570 #if 0
571 fprintf (stderr, "set silence size to %lu * %lu = %lu\n",
572 driver->frames_per_cycle, *nperiodsp,
573 driver->frames_per_cycle * *nperiodsp);
575 if ((err = snd_pcm_sw_params_set_silence_size (
576 handle, sw_params,
577 driver->frames_per_cycle * *nperiodsp)) < 0) {
578 jack_error ("ALSA: cannot set silence size for %s",
579 stream_name);
580 return -1;
582 #endif
584 if (handle == driver->playback_handle)
585 err = snd_pcm_sw_params_set_avail_min (
586 handle, sw_params,
587 driver->frames_per_cycle
588 * (*nperiodsp - driver->user_nperiods + 1));
589 else
590 err = snd_pcm_sw_params_set_avail_min (
591 handle, sw_params, driver->frames_per_cycle);
593 if (err < 0) {
594 jack_error ("ALSA: cannot set avail min for %s", stream_name);
595 return -1;
598 if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
599 jack_error ("ALSA: cannot set software parameters for %s\n",
600 stream_name);
601 return -1;
604 return 0;
607 static int
608 alsa_driver_set_parameters (alsa_driver_t *driver,
609 jack_nframes_t frames_per_cycle,
610 jack_nframes_t user_nperiods,
611 jack_nframes_t rate)
613 int dir;
614 snd_pcm_uframes_t p_period_size = 0;
615 snd_pcm_uframes_t c_period_size = 0;
616 channel_t chn;
617 unsigned int pr = 0;
618 unsigned int cr = 0;
619 int err;
621 driver->frame_rate = rate;
622 driver->frames_per_cycle = frames_per_cycle;
623 driver->user_nperiods = user_nperiods;
625 fprintf (stderr, "configuring for %" PRIu32 "Hz, period = %"
626 PRIu32 " frames, buffer = %" PRIu32 " periods\n",
627 rate, frames_per_cycle, user_nperiods);
629 if (driver->capture_handle) {
630 if (alsa_driver_configure_stream (
631 driver,
632 driver->alsa_name_capture,
633 "capture",
634 driver->capture_handle,
635 driver->capture_hw_params,
636 driver->capture_sw_params,
637 &driver->capture_nperiods,
638 &driver->capture_nchannels,
639 driver->capture_sample_bytes)) {
640 jack_error ("ALSA: cannot configure capture channel");
641 return -1;
645 if (driver->playback_handle) {
646 if (alsa_driver_configure_stream (
647 driver,
648 driver->alsa_name_playback,
649 "playback",
650 driver->playback_handle,
651 driver->playback_hw_params,
652 driver->playback_sw_params,
653 &driver->playback_nperiods,
654 &driver->playback_nchannels,
655 driver->playback_sample_bytes)) {
656 jack_error ("ALSA: cannot configure playback channel");
657 return -1;
661 /* check the rate, since thats rather important */
663 if (driver->playback_handle) {
664 snd_pcm_hw_params_get_rate (driver->playback_hw_params,
665 &pr, &dir);
668 if (driver->capture_handle) {
669 snd_pcm_hw_params_get_rate (driver->capture_hw_params,
670 &cr, &dir);
673 if (driver->capture_handle && driver->playback_handle) {
674 if (cr != pr) {
675 jack_error ("playback and capture sample rates do "
676 "not match (%d vs. %d)", pr, cr);
679 /* only change if *both* capture and playback rates
680 * don't match requested certain hardware actually
681 * still works properly in full-duplex with slightly
682 * different rate values between adc and dac
684 if (cr != driver->frame_rate && pr != driver->frame_rate) {
685 jack_error ("sample rate in use (%d Hz) does not "
686 "match requested rate (%d Hz)",
687 cr, driver->frame_rate);
688 driver->frame_rate = cr;
692 else if (driver->capture_handle && cr != driver->frame_rate) {
693 jack_error ("capture sample rate in use (%d Hz) does not "
694 "match requested rate (%d Hz)",
695 cr, driver->frame_rate);
696 driver->frame_rate = cr;
698 else if (driver->playback_handle && pr != driver->frame_rate) {
699 jack_error ("playback sample rate in use (%d Hz) does not "
700 "match requested rate (%d Hz)",
701 pr, driver->frame_rate);
702 driver->frame_rate = pr;
706 /* check the fragment size, since thats non-negotiable */
708 if (driver->playback_handle) {
709 snd_pcm_access_t access;
711 err = snd_pcm_hw_params_get_period_size (
712 driver->playback_hw_params, &p_period_size, &dir);
713 err = snd_pcm_hw_params_get_format (
714 driver->playback_hw_params,
715 &(driver->playback_sample_format));
716 err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
717 &access);
718 driver->playback_interleaved =
719 (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
720 || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
722 if (p_period_size != driver->frames_per_cycle) {
723 jack_error ("alsa_pcm: requested an interrupt every %"
724 PRIu32
725 " frames but got %u frames for playback",
726 driver->frames_per_cycle, p_period_size);
727 return -1;
731 if (driver->capture_handle) {
732 snd_pcm_access_t access;
734 err = snd_pcm_hw_params_get_period_size (
735 driver->capture_hw_params, &c_period_size, &dir);
736 err = snd_pcm_hw_params_get_format (
737 driver->capture_hw_params,
738 &(driver->capture_sample_format));
739 err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
740 &access);
741 driver->capture_interleaved =
742 (access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
743 || (access == SND_PCM_ACCESS_MMAP_COMPLEX);
745 if (c_period_size != driver->frames_per_cycle) {
746 jack_error ("alsa_pcm: requested an interrupt every %"
747 PRIu32
748 " frames but got %uc frames for capture",
749 driver->frames_per_cycle, p_period_size);
750 return -1;
754 driver->playback_sample_bytes =
755 snd_pcm_format_physical_width (driver->playback_sample_format)
756 / 8;
757 driver->capture_sample_bytes =
758 snd_pcm_format_physical_width (driver->capture_sample_format)
759 / 8;
761 if (driver->playback_handle) {
762 switch (driver->playback_sample_format) {
763 case SND_PCM_FORMAT_S32_LE:
764 case SND_PCM_FORMAT_S24_3LE:
765 case SND_PCM_FORMAT_S24_3BE:
766 case SND_PCM_FORMAT_S16_LE:
767 case SND_PCM_FORMAT_S32_BE:
768 case SND_PCM_FORMAT_S16_BE:
769 break;
771 default:
772 jack_error ("programming error: unhandled format "
773 "type for playback");
774 exit (1);
778 if (driver->capture_handle) {
779 switch (driver->capture_sample_format) {
780 case SND_PCM_FORMAT_S32_LE:
781 case SND_PCM_FORMAT_S24_3LE:
782 case SND_PCM_FORMAT_S24_3BE:
783 case SND_PCM_FORMAT_S16_LE:
784 case SND_PCM_FORMAT_S32_BE:
785 case SND_PCM_FORMAT_S16_BE:
786 break;
788 default:
789 jack_error ("programming error: unhandled format "
790 "type for capture");
791 exit (1);
795 if (driver->playback_interleaved) {
796 const snd_pcm_channel_area_t *my_areas;
797 snd_pcm_uframes_t offset, frames;
798 if (snd_pcm_mmap_begin(driver->playback_handle,
799 &my_areas, &offset, &frames) < 0) {
800 jack_error ("ALSA: %s: mmap areas info error",
801 driver->alsa_name_playback);
802 return -1;
804 driver->interleave_unit =
805 snd_pcm_format_physical_width (
806 driver->playback_sample_format) / 8;
807 } else {
808 driver->interleave_unit = 0; /* NOT USED */
811 if (driver->capture_interleaved) {
812 const snd_pcm_channel_area_t *my_areas;
813 snd_pcm_uframes_t offset, frames;
814 if (snd_pcm_mmap_begin(driver->capture_handle,
815 &my_areas, &offset, &frames) < 0) {
816 jack_error ("ALSA: %s: mmap areas info error",
817 driver->alsa_name_capture);
818 return -1;
822 if (driver->playback_nchannels > driver->capture_nchannels) {
823 driver->max_nchannels = driver->playback_nchannels;
824 driver->user_nchannels = driver->capture_nchannels;
825 } else {
826 driver->max_nchannels = driver->capture_nchannels;
827 driver->user_nchannels = driver->playback_nchannels;
830 alsa_driver_setup_io_function_pointers (driver);
832 /* Allocate and initialize structures that rely on the
833 channels counts.
835 Set up the bit pattern that is used to record which
836 channels require action on every cycle. any bits that are
837 not set after the engine's process() call indicate channels
838 that potentially need to be silenced.
841 bitset_create (&driver->channels_done, driver->max_nchannels);
842 bitset_create (&driver->channels_not_done, driver->max_nchannels);
844 if (driver->playback_handle) {
845 driver->playback_addr = (char **)
846 malloc (sizeof (char *) * driver->playback_nchannels);
847 memset (driver->playback_addr, 0,
848 sizeof (char *) * driver->playback_nchannels);
849 driver->playback_interleave_skip = (unsigned long *)
850 malloc (sizeof (unsigned long *) * driver->playback_nchannels);
851 memset (driver->playback_interleave_skip, 0,
852 sizeof (unsigned long *) * driver->playback_nchannels);
853 driver->silent = (unsigned long *)
854 malloc (sizeof (unsigned long)
855 * driver->playback_nchannels);
857 for (chn = 0; chn < driver->playback_nchannels; chn++) {
858 driver->silent[chn] = 0;
861 for (chn = 0; chn < driver->playback_nchannels; chn++) {
862 bitset_add (driver->channels_done, chn);
865 driver->dither_state = (dither_state_t *)
866 calloc ( driver->playback_nchannels,
867 sizeof (dither_state_t));
870 if (driver->capture_handle) {
871 driver->capture_addr = (char **)
872 malloc (sizeof (char *) * driver->capture_nchannels);
873 memset (driver->capture_addr, 0,
874 sizeof (char *) * driver->capture_nchannels);
875 driver->capture_interleave_skip = (unsigned long *)
876 malloc (sizeof (unsigned long *) * driver->capture_nchannels);
877 memset (driver->capture_interleave_skip, 0,
878 sizeof (unsigned long *) * driver->capture_nchannels);
881 driver->clock_sync_data = (ClockSyncStatus *)
882 malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
884 driver->period_usecs =
885 (jack_time_t) floor ((((float) driver->frames_per_cycle) /
886 driver->frame_rate) * 1000000.0f);
887 driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
889 if (driver->engine) {
890 driver->engine->set_buffer_size (driver->engine,
891 driver->frames_per_cycle);
894 return 0;
897 static int
898 alsa_driver_reset_parameters (alsa_driver_t *driver,
899 jack_nframes_t frames_per_cycle,
900 jack_nframes_t user_nperiods,
901 jack_nframes_t rate)
903 /* XXX unregister old ports ? */
904 alsa_driver_release_channel_dependent_memory (driver);
905 return alsa_driver_set_parameters (driver,
906 frames_per_cycle,
907 user_nperiods, rate);
910 static int
911 alsa_driver_get_channel_addresses (alsa_driver_t *driver,
912 snd_pcm_uframes_t *capture_avail,
913 snd_pcm_uframes_t *playback_avail,
914 snd_pcm_uframes_t *capture_offset,
915 snd_pcm_uframes_t *playback_offset)
917 unsigned long err;
918 channel_t chn;
920 if (capture_avail) {
921 if ((err = snd_pcm_mmap_begin (
922 driver->capture_handle, &driver->capture_areas,
923 (snd_pcm_uframes_t *) capture_offset,
924 (snd_pcm_uframes_t *) capture_avail)) < 0) {
925 jack_error ("ALSA: %s: mmap areas info error",
926 driver->alsa_name_capture);
927 return -1;
930 for (chn = 0; chn < driver->capture_nchannels; chn++) {
931 const snd_pcm_channel_area_t *a =
932 &driver->capture_areas[chn];
933 driver->capture_addr[chn] = (char *) a->addr
934 + ((a->first + a->step * *capture_offset) / 8);
935 driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
939 if (playback_avail) {
940 if ((err = snd_pcm_mmap_begin (
941 driver->playback_handle, &driver->playback_areas,
942 (snd_pcm_uframes_t *) playback_offset,
943 (snd_pcm_uframes_t *) playback_avail)) < 0) {
944 jack_error ("ALSA: %s: mmap areas info error ",
945 driver->alsa_name_playback);
946 return -1;
949 for (chn = 0; chn < driver->playback_nchannels; chn++) {
950 const snd_pcm_channel_area_t *a =
951 &driver->playback_areas[chn];
952 driver->playback_addr[chn] = (char *) a->addr
953 + ((a->first + a->step * *playback_offset) / 8);
954 driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
958 return 0;
961 static int
962 alsa_driver_start (alsa_driver_t *driver)
964 int err;
965 snd_pcm_uframes_t poffset, pavail;
966 channel_t chn;
968 driver->poll_last = 0;
969 driver->poll_next = 0;
971 if (driver->playback_handle) {
972 if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
973 jack_error ("ALSA: prepare error for playback on "
974 "\"%s\" (%s)", driver->alsa_name_playback,
975 snd_strerror(err));
976 return -1;
980 if ((driver->capture_handle && driver->capture_and_playback_not_synced)
981 || !driver->playback_handle) {
982 if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
983 jack_error ("ALSA: prepare error for capture on \"%s\""
984 " (%s)", driver->alsa_name_capture,
985 snd_strerror(err));
986 return -1;
990 if (driver->hw_monitoring) {
991 if (driver->input_monitor_mask || driver->all_monitor_in) {
992 if (driver->all_monitor_in) {
993 driver->hw->set_input_monitor_mask (driver->hw, ~0U);
994 } else {
995 driver->hw->set_input_monitor_mask (
996 driver->hw, driver->input_monitor_mask);
998 } else {
999 driver->hw->set_input_monitor_mask (driver->hw,
1000 driver->input_monitor_mask);
1004 if (driver->playback_handle) {
1005 driver->playback_nfds =
1006 snd_pcm_poll_descriptors_count (driver->playback_handle);
1007 } else {
1008 driver->playback_nfds = 0;
1011 if (driver->capture_handle) {
1012 driver->capture_nfds =
1013 snd_pcm_poll_descriptors_count (driver->capture_handle);
1014 } else {
1015 driver->capture_nfds = 0;
1018 if (driver->pfd) {
1019 free (driver->pfd);
1022 driver->pfd = (struct pollfd *)
1023 malloc (sizeof (struct pollfd) *
1024 (driver->playback_nfds + driver->capture_nfds + 2));
1026 if (driver->playback_handle) {
1027 /* fill playback buffer with zeroes, and mark
1028 all fragments as having data.
1031 pavail = snd_pcm_avail_update (driver->playback_handle);
1033 if (pavail !=
1034 driver->frames_per_cycle * driver->playback_nperiods) {
1035 jack_error ("ALSA: full buffer not available at start");
1036 return -1;
1039 if (alsa_driver_get_channel_addresses (driver,
1040 0, &pavail, 0, &poffset)) {
1041 return -1;
1044 /* XXX this is cheating. ALSA offers no guarantee that
1045 we can access the entire buffer at any one time. It
1046 works on most hardware tested so far, however, buts
1047 its a liability in the long run. I think that
1048 alsa-lib may have a better function for doing this
1049 here, where the goal is to silence the entire
1050 buffer.
1053 for (chn = 0; chn < driver->playback_nchannels; chn++) {
1054 alsa_driver_silence_on_channel (
1055 driver, chn,
1056 driver->user_nperiods
1057 * driver->frames_per_cycle);
1060 snd_pcm_mmap_commit (driver->playback_handle, poffset,
1061 driver->user_nperiods
1062 * driver->frames_per_cycle);
1064 if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
1065 jack_error ("ALSA: could not start playback (%s)",
1066 snd_strerror (err));
1067 return -1;
1071 if ((driver->capture_handle && driver->capture_and_playback_not_synced)
1072 || !driver->playback_handle) {
1073 if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
1074 jack_error ("ALSA: could not start capture (%s)",
1075 snd_strerror (err));
1076 return -1;
1080 return 0;
1083 static int
1084 alsa_driver_stop (alsa_driver_t *driver)
1086 int err;
1087 JSList* node;
1088 int chn;
1090 /* silence all capture port buffers, because we might
1091 be entering offline mode.
1094 for (chn = 0, node = driver->capture_ports; node;
1095 node = jack_slist_next (node), chn++) {
1097 jack_port_t* port;
1098 char* buf;
1099 jack_nframes_t nframes = driver->engine->control->buffer_size;
1101 port = (jack_port_t *) node->data;
1102 buf = jack_port_get_buffer (port, nframes);
1103 memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
1106 if (driver->playback_handle) {
1107 if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
1108 jack_error ("ALSA: channel flush for playback "
1109 "failed (%s)", snd_strerror (err));
1110 return -1;
1114 if (!driver->playback_handle
1115 || driver->capture_and_playback_not_synced) {
1116 if (driver->capture_handle) {
1117 if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
1118 jack_error ("ALSA: channel flush for "
1119 "capture failed (%s)",
1120 snd_strerror (err));
1121 return -1;
1126 if (driver->hw_monitoring) {
1127 driver->hw->set_input_monitor_mask (driver->hw, 0);
1130 return 0;
1133 static int
1134 alsa_driver_restart (alsa_driver_t *driver)
1136 if (driver->nt_stop((struct _jack_driver_nt *) driver))
1137 return -1;
1138 return driver->nt_start((struct _jack_driver_nt *) driver);
1141 static int
1142 alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
1144 snd_pcm_status_t *status;
1145 int res;
1147 snd_pcm_status_alloca(&status);
1149 if (driver->capture_handle) {
1150 if ((res = snd_pcm_status(driver->capture_handle, status))
1151 < 0) {
1152 jack_error("status error: %s", snd_strerror(res));
1154 } else {
1155 if ((res = snd_pcm_status(driver->playback_handle, status))
1156 < 0) {
1157 jack_error("status error: %s", snd_strerror(res));
1161 if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN
1162 && driver->process_count > XRUN_REPORT_DELAY) {
1163 struct timeval now, diff, tstamp;
1164 driver->xrun_count++;
1165 gettimeofday(&now, 0);
1166 snd_pcm_status_get_trigger_tstamp(status, &tstamp);
1167 timersub(&now, &tstamp, &diff);
1168 *delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
1169 MESSAGE("\n\n**** alsa_pcm: xrun of at least %.3f "
1170 "msecs\n\n",
1171 *delayed_usecs / 1000.0);
1174 if (alsa_driver_restart (driver)) {
1175 return -1;
1177 return 0;
1180 void
1181 alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
1182 jack_nframes_t nframes)
1184 channel_t chn;
1185 jack_nframes_t buffer_frames =
1186 driver->frames_per_cycle * driver->playback_nperiods;
1188 for (chn = 0; chn < driver->playback_nchannels; chn++) {
1189 if (bitset_contains (driver->channels_not_done, chn)) {
1190 if (driver->silent[chn] < buffer_frames) {
1191 alsa_driver_silence_on_channel_no_mark (
1192 driver, chn, nframes);
1193 driver->silent[chn] += nframes;
1199 void
1200 alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn,
1201 ClockSyncStatus status)
1203 driver->clock_sync_data[chn] = status;
1204 alsa_driver_clock_sync_notify (driver, chn, status);
1207 static int under_gdb = FALSE;
1209 static jack_nframes_t
1210 alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
1211 *delayed_usecs)
1213 snd_pcm_sframes_t avail = 0;
1214 snd_pcm_sframes_t capture_avail = 0;
1215 snd_pcm_sframes_t playback_avail = 0;
1216 int xrun_detected = FALSE;
1217 int need_capture;
1218 int need_playback;
1219 unsigned int i;
1220 jack_time_t poll_enter;
1221 jack_time_t poll_ret = 0;
1223 *status = -1;
1224 *delayed_usecs = 0;
1226 need_capture = driver->capture_handle ? 1 : 0;
1228 if (extra_fd >= 0) {
1229 need_playback = 0;
1230 } else {
1231 need_playback = driver->playback_handle ? 1 : 0;
1234 again:
1236 while (need_playback || need_capture) {
1238 unsigned int p_timed_out, c_timed_out;
1239 unsigned int ci = 0;
1240 unsigned int nfds;
1242 nfds = 0;
1244 if (need_playback) {
1245 snd_pcm_poll_descriptors (driver->playback_handle,
1246 &driver->pfd[0],
1247 driver->playback_nfds);
1248 nfds += driver->playback_nfds;
1251 if (need_capture) {
1252 snd_pcm_poll_descriptors (driver->capture_handle,
1253 &driver->pfd[nfds],
1254 driver->capture_nfds);
1255 ci = nfds;
1256 nfds += driver->capture_nfds;
1259 /* ALSA doesn't set POLLERR in some versions of 0.9.X */
1261 for (i = 0; i < nfds; i++) {
1262 driver->pfd[i].events |= POLLERR;
1265 if (extra_fd >= 0) {
1266 driver->pfd[nfds].fd = extra_fd;
1267 driver->pfd[nfds].events =
1268 POLLIN|POLLERR|POLLHUP|POLLNVAL;
1269 nfds++;
1272 poll_enter = jack_get_microseconds ();
1274 if (poll_enter > driver->poll_next) {
1276 * This processing cycle was delayed past the
1277 * next due interrupt! Do not account this as
1278 * a wakeup delay:
1280 driver->poll_next = 0;
1281 driver->poll_late++;
1284 if (poll (driver->pfd, nfds, driver->poll_timeout) < 0) {
1286 if (errno == EINTR) {
1287 printf ("poll interrupt\n");
1288 // this happens mostly when run
1289 // under gdb, or when exiting due to a signal
1290 if (under_gdb) {
1291 goto again;
1293 *status = -2;
1294 return 0;
1297 jack_error ("ALSA: poll call failed (%s)",
1298 strerror (errno));
1299 *status = -3;
1300 return 0;
1304 poll_ret = jack_get_microseconds ();
1306 if (extra_fd < 0) {
1307 if (driver->poll_next && poll_ret > driver->poll_next) {
1308 *delayed_usecs = poll_ret - driver->poll_next;
1310 driver->poll_last = poll_ret;
1311 driver->poll_next = poll_ret + driver->period_usecs;
1312 driver->engine->transport_cycle_start (driver->engine,
1313 poll_ret);
1316 #ifdef DEBUG_WAKEUP
1317 fprintf (stderr, "%" PRIu64 ": checked %d fds, %" PRIu64
1318 " usecs since poll entered\n", poll_ret, nfds,
1319 poll_ret - poll_enter);
1320 #endif
1322 /* check to see if it was the extra FD that caused us
1323 * to return from poll */
1325 if (extra_fd >= 0) {
1327 if (driver->pfd[nfds-1].revents == 0) {
1328 /* we timed out on the extra fd */
1330 *status = -4;
1331 return -1;
1334 /* if POLLIN was the only bit set, we're OK */
1336 *status = 0;
1337 return (driver->pfd[nfds-1].revents == POLLIN) ? 0 : -1;
1340 p_timed_out = 0;
1342 if (need_playback) {
1343 for (i = 0; i < driver->playback_nfds; i++) {
1344 if (driver->pfd[i].revents & POLLERR) {
1345 xrun_detected = TRUE;
1348 if (driver->pfd[i].revents == 0) {
1349 p_timed_out++;
1350 #ifdef DEBUG_WAKEUP
1351 fprintf (stderr, "%" PRIu64
1352 " playback stream timed out\n",
1353 poll_ret);
1354 #endif
1358 if (p_timed_out == 0) {
1359 need_playback = 0;
1360 #ifdef DEBUG_WAKEUP
1361 fprintf (stderr, "%" PRIu64
1362 " playback stream ready\n",
1363 poll_ret);
1364 #endif
1368 c_timed_out = 0;
1370 if (need_capture) {
1371 for (i = ci; i < nfds; i++) {
1372 if (driver->pfd[i].revents & POLLERR) {
1373 xrun_detected = TRUE;
1376 if (driver->pfd[i].revents == 0) {
1377 c_timed_out++;
1378 #ifdef DEBUG_WAKEUP
1379 fprintf (stderr, "%" PRIu64
1380 " capture stream timed out\n",
1381 poll_ret);
1382 #endif
1386 if (c_timed_out == 0) {
1387 need_capture = 0;
1388 #ifdef DEBUG_WAKEUP
1389 fprintf (stderr, "%" PRIu64
1390 " capture stream ready\n",
1391 poll_ret);
1392 #endif
1396 if ((p_timed_out && (p_timed_out == driver->playback_nfds)) &&
1397 (c_timed_out && (c_timed_out == driver->capture_nfds))){
1398 jack_error ("ALSA: poll time out, polled for %" PRIu64
1399 " usecs",
1400 poll_ret - poll_enter);
1401 *status = -5;
1402 return 0;
1407 if (driver->capture_handle) {
1408 if ((capture_avail = snd_pcm_avail_update (
1409 driver->capture_handle)) < 0) {
1410 if (capture_avail == -EPIPE) {
1411 xrun_detected = TRUE;
1412 } else {
1413 jack_error ("unknown ALSA avail_update return"
1414 " value (%u)", capture_avail);
1417 } else {
1418 /* odd, but see min() computation below */
1419 capture_avail = INT_MAX;
1422 if (driver->playback_handle) {
1423 if ((playback_avail = snd_pcm_avail_update (
1424 driver->playback_handle)) < 0) {
1425 if (playback_avail == -EPIPE) {
1426 xrun_detected = TRUE;
1427 } else {
1428 jack_error ("unknown ALSA avail_update return"
1429 " value (%u)", playback_avail);
1432 } else {
1433 /* odd, but see min() computation below */
1434 playback_avail = INT_MAX;
1437 if (xrun_detected) {
1438 *status = alsa_driver_xrun_recovery (driver, delayed_usecs);
1439 return 0;
1442 *status = 0;
1443 driver->last_wait_ust = poll_ret;
1445 avail = capture_avail < playback_avail ? capture_avail : playback_avail;
1447 #ifdef DEBUG_WAKEUP
1448 fprintf (stderr, "wakeup complete, avail = %lu, pavail = %lu "
1449 "cavail = %lu\n",
1450 avail, playback_avail, capture_avail);
1451 #endif
1453 /* mark all channels not done for now. read/write will change this */
1455 bitset_copy (driver->channels_not_done, driver->channels_done);
1457 /* constrain the available count to the nearest (round down) number of
1458 periods.
1461 return avail - (avail % driver->frames_per_cycle);
1464 static int
1465 alsa_driver_null_cycle (alsa_driver_t* driver, jack_nframes_t nframes)
1467 jack_nframes_t nf;
1468 snd_pcm_uframes_t offset;
1469 snd_pcm_uframes_t contiguous;
1470 int chn;
1472 if (nframes > driver->frames_per_cycle) {
1473 return -1;
1476 if (driver->capture_handle) {
1477 nf = nframes;
1478 offset = 0;
1479 while (nf) {
1480 contiguous = nf;
1482 if (snd_pcm_mmap_begin (
1483 driver->capture_handle,
1484 &driver->capture_areas,
1485 (snd_pcm_uframes_t *) &offset,
1486 (snd_pcm_uframes_t *) &contiguous)) {
1487 return -1;
1490 if (snd_pcm_mmap_commit (driver->capture_handle,
1491 offset, contiguous) < 0) {
1492 return -1;
1495 nf -= contiguous;
1499 if (driver->playback_handle) {
1500 nf = nframes;
1501 offset = 0;
1502 while (nf) {
1503 contiguous = nf;
1505 if (snd_pcm_mmap_begin (
1506 driver->playback_handle,
1507 &driver->playback_areas,
1508 (snd_pcm_uframes_t *) &offset,
1509 (snd_pcm_uframes_t *) &contiguous)) {
1510 return -1;
1513 for (chn = 0; chn < driver->playback_nchannels; chn++) {
1514 alsa_driver_silence_on_channel (driver, chn,
1515 contiguous);
1518 if (snd_pcm_mmap_commit (driver->playback_handle,
1519 offset, contiguous) < 0) {
1520 return -1;
1523 nf -= contiguous;
1527 return 0;
1530 static int
1531 alsa_driver_bufsize (alsa_driver_t* driver, jack_nframes_t nframes)
1533 return alsa_driver_reset_parameters (driver, nframes,
1534 driver->user_nperiods,
1535 driver->frame_rate);
1538 static int
1539 alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
1541 snd_pcm_sframes_t contiguous;
1542 snd_pcm_sframes_t nread;
1543 snd_pcm_uframes_t offset;
1544 jack_nframes_t orig_nframes;
1545 jack_default_audio_sample_t* buf;
1546 channel_t chn;
1547 JSList *node;
1548 jack_port_t* port;
1549 int err;
1551 if (!driver->capture_handle || driver->engine->freewheeling) {
1552 return 0;
1554 if (nframes > driver->frames_per_cycle) {
1555 return -1;
1558 nread = 0;
1559 contiguous = 0;
1560 orig_nframes = nframes;
1562 while (nframes) {
1564 contiguous = nframes;
1566 if (alsa_driver_get_channel_addresses (
1567 driver,
1568 (snd_pcm_uframes_t *) &contiguous,
1569 (snd_pcm_uframes_t *) 0,
1570 &offset, 0) < 0) {
1571 return -1;
1574 for (chn = 0, node = driver->capture_ports; node;
1575 node = jack_slist_next (node), chn++) {
1577 port = (jack_port_t *) node->data;
1579 if (!jack_port_connected (port)) {
1580 /* no-copy optimization */
1581 continue;
1583 buf = jack_port_get_buffer (port, orig_nframes);
1584 alsa_driver_read_from_channel (driver, chn,
1585 buf + nread, contiguous);
1588 if ((err = snd_pcm_mmap_commit (driver->capture_handle,
1589 offset, contiguous)) < 0) {
1590 jack_error ("ALSA: could not complete read of %"
1591 PRIu32 " frames: error = %d\n", contiguous, err);
1592 return -1;
1595 nframes -= contiguous;
1596 nread += contiguous;
1599 return 0;
1602 static int
1603 alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
1605 channel_t chn;
1606 JSList *node;
1607 JSList *mon_node;
1608 jack_default_audio_sample_t* buf;
1609 jack_default_audio_sample_t* monbuf;
1610 jack_nframes_t orig_nframes;
1611 snd_pcm_sframes_t nwritten;
1612 snd_pcm_sframes_t contiguous;
1613 snd_pcm_uframes_t offset;
1614 jack_port_t *port;
1615 int err;
1617 driver->process_count++;
1619 if (!driver->playback_handle || driver->engine->freewheeling) {
1620 return 0;
1622 if (nframes > driver->frames_per_cycle) {
1623 return -1;
1626 nwritten = 0;
1627 contiguous = 0;
1628 orig_nframes = nframes;
1630 /* check current input monitor request status */
1632 driver->input_monitor_mask = 0;
1634 for (chn = 0, node = driver->capture_ports; node;
1635 node = jack_slist_next (node), chn++) {
1636 if (((jack_port_t *) node->data)->shared->monitor_requests) {
1637 driver->input_monitor_mask |= (1<<chn);
1641 if (driver->hw_monitoring) {
1642 if ((driver->hw->input_monitor_mask
1643 != driver->input_monitor_mask)
1644 && !driver->all_monitor_in) {
1645 driver->hw->set_input_monitor_mask (
1646 driver->hw, driver->input_monitor_mask);
1650 while (nframes) {
1652 contiguous = nframes;
1654 if (alsa_driver_get_channel_addresses (
1655 driver,
1656 (snd_pcm_uframes_t *) 0,
1657 (snd_pcm_uframes_t *) &contiguous,
1658 0, &offset) < 0) {
1659 return -1;
1662 for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
1663 node;
1664 node = jack_slist_next (node), chn++) {
1666 port = (jack_port_t *) node->data;
1668 if (!jack_port_connected (port)) {
1669 continue;
1671 buf = jack_port_get_buffer (port, orig_nframes);
1672 alsa_driver_write_to_channel (driver, chn,
1673 buf + nwritten, contiguous);
1675 if (mon_node) {
1676 port = (jack_port_t *) mon_node->data;
1677 if (!jack_port_connected (port)) {
1678 continue;
1680 monbuf = jack_port_get_buffer (port, orig_nframes);
1681 memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
1682 mon_node = jack_slist_next (mon_node);
1687 if (!bitset_empty (driver->channels_not_done)) {
1688 alsa_driver_silence_untouched_channels (driver,
1689 contiguous);
1692 if ((err = snd_pcm_mmap_commit (driver->playback_handle,
1693 offset, contiguous)) < 0) {
1694 jack_error ("ALSA: could not complete playback of %"
1695 PRIu32 " frames: error = %d", contiguous, err);
1696 if (err != EPIPE && err != ESTRPIPE)
1697 return -1;
1700 nframes -= contiguous;
1701 nwritten += contiguous;
1704 return 0;
1707 static inline int
1708 alsa_driver_run_cycle (alsa_driver_t *driver)
1710 jack_engine_t *engine = driver->engine;
1711 int wait_status;
1712 float delayed_usecs;
1713 jack_nframes_t nframes;
1715 DEBUG ("alsa run cycle wait\n");
1717 nframes = alsa_driver_wait (driver, -1, &wait_status, &delayed_usecs);
1719 DEBUG ("alsaback from wait, nframes = %lu", nframes);
1721 if (unlikely(wait_status < 0))
1722 return -1; /* driver failed */
1724 if (unlikely(nframes == 0)) {
1726 /* we detected an xrun and restarted: notify
1727 * clients about the delay.
1729 engine->delay (engine, delayed_usecs);
1730 return 0;
1733 return engine->run_cycle (engine, nframes, delayed_usecs);
1736 static int
1737 alsa_driver_attach (alsa_driver_t *driver)
1739 char buf[32];
1740 channel_t chn;
1741 jack_port_t *port;
1742 int port_flags;
1744 driver->engine->set_buffer_size (driver->engine, driver->frames_per_cycle);
1745 driver->engine->set_sample_rate (driver->engine, driver->frame_rate);
1747 port_flags = JackPortIsOutput|JackPortIsPhysical|JackPortIsTerminal;
1749 if (driver->has_hw_monitoring) {
1750 port_flags |= JackPortCanMonitor;
1753 for (chn = 0; chn < driver->capture_nchannels; chn++) {
1755 snprintf (buf, sizeof(buf) - 1, "capture_%lu", chn+1);
1757 if ((port = jack_port_register (driver->client, buf,
1758 JACK_DEFAULT_AUDIO_TYPE,
1759 port_flags, 0)) == NULL) {
1760 jack_error ("ALSA: cannot register port for %s", buf);
1761 break;
1764 jack_port_set_latency (port, driver->frames_per_cycle + driver->capture_frame_latency);
1766 driver->capture_ports =
1767 jack_slist_append (driver->capture_ports, port);
1770 port_flags = JackPortIsInput|JackPortIsPhysical|JackPortIsTerminal;
1772 for (chn = 0; chn < driver->playback_nchannels; chn++) {
1773 jack_port_t *monitor_port;
1775 snprintf (buf, sizeof(buf) - 1, "playback_%lu", chn+1);
1777 if ((port = jack_port_register (driver->client, buf,
1778 JACK_DEFAULT_AUDIO_TYPE,
1779 port_flags, 0)) == NULL) {
1780 jack_error ("ALSA: cannot register port for %s", buf);
1781 break;
1784 jack_port_set_latency (port, (driver->frames_per_cycle * (driver->user_nperiods - 1)) + driver->playback_frame_latency);
1786 driver->playback_ports =
1787 jack_slist_append (driver->playback_ports, port);
1789 if (driver->with_monitor_ports) {
1790 snprintf (buf, sizeof(buf) - 1, "monitor_%lu", chn+1);
1792 if ((monitor_port = jack_port_register (
1793 driver->client, buf,
1794 JACK_DEFAULT_AUDIO_TYPE,
1795 JackPortIsOutput, 0)) == NULL) {
1796 jack_error ("ALSA: cannot register monitor "
1797 "port for %s", buf);
1798 } else {
1800 jack_port_set_latency (monitor_port, driver->frames_per_cycle);
1802 driver->monitor_ports =
1803 jack_slist_append (driver->monitor_ports, monitor_port);
1810 return jack_activate (driver->client);
1813 static int
1814 alsa_driver_detach (alsa_driver_t *driver)
1816 JSList *node;
1818 if (driver->engine == NULL) {
1819 return 0;
1822 for (node = driver->capture_ports; node;
1823 node = jack_slist_next (node)) {
1824 jack_port_unregister (driver->client,
1825 ((jack_port_t *) node->data));
1828 jack_slist_free (driver->capture_ports);
1829 driver->capture_ports = 0;
1831 for (node = driver->playback_ports; node;
1832 node = jack_slist_next (node)) {
1833 jack_port_unregister (driver->client,
1834 ((jack_port_t *) node->data));
1837 jack_slist_free (driver->playback_ports);
1838 driver->playback_ports = 0;
1840 if (driver->monitor_ports) {
1841 for (node = driver->monitor_ports; node;
1842 node = jack_slist_next (node)) {
1843 jack_port_unregister (driver->client,
1844 ((jack_port_t *) node->data));
1847 jack_slist_free (driver->monitor_ports);
1848 driver->monitor_ports = 0;
1851 return 0;
1854 #if 0
1855 static int /* UNUSED */
1856 alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
1859 return driver->hw->change_sample_clock (driver->hw, mode);
1862 static void /* UNUSED */
1863 alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
1866 if (driver->hw_monitoring) {
1867 if (yn) {
1868 driver->hw->set_input_monitor_mask (driver->hw, ~0U);
1869 } else {
1870 driver->hw->set_input_monitor_mask (
1871 driver->hw, driver->input_monitor_mask);
1875 driver->all_monitor_in = yn;
1878 static void /* UNUSED */
1879 alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
1881 if (yn) {
1882 driver->hw_monitoring = TRUE;
1884 if (driver->all_monitor_in) {
1885 driver->hw->set_input_monitor_mask (driver->hw, ~0U);
1886 } else {
1887 driver->hw->set_input_monitor_mask (
1888 driver->hw, driver->input_monitor_mask);
1890 } else {
1891 driver->hw_monitoring = FALSE;
1892 driver->hw->set_input_monitor_mask (driver->hw, 0);
1896 static ClockSyncStatus /* UNUSED */
1897 alsa_driver_clock_sync_status (channel_t chn)
1899 return Lock;
1901 #endif
1903 static void
1904 alsa_driver_delete (alsa_driver_t *driver)
1906 JSList *node;
1908 for (node = driver->clock_sync_listeners; node;
1909 node = jack_slist_next (node)) {
1910 free (node->data);
1912 jack_slist_free (driver->clock_sync_listeners);
1914 if (driver->capture_handle) {
1915 snd_pcm_close (driver->capture_handle);
1916 driver->capture_handle = 0;
1919 if (driver->playback_handle) {
1920 snd_pcm_close (driver->playback_handle);
1921 driver->capture_handle = 0;
1924 if (driver->capture_hw_params) {
1925 snd_pcm_hw_params_free (driver->capture_hw_params);
1926 driver->capture_hw_params = 0;
1929 if (driver->playback_hw_params) {
1930 snd_pcm_hw_params_free (driver->playback_hw_params);
1931 driver->playback_hw_params = 0;
1934 if (driver->capture_sw_params) {
1935 snd_pcm_sw_params_free (driver->capture_sw_params);
1936 driver->capture_sw_params = 0;
1939 if (driver->playback_sw_params) {
1940 snd_pcm_sw_params_free (driver->playback_sw_params);
1941 driver->playback_sw_params = 0;
1944 if (driver->pfd) {
1945 free (driver->pfd);
1948 if (driver->hw) {
1949 driver->hw->release (driver->hw);
1950 driver->hw = 0;
1952 free(driver->alsa_name_playback);
1953 free(driver->alsa_name_capture);
1954 free(driver->alsa_driver);
1956 alsa_driver_release_channel_dependent_memory (driver);
1957 jack_driver_nt_finish ((jack_driver_nt_t *) driver);
1958 free (driver);
1961 static jack_driver_t *
1962 alsa_driver_new (char *name, char *playback_alsa_device,
1963 char *capture_alsa_device,
1964 jack_client_t *client,
1965 jack_nframes_t frames_per_cycle,
1966 jack_nframes_t user_nperiods,
1967 jack_nframes_t rate,
1968 int hw_monitoring,
1969 int hw_metering,
1970 int capturing,
1971 int playing,
1972 DitherAlgorithm dither,
1973 int soft_mode,
1974 int monitor,
1975 int user_capture_nchnls,
1976 int user_playback_nchnls,
1977 int shorts_first,
1978 jack_nframes_t capture_latency,
1979 jack_nframes_t playback_latency
1982 int err;
1984 alsa_driver_t *driver;
1986 printf ("creating alsa driver ... %s|%s|%" PRIu32 "|%" PRIu32
1987 "|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s\n",
1988 playing ? playback_alsa_device : "-",
1989 capturing ? capture_alsa_device : "-",
1990 frames_per_cycle, user_nperiods, rate,
1991 user_capture_nchnls,user_playback_nchnls,
1992 hw_monitoring ? "hwmon": "nomon",
1993 hw_metering ? "hwmeter":"swmeter",
1994 soft_mode ? "soft-mode":"-",
1995 shorts_first ? "16bit":"32bit");
1997 driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
1999 jack_driver_nt_init ((jack_driver_nt_t *) driver);
2001 driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
2002 driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
2003 driver->read = (JackDriverReadFunction) alsa_driver_read;
2004 driver->write = (JackDriverReadFunction) alsa_driver_write;
2005 driver->null_cycle =
2006 (JackDriverNullCycleFunction) alsa_driver_null_cycle;
2007 driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
2008 driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
2009 driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
2010 driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
2012 driver->playback_handle = NULL;
2013 driver->capture_handle = NULL;
2014 driver->ctl_handle = 0;
2015 driver->hw = 0;
2016 driver->capture_and_playback_not_synced = FALSE;
2017 driver->max_nchannels = 0;
2018 driver->user_nchannels = 0;
2019 driver->playback_nchannels = user_playback_nchnls;
2020 driver->capture_nchannels = user_capture_nchnls;
2021 driver->playback_sample_bytes = (shorts_first ? 2:4);
2022 driver->capture_sample_bytes = (shorts_first ? 2:4);
2023 driver->capture_frame_latency = capture_latency;
2024 driver->playback_frame_latency = playback_latency;
2026 driver->playback_addr = 0;
2027 driver->capture_addr = 0;
2028 driver->playback_interleave_skip = NULL;
2029 driver->capture_interleave_skip = NULL;
2032 driver->silent = 0;
2033 driver->all_monitor_in = FALSE;
2034 driver->with_monitor_ports = monitor;
2036 driver->clock_mode = ClockMaster; /* XXX is it? */
2037 driver->input_monitor_mask = 0; /* XXX is it? */
2039 driver->capture_ports = 0;
2040 driver->playback_ports = 0;
2041 driver->monitor_ports = 0;
2043 driver->pfd = 0;
2044 driver->playback_nfds = 0;
2045 driver->capture_nfds = 0;
2047 driver->dither = dither;
2048 driver->soft_mode = soft_mode;
2050 driver->quirk_bswap = 0;
2052 pthread_mutex_init (&driver->clock_sync_lock, 0);
2053 driver->clock_sync_listeners = 0;
2055 driver->poll_late = 0;
2056 driver->xrun_count = 0;
2057 driver->process_count = 0;
2059 driver->alsa_name_playback = strdup (playback_alsa_device);
2060 driver->alsa_name_capture = strdup (capture_alsa_device);
2062 if (alsa_driver_check_card_type (driver)) {
2063 alsa_driver_delete (driver);
2064 return NULL;
2067 alsa_driver_hw_specific (driver, hw_monitoring, hw_metering);
2069 if (playing) {
2070 if (snd_pcm_open (&driver->playback_handle,
2071 playback_alsa_device,
2072 SND_PCM_STREAM_PLAYBACK,
2073 SND_PCM_NONBLOCK) < 0) {
2074 switch (errno) {
2075 case EBUSY:
2076 jack_error ("the playback device \"%s\" is "
2077 "already in use. Please stop the"
2078 " application using it and "
2079 "run JACK again",
2080 playback_alsa_device);
2081 alsa_driver_delete (driver);
2082 return NULL;
2083 break;
2085 case EPERM:
2086 jack_error ("you do not have permission to open "
2087 "the audio device \"%s\" for playback",
2088 playback_alsa_device);
2089 alsa_driver_delete (driver);
2090 return NULL;
2091 break;
2094 driver->playback_handle = NULL;
2097 if (driver->playback_handle) {
2098 snd_pcm_nonblock (driver->playback_handle, 0);
2102 if (capturing) {
2103 if (snd_pcm_open (&driver->capture_handle,
2104 capture_alsa_device,
2105 SND_PCM_STREAM_CAPTURE,
2106 SND_PCM_NONBLOCK) < 0) {
2107 switch (errno) {
2108 case EBUSY:
2109 jack_error ("the capture device \"%s\" is "
2110 "already in use. Please stop the"
2111 " application using it and "
2112 "run JACK again",
2113 capture_alsa_device);
2114 alsa_driver_delete (driver);
2115 return NULL;
2116 break;
2118 case EPERM:
2119 jack_error ("you do not have permission to open "
2120 "the audio device \"%s\" for capture",
2121 capture_alsa_device);
2122 alsa_driver_delete (driver);
2123 return NULL;
2124 break;
2127 driver->capture_handle = NULL;
2130 if (driver->capture_handle) {
2131 snd_pcm_nonblock (driver->capture_handle, 0);
2135 if (driver->playback_handle == NULL) {
2136 if (playing) {
2138 /* they asked for playback, but we can't do it */
2140 jack_error ("ALSA: Cannot open PCM device %s for "
2141 "playback. Falling back to capture-only"
2142 " mode", name);
2144 if (driver->capture_handle == NULL) {
2145 /* can't do anything */
2146 alsa_driver_delete (driver);
2147 return NULL;
2150 playing = FALSE;
2154 if (driver->capture_handle == NULL) {
2155 if (capturing) {
2157 /* they asked for capture, but we can't do it */
2159 jack_error ("ALSA: Cannot open PCM device %s for "
2160 "capture. Falling back to playback-only"
2161 " mode", name);
2163 if (driver->playback_handle == NULL) {
2164 /* can't do anything */
2165 alsa_driver_delete (driver);
2166 return NULL;
2169 capturing = FALSE;
2173 driver->playback_hw_params = 0;
2174 driver->capture_hw_params = 0;
2175 driver->playback_sw_params = 0;
2176 driver->capture_sw_params = 0;
2178 if (driver->playback_handle) {
2179 if ((err = snd_pcm_hw_params_malloc (
2180 &driver->playback_hw_params)) < 0) {
2181 jack_error ("ALSA: could not allocate playback hw"
2182 " params structure");
2183 alsa_driver_delete (driver);
2184 return NULL;
2187 if ((err = snd_pcm_sw_params_malloc (
2188 &driver->playback_sw_params)) < 0) {
2189 jack_error ("ALSA: could not allocate playback sw"
2190 " params structure");
2191 alsa_driver_delete (driver);
2192 return NULL;
2196 if (driver->capture_handle) {
2197 if ((err = snd_pcm_hw_params_malloc (
2198 &driver->capture_hw_params)) < 0) {
2199 jack_error ("ALSA: could not allocate capture hw"
2200 " params structure");
2201 alsa_driver_delete (driver);
2202 return NULL;
2205 if ((err = snd_pcm_sw_params_malloc (
2206 &driver->capture_sw_params)) < 0) {
2207 jack_error ("ALSA: could not allocate capture sw"
2208 " params structure");
2209 alsa_driver_delete (driver);
2210 return NULL;
2214 if (alsa_driver_set_parameters (driver, frames_per_cycle,
2215 user_nperiods, rate)) {
2216 alsa_driver_delete (driver);
2217 return NULL;
2220 driver->capture_and_playback_not_synced = FALSE;
2222 if (driver->capture_handle && driver->playback_handle) {
2223 if (snd_pcm_link (driver->playback_handle,
2224 driver->capture_handle) != 0) {
2225 driver->capture_and_playback_not_synced = TRUE;
2229 driver->client = client;
2231 return (jack_driver_t *) driver;
2235 alsa_driver_listen_for_clock_sync_status (alsa_driver_t *driver,
2236 ClockSyncListenerFunction func,
2237 void *arg)
2239 ClockSyncListener *csl;
2241 csl = (ClockSyncListener *) malloc (sizeof (ClockSyncListener));
2242 csl->function = func;
2243 csl->arg = arg;
2244 csl->id = driver->next_clock_sync_listener_id++;
2246 pthread_mutex_lock (&driver->clock_sync_lock);
2247 driver->clock_sync_listeners =
2248 jack_slist_prepend (driver->clock_sync_listeners, csl);
2249 pthread_mutex_unlock (&driver->clock_sync_lock);
2250 return csl->id;
2254 alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver,
2255 unsigned int which)
2258 JSList *node;
2259 int ret = -1;
2260 pthread_mutex_lock (&driver->clock_sync_lock);
2261 for (node = driver->clock_sync_listeners; node;
2262 node = jack_slist_next (node)) {
2263 if (((ClockSyncListener *) node->data)->id == which) {
2264 driver->clock_sync_listeners =
2265 jack_slist_remove_link (
2266 driver->clock_sync_listeners, node);
2267 free (node->data);
2268 jack_slist_free_1 (node);
2269 ret = 0;
2270 break;
2273 pthread_mutex_unlock (&driver->clock_sync_lock);
2274 return ret;
2277 void
2278 alsa_driver_clock_sync_notify (alsa_driver_t *driver, channel_t chn,
2279 ClockSyncStatus status)
2281 JSList *node;
2283 pthread_mutex_lock (&driver->clock_sync_lock);
2284 for (node = driver->clock_sync_listeners; node;
2285 node = jack_slist_next (node)) {
2286 ClockSyncListener *csl = (ClockSyncListener *) node->data;
2287 csl->function (chn, status, csl->arg);
2289 pthread_mutex_unlock (&driver->clock_sync_lock);
2293 static int
2294 dither_opt (char c, DitherAlgorithm* dither)
2296 switch (c) {
2297 case '-':
2298 case 'n':
2299 *dither = None;
2300 break;
2302 case 'r':
2303 *dither = Rectangular;
2304 break;
2306 case 's':
2307 *dither = Shaped;
2308 break;
2310 case 't':
2311 *dither = Triangular;
2312 break;
2314 default:
2315 fprintf (stderr, "ALSA driver: illegal dithering mode %c\n", c);
2316 return -1;
2318 return 0;
2322 /* DRIVER "PLUGIN" INTERFACE */
2324 const char driver_client_name[] = "alsa_pcm";
2326 const jack_driver_desc_t *
2327 driver_get_descriptor ()
2329 jack_driver_desc_t * desc;
2330 jack_driver_param_desc_t * params;
2331 unsigned int i;
2333 desc = calloc (1, sizeof (jack_driver_desc_t));
2335 strcpy (desc->name,"alsa");
2336 desc->nparams = 17;
2338 params = calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
2340 i = 0;
2341 strcpy (params[i].name, "capture");
2342 params[i].character = 'C';
2343 params[i].type = JackDriverParamString;
2344 strcpy (params[i].value.str, "none");
2345 strcpy (params[i].short_desc,
2346 "Provide capture ports. Optionally set device");
2347 strcpy (params[i].long_desc, params[i].short_desc);
2349 i++;
2350 strcpy (params[i].name, "playback");
2351 params[i].character = 'P';
2352 params[i].type = JackDriverParamString;
2353 strcpy (params[i].value.str, "none");
2354 strcpy (params[i].short_desc,
2355 "Provide playback ports. Optionally set device");
2356 strcpy (params[i].long_desc, params[i].short_desc);
2358 i++;
2359 strcpy (params[i].name, "device");
2360 params[i].character = 'd';
2361 params[i].type = JackDriverParamString;
2362 strcpy (params[i].value.str, "hw:0");
2363 strcpy (params[i].short_desc, "ALSA device name");
2364 strcpy (params[i].long_desc, params[i].short_desc);
2366 i++;
2367 strcpy (params[i].name, "rate");
2368 params[i].character = 'r';
2369 params[i].type = JackDriverParamUInt;
2370 params[i].value.ui = 48000U;
2371 strcpy (params[i].short_desc, "Sample rate");
2372 strcpy (params[i].long_desc, params[i].short_desc);
2374 i++;
2375 strcpy (params[i].name, "period");
2376 params[i].character = 'p';
2377 params[i].type = JackDriverParamUInt;
2378 params[i].value.ui = 1024U;
2379 strcpy (params[i].short_desc, "Frames per period");
2380 strcpy (params[i].long_desc, params[i].short_desc);
2382 i++;
2383 strcpy (params[i].name, "nperiods");
2384 params[i].character = 'n';
2385 params[i].type = JackDriverParamUInt;
2386 params[i].value.ui = 2U;
2387 strcpy (params[i].short_desc, "Number of periods of playback latency");
2388 strcpy (params[i].long_desc, params[i].short_desc);
2390 i++;
2391 strcpy (params[i].name, "hwmon");
2392 params[i].character = 'H';
2393 params[i].type = JackDriverParamBool;
2394 params[i].value.i = 0;
2395 strcpy (params[i].short_desc,"Hardware monitoring, if available");
2396 strcpy (params[i].long_desc, params[i].short_desc);
2398 i++;
2399 strcpy (params[i].name, "hwmeter");
2400 params[i].character = 'M';
2401 params[i].type = JackDriverParamBool;
2402 params[i].value.i = 0;
2403 strcpy (params[i].short_desc, "Hardware metering, if available");
2404 strcpy (params[i].long_desc, params[i].short_desc);
2406 i++;
2407 strcpy (params[i].name, "duplex");
2408 params[i].character = 'D';
2409 params[i].type = JackDriverParamBool;
2410 params[i].value.i = 1;
2411 strcpy (params[i].short_desc,
2412 "Provide both capture and playback ports");
2413 strcpy (params[i].long_desc, params[i].short_desc);
2415 i++;
2416 strcpy (params[i].name, "softmode");
2417 params[i].character = 's';
2418 params[i].type = JackDriverParamBool;
2419 params[i].value.i = 0;
2420 strcpy (params[i].short_desc, "Soft-mode, no xrun handling");
2421 strcpy (params[i].long_desc, params[i].short_desc);
2423 i++;
2424 strcpy (params[i].name, "monitor");
2425 params[i].character = 'm';
2426 params[i].type = JackDriverParamBool;
2427 params[i].value.i = 0;
2428 strcpy (params[i].short_desc, "Provide monitor ports for the output");
2429 strcpy (params[i].long_desc, params[i].short_desc);
2431 i++;
2432 strcpy (params[i].name, "dither");
2433 params[i].character = 'z';
2434 params[i].type = JackDriverParamChar;
2435 params[i].value.c = 'n';
2436 strcpy (params[i].short_desc, "Dithering mode");
2437 strcpy (params[i].long_desc,
2438 "Dithering mode:\n"
2439 " n - none\n"
2440 " r - rectangular\n"
2441 " s - shaped\n"
2442 " t - triangular");
2444 i++;
2445 strcpy (params[i].name, "inchannels");
2446 params[i].character = 'i';
2447 params[i].type = JackDriverParamUInt;
2448 params[i].value.i = 0;
2449 strcpy (params[i].short_desc,
2450 "Number of capture channels (defaults to hardware max)");
2451 strcpy (params[i].long_desc, params[i].short_desc);
2453 i++;
2454 strcpy (params[i].name, "outchannels");
2455 params[i].character = 'o';
2456 params[i].type = JackDriverParamUInt;
2457 params[i].value.i = 0;
2458 strcpy (params[i].short_desc,
2459 "Number of playback channels (defaults to hardware max)");
2460 strcpy (params[i].long_desc, params[i].short_desc);
2462 i++;
2463 strcpy (params[i].name, "shorts");
2464 params[i].character = 'S';
2465 params[i].type = JackDriverParamBool;
2466 params[i].value.i = FALSE;
2467 strcpy (params[i].short_desc, "Try 16-bit samples before 32-bit");
2468 strcpy (params[i].long_desc, params[i].short_desc);
2471 i++;
2472 strcpy (params[i].name, "input-latency");
2473 params[i].character = 'I';
2474 params[i].type = JackDriverParamUInt;
2475 params[i].value.i = 0;
2476 strcpy (params[i].short_desc, "Extra input latency (frames)");
2477 strcpy (params[i].long_desc, params[i].short_desc);
2479 i++;
2480 strcpy (params[i].name, "output-latency");
2481 params[i].character = 'O';
2482 params[i].type = JackDriverParamUInt;
2483 params[i].value.i = 0;
2484 strcpy (params[i].short_desc, "Extra output latency (frames)");
2485 strcpy (params[i].long_desc, params[i].short_desc);
2487 desc->params = params;
2489 return desc;
2492 jack_driver_t *
2493 driver_initialize (jack_client_t *client, const JSList * params)
2495 jack_nframes_t srate = 48000;
2496 jack_nframes_t frames_per_interrupt = 1024;
2497 unsigned long user_nperiods = 2;
2498 char *playback_pcm_name = "hw:0";
2499 char *capture_pcm_name = "hw:0";
2500 int hw_monitoring = FALSE;
2501 int hw_metering = FALSE;
2502 int capture = FALSE;
2503 int playback = FALSE;
2504 int soft_mode = FALSE;
2505 int monitor = FALSE;
2506 DitherAlgorithm dither = None;
2507 int user_capture_nchnls = 0;
2508 int user_playback_nchnls = 0;
2509 int shorts_first = FALSE;
2510 jack_nframes_t systemic_input_latency = 0;
2511 jack_nframes_t systemic_output_latency = 0;
2512 const JSList * node;
2513 const jack_driver_param_t * param;
2515 for (node = params; node; node = jack_slist_next (node)) {
2516 param = (const jack_driver_param_t *) node->data;
2518 switch (param->character) {
2520 case 'C':
2521 capture = TRUE;
2522 if (strcmp (param->value.str, "none") != 0) {
2523 capture_pcm_name = strdup (param->value.str);
2525 break;
2527 case 'P':
2528 playback = TRUE;
2529 if (strcmp (param->value.str, "none") != 0) {
2530 playback_pcm_name = strdup (param->value.str);
2532 break;
2534 case 'D':
2535 playback = TRUE;
2536 capture = TRUE;
2537 break;
2539 case 'd':
2540 playback_pcm_name = strdup (param->value.str);
2541 capture_pcm_name = strdup (param->value.str);
2542 break;
2544 case 'H':
2545 hw_monitoring = param->value.i;
2546 break;
2548 case 'm':
2549 monitor = param->value.i;
2550 break;
2552 case 'M':
2553 hw_metering = param->value.i;
2554 break;
2556 case 'r':
2557 srate = param->value.ui;
2558 fprintf (stderr, "apparent rate = %d\n", srate);
2559 break;
2561 case 'p':
2562 frames_per_interrupt = param->value.ui;
2563 break;
2565 case 'n':
2566 user_nperiods = param->value.ui;
2567 if (user_nperiods < 2) /* enforce minimum value */
2568 user_nperiods = 2;
2569 break;
2571 case 's':
2572 soft_mode = param->value.i;
2573 break;
2575 case 'z':
2576 if (dither_opt (param->value.c, &dither)) {
2577 return NULL;
2579 break;
2581 case 'i':
2582 user_capture_nchnls = param->value.ui;
2583 break;
2584 case 'o':
2585 user_playback_nchnls = param->value.ui;
2586 break;
2588 case 'S':
2589 shorts_first = param->value.i;
2590 break;
2592 case 'I':
2593 systemic_input_latency = param->value.ui;
2594 break;
2596 case 'O':
2597 systemic_output_latency = param->value.ui;
2598 break;
2603 /* duplex is the default */
2604 if (!capture && !playback) {
2605 capture = TRUE;
2606 playback = TRUE;
2609 return alsa_driver_new ("alsa_pcm", playback_pcm_name,
2610 capture_pcm_name, client,
2611 frames_per_interrupt,
2612 user_nperiods, srate, hw_monitoring,
2613 hw_metering, capture, playback, dither,
2614 soft_mode, monitor,
2615 user_capture_nchnls, user_playback_nchnls,
2616 shorts_first,
2617 systemic_input_latency,
2618 systemic_output_latency);
2621 void
2622 driver_finish (jack_driver_t *driver)
2624 alsa_driver_delete ((alsa_driver_t *) driver);