1 /* -*- mode: c; c-file-style: "linux"; -*- */
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.
22 #define __STDC_FORMAT_MACROS // For inttypes.h to work in C++
23 #define _GNU_SOURCE /* for strcasestr() from string.h */
33 #include <sys/types.h>
37 #include "alsa_driver.h"
38 #include "hammerfall.h"
44 #include "JackError.h"
46 #include "alsa_midi_impl.h"
48 extern void store_work_time (int);
49 extern void store_wait_time (int);
50 extern void show_wait_times ();
51 extern void show_work_times ();
55 char* strcasestr(const char* haystack
, const char* needle
);
57 /* Delay (in process calls) before jackd will report an xrun */
58 #define XRUN_REPORT_DELAY 0
61 jack_driver_init (jack_driver_t
*driver
)
63 memset (driver
, 0, sizeof (*driver
));
69 driver
->null_cycle
= 0;
76 jack_driver_nt_init (jack_driver_nt_t
* driver
)
78 memset (driver
, 0, sizeof (*driver
));
80 jack_driver_init ((jack_driver_t
*) driver
);
88 driver
->nt_bufsize
= 0;
91 driver
->nt_attach
= 0;
92 driver
->nt_detach
= 0;
93 driver
->nt_run_cycle
= 0;
97 alsa_driver_release_channel_dependent_memory (alsa_driver_t
*driver
)
99 bitset_destroy (&driver
->channels_done
);
100 bitset_destroy (&driver
->channels_not_done
);
102 if (driver
->playback_addr
) {
103 free (driver
->playback_addr
);
104 driver
->playback_addr
= 0;
107 if (driver
->capture_addr
) {
108 free (driver
->capture_addr
);
109 driver
->capture_addr
= 0;
112 if (driver
->playback_interleave_skip
) {
113 free (driver
->playback_interleave_skip
);
114 driver
->playback_interleave_skip
= NULL
;
117 if (driver
->capture_interleave_skip
) {
118 free (driver
->capture_interleave_skip
);
119 driver
->capture_interleave_skip
= NULL
;
122 if (driver
->silent
) {
123 free (driver
->silent
);
127 if (driver
->dither_state
) {
128 free (driver
->dither_state
);
129 driver
->dither_state
= 0;
134 alsa_driver_check_capabilities (alsa_driver_t
*driver
)
139 char* get_control_device_name(const char * device_name
);
142 alsa_driver_check_card_type (alsa_driver_t
*driver
)
145 snd_ctl_card_info_t
*card_info
;
148 snd_ctl_card_info_alloca (&card_info
);
150 ctl_name
= get_control_device_name(driver
->alsa_name_playback
);
152 // XXX: I don't know the "right" way to do this. Which to use
153 // driver->alsa_name_playback or driver->alsa_name_capture.
154 if ((err
= snd_ctl_open (&driver
->ctl_handle
, ctl_name
, 0)) < 0) {
155 jack_error ("control open \"%s\" (%s)", ctl_name
,
157 } else if ((err
= snd_ctl_card_info(driver
->ctl_handle
, card_info
)) < 0) {
158 jack_error ("control hardware info \"%s\" (%s)",
159 driver
->alsa_name_playback
, snd_strerror (err
));
160 snd_ctl_close (driver
->ctl_handle
);
163 driver
->alsa_driver
= strdup(snd_ctl_card_info_get_driver (card_info
));
167 return alsa_driver_check_capabilities (driver
);
171 alsa_driver_hammerfall_hardware (alsa_driver_t
*driver
)
173 driver
->hw
= jack_alsa_hammerfall_hw_new (driver
);
178 alsa_driver_hdsp_hardware (alsa_driver_t
*driver
)
180 driver
->hw
= jack_alsa_hdsp_hw_new (driver
);
185 alsa_driver_ice1712_hardware (alsa_driver_t
*driver
)
187 driver
->hw
= jack_alsa_ice1712_hw_new (driver
);
194 alsa_driver_usx2y_hardware (alsa_driver_t *driver)
196 driver->hw = jack_alsa_usx2y_hw_new (driver);
202 alsa_driver_generic_hardware (alsa_driver_t
*driver
)
204 driver
->hw
= jack_alsa_generic_hw_new (driver
);
209 alsa_driver_hw_specific (alsa_driver_t
*driver
, int hw_monitoring
,
214 if (!strcmp(driver
->alsa_driver
, "RME9652")) {
215 if ((err
= alsa_driver_hammerfall_hardware (driver
)) != 0) {
218 } else if (!strcmp(driver
->alsa_driver
, "H-DSP")) {
219 if ((err
= alsa_driver_hdsp_hardware (driver
)) !=0) {
222 } else if (!strcmp(driver
->alsa_driver
, "ICE1712")) {
223 if ((err
= alsa_driver_ice1712_hardware (driver
)) !=0) {
229 else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
230 if ((err = alsa_driver_usx2y_hardware (driver)) !=0) {
236 if ((err
= alsa_driver_generic_hardware (driver
)) != 0) {
241 if (driver
->hw
->capabilities
& Cap_HardwareMonitoring
) {
242 driver
->has_hw_monitoring
= TRUE
;
243 /* XXX need to ensure that this is really FALSE or
245 driver
->hw_monitoring
= hw_monitoring
;
247 driver
->has_hw_monitoring
= FALSE
;
248 driver
->hw_monitoring
= FALSE
;
251 if (driver
->hw
->capabilities
& Cap_ClockLockReporting
) {
252 driver
->has_clock_sync_reporting
= TRUE
;
254 driver
->has_clock_sync_reporting
= FALSE
;
257 if (driver
->hw
->capabilities
& Cap_HardwareMetering
) {
258 driver
->has_hw_metering
= TRUE
;
259 driver
->hw_metering
= hw_metering
;
261 driver
->has_hw_metering
= FALSE
;
262 driver
->hw_metering
= FALSE
;
269 alsa_driver_setup_io_function_pointers (alsa_driver_t
*driver
)
271 if (driver
->playback_handle
) {
272 if (SND_PCM_FORMAT_FLOAT_LE
== driver
->playback_sample_format
) {
273 driver
->write_via_copy
= sample_move_dS_floatLE
;
275 switch (driver
->playback_sample_bytes
) {
277 switch (driver
->dither
) {
279 jack_info("Rectangular dithering at 16 bits");
280 driver
->write_via_copy
= driver
->quirk_bswap
?
281 sample_move_dither_rect_d16_sSs
:
282 sample_move_dither_rect_d16_sS
;
286 jack_info("Triangular dithering at 16 bits");
287 driver
->write_via_copy
= driver
->quirk_bswap
?
288 sample_move_dither_tri_d16_sSs
:
289 sample_move_dither_tri_d16_sS
;
293 jack_info("Noise-shaped dithering at 16 bits");
294 driver
->write_via_copy
= driver
->quirk_bswap
?
295 sample_move_dither_shaped_d16_sSs
:
296 sample_move_dither_shaped_d16_sS
;
300 driver
->write_via_copy
= driver
->quirk_bswap
?
301 sample_move_d16_sSs
:
307 case 3: /* NO DITHER */
308 driver
->write_via_copy
= driver
->quirk_bswap
?
314 case 4: /* NO DITHER */
315 driver
->write_via_copy
= driver
->quirk_bswap
?
316 sample_move_d32u24_sSs
:
317 sample_move_d32u24_sS
;
321 jack_error ("impossible sample width (%d) discovered!",
322 driver
->playback_sample_bytes
);
328 if (driver
->capture_handle
) {
329 if (SND_PCM_FORMAT_FLOAT_LE
== driver
->capture_sample_format
) {
330 driver
->read_via_copy
= sample_move_floatLE_sSs
;
332 switch (driver
->capture_sample_bytes
) {
334 driver
->read_via_copy
= driver
->quirk_bswap
?
339 driver
->read_via_copy
= driver
->quirk_bswap
?
344 driver
->read_via_copy
= driver
->quirk_bswap
?
345 sample_move_dS_s32u24s
:
346 sample_move_dS_s32u24
;
354 alsa_driver_configure_stream (alsa_driver_t
*driver
, char *device_name
,
355 const char *stream_name
,
357 snd_pcm_hw_params_t
*hw_params
,
358 snd_pcm_sw_params_t
*sw_params
,
359 unsigned int *nperiodsp
,
361 unsigned long sample_width
)
364 unsigned int frame_rate
;
365 snd_pcm_uframes_t stop_th
;
368 snd_pcm_format_t format
;
371 {"32bit float little-endian", SND_PCM_FORMAT_FLOAT_LE
, IS_LE
},
372 {"32bit integer little-endian", SND_PCM_FORMAT_S32_LE
, IS_LE
},
373 {"32bit integer big-endian", SND_PCM_FORMAT_S32_BE
, IS_BE
},
374 {"24bit little-endian in 3bytes format", SND_PCM_FORMAT_S24_3LE
, IS_LE
},
375 {"24bit big-endian in 3bytes format", SND_PCM_FORMAT_S24_3BE
, IS_BE
},
376 {"24bit little-endian", SND_PCM_FORMAT_S24_LE
, IS_LE
},
377 {"24bit big-endian", SND_PCM_FORMAT_S24_BE
, IS_BE
},
378 {"16bit little-endian", SND_PCM_FORMAT_S16_LE
, IS_LE
},
379 {"16bit big-endian", SND_PCM_FORMAT_S16_BE
, IS_BE
},
381 #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
382 #define FIRST_16BIT_FORMAT 5
384 if ((err
= snd_pcm_hw_params_any (handle
, hw_params
)) < 0) {
385 jack_error ("ALSA: no playback configurations available (%s)",
390 if ((err
= snd_pcm_hw_params_set_periods_integer (handle
, hw_params
))
392 jack_error ("ALSA: cannot restrict period size to integral"
397 if ((err
= snd_pcm_hw_params_set_access (handle
, hw_params
, SND_PCM_ACCESS_MMAP_NONINTERLEAVED
)) < 0) {
398 if ((err
= snd_pcm_hw_params_set_access (handle
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
)) < 0) {
399 if ((err
= snd_pcm_hw_params_set_access (
401 SND_PCM_ACCESS_MMAP_COMPLEX
)) < 0) {
402 jack_error ("ALSA: mmap-based access is not possible"
404 "stream of this audio interface",
411 format
= (sample_width
== 4) ? 0 : NUMFORMATS
- 1;
414 if ((err
= snd_pcm_hw_params_set_format (
415 handle
, hw_params
, formats
[format
].format
)) < 0) {
417 if ((sample_width
== 4
418 ? format
++ >= NUMFORMATS
- 1
420 jack_error ("Sorry. The audio interface \"%s\""
421 " doesn't support any of the"
422 " hardware sample formats that"
423 " JACK's alsa-driver can use.",
428 if (formats
[format
].swapped
) {
429 driver
->quirk_bswap
= 1;
431 driver
->quirk_bswap
= 0;
433 jack_info ("ALSA: final selected sample format for %s: %s", stream_name
, formats
[format
].Name
);
438 frame_rate
= driver
->frame_rate
;
439 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
,
441 driver
->frame_rate
= frame_rate
;
443 jack_error ("ALSA: cannot set sample/frame rate to %"
444 PRIu32
" for %s", driver
->frame_rate
,
449 /*if not user-specified, try to find the maximum
450 * number of channels */
451 unsigned int channels_max
;
452 err
= snd_pcm_hw_params_get_channels_max (hw_params
,
454 *nchns
= channels_max
;
458 /* the hapless user is an unwitting victim of
459 the "default" ALSA PCM device, which can
460 support up to 16 million channels. since
461 they can't be bothered to set up a proper
462 default device, limit the number of
463 channels for them to a sane default.
467 "You appear to be using the ALSA software \"plug\" layer, probably\n"
468 "a result of using the \"default\" ALSA device. This is less\n"
469 "efficient than it could be. Consider using a hardware device\n"
470 "instead rather than using the plug layer. Usually the name of the\n"
471 "hardware device that corresponds to the first sound card is hw:0\n"
477 if ((err
= snd_pcm_hw_params_set_channels (handle
, hw_params
,
479 jack_error ("ALSA: cannot set channel count to %u for %s",
480 *nchns
, stream_name
);
484 if ((err
= snd_pcm_hw_params_set_period_size (handle
, hw_params
,
485 driver
->frames_per_cycle
,
488 jack_error ("ALSA: cannot set period size to %" PRIu32
489 " frames for %s", driver
->frames_per_cycle
,
494 *nperiodsp
= driver
->user_nperiods
;
495 snd_pcm_hw_params_set_periods_min (handle
, hw_params
, nperiodsp
, NULL
);
496 if (*nperiodsp
< driver
->user_nperiods
)
497 *nperiodsp
= driver
->user_nperiods
;
498 if (snd_pcm_hw_params_set_periods_near (handle
, hw_params
,
499 nperiodsp
, NULL
) < 0) {
500 jack_error ("ALSA: cannot set number of periods to %u for %s",
501 *nperiodsp
, stream_name
);
505 if (*nperiodsp
< driver
->user_nperiods
) {
506 jack_error ("ALSA: got smaller periods %u than %u for %s",
507 *nperiodsp
, (unsigned int) driver
->user_nperiods
,
511 jack_info ("ALSA: use %d periods for %s", *nperiodsp
, stream_name
);
513 if (!jack_power_of_two(driver
->frames_per_cycle
)) {
514 jack_error("JACK: frames must be a power of two "
515 "(64, 512, 1024, ...)\n");
520 if ((err
= snd_pcm_hw_params_set_buffer_size (handle
, hw_params
,
522 driver
->frames_per_cycle
))
524 jack_error ("ALSA: cannot set buffer length to %" PRIu32
526 *nperiodsp
* driver
->frames_per_cycle
,
531 if ((err
= snd_pcm_hw_params (handle
, hw_params
)) < 0) {
532 jack_error ("ALSA: cannot set hardware parameters for %s",
537 snd_pcm_sw_params_current (handle
, sw_params
);
539 if ((err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
,
541 jack_error ("ALSA: cannot set start mode for %s", stream_name
);
545 stop_th
= *nperiodsp
* driver
->frames_per_cycle
;
546 if (driver
->soft_mode
) {
547 stop_th
= (snd_pcm_uframes_t
)-1;
550 if ((err
= snd_pcm_sw_params_set_stop_threshold (
551 handle
, sw_params
, stop_th
)) < 0) {
552 jack_error ("ALSA: cannot set stop mode for %s",
557 if ((err
= snd_pcm_sw_params_set_silence_threshold (
558 handle
, sw_params
, 0)) < 0) {
559 jack_error ("ALSA: cannot set silence threshold for %s",
565 jack_info ("set silence size to %lu * %lu = %lu",
566 driver
->frames_per_cycle
, *nperiodsp
,
567 driver
->frames_per_cycle
* *nperiodsp
);
569 if ((err
= snd_pcm_sw_params_set_silence_size (
571 driver
->frames_per_cycle
* *nperiodsp
)) < 0) {
572 jack_error ("ALSA: cannot set silence size for %s",
578 if (handle
== driver
->playback_handle
)
579 err
= snd_pcm_sw_params_set_avail_min (
581 driver
->frames_per_cycle
582 * (*nperiodsp
- driver
->user_nperiods
+ 1));
584 err
= snd_pcm_sw_params_set_avail_min (
585 handle
, sw_params
, driver
->frames_per_cycle
);
588 jack_error ("ALSA: cannot set avail min for %s", stream_name
);
592 err
= snd_pcm_sw_params_set_tstamp_mode(handle
, sw_params
, SND_PCM_TSTAMP_ENABLE
);
594 jack_info("Could not enable ALSA time stamp mode for %s (err %d)",
598 #if SND_LIB_MAJOR >= 1 && SND_LIB_MINOR >= 1
599 err
= snd_pcm_sw_params_set_tstamp_type(handle
, sw_params
, SND_PCM_TSTAMP_TYPE_MONOTONIC
);
601 jack_info("Could not use monotonic ALSA time stamps for %s (err %d)",
606 if ((err
= snd_pcm_sw_params (handle
, sw_params
)) < 0) {
607 jack_error ("ALSA: cannot set software parameters for %s\n",
616 alsa_driver_set_parameters (alsa_driver_t
*driver
,
617 jack_nframes_t frames_per_cycle
,
618 jack_nframes_t user_nperiods
,
622 snd_pcm_uframes_t p_period_size
= 0;
623 snd_pcm_uframes_t c_period_size
= 0;
629 driver
->frame_rate
= rate
;
630 driver
->frames_per_cycle
= frames_per_cycle
;
631 driver
->user_nperiods
= user_nperiods
;
633 jack_info ("configuring for %" PRIu32
"Hz, period = %"
634 PRIu32
" frames (%.1f ms), buffer = %" PRIu32
" periods",
635 rate
, frames_per_cycle
, (((float)frames_per_cycle
/ (float) rate
) * 1000.0f
), user_nperiods
);
637 if (driver
->capture_handle
) {
638 if (alsa_driver_configure_stream (
640 driver
->alsa_name_capture
,
642 driver
->capture_handle
,
643 driver
->capture_hw_params
,
644 driver
->capture_sw_params
,
645 &driver
->capture_nperiods
,
646 &driver
->capture_nchannels
,
647 driver
->capture_sample_bytes
)) {
648 jack_error ("ALSA: cannot configure capture channel");
653 if (driver
->playback_handle
) {
654 if (alsa_driver_configure_stream (
656 driver
->alsa_name_playback
,
658 driver
->playback_handle
,
659 driver
->playback_hw_params
,
660 driver
->playback_sw_params
,
661 &driver
->playback_nperiods
,
662 &driver
->playback_nchannels
,
663 driver
->playback_sample_bytes
)) {
664 jack_error ("ALSA: cannot configure playback channel");
669 /* check the rate, since that's rather important */
671 if (driver
->playback_handle
) {
672 snd_pcm_hw_params_get_rate (driver
->playback_hw_params
,
676 if (driver
->capture_handle
) {
677 snd_pcm_hw_params_get_rate (driver
->capture_hw_params
,
681 if (driver
->capture_handle
&& driver
->playback_handle
) {
683 jack_error ("playback and capture sample rates do "
684 "not match (%d vs. %d)", pr
, cr
);
687 /* only change if *both* capture and playback rates
688 * don't match requested certain hardware actually
689 * still works properly in full-duplex with slightly
690 * different rate values between adc and dac
692 if (cr
!= driver
->frame_rate
&& pr
!= driver
->frame_rate
) {
693 jack_error ("sample rate in use (%d Hz) does not "
694 "match requested rate (%d Hz)",
695 cr
, driver
->frame_rate
);
696 driver
->frame_rate
= cr
;
700 else if (driver
->capture_handle
&& cr
!= driver
->frame_rate
) {
701 jack_error ("capture sample rate in use (%d Hz) does not "
702 "match requested rate (%d Hz)",
703 cr
, driver
->frame_rate
);
704 driver
->frame_rate
= cr
;
706 else if (driver
->playback_handle
&& pr
!= driver
->frame_rate
) {
707 jack_error ("playback sample rate in use (%d Hz) does not "
708 "match requested rate (%d Hz)",
709 pr
, driver
->frame_rate
);
710 driver
->frame_rate
= pr
;
714 /* check the fragment size, since that's non-negotiable */
716 if (driver
->playback_handle
) {
717 snd_pcm_access_t access
;
719 err
= snd_pcm_hw_params_get_period_size (
720 driver
->playback_hw_params
, &p_period_size
, &dir
);
721 err
= snd_pcm_hw_params_get_format (
722 driver
->playback_hw_params
,
723 &(driver
->playback_sample_format
));
724 err
= snd_pcm_hw_params_get_access (driver
->playback_hw_params
,
726 driver
->playback_interleaved
=
727 (access
== SND_PCM_ACCESS_MMAP_INTERLEAVED
)
728 || (access
== SND_PCM_ACCESS_MMAP_COMPLEX
);
730 if (p_period_size
!= driver
->frames_per_cycle
) {
731 jack_error ("alsa_pcm: requested an interrupt every %"
733 " frames but got %u frames for playback",
734 driver
->frames_per_cycle
, p_period_size
);
739 if (driver
->capture_handle
) {
740 snd_pcm_access_t access
;
742 err
= snd_pcm_hw_params_get_period_size (
743 driver
->capture_hw_params
, &c_period_size
, &dir
);
744 err
= snd_pcm_hw_params_get_format (
745 driver
->capture_hw_params
,
746 &(driver
->capture_sample_format
));
747 err
= snd_pcm_hw_params_get_access (driver
->capture_hw_params
,
749 driver
->capture_interleaved
=
750 (access
== SND_PCM_ACCESS_MMAP_INTERLEAVED
)
751 || (access
== SND_PCM_ACCESS_MMAP_COMPLEX
);
753 if (c_period_size
!= driver
->frames_per_cycle
) {
754 jack_error ("alsa_pcm: requested an interrupt every %"
756 " frames but got %uc frames for capture",
757 driver
->frames_per_cycle
, p_period_size
);
762 driver
->playback_sample_bytes
=
763 snd_pcm_format_physical_width (driver
->playback_sample_format
)
765 driver
->capture_sample_bytes
=
766 snd_pcm_format_physical_width (driver
->capture_sample_format
)
769 if (driver
->playback_handle
) {
770 switch (driver
->playback_sample_format
) {
771 case SND_PCM_FORMAT_FLOAT_LE
:
772 case SND_PCM_FORMAT_S32_LE
:
773 case SND_PCM_FORMAT_S24_3LE
:
774 case SND_PCM_FORMAT_S24_3BE
:
775 case SND_PCM_FORMAT_S24_LE
:
776 case SND_PCM_FORMAT_S24_BE
:
777 case SND_PCM_FORMAT_S16_LE
:
778 case SND_PCM_FORMAT_S32_BE
:
779 case SND_PCM_FORMAT_S16_BE
:
783 jack_error ("programming error: unhandled format "
784 "type for playback");
789 if (driver
->capture_handle
) {
790 switch (driver
->capture_sample_format
) {
791 case SND_PCM_FORMAT_FLOAT_LE
:
792 case SND_PCM_FORMAT_S32_LE
:
793 case SND_PCM_FORMAT_S24_3LE
:
794 case SND_PCM_FORMAT_S24_3BE
:
795 case SND_PCM_FORMAT_S24_LE
:
796 case SND_PCM_FORMAT_S24_BE
:
797 case SND_PCM_FORMAT_S16_LE
:
798 case SND_PCM_FORMAT_S32_BE
:
799 case SND_PCM_FORMAT_S16_BE
:
803 jack_error ("programming error: unhandled format "
809 if (driver
->playback_interleaved
) {
810 const snd_pcm_channel_area_t
*my_areas
;
811 snd_pcm_uframes_t offset
, frames
;
812 if (snd_pcm_mmap_begin(driver
->playback_handle
,
813 &my_areas
, &offset
, &frames
) < 0) {
814 jack_error ("ALSA: %s: mmap areas info error",
815 driver
->alsa_name_playback
);
818 driver
->interleave_unit
=
819 snd_pcm_format_physical_width (
820 driver
->playback_sample_format
) / 8;
822 driver
->interleave_unit
= 0; /* NOT USED */
825 if (driver
->capture_interleaved
) {
826 const snd_pcm_channel_area_t
*my_areas
;
827 snd_pcm_uframes_t offset
, frames
;
828 if (snd_pcm_mmap_begin(driver
->capture_handle
,
829 &my_areas
, &offset
, &frames
) < 0) {
830 jack_error ("ALSA: %s: mmap areas info error",
831 driver
->alsa_name_capture
);
836 if (driver
->playback_nchannels
> driver
->capture_nchannels
) {
837 driver
->max_nchannels
= driver
->playback_nchannels
;
838 driver
->user_nchannels
= driver
->capture_nchannels
;
840 driver
->max_nchannels
= driver
->capture_nchannels
;
841 driver
->user_nchannels
= driver
->playback_nchannels
;
844 alsa_driver_setup_io_function_pointers (driver
);
846 /* Allocate and initialize structures that rely on the
849 Set up the bit pattern that is used to record which
850 channels require action on every cycle. any bits that are
851 not set after the engine's process() call indicate channels
852 that potentially need to be silenced.
855 bitset_create (&driver
->channels_done
, driver
->max_nchannels
);
856 bitset_create (&driver
->channels_not_done
, driver
->max_nchannels
);
858 if (driver
->playback_handle
) {
859 driver
->playback_addr
= (char **)
860 malloc (sizeof (char *) * driver
->playback_nchannels
);
861 memset (driver
->playback_addr
, 0,
862 sizeof (char *) * driver
->playback_nchannels
);
863 driver
->playback_interleave_skip
= (unsigned long *)
864 malloc (sizeof (unsigned long *) * driver
->playback_nchannels
);
865 memset (driver
->playback_interleave_skip
, 0,
866 sizeof (unsigned long *) * driver
->playback_nchannels
);
867 driver
->silent
= (unsigned long *)
868 malloc (sizeof (unsigned long)
869 * driver
->playback_nchannels
);
871 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
872 driver
->silent
[chn
] = 0;
875 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
876 bitset_add (driver
->channels_done
, chn
);
879 driver
->dither_state
= (dither_state_t
*)
880 calloc ( driver
->playback_nchannels
,
881 sizeof (dither_state_t
));
884 if (driver
->capture_handle
) {
885 driver
->capture_addr
= (char **)
886 malloc (sizeof (char *) * driver
->capture_nchannels
);
887 memset (driver
->capture_addr
, 0,
888 sizeof (char *) * driver
->capture_nchannels
);
889 driver
->capture_interleave_skip
= (unsigned long *)
890 malloc (sizeof (unsigned long *) * driver
->capture_nchannels
);
891 memset (driver
->capture_interleave_skip
, 0,
892 sizeof (unsigned long *) * driver
->capture_nchannels
);
895 driver
->clock_sync_data
= (ClockSyncStatus
*)
896 malloc (sizeof (ClockSyncStatus
) * driver
->max_nchannels
);
898 driver
->period_usecs
=
899 (jack_time_t
) floor ((((float) driver
->frames_per_cycle
) /
900 driver
->frame_rate
) * 1000000.0f
);
901 driver
->poll_timeout
= (int) floor (1.5f
* driver
->period_usecs
);
905 if (driver->engine) {
906 if (driver->engine->set_buffer_size (driver->engine,
907 driver->frames_per_cycle)) {
908 jack_error ("ALSA: Cannot set engine buffer size to %d (check MIDI)", driver->frames_per_cycle);
921 alsa_driver_reset_parameters (alsa_driver_t
*driver
,
922 jack_nframes_t frames_per_cycle
,
923 jack_nframes_t user_nperiods
,
926 /* XXX unregister old ports ? */
927 alsa_driver_release_channel_dependent_memory (driver
);
928 return alsa_driver_set_parameters (driver
,
930 user_nperiods
, rate
);
934 alsa_driver_get_channel_addresses (alsa_driver_t
*driver
,
935 snd_pcm_uframes_t
*capture_avail
,
936 snd_pcm_uframes_t
*playback_avail
,
937 snd_pcm_uframes_t
*capture_offset
,
938 snd_pcm_uframes_t
*playback_offset
)
944 if ((err
= snd_pcm_mmap_begin (
945 driver
->capture_handle
, &driver
->capture_areas
,
946 (snd_pcm_uframes_t
*) capture_offset
,
947 (snd_pcm_uframes_t
*) capture_avail
)) < 0) {
948 jack_error ("ALSA: %s: mmap areas info error",
949 driver
->alsa_name_capture
);
953 for (chn
= 0; chn
< driver
->capture_nchannels
; chn
++) {
954 const snd_pcm_channel_area_t
*a
=
955 &driver
->capture_areas
[chn
];
956 driver
->capture_addr
[chn
] = (char *) a
->addr
957 + ((a
->first
+ a
->step
* *capture_offset
) / 8);
958 driver
->capture_interleave_skip
[chn
] = (unsigned long ) (a
->step
/ 8);
962 if (playback_avail
) {
963 if ((err
= snd_pcm_mmap_begin (
964 driver
->playback_handle
, &driver
->playback_areas
,
965 (snd_pcm_uframes_t
*) playback_offset
,
966 (snd_pcm_uframes_t
*) playback_avail
)) < 0) {
967 jack_error ("ALSA: %s: mmap areas info error ",
968 driver
->alsa_name_playback
);
972 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
973 const snd_pcm_channel_area_t
*a
=
974 &driver
->playback_areas
[chn
];
975 driver
->playback_addr
[chn
] = (char *) a
->addr
976 + ((a
->first
+ a
->step
* *playback_offset
) / 8);
977 driver
->playback_interleave_skip
[chn
] = (unsigned long ) (a
->step
/ 8);
985 alsa_driver_start (alsa_driver_t
*driver
)
988 snd_pcm_uframes_t poffset
, pavail
;
991 driver
->poll_last
= 0;
992 driver
->poll_next
= 0;
994 if (driver
->playback_handle
) {
995 if ((err
= snd_pcm_prepare (driver
->playback_handle
)) < 0) {
996 jack_error ("ALSA: prepare error for playback on "
997 "\"%s\" (%s)", driver
->alsa_name_playback
,
1003 if ((driver
->capture_handle
&& driver
->capture_and_playback_not_synced
)
1004 || !driver
->playback_handle
) {
1005 if ((err
= snd_pcm_prepare (driver
->capture_handle
)) < 0) {
1006 jack_error ("ALSA: prepare error for capture on \"%s\""
1007 " (%s)", driver
->alsa_name_capture
,
1013 if (driver
->hw_monitoring
) {
1014 if (driver
->input_monitor_mask
|| driver
->all_monitor_in
) {
1015 if (driver
->all_monitor_in
) {
1016 driver
->hw
->set_input_monitor_mask (driver
->hw
, ~0U);
1018 driver
->hw
->set_input_monitor_mask (
1019 driver
->hw
, driver
->input_monitor_mask
);
1022 driver
->hw
->set_input_monitor_mask (driver
->hw
,
1023 driver
->input_monitor_mask
);
1027 if (driver
->playback_handle
) {
1028 driver
->playback_nfds
=
1029 snd_pcm_poll_descriptors_count (driver
->playback_handle
);
1031 driver
->playback_nfds
= 0;
1034 if (driver
->capture_handle
) {
1035 driver
->capture_nfds
=
1036 snd_pcm_poll_descriptors_count (driver
->capture_handle
);
1038 driver
->capture_nfds
= 0;
1045 driver
->pfd
= (struct pollfd
*)
1046 malloc (sizeof (struct pollfd
) *
1047 (driver
->playback_nfds
+ driver
->capture_nfds
+ 2));
1049 if (driver
->midi
&& !driver
->xrun_recovery
)
1050 (driver
->midi
->start
)(driver
->midi
);
1052 if (driver
->playback_handle
) {
1053 /* fill playback buffer with zeroes, and mark
1054 all fragments as having data.
1057 pavail
= snd_pcm_avail_update (driver
->playback_handle
);
1060 driver
->frames_per_cycle
* driver
->playback_nperiods
) {
1061 jack_error ("ALSA: full buffer not available at start");
1065 if (alsa_driver_get_channel_addresses (driver
,
1066 0, &pavail
, 0, &poffset
)) {
1070 /* XXX this is cheating. ALSA offers no guarantee that
1071 we can access the entire buffer at any one time. It
1072 works on most hardware tested so far, however, buts
1073 its a liability in the long run. I think that
1074 alsa-lib may have a better function for doing this
1075 here, where the goal is to silence the entire
1079 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
1080 alsa_driver_silence_on_channel (
1082 driver
->user_nperiods
1083 * driver
->frames_per_cycle
);
1086 snd_pcm_mmap_commit (driver
->playback_handle
, poffset
,
1087 driver
->user_nperiods
1088 * driver
->frames_per_cycle
);
1090 if ((err
= snd_pcm_start (driver
->playback_handle
)) < 0) {
1091 jack_error ("ALSA: could not start playback (%s)",
1092 snd_strerror (err
));
1097 if ((driver
->capture_handle
&& driver
->capture_and_playback_not_synced
)
1098 || !driver
->playback_handle
) {
1099 if ((err
= snd_pcm_start (driver
->capture_handle
)) < 0) {
1100 jack_error ("ALSA: could not start capture (%s)",
1101 snd_strerror (err
));
1110 alsa_driver_stop (alsa_driver_t
*driver
)
1116 /* silence all capture port buffers, because we might
1117 be entering offline mode.
1122 for (chn = 0, node = driver->capture_ports; node;
1123 node = jack_slist_next (node), chn++) {
1127 jack_nframes_t nframes = driver->engine->control->buffer_size;
1129 port = (jack_port_t *) node->data;
1130 buf = jack_port_get_buffer (port, nframes);
1131 memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
1138 if (driver
->playback_handle
) {
1139 if ((err
= snd_pcm_drop (driver
->playback_handle
)) < 0) {
1140 jack_error ("ALSA: channel flush for playback "
1141 "failed (%s)", snd_strerror (err
));
1146 if (!driver
->playback_handle
1147 || driver
->capture_and_playback_not_synced
) {
1148 if (driver
->capture_handle
) {
1149 if ((err
= snd_pcm_drop (driver
->capture_handle
)) < 0) {
1150 jack_error ("ALSA: channel flush for "
1151 "capture failed (%s)",
1152 snd_strerror (err
));
1158 if (driver
->hw_monitoring
) {
1159 driver
->hw
->set_input_monitor_mask (driver
->hw
, 0);
1162 if (driver
->midi
&& !driver
->xrun_recovery
)
1163 (driver
->midi
->stop
)(driver
->midi
);
1169 alsa_driver_restart (alsa_driver_t
*driver
)
1173 driver
->xrun_recovery
= 1;
1176 if ((res = driver->nt_stop((struct _jack_driver_nt *) driver))==0)
1177 res = driver->nt_start((struct _jack_driver_nt *) driver);
1180 driver
->xrun_recovery
= 0;
1182 if (res
&& driver
->midi
)
1183 (driver
->midi
->stop
)(driver
->midi
);
1189 alsa_driver_xrun_recovery (alsa_driver_t
*driver
, float *delayed_usecs
)
1191 snd_pcm_status_t
*status
;
1194 snd_pcm_status_alloca(&status
);
1196 if (driver
->capture_handle
) {
1197 if ((res
= snd_pcm_status(driver
->capture_handle
, status
))
1199 jack_error("status error: %s", snd_strerror(res
));
1202 if ((res
= snd_pcm_status(driver
->playback_handle
, status
))
1204 jack_error("status error: %s", snd_strerror(res
));
1208 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_SUSPENDED
)
1210 jack_log("**** alsa_pcm: pcm in suspended state, resuming it" );
1211 if (driver
->capture_handle
) {
1212 if ((res
= snd_pcm_prepare(driver
->capture_handle
))
1214 jack_error("error preparing after suspend: %s", snd_strerror(res
));
1217 if (driver
->playback_handle
) {
1218 if ((res
= snd_pcm_prepare(driver
->playback_handle
))
1220 jack_error("error preparing after suspend: %s", snd_strerror(res
));
1225 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_XRUN
1226 && driver
->process_count
> XRUN_REPORT_DELAY
) {
1227 struct timeval now
, diff
, tstamp
;
1228 driver
->xrun_count
++;
1229 snd_pcm_status_get_tstamp(status
,&now
);
1230 snd_pcm_status_get_trigger_tstamp(status
, &tstamp
);
1231 timersub(&now
, &tstamp
, &diff
);
1232 *delayed_usecs
= diff
.tv_sec
* 1000000.0 + diff
.tv_usec
;
1233 jack_log("**** alsa_pcm: xrun of at least %.3f msecs",*delayed_usecs
/ 1000.0);
1234 if (driver
->capture_handle
) {
1235 jack_log("Repreparing capture");
1236 if ((res
= snd_pcm_prepare(driver
->capture_handle
)) < 0) {
1237 jack_error("error preparing after xrun: %s", snd_strerror(res
));
1240 if (driver
->playback_handle
) {
1241 jack_log("Repreparing playback");
1242 if ((res
= snd_pcm_prepare(driver
->playback_handle
)) < 0) {
1243 jack_error("error preparing after xrun: %s", snd_strerror(res
));
1248 if (alsa_driver_restart (driver
)) {
1255 alsa_driver_silence_untouched_channels (alsa_driver_t
*driver
,
1256 jack_nframes_t nframes
)
1259 jack_nframes_t buffer_frames
=
1260 driver
->frames_per_cycle
* driver
->playback_nperiods
;
1262 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
1263 if (bitset_contains (driver
->channels_not_done
, chn
)) {
1264 if (driver
->silent
[chn
] < buffer_frames
) {
1265 alsa_driver_silence_on_channel_no_mark (
1266 driver
, chn
, nframes
);
1267 driver
->silent
[chn
] += nframes
;
1274 alsa_driver_set_clock_sync_status (alsa_driver_t
*driver
, channel_t chn
,
1275 ClockSyncStatus status
)
1277 driver
->clock_sync_data
[chn
] = status
;
1278 alsa_driver_clock_sync_notify (driver
, chn
, status
);
1281 static int under_gdb
= FALSE
;
1284 alsa_driver_wait (alsa_driver_t
*driver
, int extra_fd
, int *status
, float
1287 snd_pcm_sframes_t avail
= 0;
1288 snd_pcm_sframes_t capture_avail
= 0;
1289 snd_pcm_sframes_t playback_avail
= 0;
1290 int xrun_detected
= FALSE
;
1294 jack_time_t poll_enter
;
1295 jack_time_t poll_ret
= 0;
1300 need_capture
= driver
->capture_handle
? 1 : 0;
1302 if (extra_fd
>= 0) {
1305 need_playback
= driver
->playback_handle
? 1 : 0;
1310 while (need_playback
|| need_capture
) {
1313 unsigned int ci
= 0;
1315 unsigned short revents
;
1319 if (need_playback
) {
1320 snd_pcm_poll_descriptors (driver
->playback_handle
,
1322 driver
->playback_nfds
);
1323 nfds
+= driver
->playback_nfds
;
1327 snd_pcm_poll_descriptors (driver
->capture_handle
,
1329 driver
->capture_nfds
);
1331 nfds
+= driver
->capture_nfds
;
1334 /* ALSA doesn't set POLLERR in some versions of 0.9.X */
1336 for (i
= 0; i
< nfds
; i
++) {
1337 driver
->pfd
[i
].events
|= POLLERR
;
1340 if (extra_fd
>= 0) {
1341 driver
->pfd
[nfds
].fd
= extra_fd
;
1342 driver
->pfd
[nfds
].events
=
1343 POLLIN
|POLLERR
|POLLHUP
|POLLNVAL
;
1347 poll_enter
= jack_get_microseconds ();
1349 if (poll_enter
> driver
->poll_next
) {
1351 * This processing cycle was delayed past the
1352 * next due interrupt! Do not account this as
1355 driver
->poll_next
= 0;
1356 driver
->poll_late
++;
1360 poll_result
= poll (driver
->pfd
, nfds
, -1); //fix for sleep issue
1362 poll_result
= poll (driver
->pfd
, nfds
, driver
->poll_timeout
);
1364 if (poll_result
< 0) {
1366 if (errno
== EINTR
) {
1367 jack_info ("poll interrupt");
1368 // this happens mostly when run
1369 // under gdb, or when exiting due to a signal
1377 jack_error ("ALSA: poll call failed (%s)",
1384 poll_ret
= jack_get_microseconds ();
1390 if (driver
->poll_next
&& poll_ret
> driver
->poll_next
) {
1391 *delayed_usecs
= poll_ret
- driver
->poll_next
;
1393 driver
->poll_last
= poll_ret
;
1394 driver
->poll_next
= poll_ret
+ driver
->period_usecs
;
1397 driver->engine->transport_cycle_start (driver->engine,
1403 fprintf (stderr
, "%" PRIu64
": checked %d fds, started at %" PRIu64
" %" PRIu64
" usecs since poll entered\n",
1404 poll_ret
, nfds
, poll_enter
, poll_ret
- poll_enter
);
1407 /* check to see if it was the extra FD that caused us
1408 * to return from poll */
1410 if (extra_fd
>= 0) {
1412 if (driver
->pfd
[nfds
-1].revents
== 0) {
1413 /* we timed out on the extra fd */
1419 /* if POLLIN was the only bit set, we're OK */
1422 return (driver
->pfd
[nfds
-1].revents
== POLLIN
) ? 0 : -1;
1425 if (need_playback
) {
1426 if (snd_pcm_poll_descriptors_revents
1427 (driver
->playback_handle
, &driver
->pfd
[0],
1428 driver
->playback_nfds
, &revents
) < 0) {
1429 jack_error ("ALSA: playback revents failed");
1434 if (revents
& POLLNVAL
) {
1435 jack_error ("ALSA: playback device disconnected");
1440 if (revents
& POLLERR
) {
1441 xrun_detected
= TRUE
;
1444 if (revents
& POLLOUT
) {
1447 fprintf (stderr
, "%" PRIu64
1448 " playback stream ready\n",
1455 if (snd_pcm_poll_descriptors_revents
1456 (driver
->capture_handle
, &driver
->pfd
[ci
],
1457 driver
->capture_nfds
, &revents
) < 0) {
1458 jack_error ("ALSA: capture revents failed");
1463 if (revents
& POLLNVAL
) {
1464 jack_error ("ALSA: capture device disconnected");
1469 if (revents
& POLLERR
) {
1470 xrun_detected
= TRUE
;
1473 if (revents
& POLLIN
) {
1476 fprintf (stderr
, "%" PRIu64
1477 " capture stream ready\n",
1483 if (poll_result
== 0) {
1484 jack_error ("ALSA: poll time out, polled for %" PRIu64
1486 poll_ret
- poll_enter
);
1493 if (driver
->capture_handle
) {
1494 if ((capture_avail
= snd_pcm_avail_update (
1495 driver
->capture_handle
)) < 0) {
1496 if (capture_avail
== -EPIPE
) {
1497 xrun_detected
= TRUE
;
1499 jack_error ("unknown ALSA avail_update return"
1500 " value (%u)", capture_avail
);
1504 /* odd, but see min() computation below */
1505 capture_avail
= INT_MAX
;
1508 if (driver
->playback_handle
) {
1509 if ((playback_avail
= snd_pcm_avail_update (
1510 driver
->playback_handle
)) < 0) {
1511 if (playback_avail
== -EPIPE
) {
1512 xrun_detected
= TRUE
;
1514 jack_error ("unknown ALSA avail_update return"
1515 " value (%u)", playback_avail
);
1519 /* odd, but see min() computation below */
1520 playback_avail
= INT_MAX
;
1523 if (xrun_detected
) {
1524 *status
= alsa_driver_xrun_recovery (driver
, delayed_usecs
);
1529 driver
->last_wait_ust
= poll_ret
;
1531 avail
= capture_avail
< playback_avail
? capture_avail
: playback_avail
;
1534 fprintf (stderr
, "wakeup complete, avail = %lu, pavail = %lu "
1536 avail
, playback_avail
, capture_avail
);
1539 /* mark all channels not done for now. read/write will change this */
1541 bitset_copy (driver
->channels_not_done
, driver
->channels_done
);
1543 /* constrain the available count to the nearest (round down) number of
1547 return avail
- (avail
% driver
->frames_per_cycle
);
1552 alsa_driver_read (alsa_driver_t
*driver
, jack_nframes_t nframes
)
1554 snd_pcm_sframes_t contiguous
;
1555 snd_pcm_sframes_t nread
;
1556 snd_pcm_uframes_t offset
;
1557 jack_nframes_t orig_nframes
;
1558 // jack_default_audio_sample_t* buf;
1561 // jack_port_t* port;
1564 if (nframes
> driver
->frames_per_cycle
) {
1570 if (driver->engine->freewheeling) {
1575 (driver
->midi
->read
)(driver
->midi
, nframes
);
1577 if (!driver
->capture_handle
) {
1583 orig_nframes
= nframes
;
1587 contiguous
= nframes
;
1589 if (alsa_driver_get_channel_addresses (
1591 (snd_pcm_uframes_t
*) &contiguous
,
1592 (snd_pcm_uframes_t
*) 0,
1598 for (chn = 0, node = driver->capture_ports; node;
1599 node = jack_slist_next (node), chn++) {
1601 port = (jack_port_t *) node->data;
1603 if (!jack_port_connected (port)) {
1604 // no-copy optimization
1607 buf = jack_port_get_buffer (port, orig_nframes);
1608 alsa_driver_read_from_channel (driver, chn,
1609 buf + nread, contiguous);
1612 ReadInput(orig_nframes
, contiguous
, nread
);
1614 if ((err
= snd_pcm_mmap_commit (driver
->capture_handle
,
1615 offset
, contiguous
)) < 0) {
1616 jack_error ("ALSA: could not complete read of %"
1617 PRIu32
" frames: error = %d", contiguous
, err
);
1621 nframes
-= contiguous
;
1622 nread
+= contiguous
;
1629 alsa_driver_write (alsa_driver_t
* driver
, jack_nframes_t nframes
)
1633 // JSList *mon_node;
1634 // jack_default_audio_sample_t* buf;
1635 // jack_default_audio_sample_t* monbuf;
1636 jack_nframes_t orig_nframes
;
1637 snd_pcm_sframes_t nwritten
;
1638 snd_pcm_sframes_t contiguous
;
1639 snd_pcm_uframes_t offset
;
1640 // jack_port_t *port;
1643 driver
->process_count
++;
1647 if (!driver->playback_handle || driver->engine->freewheeling) {
1651 if (!driver
->playback_handle
) {
1655 if (nframes
> driver
->frames_per_cycle
) {
1660 (driver
->midi
->write
)(driver
->midi
, nframes
);
1664 orig_nframes
= nframes
;
1666 /* check current input monitor request status */
1668 driver
->input_monitor_mask
= 0;
1672 for (chn = 0, node = driver->capture_ports; node;
1673 node = jack_slist_next (node), chn++) {
1674 if (((jack_port_t *) node->data)->shared->monitor_requests) {
1675 driver->input_monitor_mask |= (1<<chn);
1681 if (driver
->hw_monitoring
) {
1682 if ((driver
->hw
->input_monitor_mask
1683 != driver
->input_monitor_mask
)
1684 && !driver
->all_monitor_in
) {
1685 driver
->hw
->set_input_monitor_mask (
1686 driver
->hw
, driver
->input_monitor_mask
);
1692 contiguous
= nframes
;
1694 if (alsa_driver_get_channel_addresses (
1696 (snd_pcm_uframes_t
*) 0,
1697 (snd_pcm_uframes_t
*) &contiguous
,
1704 for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
1706 node = jack_slist_next (node), chn++) {
1708 port = (jack_port_t *) node->data;
1710 if (!jack_port_connected (port)) {
1713 buf = jack_port_get_buffer (port, orig_nframes);
1714 alsa_driver_write_to_channel (driver, chn,
1715 buf + nwritten, contiguous);
1718 port = (jack_port_t *) mon_node->data;
1719 if (!jack_port_connected (port)) {
1722 monbuf = jack_port_get_buffer (port, orig_nframes);
1723 memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
1724 mon_node = jack_slist_next (mon_node);
1730 WriteOutput(orig_nframes
, contiguous
, nwritten
);
1732 if (!bitset_empty (driver
->channels_not_done
)) {
1733 alsa_driver_silence_untouched_channels (driver
,
1737 if ((err
= snd_pcm_mmap_commit (driver
->playback_handle
,
1738 offset
, contiguous
)) < 0) {
1739 jack_error ("ALSA: could not complete playback of %"
1740 PRIu32
" frames: error = %d", contiguous
, err
);
1741 if (err
!= -EPIPE
&& err
!= -ESTRPIPE
)
1745 nframes
-= contiguous
;
1746 nwritten
+= contiguous
;
1753 static int /* UNUSED */
1754 alsa_driver_change_sample_clock (alsa_driver_t
*driver
, SampleClockMode mode
)
1756 return driver
->hw
->change_sample_clock (driver
->hw
, mode
);
1759 static void /* UNUSED */
1760 alsa_driver_request_all_monitor_input (alsa_driver_t
*driver
, int yn
)
1763 if (driver
->hw_monitoring
) {
1765 driver
->hw
->set_input_monitor_mask (driver
->hw
, ~0U);
1767 driver
->hw
->set_input_monitor_mask (
1768 driver
->hw
, driver
->input_monitor_mask
);
1772 driver
->all_monitor_in
= yn
;
1775 static void /* UNUSED */
1776 alsa_driver_set_hw_monitoring (alsa_driver_t
*driver
, int yn
)
1779 driver
->hw_monitoring
= TRUE
;
1781 if (driver
->all_monitor_in
) {
1782 driver
->hw
->set_input_monitor_mask (driver
->hw
, ~0U);
1784 driver
->hw
->set_input_monitor_mask (
1785 driver
->hw
, driver
->input_monitor_mask
);
1788 driver
->hw_monitoring
= FALSE
;
1789 driver
->hw
->set_input_monitor_mask (driver
->hw
, 0);
1793 static ClockSyncStatus
/* UNUSED */
1794 alsa_driver_clock_sync_status (channel_t chn
)
1801 alsa_driver_delete (alsa_driver_t
*driver
)
1806 (driver
->midi
->destroy
)(driver
->midi
);
1808 for (node
= driver
->clock_sync_listeners
; node
;
1809 node
= jack_slist_next (node
)) {
1812 jack_slist_free (driver
->clock_sync_listeners
);
1814 if (driver
->ctl_handle
) {
1815 snd_ctl_close (driver
->ctl_handle
);
1816 driver
->ctl_handle
= 0;
1819 if (driver
->capture_handle
) {
1820 snd_pcm_close (driver
->capture_handle
);
1821 driver
->capture_handle
= 0;
1824 if (driver
->playback_handle
) {
1825 snd_pcm_close (driver
->playback_handle
);
1826 driver
->capture_handle
= 0;
1829 if (driver
->capture_hw_params
) {
1830 snd_pcm_hw_params_free (driver
->capture_hw_params
);
1831 driver
->capture_hw_params
= 0;
1834 if (driver
->playback_hw_params
) {
1835 snd_pcm_hw_params_free (driver
->playback_hw_params
);
1836 driver
->playback_hw_params
= 0;
1839 if (driver
->capture_sw_params
) {
1840 snd_pcm_sw_params_free (driver
->capture_sw_params
);
1841 driver
->capture_sw_params
= 0;
1844 if (driver
->playback_sw_params
) {
1845 snd_pcm_sw_params_free (driver
->playback_sw_params
);
1846 driver
->playback_sw_params
= 0;
1854 driver
->hw
->release (driver
->hw
);
1857 free(driver
->alsa_name_playback
);
1858 free(driver
->alsa_name_capture
);
1859 free(driver
->alsa_driver
);
1861 alsa_driver_release_channel_dependent_memory (driver
);
1863 //jack_driver_nt_finish ((jack_driver_nt_t *) driver);
1868 discover_alsa_using_apps ()
1872 char* path
= getenv ("PATH");
1883 /* look for lsof and give up if its not in PATH */
1885 path
= strdup (path
);
1886 dir
= strtok (path
, ":");
1888 char maybe
[PATH_MAX
+1];
1889 snprintf (maybe
, sizeof(maybe
), "%s/lsof", dir
);
1890 if (access (maybe
, X_OK
) == 0) {
1893 dir
= strtok (NULL
, ":");
1901 snprintf (command
, sizeof (command
), "lsof -Fc0 ");
1902 cmdlen
= strlen (command
);
1904 for (card
= 0; card
< 8; ++card
) {
1905 for (device
= 0; device
< 8; ++device
) {
1908 snprintf (buf
, sizeof (buf
), "/dev/snd/pcmC%dD%dp", card
, device
);
1909 if (access (buf
, F_OK
) == 0) {
1910 snprintf (command
+cmdlen
, sizeof(command
)-cmdlen
, "%s ", buf
);
1912 cmdlen
= strlen (command
);
1914 snprintf (buf
, sizeof (buf
), "/dev/snd/pcmC%dD%dc", card
, device
);
1915 if (access (buf
, F_OK
) == 0) {
1916 snprintf (command
+cmdlen
, sizeof(command
)-cmdlen
, "%s ", buf
);
1918 cmdlen
= strlen (command
);
1922 FILE* f
= popen (command
, "r");
1929 char buf
[1024]; /* lsof doesn't output much */
1931 if (!fgets (buf
, sizeof (buf
), f
)) {
1939 /* buf contains NULL as a separator between the process field and the command field */
1941 ++pid
; /* skip leading 'p' */
1948 ++cmd
; /* skip to 'c' */
1949 ++cmd
; /* skip to first character of command */
1951 snprintf (found
+flen
, sizeof (found
)-flen
, "%s (process ID %s)\n", cmd
, pid
);
1952 flen
= strlen (found
);
1954 if (flen
>= sizeof (found
)) {
1962 return strdup (found
);
1969 alsa_driver_new (char *name
, char *playback_alsa_device
,
1970 char *capture_alsa_device
,
1971 jack_client_t
*client
,
1972 jack_nframes_t frames_per_cycle
,
1973 jack_nframes_t user_nperiods
,
1974 jack_nframes_t rate
,
1979 DitherAlgorithm dither
,
1982 int user_capture_nchnls
,
1983 int user_playback_nchnls
,
1985 jack_nframes_t capture_latency
,
1986 jack_nframes_t playback_latency
,
1987 alsa_midi_t
*midi_driver
1992 alsa_driver_t
*driver
;
1994 jack_info ("creating alsa driver ... %s|%s|%" PRIu32
"|%" PRIu32
1995 "|%" PRIu32
"|%" PRIu32
"|%" PRIu32
"|%s|%s|%s|%s",
1996 playing
? playback_alsa_device
: "-",
1997 capturing
? capture_alsa_device
: "-",
1998 frames_per_cycle
, user_nperiods
, rate
,
1999 user_capture_nchnls
,user_playback_nchnls
,
2000 hw_monitoring
? "hwmon": "nomon",
2001 hw_metering
? "hwmeter":"swmeter",
2002 soft_mode
? "soft-mode":"-",
2003 shorts_first
? "16bit":"32bit");
2005 driver
= (alsa_driver_t
*) calloc (1, sizeof (alsa_driver_t
));
2007 jack_driver_nt_init ((jack_driver_nt_t
*) driver
);
2011 driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
2012 driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
2013 driver->read = (JackDriverReadFunction) alsa_driver_read;
2014 driver->write = (JackDriverReadFunction) alsa_driver_write;
2015 driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
2016 driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
2017 driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
2018 driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
2019 driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
2022 driver
->playback_handle
= NULL
;
2023 driver
->capture_handle
= NULL
;
2024 driver
->ctl_handle
= 0;
2026 driver
->capture_and_playback_not_synced
= FALSE
;
2027 driver
->max_nchannels
= 0;
2028 driver
->user_nchannels
= 0;
2029 driver
->playback_nchannels
= user_playback_nchnls
;
2030 driver
->capture_nchannels
= user_capture_nchnls
;
2031 driver
->playback_sample_bytes
= (shorts_first
? 2:4);
2032 driver
->capture_sample_bytes
= (shorts_first
? 2:4);
2033 driver
->capture_frame_latency
= capture_latency
;
2034 driver
->playback_frame_latency
= playback_latency
;
2036 driver
->playback_addr
= 0;
2037 driver
->capture_addr
= 0;
2038 driver
->playback_interleave_skip
= NULL
;
2039 driver
->capture_interleave_skip
= NULL
;
2043 driver
->all_monitor_in
= FALSE
;
2044 driver
->with_monitor_ports
= monitor
;
2046 driver
->clock_mode
= ClockMaster
; /* XXX is it? */
2047 driver
->input_monitor_mask
= 0; /* XXX is it? */
2049 driver
->capture_ports
= 0;
2050 driver
->playback_ports
= 0;
2051 driver
->monitor_ports
= 0;
2054 driver
->playback_nfds
= 0;
2055 driver
->capture_nfds
= 0;
2057 driver
->dither
= dither
;
2058 driver
->soft_mode
= soft_mode
;
2060 driver
->quirk_bswap
= 0;
2062 pthread_mutex_init (&driver
->clock_sync_lock
, 0);
2063 driver
->clock_sync_listeners
= 0;
2065 driver
->poll_late
= 0;
2066 driver
->xrun_count
= 0;
2067 driver
->process_count
= 0;
2069 driver
->alsa_name_playback
= strdup (playback_alsa_device
);
2070 driver
->alsa_name_capture
= strdup (capture_alsa_device
);
2072 driver
->midi
= midi_driver
;
2073 driver
->xrun_recovery
= 0;
2075 if (alsa_driver_check_card_type (driver
)) {
2076 alsa_driver_delete (driver
);
2080 alsa_driver_hw_specific (driver
, hw_monitoring
, hw_metering
);
2083 if (snd_pcm_open (&driver
->playback_handle
,
2084 playback_alsa_device
,
2085 SND_PCM_STREAM_PLAYBACK
,
2086 SND_PCM_NONBLOCK
) < 0) {
2090 jack_error ("\n\nATTENTION: The playback device \"%s\" is "
2091 "already in use. Please stop the"
2092 " application using it and "
2094 playback_alsa_device
);
2096 current_apps
= discover_alsa_using_apps ();
2098 jack_error ("\n\nATTENTION: The playback device \"%s\" is "
2099 "already in use. The following applications "
2100 " are using your soundcard(s) so you should "
2101 " check them and stop them as necessary before "
2102 " trying to start JACK again:\n\n%s",
2103 playback_alsa_device
,
2105 free (current_apps
);
2107 jack_error ("\n\nATTENTION: The playback device \"%s\" is "
2108 "already in use. Please stop the"
2109 " application using it and "
2111 playback_alsa_device
);
2114 alsa_driver_delete (driver
);
2118 jack_error ("you do not have permission to open "
2119 "the audio device \"%s\" for playback",
2120 playback_alsa_device
);
2121 alsa_driver_delete (driver
);
2126 driver
->playback_handle
= NULL
;
2129 if (driver
->playback_handle
) {
2130 snd_pcm_nonblock (driver
->playback_handle
, 0);
2135 if (snd_pcm_open (&driver
->capture_handle
,
2136 capture_alsa_device
,
2137 SND_PCM_STREAM_CAPTURE
,
2138 SND_PCM_NONBLOCK
) < 0) {
2142 jack_error ("\n\nATTENTION: The capture (recording) device \"%s\" is "
2144 capture_alsa_device
);
2146 current_apps
= discover_alsa_using_apps ();
2148 jack_error ("\n\nATTENTION: The capture device \"%s\" is "
2149 "already in use. The following applications "
2150 " are using your soundcard(s) so you should "
2151 " check them and stop them as necessary before "
2152 " trying to start JACK again:\n\n%s",
2153 capture_alsa_device
,
2155 free (current_apps
);
2157 jack_error ("\n\nATTENTION: The capture (recording) device \"%s\" is "
2158 "already in use. Please stop the"
2159 " application using it and "
2161 capture_alsa_device
);
2163 alsa_driver_delete (driver
);
2169 jack_error ("you do not have permission to open "
2170 "the audio device \"%s\" for capture",
2171 capture_alsa_device
);
2172 alsa_driver_delete (driver
);
2177 driver
->capture_handle
= NULL
;
2180 if (driver
->capture_handle
) {
2181 snd_pcm_nonblock (driver
->capture_handle
, 0);
2185 if (driver
->playback_handle
== NULL
) {
2188 /* they asked for playback, but we can't do it */
2190 jack_error ("ALSA: Cannot open PCM device %s for "
2191 "playback. Falling back to capture-only"
2194 if (driver
->capture_handle
== NULL
) {
2195 /* can't do anything */
2196 alsa_driver_delete (driver
);
2204 if (driver
->capture_handle
== NULL
) {
2207 /* they asked for capture, but we can't do it */
2209 jack_error ("ALSA: Cannot open PCM device %s for "
2210 "capture. Falling back to playback-only"
2213 if (driver
->playback_handle
== NULL
) {
2214 /* can't do anything */
2215 alsa_driver_delete (driver
);
2223 driver
->playback_hw_params
= 0;
2224 driver
->capture_hw_params
= 0;
2225 driver
->playback_sw_params
= 0;
2226 driver
->capture_sw_params
= 0;
2228 if (driver
->playback_handle
) {
2229 if ((err
= snd_pcm_hw_params_malloc (
2230 &driver
->playback_hw_params
)) < 0) {
2231 jack_error ("ALSA: could not allocate playback hw"
2232 " params structure");
2233 alsa_driver_delete (driver
);
2237 if ((err
= snd_pcm_sw_params_malloc (
2238 &driver
->playback_sw_params
)) < 0) {
2239 jack_error ("ALSA: could not allocate playback sw"
2240 " params structure");
2241 alsa_driver_delete (driver
);
2246 if (driver
->capture_handle
) {
2247 if ((err
= snd_pcm_hw_params_malloc (
2248 &driver
->capture_hw_params
)) < 0) {
2249 jack_error ("ALSA: could not allocate capture hw"
2250 " params structure");
2251 alsa_driver_delete (driver
);
2255 if ((err
= snd_pcm_sw_params_malloc (
2256 &driver
->capture_sw_params
)) < 0) {
2257 jack_error ("ALSA: could not allocate capture sw"
2258 " params structure");
2259 alsa_driver_delete (driver
);
2264 if (alsa_driver_set_parameters (driver
, frames_per_cycle
,
2265 user_nperiods
, rate
)) {
2266 alsa_driver_delete (driver
);
2270 driver
->capture_and_playback_not_synced
= FALSE
;
2272 if (driver
->capture_handle
&& driver
->playback_handle
) {
2273 if (snd_pcm_link (driver
->playback_handle
,
2274 driver
->capture_handle
) != 0) {
2275 driver
->capture_and_playback_not_synced
= TRUE
;
2279 driver
->client
= client
;
2281 return (jack_driver_t
*) driver
;
2285 alsa_driver_listen_for_clock_sync_status (alsa_driver_t
*driver
,
2286 ClockSyncListenerFunction func
,
2289 ClockSyncListener
*csl
;
2291 csl
= (ClockSyncListener
*) malloc (sizeof (ClockSyncListener
));
2292 csl
->function
= func
;
2294 csl
->id
= driver
->next_clock_sync_listener_id
++;
2296 pthread_mutex_lock (&driver
->clock_sync_lock
);
2297 driver
->clock_sync_listeners
=
2298 jack_slist_prepend (driver
->clock_sync_listeners
, csl
);
2299 pthread_mutex_unlock (&driver
->clock_sync_lock
);
2304 alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t
*driver
,
2310 pthread_mutex_lock (&driver
->clock_sync_lock
);
2311 for (node
= driver
->clock_sync_listeners
; node
;
2312 node
= jack_slist_next (node
)) {
2313 if (((ClockSyncListener
*) node
->data
)->id
== which
) {
2314 driver
->clock_sync_listeners
=
2315 jack_slist_remove_link (
2316 driver
->clock_sync_listeners
, node
);
2318 jack_slist_free_1 (node
);
2323 pthread_mutex_unlock (&driver
->clock_sync_lock
);
2328 alsa_driver_clock_sync_notify (alsa_driver_t
*driver
, channel_t chn
,
2329 ClockSyncStatus status
)
2333 pthread_mutex_lock (&driver
->clock_sync_lock
);
2334 for (node
= driver
->clock_sync_listeners
; node
;
2335 node
= jack_slist_next (node
)) {
2336 ClockSyncListener
*csl
= (ClockSyncListener
*) node
->data
;
2337 csl
->function (chn
, status
, csl
->arg
);
2339 pthread_mutex_unlock (&driver
->clock_sync_lock
);
2343 /* DRIVER "PLUGIN" INTERFACE */
2345 const char driver_client_name
[] = "alsa_pcm";
2348 driver_finish (jack_driver_t
*driver
)
2350 alsa_driver_delete ((alsa_driver_t
*) driver
);