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
59 /* Max re-try count for Alsa poll timeout handling */
60 #define MAX_RETRY_COUNT 5
63 jack_driver_init (jack_driver_t
*driver
)
65 memset (driver
, 0, sizeof (*driver
));
71 driver
->null_cycle
= 0;
78 jack_driver_nt_init (jack_driver_nt_t
* driver
)
80 memset (driver
, 0, sizeof (*driver
));
82 jack_driver_init ((jack_driver_t
*) driver
);
90 driver
->nt_bufsize
= 0;
93 driver
->nt_attach
= 0;
94 driver
->nt_detach
= 0;
95 driver
->nt_run_cycle
= 0;
99 alsa_driver_release_channel_dependent_memory (alsa_driver_t
*driver
)
101 bitset_destroy (&driver
->channels_done
);
102 bitset_destroy (&driver
->channels_not_done
);
104 if (driver
->playback_addr
) {
105 free (driver
->playback_addr
);
106 driver
->playback_addr
= 0;
109 if (driver
->capture_addr
) {
110 free (driver
->capture_addr
);
111 driver
->capture_addr
= 0;
114 if (driver
->playback_interleave_skip
) {
115 free (driver
->playback_interleave_skip
);
116 driver
->playback_interleave_skip
= NULL
;
119 if (driver
->capture_interleave_skip
) {
120 free (driver
->capture_interleave_skip
);
121 driver
->capture_interleave_skip
= NULL
;
124 if (driver
->silent
) {
125 free (driver
->silent
);
129 if (driver
->dither_state
) {
130 free (driver
->dither_state
);
131 driver
->dither_state
= 0;
136 alsa_driver_check_capabilities (alsa_driver_t
*driver
)
141 char* get_control_device_name(const char * device_name
);
144 alsa_driver_check_card_type (alsa_driver_t
*driver
)
147 snd_ctl_card_info_t
*card_info
;
150 snd_ctl_card_info_alloca (&card_info
);
152 ctl_name
= get_control_device_name(driver
->alsa_name_playback
);
154 // XXX: I don't know the "right" way to do this. Which to use
155 // driver->alsa_name_playback or driver->alsa_name_capture.
156 if ((err
= snd_ctl_open (&driver
->ctl_handle
, ctl_name
, 0)) < 0) {
157 jack_error ("control open \"%s\" (%s)", ctl_name
,
159 } else if ((err
= snd_ctl_card_info(driver
->ctl_handle
, card_info
)) < 0) {
160 jack_error ("control hardware info \"%s\" (%s)",
161 driver
->alsa_name_playback
, snd_strerror (err
));
162 snd_ctl_close (driver
->ctl_handle
);
165 driver
->alsa_driver
= strdup(snd_ctl_card_info_get_driver (card_info
));
169 return alsa_driver_check_capabilities (driver
);
173 alsa_driver_hammerfall_hardware (alsa_driver_t
*driver
)
175 driver
->hw
= jack_alsa_hammerfall_hw_new (driver
);
180 alsa_driver_hdsp_hardware (alsa_driver_t
*driver
)
182 driver
->hw
= jack_alsa_hdsp_hw_new (driver
);
187 alsa_driver_ice1712_hardware (alsa_driver_t
*driver
)
189 driver
->hw
= jack_alsa_ice1712_hw_new (driver
);
196 alsa_driver_usx2y_hardware (alsa_driver_t *driver)
198 driver->hw = jack_alsa_usx2y_hw_new (driver);
204 alsa_driver_generic_hardware (alsa_driver_t
*driver
)
206 driver
->hw
= jack_alsa_generic_hw_new (driver
);
211 alsa_driver_hw_specific (alsa_driver_t
*driver
, int hw_monitoring
,
216 if (!strcmp(driver
->alsa_driver
, "RME9652")) {
217 if ((err
= alsa_driver_hammerfall_hardware (driver
)) != 0) {
220 } else if (!strcmp(driver
->alsa_driver
, "H-DSP")) {
221 if ((err
= alsa_driver_hdsp_hardware (driver
)) !=0) {
224 } else if (!strcmp(driver
->alsa_driver
, "ICE1712")) {
225 if ((err
= alsa_driver_ice1712_hardware (driver
)) !=0) {
231 else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
232 if ((err = alsa_driver_usx2y_hardware (driver)) !=0) {
238 if ((err
= alsa_driver_generic_hardware (driver
)) != 0) {
243 if (driver
->hw
->capabilities
& Cap_HardwareMonitoring
) {
244 driver
->has_hw_monitoring
= TRUE
;
245 /* XXX need to ensure that this is really FALSE or
247 driver
->hw_monitoring
= hw_monitoring
;
249 driver
->has_hw_monitoring
= FALSE
;
250 driver
->hw_monitoring
= FALSE
;
253 if (driver
->hw
->capabilities
& Cap_ClockLockReporting
) {
254 driver
->has_clock_sync_reporting
= TRUE
;
256 driver
->has_clock_sync_reporting
= FALSE
;
259 if (driver
->hw
->capabilities
& Cap_HardwareMetering
) {
260 driver
->has_hw_metering
= TRUE
;
261 driver
->hw_metering
= hw_metering
;
263 driver
->has_hw_metering
= FALSE
;
264 driver
->hw_metering
= FALSE
;
271 alsa_driver_setup_io_function_pointers (alsa_driver_t
*driver
)
273 if (driver
->playback_handle
) {
274 if (SND_PCM_FORMAT_FLOAT_LE
== driver
->playback_sample_format
) {
275 driver
->write_via_copy
= sample_move_dS_floatLE
;
277 switch (driver
->playback_sample_bytes
) {
279 switch (driver
->dither
) {
281 jack_info("Rectangular dithering at 16 bits");
282 driver
->write_via_copy
= driver
->quirk_bswap
?
283 sample_move_dither_rect_d16_sSs
:
284 sample_move_dither_rect_d16_sS
;
288 jack_info("Triangular dithering at 16 bits");
289 driver
->write_via_copy
= driver
->quirk_bswap
?
290 sample_move_dither_tri_d16_sSs
:
291 sample_move_dither_tri_d16_sS
;
295 jack_info("Noise-shaped dithering at 16 bits");
296 driver
->write_via_copy
= driver
->quirk_bswap
?
297 sample_move_dither_shaped_d16_sSs
:
298 sample_move_dither_shaped_d16_sS
;
302 driver
->write_via_copy
= driver
->quirk_bswap
?
303 sample_move_d16_sSs
:
309 case 3: /* NO DITHER */
310 driver
->write_via_copy
= driver
->quirk_bswap
?
316 case 4: /* NO DITHER */
317 driver
->write_via_copy
= driver
->quirk_bswap
?
318 sample_move_d32u24_sSs
:
319 sample_move_d32u24_sS
;
323 jack_error ("impossible sample width (%d) discovered!",
324 driver
->playback_sample_bytes
);
330 if (driver
->capture_handle
) {
331 if (SND_PCM_FORMAT_FLOAT_LE
== driver
->capture_sample_format
) {
332 driver
->read_via_copy
= sample_move_floatLE_sSs
;
334 switch (driver
->capture_sample_bytes
) {
336 driver
->read_via_copy
= driver
->quirk_bswap
?
341 driver
->read_via_copy
= driver
->quirk_bswap
?
346 driver
->read_via_copy
= driver
->quirk_bswap
?
347 sample_move_dS_s32u24s
:
348 sample_move_dS_s32u24
;
356 alsa_driver_configure_stream (alsa_driver_t
*driver
, char *device_name
,
357 const char *stream_name
,
359 snd_pcm_hw_params_t
*hw_params
,
360 snd_pcm_sw_params_t
*sw_params
,
361 unsigned int *nperiodsp
,
363 unsigned long sample_width
)
366 unsigned int frame_rate
;
367 snd_pcm_uframes_t stop_th
;
370 snd_pcm_format_t format
;
373 {"32bit float little-endian", SND_PCM_FORMAT_FLOAT_LE
, IS_LE
},
374 {"32bit integer little-endian", SND_PCM_FORMAT_S32_LE
, IS_LE
},
375 {"32bit integer big-endian", SND_PCM_FORMAT_S32_BE
, IS_BE
},
376 {"24bit little-endian in 3bytes format", SND_PCM_FORMAT_S24_3LE
, IS_LE
},
377 {"24bit big-endian in 3bytes format", SND_PCM_FORMAT_S24_3BE
, IS_BE
},
378 {"24bit little-endian", SND_PCM_FORMAT_S24_LE
, IS_LE
},
379 {"24bit big-endian", SND_PCM_FORMAT_S24_BE
, IS_BE
},
380 {"16bit little-endian", SND_PCM_FORMAT_S16_LE
, IS_LE
},
381 {"16bit big-endian", SND_PCM_FORMAT_S16_BE
, IS_BE
},
383 #define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
384 #define FIRST_16BIT_FORMAT 5
386 if ((err
= snd_pcm_hw_params_any (handle
, hw_params
)) < 0) {
387 jack_error ("ALSA: no playback configurations available (%s)",
392 if ((err
= snd_pcm_hw_params_set_periods_integer (handle
, hw_params
))
394 jack_error ("ALSA: cannot restrict period size to integral"
399 if ((err
= snd_pcm_hw_params_set_access (handle
, hw_params
, SND_PCM_ACCESS_MMAP_NONINTERLEAVED
)) < 0) {
400 if ((err
= snd_pcm_hw_params_set_access (handle
, hw_params
, SND_PCM_ACCESS_MMAP_INTERLEAVED
)) < 0) {
401 if ((err
= snd_pcm_hw_params_set_access (
403 SND_PCM_ACCESS_MMAP_COMPLEX
)) < 0) {
404 jack_error ("ALSA: mmap-based access is not possible"
406 "stream of this audio interface",
413 format
= (sample_width
== 4) ? 0 : NUMFORMATS
- 1;
416 if ((err
= snd_pcm_hw_params_set_format (
417 handle
, hw_params
, formats
[format
].format
)) < 0) {
419 if ((sample_width
== 4
420 ? format
++ >= NUMFORMATS
- 1
422 jack_error ("Sorry. The audio interface \"%s\""
423 " doesn't support any of the"
424 " hardware sample formats that"
425 " JACK's alsa-driver can use.",
430 if (formats
[format
].swapped
) {
431 driver
->quirk_bswap
= 1;
433 driver
->quirk_bswap
= 0;
435 jack_info ("ALSA: final selected sample format for %s: %s", stream_name
, formats
[format
].Name
);
440 frame_rate
= driver
->frame_rate
;
441 err
= snd_pcm_hw_params_set_rate_near (handle
, hw_params
,
443 driver
->frame_rate
= frame_rate
;
445 jack_error ("ALSA: cannot set sample/frame rate to %"
446 PRIu32
" for %s", driver
->frame_rate
,
451 /*if not user-specified, try to find the maximum
452 * number of channels */
453 unsigned int channels_max
;
454 err
= snd_pcm_hw_params_get_channels_max (hw_params
,
456 *nchns
= channels_max
;
460 /* the hapless user is an unwitting victim of
461 the "default" ALSA PCM device, which can
462 support up to 16 million channels. since
463 they can't be bothered to set up a proper
464 default device, limit the number of
465 channels for them to a sane default.
469 "You appear to be using the ALSA software \"plug\" layer, probably\n"
470 "a result of using the \"default\" ALSA device. This is less\n"
471 "efficient than it could be. Consider using a hardware device\n"
472 "instead rather than using the plug layer. Usually the name of the\n"
473 "hardware device that corresponds to the first sound card is hw:0\n"
479 if ((err
= snd_pcm_hw_params_set_channels (handle
, hw_params
,
481 jack_error ("ALSA: cannot set channel count to %u for %s",
482 *nchns
, stream_name
);
486 if ((err
= snd_pcm_hw_params_set_period_size (handle
, hw_params
,
487 driver
->frames_per_cycle
,
490 jack_error ("ALSA: cannot set period size to %" PRIu32
491 " frames for %s", driver
->frames_per_cycle
,
496 *nperiodsp
= driver
->user_nperiods
;
497 snd_pcm_hw_params_set_periods_min (handle
, hw_params
, nperiodsp
, NULL
);
498 if (*nperiodsp
< driver
->user_nperiods
)
499 *nperiodsp
= driver
->user_nperiods
;
500 if (snd_pcm_hw_params_set_periods_near (handle
, hw_params
,
501 nperiodsp
, NULL
) < 0) {
502 jack_error ("ALSA: cannot set number of periods to %u for %s",
503 *nperiodsp
, stream_name
);
507 if (*nperiodsp
< driver
->user_nperiods
) {
508 jack_error ("ALSA: got smaller periods %u than %u for %s",
509 *nperiodsp
, (unsigned int) driver
->user_nperiods
,
513 jack_info ("ALSA: use %d periods for %s", *nperiodsp
, stream_name
);
515 if (!jack_power_of_two(driver
->frames_per_cycle
)) {
516 jack_error("JACK: frames must be a power of two "
517 "(64, 512, 1024, ...)\n");
522 if ((err
= snd_pcm_hw_params_set_buffer_size (handle
, hw_params
,
524 driver
->frames_per_cycle
))
526 jack_error ("ALSA: cannot set buffer length to %" PRIu32
528 *nperiodsp
* driver
->frames_per_cycle
,
533 if ((err
= snd_pcm_hw_params (handle
, hw_params
)) < 0) {
534 jack_error ("ALSA: cannot set hardware parameters for %s",
539 snd_pcm_sw_params_current (handle
, sw_params
);
541 if ((err
= snd_pcm_sw_params_set_start_threshold (handle
, sw_params
,
543 jack_error ("ALSA: cannot set start mode for %s", stream_name
);
547 stop_th
= *nperiodsp
* driver
->frames_per_cycle
;
548 if (driver
->soft_mode
) {
549 stop_th
= (snd_pcm_uframes_t
)-1;
552 if ((err
= snd_pcm_sw_params_set_stop_threshold (
553 handle
, sw_params
, stop_th
)) < 0) {
554 jack_error ("ALSA: cannot set stop mode for %s",
559 if ((err
= snd_pcm_sw_params_set_silence_threshold (
560 handle
, sw_params
, 0)) < 0) {
561 jack_error ("ALSA: cannot set silence threshold for %s",
567 jack_info ("set silence size to %lu * %lu = %lu",
568 driver
->frames_per_cycle
, *nperiodsp
,
569 driver
->frames_per_cycle
* *nperiodsp
);
571 if ((err
= snd_pcm_sw_params_set_silence_size (
573 driver
->frames_per_cycle
* *nperiodsp
)) < 0) {
574 jack_error ("ALSA: cannot set silence size for %s",
580 if (handle
== driver
->playback_handle
)
581 err
= snd_pcm_sw_params_set_avail_min (
583 driver
->frames_per_cycle
584 * (*nperiodsp
- driver
->user_nperiods
+ 1));
586 err
= snd_pcm_sw_params_set_avail_min (
587 handle
, sw_params
, driver
->frames_per_cycle
);
590 jack_error ("ALSA: cannot set avail min for %s", stream_name
);
594 err
= snd_pcm_sw_params_set_tstamp_mode(handle
, sw_params
, SND_PCM_TSTAMP_ENABLE
);
596 jack_info("Could not enable ALSA time stamp mode for %s (err %d)",
600 #if SND_LIB_MAJOR >= 1 && SND_LIB_MINOR >= 1
601 err
= snd_pcm_sw_params_set_tstamp_type(handle
, sw_params
, SND_PCM_TSTAMP_TYPE_MONOTONIC
);
603 jack_info("Could not use monotonic ALSA time stamps for %s (err %d)",
608 if ((err
= snd_pcm_sw_params (handle
, sw_params
)) < 0) {
609 jack_error ("ALSA: cannot set software parameters for %s\n",
618 alsa_driver_set_parameters (alsa_driver_t
*driver
,
619 jack_nframes_t frames_per_cycle
,
620 jack_nframes_t user_nperiods
,
624 snd_pcm_uframes_t p_period_size
= 0;
625 snd_pcm_uframes_t c_period_size
= 0;
631 driver
->frame_rate
= rate
;
632 driver
->frames_per_cycle
= frames_per_cycle
;
633 driver
->user_nperiods
= user_nperiods
;
635 jack_info ("configuring for %" PRIu32
"Hz, period = %"
636 PRIu32
" frames (%.1f ms), buffer = %" PRIu32
" periods",
637 rate
, frames_per_cycle
, (((float)frames_per_cycle
/ (float) rate
) * 1000.0f
), user_nperiods
);
639 if (driver
->capture_handle
) {
640 if (alsa_driver_configure_stream (
642 driver
->alsa_name_capture
,
644 driver
->capture_handle
,
645 driver
->capture_hw_params
,
646 driver
->capture_sw_params
,
647 &driver
->capture_nperiods
,
648 &driver
->capture_nchannels
,
649 driver
->capture_sample_bytes
)) {
650 jack_error ("ALSA: cannot configure capture channel");
655 if (driver
->playback_handle
) {
656 if (alsa_driver_configure_stream (
658 driver
->alsa_name_playback
,
660 driver
->playback_handle
,
661 driver
->playback_hw_params
,
662 driver
->playback_sw_params
,
663 &driver
->playback_nperiods
,
664 &driver
->playback_nchannels
,
665 driver
->playback_sample_bytes
)) {
666 jack_error ("ALSA: cannot configure playback channel");
671 /* check the rate, since that's rather important */
673 if (driver
->playback_handle
) {
674 snd_pcm_hw_params_get_rate (driver
->playback_hw_params
,
678 if (driver
->capture_handle
) {
679 snd_pcm_hw_params_get_rate (driver
->capture_hw_params
,
683 if (driver
->capture_handle
&& driver
->playback_handle
) {
685 jack_error ("playback and capture sample rates do "
686 "not match (%d vs. %d)", pr
, cr
);
689 /* only change if *both* capture and playback rates
690 * don't match requested certain hardware actually
691 * still works properly in full-duplex with slightly
692 * different rate values between adc and dac
694 if (cr
!= driver
->frame_rate
&& pr
!= driver
->frame_rate
) {
695 jack_error ("sample rate in use (%d Hz) does not "
696 "match requested rate (%d Hz)",
697 cr
, driver
->frame_rate
);
698 driver
->frame_rate
= cr
;
702 else if (driver
->capture_handle
&& cr
!= driver
->frame_rate
) {
703 jack_error ("capture sample rate in use (%d Hz) does not "
704 "match requested rate (%d Hz)",
705 cr
, driver
->frame_rate
);
706 driver
->frame_rate
= cr
;
708 else if (driver
->playback_handle
&& pr
!= driver
->frame_rate
) {
709 jack_error ("playback sample rate in use (%d Hz) does not "
710 "match requested rate (%d Hz)",
711 pr
, driver
->frame_rate
);
712 driver
->frame_rate
= pr
;
716 /* check the fragment size, since that's non-negotiable */
718 if (driver
->playback_handle
) {
719 snd_pcm_access_t access
;
721 err
= snd_pcm_hw_params_get_period_size (
722 driver
->playback_hw_params
, &p_period_size
, &dir
);
723 err
= snd_pcm_hw_params_get_format (
724 driver
->playback_hw_params
,
725 &(driver
->playback_sample_format
));
726 err
= snd_pcm_hw_params_get_access (driver
->playback_hw_params
,
728 driver
->playback_interleaved
=
729 (access
== SND_PCM_ACCESS_MMAP_INTERLEAVED
)
730 || (access
== SND_PCM_ACCESS_MMAP_COMPLEX
);
732 if (p_period_size
!= driver
->frames_per_cycle
) {
733 jack_error ("alsa_pcm: requested an interrupt every %"
735 " frames but got %u frames for playback",
736 driver
->frames_per_cycle
, p_period_size
);
741 if (driver
->capture_handle
) {
742 snd_pcm_access_t access
;
744 err
= snd_pcm_hw_params_get_period_size (
745 driver
->capture_hw_params
, &c_period_size
, &dir
);
746 err
= snd_pcm_hw_params_get_format (
747 driver
->capture_hw_params
,
748 &(driver
->capture_sample_format
));
749 err
= snd_pcm_hw_params_get_access (driver
->capture_hw_params
,
751 driver
->capture_interleaved
=
752 (access
== SND_PCM_ACCESS_MMAP_INTERLEAVED
)
753 || (access
== SND_PCM_ACCESS_MMAP_COMPLEX
);
755 if (c_period_size
!= driver
->frames_per_cycle
) {
756 jack_error ("alsa_pcm: requested an interrupt every %"
758 " frames but got %u frames for capture",
759 driver
->frames_per_cycle
, c_period_size
);
764 driver
->playback_sample_bytes
=
765 snd_pcm_format_physical_width (driver
->playback_sample_format
)
767 driver
->capture_sample_bytes
=
768 snd_pcm_format_physical_width (driver
->capture_sample_format
)
771 if (driver
->playback_handle
) {
772 switch (driver
->playback_sample_format
) {
773 case SND_PCM_FORMAT_FLOAT_LE
:
774 case SND_PCM_FORMAT_S32_LE
:
775 case SND_PCM_FORMAT_S24_3LE
:
776 case SND_PCM_FORMAT_S24_3BE
:
777 case SND_PCM_FORMAT_S24_LE
:
778 case SND_PCM_FORMAT_S24_BE
:
779 case SND_PCM_FORMAT_S16_LE
:
780 case SND_PCM_FORMAT_S32_BE
:
781 case SND_PCM_FORMAT_S16_BE
:
785 jack_error ("programming error: unhandled format "
786 "type for playback");
791 if (driver
->capture_handle
) {
792 switch (driver
->capture_sample_format
) {
793 case SND_PCM_FORMAT_FLOAT_LE
:
794 case SND_PCM_FORMAT_S32_LE
:
795 case SND_PCM_FORMAT_S24_3LE
:
796 case SND_PCM_FORMAT_S24_3BE
:
797 case SND_PCM_FORMAT_S24_LE
:
798 case SND_PCM_FORMAT_S24_BE
:
799 case SND_PCM_FORMAT_S16_LE
:
800 case SND_PCM_FORMAT_S32_BE
:
801 case SND_PCM_FORMAT_S16_BE
:
805 jack_error ("programming error: unhandled format "
811 if (driver
->playback_interleaved
) {
812 const snd_pcm_channel_area_t
*my_areas
;
813 snd_pcm_uframes_t offset
, frames
;
814 if (snd_pcm_mmap_begin(driver
->playback_handle
,
815 &my_areas
, &offset
, &frames
) < 0) {
816 jack_error ("ALSA: %s: mmap areas info error",
817 driver
->alsa_name_playback
);
820 driver
->interleave_unit
=
821 snd_pcm_format_physical_width (
822 driver
->playback_sample_format
) / 8;
824 driver
->interleave_unit
= 0; /* NOT USED */
827 if (driver
->capture_interleaved
) {
828 const snd_pcm_channel_area_t
*my_areas
;
829 snd_pcm_uframes_t offset
, frames
;
830 if (snd_pcm_mmap_begin(driver
->capture_handle
,
831 &my_areas
, &offset
, &frames
) < 0) {
832 jack_error ("ALSA: %s: mmap areas info error",
833 driver
->alsa_name_capture
);
838 if (driver
->playback_nchannels
> driver
->capture_nchannels
) {
839 driver
->max_nchannels
= driver
->playback_nchannels
;
840 driver
->user_nchannels
= driver
->capture_nchannels
;
842 driver
->max_nchannels
= driver
->capture_nchannels
;
843 driver
->user_nchannels
= driver
->playback_nchannels
;
846 alsa_driver_setup_io_function_pointers (driver
);
848 /* Allocate and initialize structures that rely on the
851 Set up the bit pattern that is used to record which
852 channels require action on every cycle. any bits that are
853 not set after the engine's process() call indicate channels
854 that potentially need to be silenced.
857 bitset_create (&driver
->channels_done
, driver
->max_nchannels
);
858 bitset_create (&driver
->channels_not_done
, driver
->max_nchannels
);
860 if (driver
->playback_handle
) {
861 driver
->playback_addr
= (char **)
862 malloc (sizeof (char *) * driver
->playback_nchannels
);
863 memset (driver
->playback_addr
, 0,
864 sizeof (char *) * driver
->playback_nchannels
);
865 driver
->playback_interleave_skip
= (unsigned long *)
866 malloc (sizeof (unsigned long *) * driver
->playback_nchannels
);
867 memset (driver
->playback_interleave_skip
, 0,
868 sizeof (unsigned long *) * driver
->playback_nchannels
);
869 driver
->silent
= (unsigned long *)
870 malloc (sizeof (unsigned long)
871 * driver
->playback_nchannels
);
873 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
874 driver
->silent
[chn
] = 0;
877 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
878 bitset_add (driver
->channels_done
, chn
);
881 driver
->dither_state
= (dither_state_t
*)
882 calloc ( driver
->playback_nchannels
,
883 sizeof (dither_state_t
));
886 if (driver
->capture_handle
) {
887 driver
->capture_addr
= (char **)
888 malloc (sizeof (char *) * driver
->capture_nchannels
);
889 memset (driver
->capture_addr
, 0,
890 sizeof (char *) * driver
->capture_nchannels
);
891 driver
->capture_interleave_skip
= (unsigned long *)
892 malloc (sizeof (unsigned long *) * driver
->capture_nchannels
);
893 memset (driver
->capture_interleave_skip
, 0,
894 sizeof (unsigned long *) * driver
->capture_nchannels
);
897 driver
->clock_sync_data
= (ClockSyncStatus
*)
898 malloc (sizeof (ClockSyncStatus
) * driver
->max_nchannels
);
900 driver
->period_usecs
=
901 (jack_time_t
) floor ((((float) driver
->frames_per_cycle
) /
902 driver
->frame_rate
) * 1000000.0f
);
903 driver
->poll_timeout
= (int) floor (1.5f
* driver
->period_usecs
);
907 if (driver->engine) {
908 if (driver->engine->set_buffer_size (driver->engine,
909 driver->frames_per_cycle)) {
910 jack_error ("ALSA: Cannot set engine buffer size to %d (check MIDI)", driver->frames_per_cycle);
923 alsa_driver_reset_parameters (alsa_driver_t
*driver
,
924 jack_nframes_t frames_per_cycle
,
925 jack_nframes_t user_nperiods
,
928 /* XXX unregister old ports ? */
929 alsa_driver_release_channel_dependent_memory (driver
);
930 return alsa_driver_set_parameters (driver
,
932 user_nperiods
, rate
);
936 alsa_driver_get_channel_addresses (alsa_driver_t
*driver
,
937 snd_pcm_uframes_t
*capture_avail
,
938 snd_pcm_uframes_t
*playback_avail
,
939 snd_pcm_uframes_t
*capture_offset
,
940 snd_pcm_uframes_t
*playback_offset
)
946 if ((err
= snd_pcm_mmap_begin (
947 driver
->capture_handle
, &driver
->capture_areas
,
948 (snd_pcm_uframes_t
*) capture_offset
,
949 (snd_pcm_uframes_t
*) capture_avail
)) < 0) {
950 jack_error ("ALSA: %s: mmap areas info error",
951 driver
->alsa_name_capture
);
955 for (chn
= 0; chn
< driver
->capture_nchannels
; chn
++) {
956 const snd_pcm_channel_area_t
*a
=
957 &driver
->capture_areas
[chn
];
958 driver
->capture_addr
[chn
] = (char *) a
->addr
959 + ((a
->first
+ a
->step
* *capture_offset
) / 8);
960 driver
->capture_interleave_skip
[chn
] = (unsigned long ) (a
->step
/ 8);
964 if (playback_avail
) {
965 if ((err
= snd_pcm_mmap_begin (
966 driver
->playback_handle
, &driver
->playback_areas
,
967 (snd_pcm_uframes_t
*) playback_offset
,
968 (snd_pcm_uframes_t
*) playback_avail
)) < 0) {
969 jack_error ("ALSA: %s: mmap areas info error ",
970 driver
->alsa_name_playback
);
974 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
975 const snd_pcm_channel_area_t
*a
=
976 &driver
->playback_areas
[chn
];
977 driver
->playback_addr
[chn
] = (char *) a
->addr
978 + ((a
->first
+ a
->step
* *playback_offset
) / 8);
979 driver
->playback_interleave_skip
[chn
] = (unsigned long ) (a
->step
/ 8);
987 alsa_driver_start (alsa_driver_t
*driver
)
990 snd_pcm_uframes_t poffset
, pavail
;
993 driver
->poll_last
= 0;
994 driver
->poll_next
= 0;
996 if (driver
->playback_handle
) {
997 if ((err
= snd_pcm_prepare (driver
->playback_handle
)) < 0) {
998 jack_error ("ALSA: prepare error for playback on "
999 "\"%s\" (%s)", driver
->alsa_name_playback
,
1005 if ((driver
->capture_handle
&& driver
->capture_and_playback_not_synced
)
1006 || !driver
->playback_handle
) {
1007 if ((err
= snd_pcm_prepare (driver
->capture_handle
)) < 0) {
1008 jack_error ("ALSA: prepare error for capture on \"%s\""
1009 " (%s)", driver
->alsa_name_capture
,
1015 if (driver
->hw_monitoring
) {
1016 if (driver
->input_monitor_mask
|| driver
->all_monitor_in
) {
1017 if (driver
->all_monitor_in
) {
1018 driver
->hw
->set_input_monitor_mask (driver
->hw
, ~0U);
1020 driver
->hw
->set_input_monitor_mask (
1021 driver
->hw
, driver
->input_monitor_mask
);
1024 driver
->hw
->set_input_monitor_mask (driver
->hw
,
1025 driver
->input_monitor_mask
);
1029 if (driver
->playback_handle
) {
1030 driver
->playback_nfds
=
1031 snd_pcm_poll_descriptors_count (driver
->playback_handle
);
1033 driver
->playback_nfds
= 0;
1036 if (driver
->capture_handle
) {
1037 driver
->capture_nfds
=
1038 snd_pcm_poll_descriptors_count (driver
->capture_handle
);
1040 driver
->capture_nfds
= 0;
1047 driver
->pfd
= (struct pollfd
*)
1048 malloc (sizeof (struct pollfd
) *
1049 (driver
->playback_nfds
+ driver
->capture_nfds
+ 2));
1051 if (driver
->midi
&& !driver
->xrun_recovery
)
1052 (driver
->midi
->start
)(driver
->midi
);
1054 if (driver
->playback_handle
) {
1055 /* fill playback buffer with zeroes, and mark
1056 all fragments as having data.
1059 pavail
= snd_pcm_avail_update (driver
->playback_handle
);
1062 driver
->frames_per_cycle
* driver
->playback_nperiods
) {
1063 jack_error ("ALSA: full buffer not available at start");
1067 if (alsa_driver_get_channel_addresses (driver
,
1068 0, &pavail
, 0, &poffset
)) {
1072 /* XXX this is cheating. ALSA offers no guarantee that
1073 we can access the entire buffer at any one time. It
1074 works on most hardware tested so far, however, buts
1075 its a liability in the long run. I think that
1076 alsa-lib may have a better function for doing this
1077 here, where the goal is to silence the entire
1081 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
1082 alsa_driver_silence_on_channel (
1084 driver
->user_nperiods
1085 * driver
->frames_per_cycle
);
1088 snd_pcm_mmap_commit (driver
->playback_handle
, poffset
,
1089 driver
->user_nperiods
1090 * driver
->frames_per_cycle
);
1092 if ((err
= snd_pcm_start (driver
->playback_handle
)) < 0) {
1093 jack_error ("ALSA: could not start playback (%s)",
1094 snd_strerror (err
));
1099 if ((driver
->capture_handle
&& driver
->capture_and_playback_not_synced
)
1100 || !driver
->playback_handle
) {
1101 if ((err
= snd_pcm_start (driver
->capture_handle
)) < 0) {
1102 jack_error ("ALSA: could not start capture (%s)",
1103 snd_strerror (err
));
1112 alsa_driver_stop (alsa_driver_t
*driver
)
1118 /* silence all capture port buffers, because we might
1119 be entering offline mode.
1124 for (chn = 0, node = driver->capture_ports; node;
1125 node = jack_slist_next (node), chn++) {
1129 jack_nframes_t nframes = driver->engine->control->buffer_size;
1131 port = (jack_port_t *) node->data;
1132 buf = jack_port_get_buffer (port, nframes);
1133 memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
1140 if (driver
->playback_handle
) {
1141 if ((err
= snd_pcm_drop (driver
->playback_handle
)) < 0) {
1142 jack_error ("ALSA: channel flush for playback "
1143 "failed (%s)", snd_strerror (err
));
1148 if (!driver
->playback_handle
1149 || driver
->capture_and_playback_not_synced
) {
1150 if (driver
->capture_handle
) {
1151 if ((err
= snd_pcm_drop (driver
->capture_handle
)) < 0) {
1152 jack_error ("ALSA: channel flush for "
1153 "capture failed (%s)",
1154 snd_strerror (err
));
1160 if (driver
->hw_monitoring
) {
1161 driver
->hw
->set_input_monitor_mask (driver
->hw
, 0);
1164 if (driver
->midi
&& !driver
->xrun_recovery
)
1165 (driver
->midi
->stop
)(driver
->midi
);
1171 alsa_driver_restart (alsa_driver_t
*driver
)
1175 driver
->xrun_recovery
= 1;
1178 if ((res = driver->nt_stop((struct _jack_driver_nt *) driver))==0)
1179 res = driver->nt_start((struct _jack_driver_nt *) driver);
1182 driver
->xrun_recovery
= 0;
1184 if (res
&& driver
->midi
)
1185 (driver
->midi
->stop
)(driver
->midi
);
1191 alsa_driver_xrun_recovery (alsa_driver_t
*driver
, float *delayed_usecs
)
1193 snd_pcm_status_t
*status
;
1196 snd_pcm_status_alloca(&status
);
1198 if (driver
->capture_handle
) {
1199 if ((res
= snd_pcm_status(driver
->capture_handle
, status
))
1201 jack_error("status error: %s", snd_strerror(res
));
1204 if ((res
= snd_pcm_status(driver
->playback_handle
, status
))
1206 jack_error("status error: %s", snd_strerror(res
));
1210 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_SUSPENDED
)
1212 jack_log("**** alsa_pcm: pcm in suspended state, resuming it" );
1213 if (driver
->capture_handle
) {
1214 if ((res
= snd_pcm_prepare(driver
->capture_handle
))
1216 jack_error("error preparing after suspend: %s", snd_strerror(res
));
1219 if (driver
->playback_handle
) {
1220 if ((res
= snd_pcm_prepare(driver
->playback_handle
))
1222 jack_error("error preparing after suspend: %s", snd_strerror(res
));
1227 if (snd_pcm_status_get_state(status
) == SND_PCM_STATE_XRUN
1228 && driver
->process_count
> XRUN_REPORT_DELAY
) {
1229 struct timeval now
, diff
, tstamp
;
1230 driver
->xrun_count
++;
1231 snd_pcm_status_get_tstamp(status
,&now
);
1232 snd_pcm_status_get_trigger_tstamp(status
, &tstamp
);
1233 timersub(&now
, &tstamp
, &diff
);
1234 *delayed_usecs
= diff
.tv_sec
* 1000000.0 + diff
.tv_usec
;
1235 jack_log("**** alsa_pcm: xrun of at least %.3f msecs",*delayed_usecs
/ 1000.0);
1236 if (driver
->capture_handle
) {
1237 jack_log("Repreparing capture");
1238 if ((res
= snd_pcm_prepare(driver
->capture_handle
)) < 0) {
1239 jack_error("error preparing after xrun: %s", snd_strerror(res
));
1242 if (driver
->playback_handle
) {
1243 jack_log("Repreparing playback");
1244 if ((res
= snd_pcm_prepare(driver
->playback_handle
)) < 0) {
1245 jack_error("error preparing after xrun: %s", snd_strerror(res
));
1250 if (alsa_driver_restart (driver
)) {
1257 alsa_driver_silence_untouched_channels (alsa_driver_t
*driver
,
1258 jack_nframes_t nframes
)
1261 jack_nframes_t buffer_frames
=
1262 driver
->frames_per_cycle
* driver
->playback_nperiods
;
1264 for (chn
= 0; chn
< driver
->playback_nchannels
; chn
++) {
1265 if (bitset_contains (driver
->channels_not_done
, chn
)) {
1266 if (driver
->silent
[chn
] < buffer_frames
) {
1267 alsa_driver_silence_on_channel_no_mark (
1268 driver
, chn
, nframes
);
1269 driver
->silent
[chn
] += nframes
;
1276 alsa_driver_set_clock_sync_status (alsa_driver_t
*driver
, channel_t chn
,
1277 ClockSyncStatus status
)
1279 driver
->clock_sync_data
[chn
] = status
;
1280 alsa_driver_clock_sync_notify (driver
, chn
, status
);
1283 static int under_gdb
= FALSE
;
1286 alsa_driver_wait (alsa_driver_t
*driver
, int extra_fd
, int *status
, float
1289 snd_pcm_sframes_t avail
= 0;
1290 snd_pcm_sframes_t capture_avail
= 0;
1291 snd_pcm_sframes_t playback_avail
= 0;
1292 int xrun_detected
= FALSE
;
1297 jack_time_t poll_enter
;
1298 jack_time_t poll_ret
= 0;
1303 need_capture
= driver
->capture_handle
? 1 : 0;
1305 if (extra_fd
>= 0) {
1308 need_playback
= driver
->playback_handle
? 1 : 0;
1313 while ((need_playback
|| need_capture
) && !xrun_detected
) {
1316 unsigned int ci
= 0;
1318 unsigned short revents
;
1322 if (need_playback
) {
1323 snd_pcm_poll_descriptors (driver
->playback_handle
,
1325 driver
->playback_nfds
);
1326 nfds
+= driver
->playback_nfds
;
1330 snd_pcm_poll_descriptors (driver
->capture_handle
,
1332 driver
->capture_nfds
);
1334 nfds
+= driver
->capture_nfds
;
1337 /* ALSA doesn't set POLLERR in some versions of 0.9.X */
1339 for (i
= 0; i
< nfds
; i
++) {
1340 driver
->pfd
[i
].events
|= POLLERR
;
1343 if (extra_fd
>= 0) {
1344 driver
->pfd
[nfds
].fd
= extra_fd
;
1345 driver
->pfd
[nfds
].events
=
1346 POLLIN
|POLLERR
|POLLHUP
|POLLNVAL
;
1350 poll_enter
= jack_get_microseconds ();
1352 if (poll_enter
> driver
->poll_next
) {
1354 * This processing cycle was delayed past the
1355 * next due interrupt! Do not account this as
1358 driver
->poll_next
= 0;
1359 driver
->poll_late
++;
1363 poll_result
= poll (driver
->pfd
, nfds
, -1); //fix for sleep issue
1365 poll_result
= poll (driver
->pfd
, nfds
, driver
->poll_timeout
);
1367 if (poll_result
< 0) {
1369 if (errno
== EINTR
) {
1370 const char poll_log
[] = "ALSA: poll interrupt";
1371 // this happens mostly when run
1372 // under gdb, or when exiting due to a signal
1374 jack_info(poll_log
);
1377 jack_error(poll_log
);
1382 jack_error ("ALSA: poll call failed (%s)",
1389 poll_ret
= jack_get_microseconds ();
1391 if (poll_result
== 0) {
1393 if(retry_cnt
> MAX_RETRY_COUNT
) {
1394 jack_error ("ALSA: poll time out, polled for %" PRIu64
1395 " usecs, Reached max retry cnt = %d, Exiting",
1396 poll_ret
- poll_enter
, MAX_RETRY_COUNT
);
1400 jack_error ("ALSA: poll time out, polled for %" PRIu64
1401 " usecs, Retrying with a recovery, retry cnt = %d",
1402 poll_ret
- poll_enter
, retry_cnt
);
1403 *status
= alsa_driver_xrun_recovery (driver
, delayed_usecs
);
1405 jack_error ("ALSA: poll time out, recovery failed with status = %d", *status
);
1414 if (driver
->poll_next
&& poll_ret
> driver
->poll_next
) {
1415 *delayed_usecs
= poll_ret
- driver
->poll_next
;
1417 driver
->poll_last
= poll_ret
;
1418 driver
->poll_next
= poll_ret
+ driver
->period_usecs
;
1421 driver->engine->transport_cycle_start (driver->engine,
1427 fprintf (stderr
, "%" PRIu64
": checked %d fds, started at %" PRIu64
" %" PRIu64
" usecs since poll entered\n",
1428 poll_ret
, nfds
, poll_enter
, poll_ret
- poll_enter
);
1431 /* check to see if it was the extra FD that caused us
1432 * to return from poll */
1434 if (extra_fd
>= 0) {
1436 if (driver
->pfd
[nfds
-1].revents
== 0) {
1437 /* we timed out on the extra fd */
1443 /* if POLLIN was the only bit set, we're OK */
1446 return (driver
->pfd
[nfds
-1].revents
== POLLIN
) ? 0 : -1;
1449 if (need_playback
) {
1450 if (snd_pcm_poll_descriptors_revents
1451 (driver
->playback_handle
, &driver
->pfd
[0],
1452 driver
->playback_nfds
, &revents
) < 0) {
1453 jack_error ("ALSA: playback revents failed");
1458 if (revents
& POLLNVAL
) {
1459 jack_error ("ALSA: playback device disconnected");
1464 if (revents
& POLLERR
) {
1465 xrun_detected
= TRUE
;
1468 if (revents
& POLLOUT
) {
1471 fprintf (stderr
, "%" PRIu64
1472 " playback stream ready\n",
1479 if (snd_pcm_poll_descriptors_revents
1480 (driver
->capture_handle
, &driver
->pfd
[ci
],
1481 driver
->capture_nfds
, &revents
) < 0) {
1482 jack_error ("ALSA: capture revents failed");
1487 if (revents
& POLLNVAL
) {
1488 jack_error ("ALSA: capture device disconnected");
1493 if (revents
& POLLERR
) {
1494 xrun_detected
= TRUE
;
1497 if (revents
& POLLIN
) {
1500 fprintf (stderr
, "%" PRIu64
1501 " capture stream ready\n",
1508 if (driver
->capture_handle
) {
1509 if ((capture_avail
= snd_pcm_avail_update (
1510 driver
->capture_handle
)) < 0) {
1511 if (capture_avail
== -EPIPE
) {
1512 xrun_detected
= TRUE
;
1514 jack_error ("unknown ALSA avail_update return"
1515 " value (%u)", capture_avail
);
1519 /* odd, but see min() computation below */
1520 capture_avail
= INT_MAX
;
1523 if (driver
->playback_handle
) {
1524 if ((playback_avail
= snd_pcm_avail_update (
1525 driver
->playback_handle
)) < 0) {
1526 if (playback_avail
== -EPIPE
) {
1527 xrun_detected
= TRUE
;
1529 jack_error ("unknown ALSA avail_update return"
1530 " value (%u)", playback_avail
);
1534 /* odd, but see min() computation below */
1535 playback_avail
= INT_MAX
;
1538 if (xrun_detected
) {
1539 *status
= alsa_driver_xrun_recovery (driver
, delayed_usecs
);
1544 driver
->last_wait_ust
= poll_ret
;
1546 avail
= capture_avail
< playback_avail
? capture_avail
: playback_avail
;
1549 fprintf (stderr
, "wakeup complete, avail = %lu, pavail = %lu "
1551 avail
, playback_avail
, capture_avail
);
1554 /* mark all channels not done for now. read/write will change this */
1556 bitset_copy (driver
->channels_not_done
, driver
->channels_done
);
1558 /* constrain the available count to the nearest (round down) number of
1562 return avail
- (avail
% driver
->frames_per_cycle
);
1567 alsa_driver_read (alsa_driver_t
*driver
, jack_nframes_t nframes
)
1569 snd_pcm_sframes_t contiguous
;
1570 snd_pcm_sframes_t nread
;
1571 snd_pcm_uframes_t offset
;
1572 jack_nframes_t orig_nframes
;
1573 // jack_default_audio_sample_t* buf;
1576 // jack_port_t* port;
1579 if (nframes
> driver
->frames_per_cycle
) {
1585 if (driver->engine->freewheeling) {
1590 (driver
->midi
->read
)(driver
->midi
, nframes
);
1592 if (!driver
->capture_handle
) {
1598 orig_nframes
= nframes
;
1602 contiguous
= nframes
;
1604 if (alsa_driver_get_channel_addresses (
1606 (snd_pcm_uframes_t
*) &contiguous
,
1607 (snd_pcm_uframes_t
*) 0,
1613 for (chn = 0, node = driver->capture_ports; node;
1614 node = jack_slist_next (node), chn++) {
1616 port = (jack_port_t *) node->data;
1618 if (!jack_port_connected (port)) {
1619 // no-copy optimization
1622 buf = jack_port_get_buffer (port, orig_nframes);
1623 alsa_driver_read_from_channel (driver, chn,
1624 buf + nread, contiguous);
1627 ReadInput(orig_nframes
, contiguous
, nread
);
1629 if ((err
= snd_pcm_mmap_commit (driver
->capture_handle
,
1630 offset
, contiguous
)) < 0) {
1631 jack_error ("ALSA: could not complete read of %"
1632 PRIu32
" frames: error = %d", contiguous
, err
);
1636 nframes
-= contiguous
;
1637 nread
+= contiguous
;
1644 alsa_driver_write (alsa_driver_t
* driver
, jack_nframes_t nframes
)
1648 // JSList *mon_node;
1649 // jack_default_audio_sample_t* buf;
1650 // jack_default_audio_sample_t* monbuf;
1651 jack_nframes_t orig_nframes
;
1652 snd_pcm_sframes_t nwritten
;
1653 snd_pcm_sframes_t contiguous
;
1654 snd_pcm_uframes_t offset
;
1655 // jack_port_t *port;
1658 driver
->process_count
++;
1662 if (!driver->playback_handle || driver->engine->freewheeling) {
1666 if (!driver
->playback_handle
) {
1670 if (nframes
> driver
->frames_per_cycle
) {
1675 (driver
->midi
->write
)(driver
->midi
, nframes
);
1679 orig_nframes
= nframes
;
1681 /* check current input monitor request status */
1683 driver
->input_monitor_mask
= 0;
1687 for (chn = 0, node = driver->capture_ports; node;
1688 node = jack_slist_next (node), chn++) {
1689 if (((jack_port_t *) node->data)->shared->monitor_requests) {
1690 driver->input_monitor_mask |= (1<<chn);
1696 if (driver
->hw_monitoring
) {
1697 if ((driver
->hw
->input_monitor_mask
1698 != driver
->input_monitor_mask
)
1699 && !driver
->all_monitor_in
) {
1700 driver
->hw
->set_input_monitor_mask (
1701 driver
->hw
, driver
->input_monitor_mask
);
1707 contiguous
= nframes
;
1709 if (alsa_driver_get_channel_addresses (
1711 (snd_pcm_uframes_t
*) 0,
1712 (snd_pcm_uframes_t
*) &contiguous
,
1719 for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
1721 node = jack_slist_next (node), chn++) {
1723 port = (jack_port_t *) node->data;
1725 if (!jack_port_connected (port)) {
1728 buf = jack_port_get_buffer (port, orig_nframes);
1729 alsa_driver_write_to_channel (driver, chn,
1730 buf + nwritten, contiguous);
1733 port = (jack_port_t *) mon_node->data;
1734 if (!jack_port_connected (port)) {
1737 monbuf = jack_port_get_buffer (port, orig_nframes);
1738 memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
1739 mon_node = jack_slist_next (mon_node);
1745 WriteOutput(orig_nframes
, contiguous
, nwritten
);
1747 if (!bitset_empty (driver
->channels_not_done
)) {
1748 alsa_driver_silence_untouched_channels (driver
,
1752 if ((err
= snd_pcm_mmap_commit (driver
->playback_handle
,
1753 offset
, contiguous
)) < 0) {
1754 jack_error ("ALSA: could not complete playback of %"
1755 PRIu32
" frames: error = %d", contiguous
, err
);
1756 if (err
!= -EPIPE
&& err
!= -ESTRPIPE
)
1760 nframes
-= contiguous
;
1761 nwritten
+= contiguous
;
1768 static int /* UNUSED */
1769 alsa_driver_change_sample_clock (alsa_driver_t
*driver
, SampleClockMode mode
)
1771 return driver
->hw
->change_sample_clock (driver
->hw
, mode
);
1774 static void /* UNUSED */
1775 alsa_driver_request_all_monitor_input (alsa_driver_t
*driver
, int yn
)
1778 if (driver
->hw_monitoring
) {
1780 driver
->hw
->set_input_monitor_mask (driver
->hw
, ~0U);
1782 driver
->hw
->set_input_monitor_mask (
1783 driver
->hw
, driver
->input_monitor_mask
);
1787 driver
->all_monitor_in
= yn
;
1790 static void /* UNUSED */
1791 alsa_driver_set_hw_monitoring (alsa_driver_t
*driver
, int yn
)
1794 driver
->hw_monitoring
= TRUE
;
1796 if (driver
->all_monitor_in
) {
1797 driver
->hw
->set_input_monitor_mask (driver
->hw
, ~0U);
1799 driver
->hw
->set_input_monitor_mask (
1800 driver
->hw
, driver
->input_monitor_mask
);
1803 driver
->hw_monitoring
= FALSE
;
1804 driver
->hw
->set_input_monitor_mask (driver
->hw
, 0);
1808 static ClockSyncStatus
/* UNUSED */
1809 alsa_driver_clock_sync_status (channel_t chn
)
1816 alsa_driver_delete (alsa_driver_t
*driver
)
1821 (driver
->midi
->destroy
)(driver
->midi
);
1823 for (node
= driver
->clock_sync_listeners
; node
;
1824 node
= jack_slist_next (node
)) {
1827 jack_slist_free (driver
->clock_sync_listeners
);
1829 if (driver
->ctl_handle
) {
1830 snd_ctl_close (driver
->ctl_handle
);
1831 driver
->ctl_handle
= 0;
1834 if (driver
->capture_handle
) {
1835 snd_pcm_close (driver
->capture_handle
);
1836 driver
->capture_handle
= 0;
1839 if (driver
->playback_handle
) {
1840 snd_pcm_close (driver
->playback_handle
);
1841 driver
->capture_handle
= 0;
1844 if (driver
->capture_hw_params
) {
1845 snd_pcm_hw_params_free (driver
->capture_hw_params
);
1846 driver
->capture_hw_params
= 0;
1849 if (driver
->playback_hw_params
) {
1850 snd_pcm_hw_params_free (driver
->playback_hw_params
);
1851 driver
->playback_hw_params
= 0;
1854 if (driver
->capture_sw_params
) {
1855 snd_pcm_sw_params_free (driver
->capture_sw_params
);
1856 driver
->capture_sw_params
= 0;
1859 if (driver
->playback_sw_params
) {
1860 snd_pcm_sw_params_free (driver
->playback_sw_params
);
1861 driver
->playback_sw_params
= 0;
1869 driver
->hw
->release (driver
->hw
);
1872 free(driver
->alsa_name_playback
);
1873 free(driver
->alsa_name_capture
);
1874 free(driver
->alsa_driver
);
1876 alsa_driver_release_channel_dependent_memory (driver
);
1878 //jack_driver_nt_finish ((jack_driver_nt_t *) driver);
1883 discover_alsa_using_apps ()
1887 char* path
= getenv ("PATH");
1898 /* look for lsof and give up if its not in PATH */
1900 path
= strdup (path
);
1901 dir
= strtok (path
, ":");
1903 char maybe
[PATH_MAX
+1];
1904 snprintf (maybe
, sizeof(maybe
), "%s/lsof", dir
);
1905 if (access (maybe
, X_OK
) == 0) {
1908 dir
= strtok (NULL
, ":");
1916 snprintf (command
, sizeof (command
), "lsof -Fc0 ");
1917 cmdlen
= strlen (command
);
1919 for (card
= 0; card
< 8; ++card
) {
1920 for (device
= 0; device
< 8; ++device
) {
1923 snprintf (buf
, sizeof (buf
), "/dev/snd/pcmC%dD%dp", card
, device
);
1924 if (access (buf
, F_OK
) == 0) {
1925 snprintf (command
+cmdlen
, sizeof(command
)-cmdlen
, "%s ", buf
);
1927 cmdlen
= strlen (command
);
1929 snprintf (buf
, sizeof (buf
), "/dev/snd/pcmC%dD%dc", card
, device
);
1930 if (access (buf
, F_OK
) == 0) {
1931 snprintf (command
+cmdlen
, sizeof(command
)-cmdlen
, "%s ", buf
);
1933 cmdlen
= strlen (command
);
1937 FILE* f
= popen (command
, "r");
1944 char buf
[1024]; /* lsof doesn't output much */
1946 if (!fgets (buf
, sizeof (buf
), f
)) {
1954 /* buf contains NULL as a separator between the process field and the command field */
1956 ++pid
; /* skip leading 'p' */
1963 ++cmd
; /* skip to 'c' */
1964 ++cmd
; /* skip to first character of command */
1966 snprintf (found
+flen
, sizeof (found
)-flen
, "%s (process ID %s)\n", cmd
, pid
);
1967 flen
= strlen (found
);
1969 if (flen
>= sizeof (found
)) {
1977 return strdup (found
);
1984 alsa_driver_new (char *name
, char *playback_alsa_device
,
1985 char *capture_alsa_device
,
1986 jack_client_t
*client
,
1987 jack_nframes_t frames_per_cycle
,
1988 jack_nframes_t user_nperiods
,
1989 jack_nframes_t rate
,
1994 DitherAlgorithm dither
,
1997 int user_capture_nchnls
,
1998 int user_playback_nchnls
,
2000 jack_nframes_t capture_latency
,
2001 jack_nframes_t playback_latency
,
2002 alsa_midi_t
*midi_driver
2007 alsa_driver_t
*driver
;
2009 jack_info ("creating alsa driver ... %s|%s|%" PRIu32
"|%" PRIu32
2010 "|%" PRIu32
"|%" PRIu32
"|%" PRIu32
"|%s|%s|%s|%s",
2011 playing
? playback_alsa_device
: "-",
2012 capturing
? capture_alsa_device
: "-",
2013 frames_per_cycle
, user_nperiods
, rate
,
2014 user_capture_nchnls
,user_playback_nchnls
,
2015 hw_monitoring
? "hwmon": "nomon",
2016 hw_metering
? "hwmeter":"swmeter",
2017 soft_mode
? "soft-mode":"-",
2018 shorts_first
? "16bit":"32bit");
2020 driver
= (alsa_driver_t
*) calloc (1, sizeof (alsa_driver_t
));
2022 jack_driver_nt_init ((jack_driver_nt_t
*) driver
);
2026 driver->nt_attach = (JackDriverNTAttachFunction) alsa_driver_attach;
2027 driver->nt_detach = (JackDriverNTDetachFunction) alsa_driver_detach;
2028 driver->read = (JackDriverReadFunction) alsa_driver_read;
2029 driver->write = (JackDriverReadFunction) alsa_driver_write;
2030 driver->null_cycle = (JackDriverNullCycleFunction) alsa_driver_null_cycle;
2031 driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
2032 driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
2033 driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
2034 driver->nt_run_cycle = (JackDriverNTRunCycleFunction) alsa_driver_run_cycle;
2037 driver
->playback_handle
= NULL
;
2038 driver
->capture_handle
= NULL
;
2039 driver
->ctl_handle
= 0;
2041 driver
->capture_and_playback_not_synced
= FALSE
;
2042 driver
->max_nchannels
= 0;
2043 driver
->user_nchannels
= 0;
2044 driver
->playback_nchannels
= user_playback_nchnls
;
2045 driver
->capture_nchannels
= user_capture_nchnls
;
2046 driver
->playback_sample_bytes
= (shorts_first
? 2:4);
2047 driver
->capture_sample_bytes
= (shorts_first
? 2:4);
2048 driver
->capture_frame_latency
= capture_latency
;
2049 driver
->playback_frame_latency
= playback_latency
;
2051 driver
->playback_addr
= 0;
2052 driver
->capture_addr
= 0;
2053 driver
->playback_interleave_skip
= NULL
;
2054 driver
->capture_interleave_skip
= NULL
;
2058 driver
->all_monitor_in
= FALSE
;
2059 driver
->with_monitor_ports
= monitor
;
2061 driver
->clock_mode
= ClockMaster
; /* XXX is it? */
2062 driver
->input_monitor_mask
= 0; /* XXX is it? */
2064 driver
->capture_ports
= 0;
2065 driver
->playback_ports
= 0;
2066 driver
->monitor_ports
= 0;
2069 driver
->playback_nfds
= 0;
2070 driver
->capture_nfds
= 0;
2072 driver
->dither
= dither
;
2073 driver
->soft_mode
= soft_mode
;
2075 driver
->quirk_bswap
= 0;
2077 pthread_mutex_init (&driver
->clock_sync_lock
, 0);
2078 driver
->clock_sync_listeners
= 0;
2080 driver
->poll_late
= 0;
2081 driver
->xrun_count
= 0;
2082 driver
->process_count
= 0;
2084 driver
->alsa_name_playback
= strdup (playback_alsa_device
);
2085 driver
->alsa_name_capture
= strdup (capture_alsa_device
);
2087 driver
->midi
= midi_driver
;
2088 driver
->xrun_recovery
= 0;
2090 if (alsa_driver_check_card_type (driver
)) {
2091 alsa_driver_delete (driver
);
2095 alsa_driver_hw_specific (driver
, hw_monitoring
, hw_metering
);
2098 if (snd_pcm_open (&driver
->playback_handle
,
2099 playback_alsa_device
,
2100 SND_PCM_STREAM_PLAYBACK
,
2101 SND_PCM_NONBLOCK
) < 0) {
2105 jack_error ("\n\nATTENTION: The playback device \"%s\" is "
2106 "already in use. Please stop the"
2107 " application using it and "
2109 playback_alsa_device
);
2111 current_apps
= discover_alsa_using_apps ();
2113 jack_error ("\n\nATTENTION: The playback device \"%s\" is "
2114 "already in use. The following applications "
2115 " are using your soundcard(s) so you should "
2116 " check them and stop them as necessary before "
2117 " trying to start JACK again:\n\n%s",
2118 playback_alsa_device
,
2120 free (current_apps
);
2122 jack_error ("\n\nATTENTION: The playback device \"%s\" is "
2123 "already in use. Please stop the"
2124 " application using it and "
2126 playback_alsa_device
);
2129 alsa_driver_delete (driver
);
2133 jack_error ("you do not have permission to open "
2134 "the audio device \"%s\" for playback",
2135 playback_alsa_device
);
2136 alsa_driver_delete (driver
);
2141 driver
->playback_handle
= NULL
;
2144 if (driver
->playback_handle
) {
2145 snd_pcm_nonblock (driver
->playback_handle
, 0);
2150 if (snd_pcm_open (&driver
->capture_handle
,
2151 capture_alsa_device
,
2152 SND_PCM_STREAM_CAPTURE
,
2153 SND_PCM_NONBLOCK
) < 0) {
2157 jack_error ("\n\nATTENTION: The capture (recording) device \"%s\" is "
2159 capture_alsa_device
);
2161 current_apps
= discover_alsa_using_apps ();
2163 jack_error ("\n\nATTENTION: The capture device \"%s\" is "
2164 "already in use. The following applications "
2165 " are using your soundcard(s) so you should "
2166 " check them and stop them as necessary before "
2167 " trying to start JACK again:\n\n%s",
2168 capture_alsa_device
,
2170 free (current_apps
);
2172 jack_error ("\n\nATTENTION: The capture (recording) device \"%s\" is "
2173 "already in use. Please stop the"
2174 " application using it and "
2176 capture_alsa_device
);
2178 alsa_driver_delete (driver
);
2184 jack_error ("you do not have permission to open "
2185 "the audio device \"%s\" for capture",
2186 capture_alsa_device
);
2187 alsa_driver_delete (driver
);
2192 driver
->capture_handle
= NULL
;
2195 if (driver
->capture_handle
) {
2196 snd_pcm_nonblock (driver
->capture_handle
, 0);
2200 if (driver
->playback_handle
== NULL
) {
2203 /* they asked for playback, but we can't do it */
2205 jack_error ("ALSA: Cannot open PCM device %s for "
2206 "playback. Falling back to capture-only"
2209 if (driver
->capture_handle
== NULL
) {
2210 /* can't do anything */
2211 alsa_driver_delete (driver
);
2219 if (driver
->capture_handle
== NULL
) {
2222 /* they asked for capture, but we can't do it */
2224 jack_error ("ALSA: Cannot open PCM device %s for "
2225 "capture. Falling back to playback-only"
2228 if (driver
->playback_handle
== NULL
) {
2229 /* can't do anything */
2230 alsa_driver_delete (driver
);
2238 driver
->playback_hw_params
= 0;
2239 driver
->capture_hw_params
= 0;
2240 driver
->playback_sw_params
= 0;
2241 driver
->capture_sw_params
= 0;
2243 if (driver
->playback_handle
) {
2244 if ((err
= snd_pcm_hw_params_malloc (
2245 &driver
->playback_hw_params
)) < 0) {
2246 jack_error ("ALSA: could not allocate playback hw"
2247 " params structure");
2248 alsa_driver_delete (driver
);
2252 if ((err
= snd_pcm_sw_params_malloc (
2253 &driver
->playback_sw_params
)) < 0) {
2254 jack_error ("ALSA: could not allocate playback sw"
2255 " params structure");
2256 alsa_driver_delete (driver
);
2261 if (driver
->capture_handle
) {
2262 if ((err
= snd_pcm_hw_params_malloc (
2263 &driver
->capture_hw_params
)) < 0) {
2264 jack_error ("ALSA: could not allocate capture hw"
2265 " params structure");
2266 alsa_driver_delete (driver
);
2270 if ((err
= snd_pcm_sw_params_malloc (
2271 &driver
->capture_sw_params
)) < 0) {
2272 jack_error ("ALSA: could not allocate capture sw"
2273 " params structure");
2274 alsa_driver_delete (driver
);
2279 if (alsa_driver_set_parameters (driver
, frames_per_cycle
,
2280 user_nperiods
, rate
)) {
2281 alsa_driver_delete (driver
);
2285 driver
->capture_and_playback_not_synced
= FALSE
;
2287 if (driver
->capture_handle
&& driver
->playback_handle
) {
2288 if (snd_pcm_link (driver
->playback_handle
,
2289 driver
->capture_handle
) != 0) {
2290 driver
->capture_and_playback_not_synced
= TRUE
;
2294 driver
->client
= client
;
2296 return (jack_driver_t
*) driver
;
2300 alsa_driver_listen_for_clock_sync_status (alsa_driver_t
*driver
,
2301 ClockSyncListenerFunction func
,
2304 ClockSyncListener
*csl
;
2306 csl
= (ClockSyncListener
*) malloc (sizeof (ClockSyncListener
));
2307 csl
->function
= func
;
2309 csl
->id
= driver
->next_clock_sync_listener_id
++;
2311 pthread_mutex_lock (&driver
->clock_sync_lock
);
2312 driver
->clock_sync_listeners
=
2313 jack_slist_prepend (driver
->clock_sync_listeners
, csl
);
2314 pthread_mutex_unlock (&driver
->clock_sync_lock
);
2319 alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t
*driver
,
2325 pthread_mutex_lock (&driver
->clock_sync_lock
);
2326 for (node
= driver
->clock_sync_listeners
; node
;
2327 node
= jack_slist_next (node
)) {
2328 if (((ClockSyncListener
*) node
->data
)->id
== which
) {
2329 driver
->clock_sync_listeners
=
2330 jack_slist_remove_link (
2331 driver
->clock_sync_listeners
, node
);
2333 jack_slist_free_1 (node
);
2338 pthread_mutex_unlock (&driver
->clock_sync_lock
);
2343 alsa_driver_clock_sync_notify (alsa_driver_t
*driver
, channel_t chn
,
2344 ClockSyncStatus status
)
2348 pthread_mutex_lock (&driver
->clock_sync_lock
);
2349 for (node
= driver
->clock_sync_listeners
; node
;
2350 node
= jack_slist_next (node
)) {
2351 ClockSyncListener
*csl
= (ClockSyncListener
*) node
->data
;
2352 csl
->function (chn
, status
, csl
->arg
);
2354 pthread_mutex_unlock (&driver
->clock_sync_lock
);
2358 /* DRIVER "PLUGIN" INTERFACE */
2360 const char driver_client_name
[] = "alsa_pcm";
2363 driver_finish (jack_driver_t
*driver
)
2365 alsa_driver_delete ((alsa_driver_t
*) driver
);