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.
29
#include <sys/types.h>
33
#include <jack/internal.h>
34
#include <jack/engine.h>
35
#include <jack/messagebuffer.h>
37
#include <sysdeps/time.h>
39
#include "alsa_driver.h"
40
#include "hammerfall.h"
46
extern void store_work_time (int);
47
extern void store_wait_time (int);
48
extern void show_wait_times ();
49
extern void show_work_times ();
53
/* Delay (in process calls) before jackd will report an xrun */
54
#define XRUN_REPORT_DELAY 0
57
alsa_driver_release_channel_dependent_memory (alsa_driver_t *driver)
59
bitset_destroy (&driver->channels_done);
60
bitset_destroy (&driver->channels_not_done);
62
if (driver->playback_addr) {
63
free (driver->playback_addr);
64
driver->playback_addr = 0;
67
if (driver->capture_addr) {
68
free (driver->capture_addr);
69
driver->capture_addr = 0;
72
if (driver->playback_interleave_skip) {
73
free (driver->playback_interleave_skip);
74
driver->playback_interleave_skip = NULL;
77
if (driver->capture_interleave_skip) {
78
free (driver->capture_interleave_skip);
79
driver->capture_interleave_skip = NULL;
83
free (driver->silent);
87
if (driver->dither_state) {
88
free (driver->dither_state);
89
driver->dither_state = 0;
94
alsa_driver_check_capabilities (alsa_driver_t *driver)
100
alsa_driver_check_card_type (alsa_driver_t *driver)
103
snd_ctl_card_info_t *card_info;
107
snd_ctl_card_info_alloca (&card_info);
109
regcomp(&expression,"(plug)?hw:[0-9](,[0-9])?",REG_ICASE|REG_EXTENDED);
111
if (!regexec(&expression,driver->alsa_name_playback,0,NULL,0)) {
112
/* the user wants a hw or plughw device, the ctl name
113
* should be hw:x where x is the card number */
116
strncpy(tmp,strstr(driver->alsa_name_playback,"hw"),4);
118
jack_info("control device %s",tmp);
119
ctl_name = strdup(tmp);
121
ctl_name = strdup(driver->alsa_name_playback);
124
// XXX: I don't know the "right" way to do this. Which to use
125
// driver->alsa_name_playback or driver->alsa_name_capture.
126
if ((err = snd_ctl_open (&driver->ctl_handle, ctl_name, 0)) < 0) {
127
jack_error ("control open \"%s\" (%s)", ctl_name,
129
} else if ((err = snd_ctl_card_info(driver->ctl_handle, card_info)) < 0) {
130
jack_error ("control hardware info \"%s\" (%s)",
131
driver->alsa_name_playback, snd_strerror (err));
132
snd_ctl_close (driver->ctl_handle);
135
driver->alsa_driver = strdup(snd_ctl_card_info_get_driver (card_info));
137
regfree(&expression);
140
return alsa_driver_check_capabilities (driver);
144
alsa_driver_hammerfall_hardware (alsa_driver_t *driver)
146
driver->hw = jack_alsa_hammerfall_hw_new (driver);
151
alsa_driver_hdsp_hardware (alsa_driver_t *driver)
153
driver->hw = jack_alsa_hdsp_hw_new (driver);
158
alsa_driver_ice1712_hardware (alsa_driver_t *driver)
160
driver->hw = jack_alsa_ice1712_hw_new (driver);
165
alsa_driver_usx2y_hardware (alsa_driver_t *driver)
167
driver->hw = jack_alsa_usx2y_hw_new (driver);
172
alsa_driver_generic_hardware (alsa_driver_t *driver)
174
driver->hw = jack_alsa_generic_hw_new (driver);
179
alsa_driver_hw_specific (alsa_driver_t *driver, int hw_monitoring,
184
if (!strcmp(driver->alsa_driver, "RME9652")) {
185
if ((err = alsa_driver_hammerfall_hardware (driver)) != 0) {
188
} else if (!strcmp(driver->alsa_driver, "H-DSP")) {
189
if ((err = alsa_driver_hdsp_hardware (driver)) !=0) {
192
} else if (!strcmp(driver->alsa_driver, "ICE1712")) {
193
if ((err = alsa_driver_ice1712_hardware (driver)) !=0) {
196
} else if (!strcmp(driver->alsa_driver, "USB US-X2Y")) {
197
if ((err = alsa_driver_usx2y_hardware (driver)) !=0) {
201
if ((err = alsa_driver_generic_hardware (driver)) != 0) {
206
if (driver->hw->capabilities & Cap_HardwareMonitoring) {
207
driver->has_hw_monitoring = TRUE;
208
/* XXX need to ensure that this is really FALSE or
210
driver->hw_monitoring = hw_monitoring;
212
driver->has_hw_monitoring = FALSE;
213
driver->hw_monitoring = FALSE;
216
if (driver->hw->capabilities & Cap_ClockLockReporting) {
217
driver->has_clock_sync_reporting = TRUE;
219
driver->has_clock_sync_reporting = FALSE;
222
if (driver->hw->capabilities & Cap_HardwareMetering) {
223
driver->has_hw_metering = TRUE;
224
driver->hw_metering = hw_metering;
226
driver->has_hw_metering = FALSE;
227
driver->hw_metering = FALSE;
234
alsa_driver_setup_io_function_pointers (alsa_driver_t *driver)
236
if (SND_PCM_FORMAT_FLOAT_LE == driver->playback_sample_format) {
237
if (driver->playback_interleaved) {
238
driver->channel_copy = memcpy_interleave_d32_s32;
240
driver->channel_copy = memcpy_fake;
242
driver->read_via_copy = sample_move_floatLE_sSs;
243
driver->write_via_copy = sample_move_dS_floatLE;
246
switch (driver->playback_sample_bytes) {
248
if (driver->playback_interleaved) {
249
driver->channel_copy = memcpy_interleave_d16_s16;
251
driver->channel_copy = memcpy_fake;
254
switch (driver->dither) {
256
jack_info("Rectangular dithering at 16 bits");
257
driver->write_via_copy = driver->quirk_bswap?
258
sample_move_dither_rect_d16_sSs:
259
sample_move_dither_rect_d16_sS;
263
jack_info("Triangular dithering at 16 bits");
264
driver->write_via_copy = driver->quirk_bswap?
265
sample_move_dither_tri_d16_sSs:
266
sample_move_dither_tri_d16_sS;
270
jack_info("Noise-shaped dithering at 16 bits");
271
driver->write_via_copy = driver->quirk_bswap?
272
sample_move_dither_shaped_d16_sSs:
273
sample_move_dither_shaped_d16_sS;
277
driver->write_via_copy = driver->quirk_bswap?
278
sample_move_d16_sSs :
284
case 3: /* NO DITHER */
285
if (driver->playback_interleaved) {
286
driver->channel_copy = memcpy_interleave_d24_s24;
288
driver->channel_copy = memcpy_fake;
291
driver->write_via_copy = driver->quirk_bswap?
297
case 4: /* NO DITHER */
298
if (driver->playback_interleaved) {
299
driver->channel_copy = memcpy_interleave_d32_s32;
301
driver->channel_copy = memcpy_fake;
304
driver->write_via_copy = driver->quirk_bswap?
305
sample_move_d32u24_sSs:
306
sample_move_d32u24_sS;
310
jack_error ("impossible sample width (%d) discovered!",
311
driver->playback_sample_bytes);
316
switch (driver->capture_sample_bytes) {
318
driver->read_via_copy = driver->quirk_bswap?
323
driver->read_via_copy = driver->quirk_bswap?
328
driver->read_via_copy = driver->quirk_bswap?
329
sample_move_dS_s32u24s:
330
sample_move_dS_s32u24;
336
alsa_driver_configure_stream (alsa_driver_t *driver, char *device_name,
337
const char *stream_name,
339
snd_pcm_hw_params_t *hw_params,
340
snd_pcm_sw_params_t *sw_params,
341
unsigned int *nperiodsp,
343
unsigned long sample_width)
346
unsigned int frame_rate;
347
snd_pcm_uframes_t stop_th;
350
snd_pcm_format_t format;
353
{"32bit float little-endian", SND_PCM_FORMAT_FLOAT_LE},
354
{"32bit integer little-endian", SND_PCM_FORMAT_S32_LE, IS_LE},
355
{"32bit integer big-endian", SND_PCM_FORMAT_S32_BE, IS_BE},
356
{"24bit little-endian", SND_PCM_FORMAT_S24_3LE, IS_LE},
357
{"24bit big-endian", SND_PCM_FORMAT_S24_3BE, IS_BE},
358
{"16bit little-endian", SND_PCM_FORMAT_S16_LE, IS_LE},
359
{"16bit big-endian", SND_PCM_FORMAT_S16_BE, IS_BE},
361
#define NUMFORMATS (sizeof(formats)/sizeof(formats[0]))
362
#define FIRST_16BIT_FORMAT 5
364
if ((err = snd_pcm_hw_params_any (handle, hw_params)) < 0) {
365
jack_error ("ALSA: no playback configurations available (%s)",
370
if ((err = snd_pcm_hw_params_set_periods_integer (handle, hw_params))
372
jack_error ("ALSA: cannot restrict period size to integral"
377
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) < 0) {
378
if ((err = snd_pcm_hw_params_set_access (handle, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED)) < 0) {
379
if ((err = snd_pcm_hw_params_set_access (
381
SND_PCM_ACCESS_MMAP_COMPLEX)) < 0) {
382
jack_error ("ALSA: mmap-based access is not possible"
384
"stream of this audio interface",
391
format = (sample_width == 4) ? 0 : NUMFORMATS - 1;
394
if ((err = snd_pcm_hw_params_set_format (
395
handle, hw_params, formats[format].format)) < 0) {
397
if ((sample_width == 4
398
? format++ >= NUMFORMATS - 1
400
jack_error ("Sorry. The audio interface \"%s\""
401
" doesn't support any of the"
402
" hardware sample formats that"
403
" JACK's alsa-driver can use.",
408
if (formats[format].swapped) {
409
driver->quirk_bswap = 1;
411
driver->quirk_bswap = 0;
413
jack_info ("ALSA: final selected sample format for %s: %s", stream_name, formats[format].Name);
418
frame_rate = driver->frame_rate ;
419
err = snd_pcm_hw_params_set_rate_near (handle, hw_params,
421
driver->frame_rate = frame_rate ;
423
jack_error ("ALSA: cannot set sample/frame rate to %"
424
PRIu32 " for %s", driver->frame_rate,
429
/*if not user-specified, try to find the maximum
430
* number of channels */
431
unsigned int channels_max ;
432
err = snd_pcm_hw_params_get_channels_max (hw_params,
434
*nchns = channels_max ;
438
/* the hapless user is an unwitting victim of
439
the "default" ALSA PCM device, which can
440
support up to 16 million channels. since
441
they can't be bothered to set up a proper
442
default device, limit the number of
443
channels for them to a sane default.
447
"You appear to be using the ALSA software \"plug\" layer, probably\n"
448
"a result of using the \"default\" ALSA device. This is less\n"
449
"efficient than it could be. Consider using a hardware device\n"
450
"instead rather than using the plug layer. Usually the name of the\n"
451
"hardware device that corresponds to the first sound card is hw:0\n"
457
if ((err = snd_pcm_hw_params_set_channels (handle, hw_params,
459
jack_error ("ALSA: cannot set channel count to %u for %s",
460
*nchns, stream_name);
464
if ((err = snd_pcm_hw_params_set_period_size (handle, hw_params,
465
driver->frames_per_cycle,
468
jack_error ("ALSA: cannot set period size to %" PRIu32
469
" frames for %s", driver->frames_per_cycle,
474
*nperiodsp = driver->user_nperiods;
475
snd_pcm_hw_params_set_periods_min (handle, hw_params, nperiodsp, NULL);
476
if (*nperiodsp < driver->user_nperiods)
477
*nperiodsp = driver->user_nperiods;
478
if (snd_pcm_hw_params_set_periods_near (handle, hw_params,
479
nperiodsp, NULL) < 0) {
480
jack_error ("ALSA: cannot set number of periods to %u for %s",
481
*nperiodsp, stream_name);
485
if (*nperiodsp < driver->user_nperiods) {
486
jack_error ("ALSA: got smaller periods %u than %u for %s",
487
*nperiodsp, (unsigned int) driver->user_nperiods,
491
jack_info ("ALSA: use %d periods for %s", *nperiodsp, stream_name);
493
if (!jack_power_of_two(driver->frames_per_cycle)) {
494
jack_error("JACK: frames must be a power of two "
495
"(64, 512, 1024, ...)\n");
500
if ((err = snd_pcm_hw_params_set_buffer_size (handle, hw_params,
502
driver->frames_per_cycle))
504
jack_error ("ALSA: cannot set buffer length to %" PRIu32
506
*nperiodsp * driver->frames_per_cycle,
511
if ((err = snd_pcm_hw_params (handle, hw_params)) < 0) {
512
jack_error ("ALSA: cannot set hardware parameters for %s",
517
snd_pcm_sw_params_current (handle, sw_params);
519
if ((err = snd_pcm_sw_params_set_start_threshold (handle, sw_params,
521
jack_error ("ALSA: cannot set start mode for %s", stream_name);
525
stop_th = *nperiodsp * driver->frames_per_cycle;
526
if (driver->soft_mode) {
527
stop_th = (snd_pcm_uframes_t)-1;
530
if ((err = snd_pcm_sw_params_set_stop_threshold (
531
handle, sw_params, stop_th)) < 0) {
532
jack_error ("ALSA: cannot set stop mode for %s",
537
if ((err = snd_pcm_sw_params_set_silence_threshold (
538
handle, sw_params, 0)) < 0) {
539
jack_error ("ALSA: cannot set silence threshold for %s",
545
jack_info ("set silence size to %lu * %lu = %lu",
546
driver->frames_per_cycle, *nperiodsp,
547
driver->frames_per_cycle * *nperiodsp);
549
if ((err = snd_pcm_sw_params_set_silence_size (
551
driver->frames_per_cycle * *nperiodsp)) < 0) {
552
jack_error ("ALSA: cannot set silence size for %s",
558
if (handle == driver->playback_handle)
559
err = snd_pcm_sw_params_set_avail_min (
561
driver->frames_per_cycle
562
* (*nperiodsp - driver->user_nperiods + 1));
564
err = snd_pcm_sw_params_set_avail_min (
565
handle, sw_params, driver->frames_per_cycle);
568
jack_error ("ALSA: cannot set avail min for %s", stream_name);
572
if ((err = snd_pcm_sw_params (handle, sw_params)) < 0) {
573
jack_error ("ALSA: cannot set software parameters for %s\n",
582
alsa_driver_set_parameters (alsa_driver_t *driver,
583
jack_nframes_t frames_per_cycle,
584
jack_nframes_t user_nperiods,
588
snd_pcm_uframes_t p_period_size = 0;
589
snd_pcm_uframes_t c_period_size = 0;
595
driver->frame_rate = rate;
596
driver->frames_per_cycle = frames_per_cycle;
597
driver->user_nperiods = user_nperiods;
599
jack_info ("configuring for %" PRIu32 "Hz, period = %"
600
PRIu32 " frames (%.1f ms), buffer = %" PRIu32 " periods",
601
rate, frames_per_cycle, (((float)frames_per_cycle / (float) rate) * 1000.0f), user_nperiods);
603
if (driver->capture_handle) {
604
if (alsa_driver_configure_stream (
606
driver->alsa_name_capture,
608
driver->capture_handle,
609
driver->capture_hw_params,
610
driver->capture_sw_params,
611
&driver->capture_nperiods,
612
&driver->capture_nchannels,
613
driver->capture_sample_bytes)) {
614
jack_error ("ALSA: cannot configure capture channel");
619
if (driver->playback_handle) {
620
if (alsa_driver_configure_stream (
622
driver->alsa_name_playback,
624
driver->playback_handle,
625
driver->playback_hw_params,
626
driver->playback_sw_params,
627
&driver->playback_nperiods,
628
&driver->playback_nchannels,
629
driver->playback_sample_bytes)) {
630
jack_error ("ALSA: cannot configure playback channel");
635
/* check the rate, since thats rather important */
637
if (driver->playback_handle) {
638
snd_pcm_hw_params_get_rate (driver->playback_hw_params,
642
if (driver->capture_handle) {
643
snd_pcm_hw_params_get_rate (driver->capture_hw_params,
647
if (driver->capture_handle && driver->playback_handle) {
649
jack_error ("playback and capture sample rates do "
650
"not match (%d vs. %d)", pr, cr);
653
/* only change if *both* capture and playback rates
654
* don't match requested certain hardware actually
655
* still works properly in full-duplex with slightly
656
* different rate values between adc and dac
658
if (cr != driver->frame_rate && pr != driver->frame_rate) {
659
jack_error ("sample rate in use (%d Hz) does not "
660
"match requested rate (%d Hz)",
661
cr, driver->frame_rate);
662
driver->frame_rate = cr;
666
else if (driver->capture_handle && cr != driver->frame_rate) {
667
jack_error ("capture sample rate in use (%d Hz) does not "
668
"match requested rate (%d Hz)",
669
cr, driver->frame_rate);
670
driver->frame_rate = cr;
672
else if (driver->playback_handle && pr != driver->frame_rate) {
673
jack_error ("playback sample rate in use (%d Hz) does not "
674
"match requested rate (%d Hz)",
675
pr, driver->frame_rate);
676
driver->frame_rate = pr;
680
/* check the fragment size, since thats non-negotiable */
682
if (driver->playback_handle) {
683
snd_pcm_access_t access;
685
err = snd_pcm_hw_params_get_period_size (
686
driver->playback_hw_params, &p_period_size, &dir);
687
err = snd_pcm_hw_params_get_format (
688
driver->playback_hw_params,
689
&(driver->playback_sample_format));
690
err = snd_pcm_hw_params_get_access (driver->playback_hw_params,
692
driver->playback_interleaved =
693
(access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
694
|| (access == SND_PCM_ACCESS_MMAP_COMPLEX);
696
if (p_period_size != driver->frames_per_cycle) {
697
jack_error ("alsa_pcm: requested an interrupt every %"
699
" frames but got %u frames for playback",
700
driver->frames_per_cycle, p_period_size);
705
if (driver->capture_handle) {
706
snd_pcm_access_t access;
708
err = snd_pcm_hw_params_get_period_size (
709
driver->capture_hw_params, &c_period_size, &dir);
710
err = snd_pcm_hw_params_get_format (
711
driver->capture_hw_params,
712
&(driver->capture_sample_format));
713
err = snd_pcm_hw_params_get_access (driver->capture_hw_params,
715
driver->capture_interleaved =
716
(access == SND_PCM_ACCESS_MMAP_INTERLEAVED)
717
|| (access == SND_PCM_ACCESS_MMAP_COMPLEX);
719
if (c_period_size != driver->frames_per_cycle) {
720
jack_error ("alsa_pcm: requested an interrupt every %"
722
" frames but got %uc frames for capture",
723
driver->frames_per_cycle, p_period_size);
728
driver->playback_sample_bytes =
729
snd_pcm_format_physical_width (driver->playback_sample_format)
731
driver->capture_sample_bytes =
732
snd_pcm_format_physical_width (driver->capture_sample_format)
735
if (driver->playback_handle) {
736
switch (driver->playback_sample_format) {
737
case SND_PCM_FORMAT_FLOAT_LE:
738
case SND_PCM_FORMAT_S32_LE:
739
case SND_PCM_FORMAT_S24_3LE:
740
case SND_PCM_FORMAT_S24_3BE:
741
case SND_PCM_FORMAT_S16_LE:
742
case SND_PCM_FORMAT_S32_BE:
743
case SND_PCM_FORMAT_S16_BE:
747
jack_error ("programming error: unhandled format "
748
"type for playback");
753
if (driver->capture_handle) {
754
switch (driver->capture_sample_format) {
755
case SND_PCM_FORMAT_FLOAT_LE:
756
case SND_PCM_FORMAT_S32_LE:
757
case SND_PCM_FORMAT_S24_3LE:
758
case SND_PCM_FORMAT_S24_3BE:
759
case SND_PCM_FORMAT_S16_LE:
760
case SND_PCM_FORMAT_S32_BE:
761
case SND_PCM_FORMAT_S16_BE:
765
jack_error ("programming error: unhandled format "
771
if (driver->playback_interleaved) {
772
const snd_pcm_channel_area_t *my_areas;
773
snd_pcm_uframes_t offset, frames;
774
if (snd_pcm_mmap_begin(driver->playback_handle,
775
&my_areas, &offset, &frames) < 0) {
776
jack_error ("ALSA: %s: mmap areas info error",
777
driver->alsa_name_playback);
780
driver->interleave_unit =
781
snd_pcm_format_physical_width (
782
driver->playback_sample_format) / 8;
784
driver->interleave_unit = 0; /* NOT USED */
787
if (driver->capture_interleaved) {
788
const snd_pcm_channel_area_t *my_areas;
789
snd_pcm_uframes_t offset, frames;
790
if (snd_pcm_mmap_begin(driver->capture_handle,
791
&my_areas, &offset, &frames) < 0) {
792
jack_error ("ALSA: %s: mmap areas info error",
793
driver->alsa_name_capture);
798
if (driver->playback_nchannels > driver->capture_nchannels) {
799
driver->max_nchannels = driver->playback_nchannels;
800
driver->user_nchannels = driver->capture_nchannels;
802
driver->max_nchannels = driver->capture_nchannels;
803
driver->user_nchannels = driver->playback_nchannels;
806
alsa_driver_setup_io_function_pointers (driver);
808
/* Allocate and initialize structures that rely on the
811
Set up the bit pattern that is used to record which
812
channels require action on every cycle. any bits that are
813
not set after the engine's process() call indicate channels
814
that potentially need to be silenced.
817
bitset_create (&driver->channels_done, driver->max_nchannels);
818
bitset_create (&driver->channels_not_done, driver->max_nchannels);
820
if (driver->playback_handle) {
821
driver->playback_addr = (char **)
822
malloc (sizeof (char *) * driver->playback_nchannels);
823
memset (driver->playback_addr, 0,
824
sizeof (char *) * driver->playback_nchannels);
825
driver->playback_interleave_skip = (unsigned long *)
826
malloc (sizeof (unsigned long *) * driver->playback_nchannels);
827
memset (driver->playback_interleave_skip, 0,
828
sizeof (unsigned long *) * driver->playback_nchannels);
829
driver->silent = (unsigned long *)
830
malloc (sizeof (unsigned long)
831
* driver->playback_nchannels);
833
for (chn = 0; chn < driver->playback_nchannels; chn++) {
834
driver->silent[chn] = 0;
837
for (chn = 0; chn < driver->playback_nchannels; chn++) {
838
bitset_add (driver->channels_done, chn);
841
driver->dither_state = (dither_state_t *)
842
calloc ( driver->playback_nchannels,
843
sizeof (dither_state_t));
846
if (driver->capture_handle) {
847
driver->capture_addr = (char **)
848
malloc (sizeof (char *) * driver->capture_nchannels);
849
memset (driver->capture_addr, 0,
850
sizeof (char *) * driver->capture_nchannels);
851
driver->capture_interleave_skip = (unsigned long *)
852
malloc (sizeof (unsigned long *) * driver->capture_nchannels);
853
memset (driver->capture_interleave_skip, 0,
854
sizeof (unsigned long *) * driver->capture_nchannels);
857
driver->clock_sync_data = (ClockSyncStatus *)
858
malloc (sizeof (ClockSyncStatus) * driver->max_nchannels);
860
driver->period_usecs =
861
(jack_time_t) floor ((((float) driver->frames_per_cycle) /
862
driver->frame_rate) * 1000000.0f);
863
driver->poll_timeout = (int) floor (1.5f * driver->period_usecs);
865
if (driver->engine) {
866
driver->engine->set_buffer_size (driver->engine,
867
driver->frames_per_cycle);
874
alsa_driver_reset_parameters (alsa_driver_t *driver,
875
jack_nframes_t frames_per_cycle,
876
jack_nframes_t user_nperiods,
879
/* XXX unregister old ports ? */
880
alsa_driver_release_channel_dependent_memory (driver);
881
return alsa_driver_set_parameters (driver,
883
user_nperiods, rate);
887
alsa_driver_get_channel_addresses (alsa_driver_t *driver,
888
snd_pcm_uframes_t *capture_avail,
889
snd_pcm_uframes_t *playback_avail,
890
snd_pcm_uframes_t *capture_offset,
891
snd_pcm_uframes_t *playback_offset)
897
if ((err = snd_pcm_mmap_begin (
898
driver->capture_handle, &driver->capture_areas,
899
(snd_pcm_uframes_t *) capture_offset,
900
(snd_pcm_uframes_t *) capture_avail)) < 0) {
901
jack_error ("ALSA: %s: mmap areas info error",
902
driver->alsa_name_capture);
906
for (chn = 0; chn < driver->capture_nchannels; chn++) {
907
const snd_pcm_channel_area_t *a =
908
&driver->capture_areas[chn];
909
driver->capture_addr[chn] = (char *) a->addr
910
+ ((a->first + a->step * *capture_offset) / 8);
911
driver->capture_interleave_skip[chn] = (unsigned long ) (a->step / 8);
915
if (playback_avail) {
916
if ((err = snd_pcm_mmap_begin (
917
driver->playback_handle, &driver->playback_areas,
918
(snd_pcm_uframes_t *) playback_offset,
919
(snd_pcm_uframes_t *) playback_avail)) < 0) {
920
jack_error ("ALSA: %s: mmap areas info error ",
921
driver->alsa_name_playback);
925
for (chn = 0; chn < driver->playback_nchannels; chn++) {
926
const snd_pcm_channel_area_t *a =
927
&driver->playback_areas[chn];
928
driver->playback_addr[chn] = (char *) a->addr
929
+ ((a->first + a->step * *playback_offset) / 8);
930
driver->playback_interleave_skip[chn] = (unsigned long ) (a->step / 8);
938
alsa_driver_start (alsa_driver_t *driver)
941
snd_pcm_uframes_t poffset, pavail;
944
driver->poll_last = 0;
945
driver->poll_next = 0;
947
if (driver->playback_handle) {
948
if ((err = snd_pcm_prepare (driver->playback_handle)) < 0) {
949
jack_error ("ALSA: prepare error for playback on "
950
"\"%s\" (%s)", driver->alsa_name_playback,
956
if ((driver->capture_handle && driver->capture_and_playback_not_synced)
957
|| !driver->playback_handle) {
958
if ((err = snd_pcm_prepare (driver->capture_handle)) < 0) {
959
jack_error ("ALSA: prepare error for capture on \"%s\""
960
" (%s)", driver->alsa_name_capture,
966
if (driver->hw_monitoring) {
967
if (driver->input_monitor_mask || driver->all_monitor_in) {
968
if (driver->all_monitor_in) {
969
driver->hw->set_input_monitor_mask (driver->hw, ~0U);
971
driver->hw->set_input_monitor_mask (
972
driver->hw, driver->input_monitor_mask);
975
driver->hw->set_input_monitor_mask (driver->hw,
976
driver->input_monitor_mask);
980
if (driver->playback_handle) {
981
driver->playback_nfds =
982
snd_pcm_poll_descriptors_count (driver->playback_handle);
984
driver->playback_nfds = 0;
987
if (driver->capture_handle) {
988
driver->capture_nfds =
989
snd_pcm_poll_descriptors_count (driver->capture_handle);
991
driver->capture_nfds = 0;
998
driver->pfd = (struct pollfd *)
999
malloc (sizeof (struct pollfd) *
1000
(driver->playback_nfds + driver->capture_nfds + 2));
1002
if (driver->midi && !driver->xrun_recovery)
1003
(driver->midi->start)(driver->midi);
1005
if (driver->playback_handle) {
1006
/* fill playback buffer with zeroes, and mark
1007
all fragments as having data.
1010
pavail = snd_pcm_avail_update (driver->playback_handle);
1013
driver->frames_per_cycle * driver->playback_nperiods) {
1014
jack_error ("ALSA: full buffer not available at start");
1018
if (alsa_driver_get_channel_addresses (driver,
1019
0, &pavail, 0, &poffset)) {
1023
/* XXX this is cheating. ALSA offers no guarantee that
1024
we can access the entire buffer at any one time. It
1025
works on most hardware tested so far, however, buts
1026
its a liability in the long run. I think that
1027
alsa-lib may have a better function for doing this
1028
here, where the goal is to silence the entire
1032
for (chn = 0; chn < driver->playback_nchannels; chn++) {
1033
alsa_driver_silence_on_channel (
1035
driver->user_nperiods
1036
* driver->frames_per_cycle);
1039
snd_pcm_mmap_commit (driver->playback_handle, poffset,
1040
driver->user_nperiods
1041
* driver->frames_per_cycle);
1043
if ((err = snd_pcm_start (driver->playback_handle)) < 0) {
1044
jack_error ("ALSA: could not start playback (%s)",
1045
snd_strerror (err));
1050
if ((driver->capture_handle && driver->capture_and_playback_not_synced)
1051
|| !driver->playback_handle) {
1052
if ((err = snd_pcm_start (driver->capture_handle)) < 0) {
1053
jack_error ("ALSA: could not start capture (%s)",
1054
snd_strerror (err));
1063
alsa_driver_stop (alsa_driver_t *driver)
1069
/* silence all capture port buffers, because we might
1070
be entering offline mode.
1073
for (chn = 0, node = driver->capture_ports; node;
1074
node = jack_slist_next (node), chn++) {
1078
jack_nframes_t nframes = driver->engine->control->buffer_size;
1080
port = (jack_port_t *) node->data;
1081
buf = jack_port_get_buffer (port, nframes);
1082
memset (buf, 0, sizeof (jack_default_audio_sample_t) * nframes);
1085
if (driver->playback_handle) {
1086
if ((err = snd_pcm_drop (driver->playback_handle)) < 0) {
1087
jack_error ("ALSA: channel flush for playback "
1088
"failed (%s)", snd_strerror (err));
1093
if (!driver->playback_handle
1094
|| driver->capture_and_playback_not_synced) {
1095
if (driver->capture_handle) {
1096
if ((err = snd_pcm_drop (driver->capture_handle)) < 0) {
1097
jack_error ("ALSA: channel flush for "
1098
"capture failed (%s)",
1099
snd_strerror (err));
1105
if (driver->hw_monitoring) {
1106
driver->hw->set_input_monitor_mask (driver->hw, 0);
1109
if (driver->midi && !driver->xrun_recovery)
1110
(driver->midi->stop)(driver->midi);
1116
alsa_driver_restart (alsa_driver_t *driver)
1120
driver->xrun_recovery = 1;
1121
if ((res = driver->nt_stop((struct _jack_driver_nt *) driver))==0)
1122
res = driver->nt_start((struct _jack_driver_nt *) driver);
1123
driver->xrun_recovery = 0;
1125
if (res && driver->midi)
1126
(driver->midi->stop)(driver->midi);
1132
alsa_driver_xrun_recovery (alsa_driver_t *driver, float *delayed_usecs)
1134
snd_pcm_status_t *status;
1137
snd_pcm_status_alloca(&status);
1139
if (driver->capture_handle) {
1140
if ((res = snd_pcm_status(driver->capture_handle, status))
1142
jack_error("status error: %s", snd_strerror(res));
1145
if ((res = snd_pcm_status(driver->playback_handle, status))
1147
jack_error("status error: %s", snd_strerror(res));
1151
if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN
1152
&& driver->process_count > XRUN_REPORT_DELAY) {
1153
struct timeval now, diff, tstamp;
1154
driver->xrun_count++;
1155
snd_pcm_status_get_tstamp(status,&now);
1156
snd_pcm_status_get_trigger_tstamp(status, &tstamp);
1157
timersub(&now, &tstamp, &diff);
1158
*delayed_usecs = diff.tv_sec * 1000000.0 + diff.tv_usec;
1159
MESSAGE("\n\n**** alsa_pcm: xrun of at least %.3f "
1161
*delayed_usecs / 1000.0);
1164
if (alsa_driver_restart (driver)) {
1171
alsa_driver_silence_untouched_channels (alsa_driver_t *driver,
1172
jack_nframes_t nframes)
1175
jack_nframes_t buffer_frames =
1176
driver->frames_per_cycle * driver->playback_nperiods;
1178
for (chn = 0; chn < driver->playback_nchannels; chn++) {
1179
if (bitset_contains (driver->channels_not_done, chn)) {
1180
if (driver->silent[chn] < buffer_frames) {
1181
alsa_driver_silence_on_channel_no_mark (
1182
driver, chn, nframes);
1183
driver->silent[chn] += nframes;
1190
alsa_driver_set_clock_sync_status (alsa_driver_t *driver, channel_t chn,
1191
ClockSyncStatus status)
1193
driver->clock_sync_data[chn] = status;
1194
alsa_driver_clock_sync_notify (driver, chn, status);
1197
static int under_gdb = FALSE;
1199
static jack_nframes_t
1200
alsa_driver_wait (alsa_driver_t *driver, int extra_fd, int *status, float
1203
snd_pcm_sframes_t avail = 0;
1204
snd_pcm_sframes_t capture_avail = 0;
1205
snd_pcm_sframes_t playback_avail = 0;
1206
int xrun_detected = FALSE;
1210
jack_time_t poll_enter;
1211
jack_time_t poll_ret = 0;
1216
need_capture = driver->capture_handle ? 1 : 0;
1218
if (extra_fd >= 0) {
1221
need_playback = driver->playback_handle ? 1 : 0;
1226
while (need_playback || need_capture) {
1229
unsigned int ci = 0;
1231
unsigned short revents;
1235
if (need_playback) {
1236
snd_pcm_poll_descriptors (driver->playback_handle,
1238
driver->playback_nfds);
1239
nfds += driver->playback_nfds;
1243
snd_pcm_poll_descriptors (driver->capture_handle,
1245
driver->capture_nfds);
1247
nfds += driver->capture_nfds;
1250
/* ALSA doesn't set POLLERR in some versions of 0.9.X */
1252
for (i = 0; i < nfds; i++) {
1253
driver->pfd[i].events |= POLLERR;
1256
if (extra_fd >= 0) {
1257
driver->pfd[nfds].fd = extra_fd;
1258
driver->pfd[nfds].events =
1259
POLLIN|POLLERR|POLLHUP|POLLNVAL;
1263
poll_enter = jack_get_microseconds ();
1265
if (poll_enter > driver->poll_next) {
1267
* This processing cycle was delayed past the
1268
* next due interrupt! Do not account this as
1271
driver->poll_next = 0;
1272
driver->poll_late++;
1275
poll_result = poll (driver->pfd, nfds, driver->poll_timeout);
1276
if (poll_result < 0) {
1278
if (errno == EINTR) {
1279
jack_info ("poll interrupt");
1280
// this happens mostly when run
1281
// under gdb, or when exiting due to a signal
1289
jack_error ("ALSA: poll call failed (%s)",
1296
poll_ret = jack_get_microseconds ();
1299
if (driver->poll_next && poll_ret > driver->poll_next) {
1300
*delayed_usecs = poll_ret - driver->poll_next;
1302
driver->poll_last = poll_ret;
1303
driver->poll_next = poll_ret + driver->period_usecs;
1304
driver->engine->transport_cycle_start (driver->engine,
1309
jack_info ("%" PRIu64 ": checked %d fds, %" PRIu64
1310
" usecs since poll entered", poll_ret, nfds,
1311
poll_ret - poll_enter);
1314
/* check to see if it was the extra FD that caused us
1315
* to return from poll */
1317
if (extra_fd >= 0) {
1319
if (driver->pfd[nfds-1].revents == 0) {
1320
/* we timed out on the extra fd */
1326
/* if POLLIN was the only bit set, we're OK */
1329
return (driver->pfd[nfds-1].revents == POLLIN) ? 0 : -1;
1332
if (need_playback) {
1333
if (snd_pcm_poll_descriptors_revents
1334
(driver->playback_handle, &driver->pfd[0],
1335
driver->playback_nfds, &revents) < 0) {
1336
jack_error ("ALSA: playback revents failed");
1341
if (revents & POLLERR) {
1342
xrun_detected = TRUE;
1345
if (revents & POLLOUT) {
1348
jack_info ("%" PRIu64
1349
" playback stream ready",
1356
if (snd_pcm_poll_descriptors_revents
1357
(driver->capture_handle, &driver->pfd[ci],
1358
driver->capture_nfds, &revents) < 0) {
1359
jack_error ("ALSA: capture revents failed");
1364
if (revents & POLLERR) {
1365
xrun_detected = TRUE;
1368
if (revents & POLLIN) {
1371
jack_info ("%" PRIu64
1372
" capture stream ready",
1378
if (poll_result == 0) {
1379
jack_error ("ALSA: poll time out, polled for %" PRIu64
1381
poll_ret - poll_enter);
1388
if (driver->capture_handle) {
1389
if ((capture_avail = snd_pcm_avail_update (
1390
driver->capture_handle)) < 0) {
1391
if (capture_avail == -EPIPE) {
1392
xrun_detected = TRUE;
1394
jack_error ("unknown ALSA avail_update return"
1395
" value (%u)", capture_avail);
1399
/* odd, but see min() computation below */
1400
capture_avail = INT_MAX;
1403
if (driver->playback_handle) {
1404
if ((playback_avail = snd_pcm_avail_update (
1405
driver->playback_handle)) < 0) {
1406
if (playback_avail == -EPIPE) {
1407
xrun_detected = TRUE;
1409
jack_error ("unknown ALSA avail_update return"
1410
" value (%u)", playback_avail);
1414
/* odd, but see min() computation below */
1415
playback_avail = INT_MAX;
1418
if (xrun_detected) {
1419
*status = alsa_driver_xrun_recovery (driver, delayed_usecs);
1424
driver->last_wait_ust = poll_ret;
1426
avail = capture_avail < playback_avail ? capture_avail : playback_avail;
1429
jack_info ("wakeup complete, avail = %lu, pavail = %lu "
1431
avail, playback_avail, capture_avail);
1434
/* mark all channels not done for now. read/write will change this */
1436
bitset_copy (driver->channels_not_done, driver->channels_done);
1438
/* constrain the available count to the nearest (round down) number of
1442
return avail - (avail % driver->frames_per_cycle);
1446
alsa_driver_null_cycle (alsa_driver_t* driver, jack_nframes_t nframes)
1449
snd_pcm_uframes_t offset;
1450
snd_pcm_uframes_t contiguous;
1453
if (nframes > driver->frames_per_cycle) {
1457
if (driver->capture_handle) {
1463
if (snd_pcm_mmap_begin (
1464
driver->capture_handle,
1465
&driver->capture_areas,
1466
(snd_pcm_uframes_t *) &offset,
1467
(snd_pcm_uframes_t *) &contiguous)) {
1471
if (snd_pcm_mmap_commit (driver->capture_handle,
1472
offset, contiguous) < 0) {
1480
if (driver->playback_handle) {
1486
if (snd_pcm_mmap_begin (
1487
driver->playback_handle,
1488
&driver->playback_areas,
1489
(snd_pcm_uframes_t *) &offset,
1490
(snd_pcm_uframes_t *) &contiguous)) {
1494
for (chn = 0; chn < driver->playback_nchannels; chn++) {
1495
alsa_driver_silence_on_channel (driver, chn,
1499
if (snd_pcm_mmap_commit (driver->playback_handle,
1500
offset, contiguous) < 0) {
1512
alsa_driver_bufsize (alsa_driver_t* driver, jack_nframes_t nframes)
1514
return alsa_driver_reset_parameters (driver, nframes,
1515
driver->user_nperiods,
1516
driver->frame_rate);
1520
alsa_driver_read (alsa_driver_t *driver, jack_nframes_t nframes)
1522
snd_pcm_sframes_t contiguous;
1523
snd_pcm_sframes_t nread;
1524
snd_pcm_uframes_t offset;
1525
jack_nframes_t orig_nframes;
1526
jack_default_audio_sample_t* buf;
1532
if (nframes > driver->frames_per_cycle) {
1536
if (driver->engine->freewheeling) {
1541
(driver->midi->read)(driver->midi, nframes);
1543
if (!driver->capture_handle) {
1549
orig_nframes = nframes;
1553
contiguous = nframes;
1555
if (alsa_driver_get_channel_addresses (
1557
(snd_pcm_uframes_t *) &contiguous,
1558
(snd_pcm_uframes_t *) 0,
1563
for (chn = 0, node = driver->capture_ports; node;
1564
node = jack_slist_next (node), chn++) {
1566
port = (jack_port_t *) node->data;
1568
if (!jack_port_connected (port)) {
1569
/* no-copy optimization */
1572
buf = jack_port_get_buffer (port, orig_nframes);
1573
alsa_driver_read_from_channel (driver, chn,
1574
buf + nread, contiguous);
1577
if ((err = snd_pcm_mmap_commit (driver->capture_handle,
1578
offset, contiguous)) < 0) {
1579
jack_error ("ALSA: could not complete read of %"
1580
PRIu32 " frames: error = %d", contiguous, err);
1584
nframes -= contiguous;
1585
nread += contiguous;
1592
alsa_driver_write (alsa_driver_t* driver, jack_nframes_t nframes)
1597
jack_default_audio_sample_t* buf;
1598
jack_default_audio_sample_t* monbuf;
1599
jack_nframes_t orig_nframes;
1600
snd_pcm_sframes_t nwritten;
1601
snd_pcm_sframes_t contiguous;
1602
snd_pcm_uframes_t offset;
1606
driver->process_count++;
1608
if (!driver->playback_handle || driver->engine->freewheeling) {
1611
if (nframes > driver->frames_per_cycle) {
1616
(driver->midi->write)(driver->midi, nframes);
1620
orig_nframes = nframes;
1622
/* check current input monitor request status */
1624
driver->input_monitor_mask = 0;
1626
for (chn = 0, node = driver->capture_ports; node;
1627
node = jack_slist_next (node), chn++) {
1628
if (((jack_port_t *) node->data)->shared->monitor_requests) {
1629
driver->input_monitor_mask |= (1<<chn);
1633
if (driver->hw_monitoring) {
1634
if ((driver->hw->input_monitor_mask
1635
!= driver->input_monitor_mask)
1636
&& !driver->all_monitor_in) {
1637
driver->hw->set_input_monitor_mask (
1638
driver->hw, driver->input_monitor_mask);
1644
contiguous = nframes;
1646
if (alsa_driver_get_channel_addresses (
1648
(snd_pcm_uframes_t *) 0,
1649
(snd_pcm_uframes_t *) &contiguous,
1654
for (chn = 0, node = driver->playback_ports, mon_node=driver->monitor_ports;
1656
node = jack_slist_next (node), chn++) {
1658
port = (jack_port_t *) node->data;
1660
if (!jack_port_connected (port)) {
1663
buf = jack_port_get_buffer (port, orig_nframes);
1664
alsa_driver_write_to_channel (driver, chn,
1665
buf + nwritten, contiguous);
1668
port = (jack_port_t *) mon_node->data;
1669
if (!jack_port_connected (port)) {
1672
monbuf = jack_port_get_buffer (port, orig_nframes);
1673
memcpy (monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
1674
mon_node = jack_slist_next (mon_node);
1679
if (!bitset_empty (driver->channels_not_done)) {
1680
alsa_driver_silence_untouched_channels (driver,
1684
if ((err = snd_pcm_mmap_commit (driver->playback_handle,
1685
offset, contiguous)) < 0) {
1686
jack_error ("ALSA: could not complete playback of %"
1687
PRIu32 " frames: error = %d", contiguous, err);
1688
if (err != EPIPE && err != ESTRPIPE)
1692
nframes -= contiguous;
1693
nwritten += contiguous;
1700
alsa_driver_run_cycle (alsa_driver_t *driver)
1702
jack_engine_t *engine = driver->engine;
1704
float delayed_usecs;
1705
jack_nframes_t nframes;
1707
DEBUG ("alsa run cycle wait\n");
1709
nframes = alsa_driver_wait (driver, -1, &wait_status, &delayed_usecs);
1711
DEBUG ("alsaback from wait, nframes = %lu", nframes);
1713
if (unlikely(wait_status < 0))
1714
return -1; /* driver failed */
1716
if (unlikely(nframes == 0)) {
1718
/* we detected an xrun and restarted: notify
1719
* clients about the delay.
1721
engine->delay (engine, delayed_usecs);
1725
return engine->run_cycle (engine, nframes, delayed_usecs);
1729
alsa_driver_attach (alsa_driver_t *driver)
1736
driver->engine->set_buffer_size (driver->engine, driver->frames_per_cycle);
1737
driver->engine->set_sample_rate (driver->engine, driver->frame_rate);
1739
port_flags = JackPortIsOutput|JackPortIsPhysical|JackPortIsTerminal;
1741
if (driver->has_hw_monitoring) {
1742
port_flags |= JackPortCanMonitor;
1745
for (chn = 0; chn < driver->capture_nchannels; chn++) {
1747
snprintf (buf, sizeof(buf), "capture_%lu", chn+1);
1749
if ((port = jack_port_register (driver->client, buf,
1750
JACK_DEFAULT_AUDIO_TYPE,
1751
port_flags, 0)) == NULL) {
1752
jack_error ("ALSA: cannot register port for %s", buf);
1756
jack_port_set_latency (port, driver->frames_per_cycle + driver->capture_frame_latency);
1758
driver->capture_ports =
1759
jack_slist_append (driver->capture_ports, port);
1762
port_flags = JackPortIsInput|JackPortIsPhysical|JackPortIsTerminal;
1764
for (chn = 0; chn < driver->playback_nchannels; chn++) {
1765
jack_port_t *monitor_port;
1767
snprintf (buf, sizeof(buf) - 1, "playback_%lu", chn+1);
1769
if ((port = jack_port_register (driver->client, buf,
1770
JACK_DEFAULT_AUDIO_TYPE,
1771
port_flags, 0)) == NULL) {
1772
jack_error ("ALSA: cannot register port for %s", buf);
1776
jack_port_set_latency (port, (driver->frames_per_cycle * (driver->user_nperiods - 1)) + driver->playback_frame_latency);
1778
driver->playback_ports =
1779
jack_slist_append (driver->playback_ports, port);
1781
if (driver->with_monitor_ports) {
1782
snprintf (buf, sizeof(buf) - 1, "monitor_%lu", chn+1);
1784
if ((monitor_port = jack_port_register (
1785
driver->client, buf,
1786
JACK_DEFAULT_AUDIO_TYPE,
1787
JackPortIsOutput, 0)) == NULL) {
1788
jack_error ("ALSA: cannot register monitor "
1789
"port for %s", buf);
1792
jack_port_set_latency (monitor_port, driver->frames_per_cycle);
1794
driver->monitor_ports =
1795
jack_slist_append (driver->monitor_ports, monitor_port);
1802
int err = (driver->midi->attach)(driver->midi);
1804
jack_error("ALSA: cannot attach midi: %d", err);
1808
return jack_activate (driver->client);
1812
alsa_driver_detach (alsa_driver_t *driver)
1816
if (driver->engine == NULL) {
1821
(driver->midi->detach)(driver->midi);
1823
for (node = driver->capture_ports; node;
1824
node = jack_slist_next (node)) {
1825
jack_port_unregister (driver->client,
1826
((jack_port_t *) node->data));
1829
jack_slist_free (driver->capture_ports);
1830
driver->capture_ports = 0;
1832
for (node = driver->playback_ports; node;
1833
node = jack_slist_next (node)) {
1834
jack_port_unregister (driver->client,
1835
((jack_port_t *) node->data));
1838
jack_slist_free (driver->playback_ports);
1839
driver->playback_ports = 0;
1841
if (driver->monitor_ports) {
1842
for (node = driver->monitor_ports; node;
1843
node = jack_slist_next (node)) {
1844
jack_port_unregister (driver->client,
1845
((jack_port_t *) node->data));
1848
jack_slist_free (driver->monitor_ports);
1849
driver->monitor_ports = 0;
1856
static int /* UNUSED */
1857
alsa_driver_change_sample_clock (alsa_driver_t *driver, SampleClockMode mode)
1860
return driver->hw->change_sample_clock (driver->hw, mode);
1863
static void /* UNUSED */
1864
alsa_driver_request_all_monitor_input (alsa_driver_t *driver, int yn)
1867
if (driver->hw_monitoring) {
1869
driver->hw->set_input_monitor_mask (driver->hw, ~0U);
1871
driver->hw->set_input_monitor_mask (
1872
driver->hw, driver->input_monitor_mask);
1876
driver->all_monitor_in = yn;
1879
static void /* UNUSED */
1880
alsa_driver_set_hw_monitoring (alsa_driver_t *driver, int yn)
1883
driver->hw_monitoring = TRUE;
1885
if (driver->all_monitor_in) {
1886
driver->hw->set_input_monitor_mask (driver->hw, ~0U);
1888
driver->hw->set_input_monitor_mask (
1889
driver->hw, driver->input_monitor_mask);
1892
driver->hw_monitoring = FALSE;
1893
driver->hw->set_input_monitor_mask (driver->hw, 0);
1897
static ClockSyncStatus /* UNUSED */
1898
alsa_driver_clock_sync_status (channel_t chn)
1905
alsa_driver_delete (alsa_driver_t *driver)
1910
(driver->midi->destroy)(driver->midi);
1912
for (node = driver->clock_sync_listeners; node;
1913
node = jack_slist_next (node)) {
1916
jack_slist_free (driver->clock_sync_listeners);
1918
if (driver->ctl_handle) {
1919
snd_ctl_close (driver->ctl_handle);
1920
driver->ctl_handle = 0;
1923
if (driver->capture_handle) {
1924
snd_pcm_close (driver->capture_handle);
1925
driver->capture_handle = 0;
1928
if (driver->playback_handle) {
1929
snd_pcm_close (driver->playback_handle);
1930
driver->capture_handle = 0;
1933
if (driver->capture_hw_params) {
1934
snd_pcm_hw_params_free (driver->capture_hw_params);
1935
driver->capture_hw_params = 0;
1938
if (driver->playback_hw_params) {
1939
snd_pcm_hw_params_free (driver->playback_hw_params);
1940
driver->playback_hw_params = 0;
1943
if (driver->capture_sw_params) {
1944
snd_pcm_sw_params_free (driver->capture_sw_params);
1945
driver->capture_sw_params = 0;
1948
if (driver->playback_sw_params) {
1949
snd_pcm_sw_params_free (driver->playback_sw_params);
1950
driver->playback_sw_params = 0;
1958
driver->hw->release (driver->hw);
1961
free(driver->alsa_name_playback);
1962
free(driver->alsa_name_capture);
1963
free(driver->alsa_driver);
1965
alsa_driver_release_channel_dependent_memory (driver);
1966
jack_driver_nt_finish ((jack_driver_nt_t *) driver);
1970
static jack_driver_t *
1971
alsa_driver_new (char *name, char *playback_alsa_device,
1972
char *capture_alsa_device,
1973
jack_client_t *client,
1974
jack_nframes_t frames_per_cycle,
1975
jack_nframes_t user_nperiods,
1976
jack_nframes_t rate,
1981
DitherAlgorithm dither,
1984
int user_capture_nchnls,
1985
int user_playback_nchnls,
1987
jack_nframes_t capture_latency,
1988
jack_nframes_t playback_latency,
1989
alsa_midi_t *midi_driver
1994
alsa_driver_t *driver;
1996
jack_info ("creating alsa driver ... %s|%s|%" PRIu32 "|%" PRIu32
1997
"|%" PRIu32"|%" PRIu32"|%" PRIu32 "|%s|%s|%s|%s",
1998
playing ? playback_alsa_device : "-",
1999
capturing ? capture_alsa_device : "-",
2000
frames_per_cycle, user_nperiods, rate,
2001
user_capture_nchnls,user_playback_nchnls,
2002
hw_monitoring ? "hwmon": "nomon",
2003
hw_metering ? "hwmeter":"swmeter",
2004
soft_mode ? "soft-mode":"-",
2005
shorts_first ? "16bit":"32bit");
2007
driver = (alsa_driver_t *) calloc (1, sizeof (alsa_driver_t));
2009
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 =
2016
(JackDriverNullCycleFunction) alsa_driver_null_cycle;
2017
driver->nt_bufsize = (JackDriverNTBufSizeFunction) alsa_driver_bufsize;
2018
driver->nt_start = (JackDriverNTStartFunction) alsa_driver_start;
2019
driver->nt_stop = (JackDriverNTStopFunction) alsa_driver_stop;
2020
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) {
2089
jack_error ("the playback device \"%s\" is "
2090
"already in use. Please stop the"
2091
" application using it and "
2093
playback_alsa_device);
2094
alsa_driver_delete (driver);
2099
jack_error ("you do not have permission to open "
2100
"the audio device \"%s\" for playback",
2101
playback_alsa_device);
2102
alsa_driver_delete (driver);
2107
driver->playback_handle = NULL;
2110
if (driver->playback_handle) {
2111
snd_pcm_nonblock (driver->playback_handle, 0);
2116
if (snd_pcm_open (&driver->capture_handle,
2117
capture_alsa_device,
2118
SND_PCM_STREAM_CAPTURE,
2119
SND_PCM_NONBLOCK) < 0) {
2122
jack_error ("the capture device \"%s\" is "
2123
"already in use. Please stop the"
2124
" application using it and "
2126
capture_alsa_device);
2127
alsa_driver_delete (driver);
2132
jack_error ("you do not have permission to open "
2133
"the audio device \"%s\" for capture",
2134
capture_alsa_device);
2135
alsa_driver_delete (driver);
2140
driver->capture_handle = NULL;
2143
if (driver->capture_handle) {
2144
snd_pcm_nonblock (driver->capture_handle, 0);
2148
if (driver->playback_handle == NULL) {
2151
/* they asked for playback, but we can't do it */
2153
jack_error ("ALSA: Cannot open PCM device %s for "
2154
"playback. Falling back to capture-only"
2157
if (driver->capture_handle == NULL) {
2158
/* can't do anything */
2159
alsa_driver_delete (driver);
2167
if (driver->capture_handle == NULL) {
2170
/* they asked for capture, but we can't do it */
2172
jack_error ("ALSA: Cannot open PCM device %s for "
2173
"capture. Falling back to playback-only"
2176
if (driver->playback_handle == NULL) {
2177
/* can't do anything */
2178
alsa_driver_delete (driver);
2186
driver->playback_hw_params = 0;
2187
driver->capture_hw_params = 0;
2188
driver->playback_sw_params = 0;
2189
driver->capture_sw_params = 0;
2191
if (driver->playback_handle) {
2192
if ((err = snd_pcm_hw_params_malloc (
2193
&driver->playback_hw_params)) < 0) {
2194
jack_error ("ALSA: could not allocate playback hw"
2195
" params structure");
2196
alsa_driver_delete (driver);
2200
if ((err = snd_pcm_sw_params_malloc (
2201
&driver->playback_sw_params)) < 0) {
2202
jack_error ("ALSA: could not allocate playback sw"
2203
" params structure");
2204
alsa_driver_delete (driver);
2209
if (driver->capture_handle) {
2210
if ((err = snd_pcm_hw_params_malloc (
2211
&driver->capture_hw_params)) < 0) {
2212
jack_error ("ALSA: could not allocate capture hw"
2213
" params structure");
2214
alsa_driver_delete (driver);
2218
if ((err = snd_pcm_sw_params_malloc (
2219
&driver->capture_sw_params)) < 0) {
2220
jack_error ("ALSA: could not allocate capture sw"
2221
" params structure");
2222
alsa_driver_delete (driver);
2227
if (alsa_driver_set_parameters (driver, frames_per_cycle,
2228
user_nperiods, rate)) {
2229
alsa_driver_delete (driver);
2233
driver->capture_and_playback_not_synced = FALSE;
2235
if (driver->capture_handle && driver->playback_handle) {
2236
if (snd_pcm_link (driver->playback_handle,
2237
driver->capture_handle) != 0) {
2238
driver->capture_and_playback_not_synced = TRUE;
2242
driver->client = client;
2244
return (jack_driver_t *) driver;
2248
alsa_driver_listen_for_clock_sync_status (alsa_driver_t *driver,
2249
ClockSyncListenerFunction func,
2252
ClockSyncListener *csl;
2254
csl = (ClockSyncListener *) malloc (sizeof (ClockSyncListener));
2255
csl->function = func;
2257
csl->id = driver->next_clock_sync_listener_id++;
2259
pthread_mutex_lock (&driver->clock_sync_lock);
2260
driver->clock_sync_listeners =
2261
jack_slist_prepend (driver->clock_sync_listeners, csl);
2262
pthread_mutex_unlock (&driver->clock_sync_lock);
2267
alsa_driver_stop_listening_to_clock_sync_status (alsa_driver_t *driver,
2273
pthread_mutex_lock (&driver->clock_sync_lock);
2274
for (node = driver->clock_sync_listeners; node;
2275
node = jack_slist_next (node)) {
2276
if (((ClockSyncListener *) node->data)->id == which) {
2277
driver->clock_sync_listeners =
2278
jack_slist_remove_link (
2279
driver->clock_sync_listeners, node);
2281
jack_slist_free_1 (node);
2286
pthread_mutex_unlock (&driver->clock_sync_lock);
2291
alsa_driver_clock_sync_notify (alsa_driver_t *driver, channel_t chn,
2292
ClockSyncStatus status)
2296
pthread_mutex_lock (&driver->clock_sync_lock);
2297
for (node = driver->clock_sync_listeners; node;
2298
node = jack_slist_next (node)) {
2299
ClockSyncListener *csl = (ClockSyncListener *) node->data;
2300
csl->function (chn, status, csl->arg);
2302
pthread_mutex_unlock (&driver->clock_sync_lock);
2307
dither_opt (char c, DitherAlgorithm* dither)
2316
*dither = Rectangular;
2324
*dither = Triangular;
2328
jack_error ("ALSA driver: illegal dithering mode %c", c);
2335
/* DRIVER "PLUGIN" INTERFACE */
2337
const char driver_client_name[] = "alsa_pcm";
2339
const jack_driver_desc_t *
2340
driver_get_descriptor ()
2342
jack_driver_desc_t * desc;
2343
jack_driver_param_desc_t * params;
2346
desc = calloc (1, sizeof (jack_driver_desc_t));
2348
strcpy (desc->name,"alsa");
2351
params = calloc (desc->nparams, sizeof (jack_driver_param_desc_t));
2354
strcpy (params[i].name, "capture");
2355
params[i].character = 'C';
2356
params[i].type = JackDriverParamString;
2357
strcpy (params[i].value.str, "none");
2358
strcpy (params[i].short_desc,
2359
"Provide capture ports. Optionally set device");
2360
strcpy (params[i].long_desc, params[i].short_desc);
2363
strcpy (params[i].name, "playback");
2364
params[i].character = 'P';
2365
params[i].type = JackDriverParamString;
2366
strcpy (params[i].value.str, "none");
2367
strcpy (params[i].short_desc,
2368
"Provide playback ports. Optionally set device");
2369
strcpy (params[i].long_desc, params[i].short_desc);
2372
strcpy (params[i].name, "device");
2373
params[i].character = 'd';
2374
params[i].type = JackDriverParamString;
2375
strcpy (params[i].value.str, "hw:0");
2376
strcpy (params[i].short_desc, "ALSA device name");
2377
strcpy (params[i].long_desc, params[i].short_desc);
2380
strcpy (params[i].name, "rate");
2381
params[i].character = 'r';
2382
params[i].type = JackDriverParamUInt;
2383
params[i].value.ui = 48000U;
2384
strcpy (params[i].short_desc, "Sample rate");
2385
strcpy (params[i].long_desc, params[i].short_desc);
2388
strcpy (params[i].name, "period");
2389
params[i].character = 'p';
2390
params[i].type = JackDriverParamUInt;
2391
params[i].value.ui = 1024U;
2392
strcpy (params[i].short_desc, "Frames per period");
2393
strcpy (params[i].long_desc, params[i].short_desc);
2396
strcpy (params[i].name, "nperiods");
2397
params[i].character = 'n';
2398
params[i].type = JackDriverParamUInt;
2399
params[i].value.ui = 2U;
2400
strcpy (params[i].short_desc, "Number of periods of playback latency");
2401
strcpy (params[i].long_desc, params[i].short_desc);
2404
strcpy (params[i].name, "hwmon");
2405
params[i].character = 'H';
2406
params[i].type = JackDriverParamBool;
2407
params[i].value.i = 0;
2408
strcpy (params[i].short_desc,"Hardware monitoring, if available");
2409
strcpy (params[i].long_desc, params[i].short_desc);
2412
strcpy (params[i].name, "hwmeter");
2413
params[i].character = 'M';
2414
params[i].type = JackDriverParamBool;
2415
params[i].value.i = 0;
2416
strcpy (params[i].short_desc, "Hardware metering, if available");
2417
strcpy (params[i].long_desc, params[i].short_desc);
2420
strcpy (params[i].name, "duplex");
2421
params[i].character = 'D';
2422
params[i].type = JackDriverParamBool;
2423
params[i].value.i = 1;
2424
strcpy (params[i].short_desc,
2425
"Provide both capture and playback ports");
2426
strcpy (params[i].long_desc, params[i].short_desc);
2429
strcpy (params[i].name, "softmode");
2430
params[i].character = 's';
2431
params[i].type = JackDriverParamBool;
2432
params[i].value.i = 0;
2433
strcpy (params[i].short_desc, "Soft-mode, no xrun handling");
2434
strcpy (params[i].long_desc, params[i].short_desc);
2437
strcpy (params[i].name, "monitor");
2438
params[i].character = 'm';
2439
params[i].type = JackDriverParamBool;
2440
params[i].value.i = 0;
2441
strcpy (params[i].short_desc, "Provide monitor ports for the output");
2442
strcpy (params[i].long_desc, params[i].short_desc);
2445
strcpy (params[i].name, "dither");
2446
params[i].character = 'z';
2447
params[i].type = JackDriverParamChar;
2448
params[i].value.c = 'n';
2449
strcpy (params[i].short_desc, "Dithering mode");
2450
strcpy (params[i].long_desc,
2453
" r - rectangular\n"
2458
strcpy (params[i].name, "inchannels");
2459
params[i].character = 'i';
2460
params[i].type = JackDriverParamUInt;
2461
params[i].value.i = 0;
2462
strcpy (params[i].short_desc,
2463
"Number of capture channels (defaults to hardware max)");
2464
strcpy (params[i].long_desc, params[i].short_desc);
2467
strcpy (params[i].name, "outchannels");
2468
params[i].character = 'o';
2469
params[i].type = JackDriverParamUInt;
2470
params[i].value.i = 0;
2471
strcpy (params[i].short_desc,
2472
"Number of playback channels (defaults to hardware max)");
2473
strcpy (params[i].long_desc, params[i].short_desc);
2476
strcpy (params[i].name, "shorts");
2477
params[i].character = 'S';
2478
params[i].type = JackDriverParamBool;
2479
params[i].value.i = FALSE;
2480
strcpy (params[i].short_desc, "Try 16-bit samples before 32-bit");
2481
strcpy (params[i].long_desc, params[i].short_desc);
2485
strcpy (params[i].name, "input-latency");
2486
params[i].character = 'I';
2487
params[i].type = JackDriverParamUInt;
2488
params[i].value.i = 0;
2489
strcpy (params[i].short_desc, "Extra input latency (frames)");
2490
strcpy (params[i].long_desc, params[i].short_desc);
2493
strcpy (params[i].name, "output-latency");
2494
params[i].character = 'O';
2495
params[i].type = JackDriverParamUInt;
2496
params[i].value.i = 0;
2497
strcpy (params[i].short_desc, "Extra output latency (frames)");
2498
strcpy (params[i].long_desc, params[i].short_desc);
2501
strcpy (params[i].name, "midi");
2502
params[i].character = 'X';
2503
params[i].type = JackDriverParamString;
2504
strcpy (params[i].value.str, "none");
2505
strcpy (params[i].short_desc, "ALSA MIDI driver (seq|raw)");
2506
strcpy (params[i].long_desc,
2507
"ALSA MIDI driver:\n"
2508
" none - no MIDI driver\n"
2509
" seq - ALSA Sequencer driver\n"
2510
" raw - ALSA RawMIDI driver\n");
2512
desc->params = params;
2518
driver_initialize (jack_client_t *client, const JSList * params)
2520
jack_nframes_t srate = 48000;
2521
jack_nframes_t frames_per_interrupt = 1024;
2522
unsigned long user_nperiods = 2;
2523
char *playback_pcm_name = "hw:0";
2524
char *capture_pcm_name = "hw:0";
2525
int hw_monitoring = FALSE;
2526
int hw_metering = FALSE;
2527
int capture = FALSE;
2528
int playback = FALSE;
2529
int soft_mode = FALSE;
2530
int monitor = FALSE;
2531
DitherAlgorithm dither = None;
2532
int user_capture_nchnls = 0;
2533
int user_playback_nchnls = 0;
2534
int shorts_first = FALSE;
2535
jack_nframes_t systemic_input_latency = 0;
2536
jack_nframes_t systemic_output_latency = 0;
2537
char *midi_driver_name = "none";
2538
alsa_midi_t *midi = NULL;
2539
const JSList * node;
2540
const jack_driver_param_t * param;
2542
for (node = params; node; node = jack_slist_next (node)) {
2543
param = (const jack_driver_param_t *) node->data;
2545
switch (param->character) {
2549
if (strcmp (param->value.str, "none") != 0) {
2550
capture_pcm_name = strdup (param->value.str);
2556
if (strcmp (param->value.str, "none") != 0) {
2557
playback_pcm_name = strdup (param->value.str);
2567
playback_pcm_name = strdup (param->value.str);
2568
capture_pcm_name = strdup (param->value.str);
2572
hw_monitoring = param->value.i;
2576
monitor = param->value.i;
2580
hw_metering = param->value.i;
2584
srate = param->value.ui;
2585
jack_info ("apparent rate = %d", srate);
2589
frames_per_interrupt = param->value.ui;
2593
user_nperiods = param->value.ui;
2594
if (user_nperiods < 2) /* enforce minimum value */
2599
soft_mode = param->value.i;
2603
if (dither_opt (param->value.c, &dither)) {
2609
user_capture_nchnls = param->value.ui;
2612
user_playback_nchnls = param->value.ui;
2616
shorts_first = param->value.i;
2620
systemic_input_latency = param->value.ui;
2624
systemic_output_latency = param->value.ui;
2628
midi_driver_name = strdup (param->value.str);
2634
/* duplex is the default */
2635
if (!capture && !playback) {
2640
if (strcmp(midi_driver_name, "seq")==0) {
2641
midi = alsa_seqmidi_new(client, NULL);
2642
} else if (strcmp(midi_driver_name, "raw")==0) {
2643
midi = alsa_rawmidi_new(client);
2646
return alsa_driver_new ("alsa_pcm", playback_pcm_name,
2647
capture_pcm_name, client,
2648
frames_per_interrupt,
2649
user_nperiods, srate, hw_monitoring,
2650
hw_metering, capture, playback, dither,
2652
user_capture_nchnls, user_playback_nchnls,
2654
systemic_input_latency,
2655
systemic_output_latency, midi);
2659
driver_finish (jack_driver_t *driver)
2661
alsa_driver_delete ((alsa_driver_t *) driver);