2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004 Grame
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.
21 #define __STDC_FORMAT_MACROS // For inttypes.h to work in C++
32 #include <sys/types.h>
36 #include "JackAlsaDriver.h"
37 #include "JackEngineControl.h"
38 #include "JackClientControl.h"
40 #include "JackGraphManager.h"
41 #include "JackLockedEngine.h"
43 #include "JackAndroidThread.h"
45 #include "JackPosixThread.h"
47 #include "JackCompilerDeps.h"
48 #include "JackServerGlobals.h"
50 static struct jack_constraint_enum_str_descriptor midi_constraint_descr_array
[] =
52 { "none", "no MIDI driver" },
53 { "seq", "ALSA Sequencer driver" },
54 { "raw", "ALSA RawMIDI driver" },
58 static struct jack_constraint_enum_char_descriptor dither_constraint_descr_array
[] =
61 { 'r', "rectangular" },
63 { 't', "triangular" },
70 int JackAlsaDriver::SetBufferSize(jack_nframes_t buffer_size
)
72 jack_log("JackAlsaDriver::SetBufferSize %ld", buffer_size
);
73 int res
= alsa_driver_reset_parameters((alsa_driver_t
*)fDriver
, buffer_size
,
74 ((alsa_driver_t
*)fDriver
)->user_nperiods
,
75 ((alsa_driver_t
*)fDriver
)->frame_rate
);
77 if (res
== 0) { // update fEngineControl and fGraphManager
78 JackAudioDriver::SetBufferSize(buffer_size
); // Generic change, never fails
83 alsa_driver_reset_parameters((alsa_driver_t
*)fDriver
, fEngineControl
->fBufferSize
,
84 ((alsa_driver_t
*)fDriver
)->user_nperiods
,
85 ((alsa_driver_t
*)fDriver
)->frame_rate
);
91 void JackAlsaDriver::UpdateLatencies()
93 jack_latency_range_t range
;
94 alsa_driver_t
* alsa_driver
= (alsa_driver_t
*)fDriver
;
96 for (int i
= 0; i
< fCaptureChannels
; i
++) {
97 range
.min
= range
.max
= alsa_driver
->frames_per_cycle
+ alsa_driver
->capture_frame_latency
;
98 fGraphManager
->GetPort(fCapturePortList
[i
])->SetLatencyRange(JackCaptureLatency
, &range
);
101 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
102 // Add one buffer more latency if "async" mode is used...
103 range
.min
= range
.max
= (alsa_driver
->frames_per_cycle
* (alsa_driver
->user_nperiods
- 1)) +
104 ((fEngineControl
->fSyncMode
) ? 0 : fEngineControl
->fBufferSize
) + alsa_driver
->playback_frame_latency
;
105 fGraphManager
->GetPort(fPlaybackPortList
[i
])->SetLatencyRange(JackPlaybackLatency
, &range
);
107 if (fWithMonitorPorts
) {
108 range
.min
= range
.max
= alsa_driver
->frames_per_cycle
;
109 fGraphManager
->GetPort(fMonitorPortList
[i
])->SetLatencyRange(JackCaptureLatency
, &range
);
114 int JackAlsaDriver::Attach()
117 jack_port_id_t port_index
;
118 unsigned long port_flags
= (unsigned long)CaptureDriverFlags
;
119 char name
[REAL_JACK_PORT_NAME_SIZE
];
120 char alias
[REAL_JACK_PORT_NAME_SIZE
];
122 assert(fCaptureChannels
< DRIVER_PORT_NUM
);
123 assert(fPlaybackChannels
< DRIVER_PORT_NUM
);
125 alsa_driver_t
* alsa_driver
= (alsa_driver_t
*)fDriver
;
127 if (alsa_driver
->has_hw_monitoring
)
128 port_flags
|= JackPortCanMonitor
;
130 // ALSA driver may have changed the values
131 JackAudioDriver::SetBufferSize(alsa_driver
->frames_per_cycle
);
132 JackAudioDriver::SetSampleRate(alsa_driver
->frame_rate
);
134 jack_log("JackAlsaDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl
->fBufferSize
, fEngineControl
->fSampleRate
);
136 for (int i
= 0; i
< fCaptureChannels
; i
++) {
137 snprintf(alias
, sizeof(alias
), "%s:%s:out%d", fAliasName
, fCaptureDriverName
, i
+ 1);
138 snprintf(name
, sizeof(name
), "%s:capture_%d", fClientControl
.fName
, i
+ 1);
139 if (fEngine
->PortRegister(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, (JackPortFlags
)port_flags
, fEngineControl
->fBufferSize
, &port_index
) < 0) {
140 jack_error("driver: cannot register port for %s", name
);
143 port
= fGraphManager
->GetPort(port_index
);
144 port
->SetAlias(alias
);
145 fCapturePortList
[i
] = port_index
;
146 jack_log("JackAlsaDriver::Attach fCapturePortList[i] %ld ", port_index
);
149 port_flags
= (unsigned long)PlaybackDriverFlags
;
151 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
152 snprintf(alias
, sizeof(alias
), "%s:%s:in%d", fAliasName
, fPlaybackDriverName
, i
+ 1);
153 snprintf(name
, sizeof(name
), "%s:playback_%d", fClientControl
.fName
, i
+ 1);
154 if (fEngine
->PortRegister(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, (JackPortFlags
)port_flags
, fEngineControl
->fBufferSize
, &port_index
) < 0) {
155 jack_error("driver: cannot register port for %s", name
);
158 port
= fGraphManager
->GetPort(port_index
);
159 port
->SetAlias(alias
);
160 fPlaybackPortList
[i
] = port_index
;
161 jack_log("JackAlsaDriver::Attach fPlaybackPortList[i] %ld ", port_index
);
164 if (fWithMonitorPorts
) {
165 jack_log("Create monitor port");
166 snprintf(name
, sizeof(name
), "%s:monitor_%d", fClientControl
.fName
, i
+ 1);
167 if (fEngine
->PortRegister(fClientControl
.fRefNum
, name
, JACK_DEFAULT_AUDIO_TYPE
, MonitorDriverFlags
, fEngineControl
->fBufferSize
, &port_index
) < 0) {
168 jack_error("ALSA: cannot register monitor port for %s", name
);
170 fMonitorPortList
[i
] = port_index
;
177 if (alsa_driver
->midi
) {
178 int err
= (alsa_driver
->midi
->attach
)(alsa_driver
->midi
);
180 jack_error ("ALSA: cannot attach MIDI: %d", err
);
186 int JackAlsaDriver::Detach()
188 alsa_driver_t
* alsa_driver
= (alsa_driver_t
*)fDriver
;
189 if (alsa_driver
->midi
)
190 (alsa_driver
->midi
->detach
)(alsa_driver
->midi
);
192 return JackAudioDriver::Detach();
195 extern "C" char* get_control_device_name(const char * device_name
)
200 /* the user wants a hw or plughw device, the ctl name
201 * should be hw:x where x is the card identification.
202 * We skip the subdevice suffix that starts with comma */
204 if (strncasecmp(device_name
, "plughw:", 7) == 0) {
205 /* skip the "plug" prefix" */
209 comma
= strchr(device_name
, ',');
211 ctl_name
= strdup(device_name
);
212 if (ctl_name
== NULL
) {
213 jack_error("strdup(\"%s\") failed.", device_name
);
216 ctl_name
= strndup(device_name
, comma
- device_name
);
217 if (ctl_name
== NULL
) {
218 jack_error("strndup(\"%s\", %u) failed.", device_name
, (unsigned int)(comma
- device_name
));
225 static int card_to_num(const char* device
)
229 snd_ctl_card_info_t
*card_info
;
230 snd_ctl_t
* ctl_handle
;
233 snd_ctl_card_info_alloca (&card_info
);
235 ctl_name
= get_control_device_name(device
);
236 if (ctl_name
== NULL
) {
237 jack_error("get_control_device_name() failed.");
241 if ((err
= snd_ctl_open (&ctl_handle
, ctl_name
, 0)) < 0) {
242 jack_error ("control open \"%s\" (%s)", ctl_name
,
247 if ((err
= snd_ctl_card_info(ctl_handle
, card_info
)) < 0) {
248 jack_error ("control hardware info \"%s\" (%s)",
249 device
, snd_strerror (err
));
253 i
= snd_ctl_card_info_get_card(card_info
);
256 snd_ctl_close(ctl_handle
);
265 int JackAlsaDriver::Open(jack_nframes_t nframes
,
266 jack_nframes_t user_nperiods
,
267 jack_nframes_t samplerate
,
272 DitherAlgorithm dither
,
278 const char* capture_driver_name
,
279 const char* playback_driver_name
,
280 jack_nframes_t capture_latency
,
281 jack_nframes_t playback_latency
,
282 const char* midi_driver_name
)
284 // Generic JackAudioDriver Open
285 if (JackAudioDriver::Open(nframes
, samplerate
, capturing
, playing
,
286 inchannels
, outchannels
, monitor
, capture_driver_name
, playback_driver_name
,
287 capture_latency
, playback_latency
) != 0) {
291 alsa_midi_t
*midi
= 0;
293 if (strcmp(midi_driver_name
, "seq") == 0)
294 midi
= alsa_seqmidi_new((jack_client_t
*)this, 0);
295 else if (strcmp(midi_driver_name
, "raw") == 0)
296 midi
= alsa_rawmidi_new((jack_client_t
*)this);
299 if (JackServerGlobals::on_device_acquire
!= NULL
) {
300 int capture_card
= card_to_num(capture_driver_name
);
301 int playback_card
= card_to_num(playback_driver_name
);
304 if (capture_card
>= 0) {
305 snprintf(audio_name
, sizeof(audio_name
), "Audio%d", capture_card
);
306 if (!JackServerGlobals::on_device_acquire(audio_name
)) {
307 jack_error("Audio device %s cannot be acquired...", capture_driver_name
);
312 if (playback_card
>= 0 && playback_card
!= capture_card
) {
313 snprintf(audio_name
, sizeof(audio_name
), "Audio%d", playback_card
);
314 if (!JackServerGlobals::on_device_acquire(audio_name
)) {
315 jack_error("Audio device %s cannot be acquired...", playback_driver_name
);
316 if (capture_card
>= 0) {
317 snprintf(audio_name
, sizeof(audio_name
), "Audio%d", capture_card
);
318 JackServerGlobals::on_device_release(audio_name
);
325 fDriver
= alsa_driver_new ((char*)"alsa_pcm", (char*)playback_driver_name
, (char*)capture_driver_name
,
344 // ALSA driver may have changed the in/out values
345 fCaptureChannels
= ((alsa_driver_t
*)fDriver
)->capture_nchannels
;
346 fPlaybackChannels
= ((alsa_driver_t
*)fDriver
)->playback_nchannels
;
349 JackAudioDriver::Close();
354 int JackAlsaDriver::Close()
356 // Generic audio driver close
357 int res
= JackAudioDriver::Close();
359 alsa_driver_delete((alsa_driver_t
*)fDriver
);
361 if (JackServerGlobals::on_device_release
!= NULL
)
364 int capture_card
= card_to_num(fCaptureDriverName
);
365 if (capture_card
>= 0) {
366 snprintf(audio_name
, sizeof(audio_name
), "Audio%d", capture_card
);
367 JackServerGlobals::on_device_release(audio_name
);
370 int playback_card
= card_to_num(fPlaybackDriverName
);
371 if (playback_card
>= 0 && playback_card
!= capture_card
) {
372 snprintf(audio_name
, sizeof(audio_name
), "Audio%d", playback_card
);
373 JackServerGlobals::on_device_release(audio_name
);
380 int JackAlsaDriver::Start()
382 int res
= JackAudioDriver::Start();
384 res
= alsa_driver_start((alsa_driver_t
*)fDriver
);
386 JackAudioDriver::Stop();
392 int JackAlsaDriver::Stop()
394 int res
= alsa_driver_stop((alsa_driver_t
*)fDriver
);
395 if (JackAudioDriver::Stop() < 0) {
401 int JackAlsaDriver::Read()
403 /* Taken from alsa_driver_run_cycle */
405 jack_nframes_t nframes
;
410 nframes
= alsa_driver_wait((alsa_driver_t
*)fDriver
, -1, &wait_status
, &fDelayedUsecs
);
413 return -1; /* driver failed */
416 /* we detected an xrun and restarted: notify
417 * clients about the delay.
419 jack_log("ALSA XRun wait_status = %d", wait_status
);
420 NotifyXRun(fBeginDateUst
, fDelayedUsecs
);
421 goto retry
; /* recoverable error*/
424 if (nframes
!= fEngineControl
->fBufferSize
)
425 jack_log("JackAlsaDriver::Read warning fBufferSize = %ld nframes = %ld", fEngineControl
->fBufferSize
, nframes
);
427 // Has to be done before read
428 JackDriver::CycleIncTime();
430 return alsa_driver_read((alsa_driver_t
*)fDriver
, fEngineControl
->fBufferSize
);
433 int JackAlsaDriver::Write()
435 return alsa_driver_write((alsa_driver_t
*)fDriver
, fEngineControl
->fBufferSize
);
438 void JackAlsaDriver::ReadInputAux(jack_nframes_t orig_nframes
, snd_pcm_sframes_t contiguous
, snd_pcm_sframes_t nread
)
440 for (int chn
= 0; chn
< fCaptureChannels
; chn
++) {
441 if (fGraphManager
->GetConnectionsNum(fCapturePortList
[chn
]) > 0) {
442 jack_default_audio_sample_t
* buf
= (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fCapturePortList
[chn
], orig_nframes
);
443 alsa_driver_read_from_channel((alsa_driver_t
*)fDriver
, chn
, buf
+ nread
, contiguous
);
448 void JackAlsaDriver::MonitorInputAux()
450 for (int chn
= 0; chn
< fCaptureChannels
; chn
++) {
451 JackPort
* port
= fGraphManager
->GetPort(fCapturePortList
[chn
]);
452 if (port
->MonitoringInput()) {
453 ((alsa_driver_t
*)fDriver
)->input_monitor_mask
|= (1 << chn
);
458 void JackAlsaDriver::ClearOutputAux()
460 for (int chn
= 0; chn
< fPlaybackChannels
; chn
++) {
461 jack_default_audio_sample_t
* buf
=
462 (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fPlaybackPortList
[chn
], fEngineControl
->fBufferSize
);
463 memset(buf
, 0, sizeof (jack_default_audio_sample_t
) * fEngineControl
->fBufferSize
);
467 void JackAlsaDriver::SetTimetAux(jack_time_t time
)
469 fBeginDateUst
= time
;
472 void JackAlsaDriver::WriteOutputAux(jack_nframes_t orig_nframes
, snd_pcm_sframes_t contiguous
, snd_pcm_sframes_t nwritten
)
474 for (int chn
= 0; chn
< fPlaybackChannels
; chn
++) {
476 if (fGraphManager
->GetConnectionsNum(fPlaybackPortList
[chn
]) > 0) {
477 jack_default_audio_sample_t
* buf
= (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fPlaybackPortList
[chn
], orig_nframes
);
478 alsa_driver_write_to_channel(((alsa_driver_t
*)fDriver
), chn
, buf
+ nwritten
, contiguous
);
480 if (fWithMonitorPorts
&& fGraphManager
->GetConnectionsNum(fMonitorPortList
[chn
]) > 0) {
481 jack_default_audio_sample_t
* monbuf
= (jack_default_audio_sample_t
*)fGraphManager
->GetBuffer(fMonitorPortList
[chn
], orig_nframes
);
482 memcpy(monbuf
+ nwritten
, buf
+ nwritten
, contiguous
* sizeof(jack_default_audio_sample_t
));
488 int JackAlsaDriver::is_realtime() const
490 return fEngineControl
->fRealTime
;
493 int JackAlsaDriver::create_thread(pthread_t
*thread
, int priority
, int realtime
, void *(*start_routine
)(void*), void *arg
)
496 return JackAndroidThread::StartImp(thread
, priority
, realtime
, start_routine
, arg
);
498 return JackPosixThread::StartImp(thread
, priority
, realtime
, start_routine
, arg
);
502 jack_port_id_t
JackAlsaDriver::port_register(const char *port_name
, const char *port_type
, unsigned long flags
, unsigned long buffer_size
)
504 jack_port_id_t port_index
;
505 int res
= fEngine
->PortRegister(fClientControl
.fRefNum
, port_name
, port_type
, flags
, buffer_size
, &port_index
);
506 return (res
== 0) ? port_index
: 0;
509 int JackAlsaDriver::port_unregister(jack_port_id_t port_index
)
511 return fEngine
->PortUnRegister(fClientControl
.fRefNum
, port_index
);
514 void* JackAlsaDriver::port_get_buffer(int port
, jack_nframes_t nframes
)
516 return fGraphManager
->GetBuffer(port
, nframes
);
519 int JackAlsaDriver::port_set_alias(int port
, const char* name
)
521 return fGraphManager
->GetPort(port
)->SetAlias(name
);
524 jack_nframes_t
JackAlsaDriver::get_sample_rate() const
526 return fEngineControl
->fSampleRate
;
529 jack_nframes_t
JackAlsaDriver::frame_time() const
532 fEngineControl
->ReadFrameTime(&timer
);
533 return timer
.Time2Frames(GetMicroSeconds(), fEngineControl
->fBufferSize
);
536 jack_nframes_t
JackAlsaDriver::last_frame_time() const
539 fEngineControl
->ReadFrameTime(&timer
);
540 return timer
.CurFrame();
543 } // end of namespace
552 jack_driver_param_constraint_desc_t
*
556 snd_ctl_card_info_t
* info
;
557 snd_pcm_info_t
* pcminfo_capture
;
558 snd_pcm_info_t
* pcminfo_playback
;
560 jack_driver_param_value_t card_id
;
561 jack_driver_param_value_t device_id
;
562 char description
[64];
566 jack_driver_param_constraint_desc_t
* constraint_ptr
;
567 uint32_t array_size
= 0;
569 snd_ctl_card_info_alloca(&info
);
570 snd_pcm_info_alloca(&pcminfo_capture
);
571 snd_pcm_info_alloca(&pcminfo_playback
);
573 constraint_ptr
= NULL
;
575 while(snd_card_next(&card_no
) >= 0 && card_no
>= 0)
577 snprintf(card_id
.str
, sizeof(card_id
.str
), "hw:%d", card_no
);
579 if (snd_ctl_open(&handle
, card_id
.str
, 0) >= 0 &&
580 snd_ctl_card_info(handle
, info
) >= 0)
582 snprintf(card_id
.str
, sizeof(card_id
.str
), "hw:%s", snd_ctl_card_info_get_id(info
));
583 if (!jack_constraint_add_enum(
587 snd_ctl_card_info_get_name(info
)))
592 while (snd_ctl_pcm_next_device(handle
, &device_no
) >= 0 && device_no
!= -1)
594 snprintf(device_id
.str
, sizeof(device_id
.str
), "%s,%d", card_id
.str
, device_no
);
596 snd_pcm_info_set_device(pcminfo_capture
, device_no
);
597 snd_pcm_info_set_subdevice(pcminfo_capture
, 0);
598 snd_pcm_info_set_stream(pcminfo_capture
, SND_PCM_STREAM_CAPTURE
);
599 has_capture
= snd_ctl_pcm_info(handle
, pcminfo_capture
) >= 0;
601 snd_pcm_info_set_device(pcminfo_playback
, device_no
);
602 snd_pcm_info_set_subdevice(pcminfo_playback
, 0);
603 snd_pcm_info_set_stream(pcminfo_playback
, SND_PCM_STREAM_PLAYBACK
);
604 has_playback
= snd_ctl_pcm_info(handle
, pcminfo_playback
) >= 0;
606 if (has_capture
&& has_playback
)
608 snprintf(description
, sizeof(description
),"%s (duplex)", snd_pcm_info_get_name(pcminfo_capture
));
610 else if (has_capture
)
612 snprintf(description
, sizeof(description
),"%s (capture)", snd_pcm_info_get_name(pcminfo_capture
));
614 else if (has_playback
)
616 snprintf(description
, sizeof(description
),"%s (playback)", snd_pcm_info_get_name(pcminfo_playback
));
623 if (!jack_constraint_add_enum(
631 snd_ctl_close(handle
);
635 return constraint_ptr
;
637 jack_constraint_free(constraint_ptr
);
642 dither_opt (char c
, DitherAlgorithm
* dither
)
651 *dither
= Rectangular
;
659 *dither
= Triangular
;
663 fprintf (stderr
, "ALSA driver: illegal dithering mode %c\n", c
);
669 SERVER_EXPORT
const jack_driver_desc_t
* driver_get_descriptor ()
671 jack_driver_desc_t
* desc
;
672 jack_driver_desc_filler_t filler
;
673 jack_driver_param_value_t value
;
675 desc
= jack_driver_descriptor_construct("alsa", JackDriverMaster
, "Linux ALSA API based audio backend", &filler
);
677 strcpy(value
.str
, "hw:0");
679 jack_driver_descriptor_add_parameter(desc
, &filler
, "device", 'd', JackDriverParamString
, &value
, NULL
, "ALSA device name", NULL
);
681 jack_driver_descriptor_add_parameter(desc
, &filler
, "device", 'd', JackDriverParamString
, &value
, enum_alsa_devices(), "ALSA device name", NULL
);
684 strcpy(value
.str
, "none");
685 jack_driver_descriptor_add_parameter(desc
, &filler
, "capture", 'C', JackDriverParamString
, &value
, NULL
, "Provide capture ports. Optionally set device", NULL
);
686 jack_driver_descriptor_add_parameter(desc
, &filler
, "playback", 'P', JackDriverParamString
, &value
, NULL
, "Provide playback ports. Optionally set device", NULL
);
689 jack_driver_descriptor_add_parameter(desc
, &filler
, "rate", 'r', JackDriverParamUInt
, &value
, NULL
, "Sample rate", NULL
);
692 jack_driver_descriptor_add_parameter(desc
, &filler
, "period", 'p', JackDriverParamUInt
, &value
, NULL
, "Frames per period", NULL
);
695 jack_driver_descriptor_add_parameter(desc
, &filler
, "nperiods", 'n', JackDriverParamUInt
, &value
, NULL
, "Number of periods of playback latency", NULL
);
698 jack_driver_descriptor_add_parameter(desc
, &filler
, "hwmon", 'H', JackDriverParamBool
, &value
, NULL
, "Hardware monitoring, if available", NULL
);
701 jack_driver_descriptor_add_parameter(desc
, &filler
, "hwmeter", 'M', JackDriverParamBool
, &value
, NULL
, "Hardware metering, if available", NULL
);
704 jack_driver_descriptor_add_parameter(desc
, &filler
, "duplex", 'D', JackDriverParamBool
, &value
, NULL
, "Provide both capture and playback ports", NULL
);
707 jack_driver_descriptor_add_parameter(desc
, &filler
, "softmode", 's', JackDriverParamBool
, &value
, NULL
, "Soft-mode, no xrun handling", NULL
);
710 jack_driver_descriptor_add_parameter(desc
, &filler
, "monitor", 'm', JackDriverParamBool
, &value
, NULL
, "Provide monitor ports for the output", NULL
);
713 jack_driver_descriptor_add_parameter(
720 jack_constraint_compose_enum_char(
721 JACK_CONSTRAINT_FLAG_STRICT
| JACK_CONSTRAINT_FLAG_FAKE_VALUE
,
722 dither_constraint_descr_array
),
727 jack_driver_descriptor_add_parameter(desc
, &filler
, "inchannels", 'i', JackDriverParamUInt
, &value
, NULL
, "Number of capture channels (defaults to hardware max)", NULL
);
728 jack_driver_descriptor_add_parameter(desc
, &filler
, "outchannels", 'o', JackDriverParamUInt
, &value
, NULL
, "Number of playback channels (defaults to hardware max)", NULL
);
731 jack_driver_descriptor_add_parameter(desc
, &filler
, "shorts", 'S', JackDriverParamBool
, &value
, NULL
, "Try 16-bit samples before 32-bit", NULL
);
734 jack_driver_descriptor_add_parameter(desc
, &filler
, "input-latency", 'I', JackDriverParamUInt
, &value
, NULL
, "Extra input latency (frames)", NULL
);
735 jack_driver_descriptor_add_parameter(desc
, &filler
, "output-latency", 'O', JackDriverParamUInt
, &value
, NULL
, "Extra output latency (frames)", NULL
);
737 strcpy(value
.str
, "none");
738 jack_driver_descriptor_add_parameter(
743 JackDriverParamString
,
745 jack_constraint_compose_enum_str(
746 JACK_CONSTRAINT_FLAG_STRICT
| JACK_CONSTRAINT_FLAG_FAKE_VALUE
,
747 midi_constraint_descr_array
),
754 static Jack::JackAlsaDriver
* g_alsa_driver
;
756 SERVER_EXPORT
Jack::JackDriverClientInterface
* driver_initialize(Jack::JackLockedEngine
* engine
, Jack::JackSynchro
* table
, const JSList
* params
)
758 jack_nframes_t srate
= 48000;
759 jack_nframes_t frames_per_interrupt
= 1024;
760 unsigned long user_nperiods
= 2;
761 const char *playback_pcm_name
= "hw:0";
762 const char *capture_pcm_name
= "hw:0";
763 int hw_monitoring
= FALSE
;
764 int hw_metering
= FALSE
;
766 int playback
= FALSE
;
767 int soft_mode
= FALSE
;
769 DitherAlgorithm dither
= None
;
770 int user_capture_nchnls
= 0;
771 int user_playback_nchnls
= 0;
772 int shorts_first
= FALSE
;
773 jack_nframes_t systemic_input_latency
= 0;
774 jack_nframes_t systemic_output_latency
= 0;
776 const jack_driver_param_t
* param
;
777 const char *midi_driver
= "none";
779 for (node
= params
; node
; node
= jack_slist_next (node
)) {
780 param
= (const jack_driver_param_t
*) node
->data
;
782 switch (param
->character
) {
786 if (strcmp (param
->value
.str
, "none") != 0) {
787 capture_pcm_name
= strdup (param
->value
.str
);
788 jack_log("capture device %s", capture_pcm_name
);
794 if (strcmp (param
->value
.str
, "none") != 0) {
795 playback_pcm_name
= strdup (param
->value
.str
);
796 jack_log("playback device %s", playback_pcm_name
);
806 if (strcmp (param
->value
.str
, "none") != 0) {
807 playback_pcm_name
= strdup (param
->value
.str
);
808 capture_pcm_name
= strdup (param
->value
.str
);
809 jack_log("playback device %s", playback_pcm_name
);
810 jack_log("capture device %s", capture_pcm_name
);
815 hw_monitoring
= param
->value
.i
;
819 monitor
= param
->value
.i
;
823 hw_metering
= param
->value
.i
;
827 srate
= param
->value
.ui
;
828 jack_log("apparent rate = %d", srate
);
832 frames_per_interrupt
= param
->value
.ui
;
833 jack_log("frames per period = %d", frames_per_interrupt
);
837 user_nperiods
= param
->value
.ui
;
838 if (user_nperiods
< 2) { /* enforce minimum value */
844 soft_mode
= param
->value
.i
;
848 if (dither_opt (param
->value
.c
, &dither
)) {
854 user_capture_nchnls
= param
->value
.ui
;
858 user_playback_nchnls
= param
->value
.ui
;
862 shorts_first
= param
->value
.i
;
866 systemic_input_latency
= param
->value
.ui
;
870 systemic_output_latency
= param
->value
.ui
;
874 midi_driver
= strdup(param
->value
.str
);
879 /* duplex is the default */
880 if (!capture
&& !playback
) {
885 g_alsa_driver
= new Jack::JackAlsaDriver("system", "alsa_pcm", engine
, table
);
886 Jack::JackDriverClientInterface
* threaded_driver
= new Jack::JackThreadedDriver(g_alsa_driver
);
887 // Special open for ALSA driver...
888 if (g_alsa_driver
->Open(frames_per_interrupt
, user_nperiods
, srate
, hw_monitoring
, hw_metering
, capture
, playback
, dither
, soft_mode
, monitor
,
889 user_capture_nchnls
, user_playback_nchnls
, shorts_first
, capture_pcm_name
, playback_pcm_name
,
890 systemic_input_latency
, systemic_output_latency
, midi_driver
) == 0) {
891 return threaded_driver
;
893 delete threaded_driver
; // Delete the decorated driver
898 // Code to be used in alsa_driver.c
900 void ReadInput(jack_nframes_t orig_nframes
, snd_pcm_sframes_t contiguous
, snd_pcm_sframes_t nread
)
902 g_alsa_driver
->ReadInputAux(orig_nframes
, contiguous
, nread
);
906 g_alsa_driver
->MonitorInputAux();
910 g_alsa_driver
->ClearOutputAux();
912 void WriteOutput(jack_nframes_t orig_nframes
, snd_pcm_sframes_t contiguous
, snd_pcm_sframes_t nwritten
)
914 g_alsa_driver
->WriteOutputAux(orig_nframes
, contiguous
, nwritten
);
916 void SetTime(jack_time_t time
)
918 g_alsa_driver
->SetTimetAux(time
);
924 if ((res
= g_alsa_driver
->Stop()) == 0) {
925 res
= g_alsa_driver
->Start();