Jack2 1.9.12

JackAlsaDriver.cpp
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004 Grame
4
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.
9
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.
14
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.
18
19 */
20
21 #define __STDC_FORMAT_MACROS // For inttypes.h to work in C++
22
23 #include <iostream>
24 #include <math.h>
25 #include <stdio.h>
26 #include <memory.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <signal.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <string.h>
35
36 #include "JackAlsaDriver.h"
37 #include "JackEngineControl.h"
38 #include "JackClientControl.h"
39 #include "JackPort.h"
40 #include "JackGraphManager.h"
41 #include "JackLockedEngine.h"
42 #ifdef __ANDROID__
43 #include "JackAndroidThread.h"
44 #else
45 #include "JackPosixThread.h"
46 #endif
47 #include "JackCompilerDeps.h"
48 #include "JackServerGlobals.h"
49
50 static struct jack_constraint_enum_str_descriptor midi_constraint_descr_array[] =
51 {
52 { "none", "no MIDI driver" },
53 { "seq", "ALSA Sequencer driver" },
54 { "raw", "ALSA RawMIDI driver" },
55 { 0 }
56 };
57
58 static struct jack_constraint_enum_char_descriptor dither_constraint_descr_array[] =
59 {
60 { 'n', "none" },
61 { 'r', "rectangular" },
62 { 's', "shaped" },
63 { 't', "triangular" },
64 { 0 }
65 };
66
67 namespace Jack
68 {
69
70 int JackAlsaDriver::SetBufferSize(jack_nframes_t buffer_size)
71 {
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);
76
77 if (res == 0) { // update fEngineControl and fGraphManager
78 JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
79 // ALSA specific
80 UpdateLatencies();
81 } else {
82 // Restore old values
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);
86 }
87
88 return res;
89 }
90
91 void JackAlsaDriver::UpdateLatencies()
92 {
93 jack_latency_range_t range;
94 alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
95
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);
99 }
100
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);
106 // Monitor port
107 if (fWithMonitorPorts) {
108 range.min = range.max = alsa_driver->frames_per_cycle;
109 fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &range);
110 }
111 }
112 }
113
114 int JackAlsaDriver::Attach()
115 {
116 JackPort* port;
117 jack_port_id_t port_index;
118 unsigned long port_flags = (unsigned long)CaptureDriverFlags;
119 char name[REAL_JACK_PORT_NAME_SIZE+1];
120 char alias[REAL_JACK_PORT_NAME_SIZE+1];
121
122 assert(fCaptureChannels < DRIVER_PORT_NUM);
123 assert(fPlaybackChannels < DRIVER_PORT_NUM);
124
125 alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
126
127 if (alsa_driver->has_hw_monitoring)
128 port_flags |= JackPortCanMonitor;
129
130 // ALSA driver may have changed the values
131 JackAudioDriver::SetBufferSize(alsa_driver->frames_per_cycle);
132 JackAudioDriver::SetSampleRate(alsa_driver->frame_rate);
133
134 jack_log("JackAlsaDriver::Attach fBufferSize %ld fSampleRate %ld", fEngineControl->fBufferSize, fEngineControl->fSampleRate);
135
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);
141 return -1;
142 }
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);
147 }
148
149 port_flags = (unsigned long)PlaybackDriverFlags;
150
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);
156 return -1;
157 }
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);
162
163 // Monitor ports
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);
169 } else {
170 fMonitorPortList[i] = port_index;
171 }
172 }
173 }
174
175 UpdateLatencies();
176
177 if (alsa_driver->midi) {
178 int err = (alsa_driver->midi->attach)(alsa_driver->midi);
179 if (err)
180 jack_error ("ALSA: cannot attach MIDI: %d", err);
181 }
182
183 return 0;
184 }
185
186 int JackAlsaDriver::Detach()
187 {
188 alsa_driver_t* alsa_driver = (alsa_driver_t*)fDriver;
189 if (alsa_driver->midi)
190 (alsa_driver->midi->detach)(alsa_driver->midi);
191
192 return JackAudioDriver::Detach();
193 }
194
195 extern "C" char* get_control_device_name(const char * device_name)
196 {
197 char * ctl_name;
198 const char * comma;
199
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 */
203
204 if (strncasecmp(device_name, "plughw:", 7) == 0) {
205 /* skip the "plug" prefix" */
206 device_name += 4;
207 }
208
209 comma = strchr(device_name, ',');
210 if (comma == NULL) {
211 ctl_name = strdup(device_name);
212 if (ctl_name == NULL) {
213 jack_error("strdup(\"%s\") failed.", device_name);
214 }
215 } else {
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));
219 }
220 }
221
222 return ctl_name;
223 }
224
225 static int card_to_num(const char* device)
226 {
227 int err;
228 char* ctl_name;
229 snd_ctl_card_info_t *card_info;
230 snd_ctl_t* ctl_handle;
231 int i = -1;
232
233 snd_ctl_card_info_alloca (&card_info);
234
235 ctl_name = get_control_device_name(device);
236 if (ctl_name == NULL) {
237 jack_error("get_control_device_name() failed.");
238 goto fail;
239 }
240
241 if ((err = snd_ctl_open (&ctl_handle, ctl_name, 0)) < 0) {
242 jack_error ("control open \"%s\" (%s)", ctl_name,
243 snd_strerror(err));
244 goto free;
245 }
246
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));
250 goto close;
251 }
252
253 i = snd_ctl_card_info_get_card(card_info);
254
255 close:
256 snd_ctl_close(ctl_handle);
257
258 free:
259 free(ctl_name);
260
261 fail:
262 return i;
263 }
264
265 int JackAlsaDriver::Open(jack_nframes_t nframes,
266 jack_nframes_t user_nperiods,
267 jack_nframes_t samplerate,
268 bool hw_monitoring,
269 bool hw_metering,
270 bool capturing,
271 bool playing,
272 DitherAlgorithm dither,
273 bool soft_mode,
274 bool monitor,
275 int inchannels,
276 int outchannels,
277 bool shorts_first,
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)
283 {
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) {
288 return -1;
289 }
290
291 alsa_midi_t *midi = 0;
292 #ifndef __ANDROID__
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);
297 #endif
298
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);
302 char audio_name[32];
303
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);
308 return -1;
309 }
310 }
311
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);
319 }
320 return -1;
321 }
322 }
323 }
324
325 fDriver = alsa_driver_new ((char*)"alsa_pcm", (char*)playback_driver_name, (char*)capture_driver_name,
326 NULL,
327 nframes,
328 user_nperiods,
329 samplerate,
330 hw_monitoring,
331 hw_metering,
332 capturing,
333 playing,
334 dither,
335 soft_mode,
336 monitor,
337 inchannels,
338 outchannels,
339 shorts_first,
340 capture_latency,
341 playback_latency,
342 midi);
343 if (fDriver) {
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;
347 return 0;
348 } else {
349 Close();
350 return -1;
351 }
352 }
353
354 int JackAlsaDriver::Close()
355 {
356 // Generic audio driver close
357 int res = JackAudioDriver::Close();
358
359 if (fDriver) {
360 alsa_driver_delete((alsa_driver_t*)fDriver);
361 }
362
363 if (JackServerGlobals::on_device_release != NULL)
364 {
365 char audio_name[32];
366 int capture_card = card_to_num(fCaptureDriverName);
367 if (capture_card >= 0) {
368 snprintf(audio_name, sizeof(audio_name), "Audio%d", capture_card);
369 JackServerGlobals::on_device_release(audio_name);
370 }
371
372 int playback_card = card_to_num(fPlaybackDriverName);
373 if (playback_card >= 0 && playback_card != capture_card) {
374 snprintf(audio_name, sizeof(audio_name), "Audio%d", playback_card);
375 JackServerGlobals::on_device_release(audio_name);
376 }
377 }
378
379 return res;
380 }
381
382 int JackAlsaDriver::Start()
383 {
384 int res = JackAudioDriver::Start();
385 if (res >= 0) {
386 res = alsa_driver_start((alsa_driver_t *)fDriver);
387 if (res < 0) {
388 JackAudioDriver::Stop();
389 }
390 }
391 return res;
392 }
393
394 int JackAlsaDriver::Stop()
395 {
396 int res = alsa_driver_stop((alsa_driver_t *)fDriver);
397 if (JackAudioDriver::Stop() < 0) {
398 res = -1;
399 }
400 return res;
401 }
402
403 int JackAlsaDriver::Read()
404 {
405 /* Taken from alsa_driver_run_cycle */
406 int wait_status;
407 jack_nframes_t nframes;
408 fDelayedUsecs = 0.f;
409
410 retry:
411
412 nframes = alsa_driver_wait((alsa_driver_t *)fDriver, -1, &wait_status, &fDelayedUsecs);
413
414 if (wait_status < 0)
415 return -1; /* driver failed */
416
417 if (nframes == 0) {
418 /* we detected an xrun and restarted: notify
419 * clients about the delay.
420 */
421 jack_log("ALSA XRun wait_status = %d", wait_status);
422 NotifyXRun(fBeginDateUst, fDelayedUsecs);
423 goto retry; /* recoverable error*/
424 }
425
426 if (nframes != fEngineControl->fBufferSize)
427 jack_log("JackAlsaDriver::Read warning fBufferSize = %ld nframes = %ld", fEngineControl->fBufferSize, nframes);
428
429 // Has to be done before read
430 JackDriver::CycleIncTime();
431
432 return alsa_driver_read((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
433 }
434
435 int JackAlsaDriver::Write()
436 {
437 return alsa_driver_write((alsa_driver_t *)fDriver, fEngineControl->fBufferSize);
438 }
439
440 void JackAlsaDriver::ReadInputAux(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nread)
441 {
442 for (int chn = 0; chn < fCaptureChannels; chn++) {
443 if (fGraphManager->GetConnectionsNum(fCapturePortList[chn]) > 0) {
444 jack_default_audio_sample_t* buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fCapturePortList[chn], orig_nframes);
445 alsa_driver_read_from_channel((alsa_driver_t *)fDriver, chn, buf + nread, contiguous);
446 }
447 }
448 }
449
450 void JackAlsaDriver::MonitorInputAux()
451 {
452 for (int chn = 0; chn < fCaptureChannels; chn++) {
453 JackPort* port = fGraphManager->GetPort(fCapturePortList[chn]);
454 if (port->MonitoringInput()) {
455 ((alsa_driver_t *)fDriver)->input_monitor_mask |= (1 << chn);
456 }
457 }
458 }
459
460 void JackAlsaDriver::ClearOutputAux()
461 {
462 for (int chn = 0; chn < fPlaybackChannels; chn++) {
463 jack_default_audio_sample_t* buf =
464 (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[chn], fEngineControl->fBufferSize);
465 memset(buf, 0, sizeof (jack_default_audio_sample_t) * fEngineControl->fBufferSize);
466 }
467 }
468
469 void JackAlsaDriver::SetTimetAux(jack_time_t time)
470 {
471 fBeginDateUst = time;
472 }
473
474 void JackAlsaDriver::WriteOutputAux(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nwritten)
475 {
476 for (int chn = 0; chn < fPlaybackChannels; chn++) {
477 // Output ports
478 if (fGraphManager->GetConnectionsNum(fPlaybackPortList[chn]) > 0) {
479 jack_default_audio_sample_t* buf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fPlaybackPortList[chn], orig_nframes);
480 alsa_driver_write_to_channel(((alsa_driver_t *)fDriver), chn, buf + nwritten, contiguous);
481 // Monitor ports
482 if (fWithMonitorPorts && fGraphManager->GetConnectionsNum(fMonitorPortList[chn]) > 0) {
483 jack_default_audio_sample_t* monbuf = (jack_default_audio_sample_t*)fGraphManager->GetBuffer(fMonitorPortList[chn], orig_nframes);
484 memcpy(monbuf + nwritten, buf + nwritten, contiguous * sizeof(jack_default_audio_sample_t));
485 }
486 }
487 }
488 }
489
490 int JackAlsaDriver::is_realtime() const
491 {
492 return fEngineControl->fRealTime;
493 }
494
495 int JackAlsaDriver::create_thread(pthread_t *thread, int priority, int realtime, void *(*start_routine)(void*), void *arg)
496 {
497 #ifdef __ANDROID__
498 return JackAndroidThread::StartImp(thread, priority, realtime, start_routine, arg);
499 #else
500 return JackPosixThread::StartImp(thread, priority, realtime, start_routine, arg);
501 #endif
502 }
503
504 jack_port_id_t JackAlsaDriver::port_register(const char *port_name, const char *port_type, unsigned long flags, unsigned long buffer_size)
505 {
506 jack_port_id_t port_index;
507 int res = fEngine->PortRegister(fClientControl.fRefNum, port_name, port_type, flags, buffer_size, &port_index);
508 return (res == 0) ? port_index : 0;
509 }
510
511 int JackAlsaDriver::port_unregister(jack_port_id_t port_index)
512 {
513 return fEngine->PortUnRegister(fClientControl.fRefNum, port_index);
514 }
515
516 void* JackAlsaDriver::port_get_buffer(int port, jack_nframes_t nframes)
517 {
518 return fGraphManager->GetBuffer(port, nframes);
519 }
520
521 int JackAlsaDriver::port_set_alias(int port, const char* name)
522 {
523 return fGraphManager->GetPort(port)->SetAlias(name);
524 }
525
526 jack_nframes_t JackAlsaDriver::get_sample_rate() const
527 {
528 return fEngineControl->fSampleRate;
529 }
530
531 jack_nframes_t JackAlsaDriver::frame_time() const
532 {
533 JackTimer timer;
534 fEngineControl->ReadFrameTime(&timer);
535 return timer.Time2Frames(GetMicroSeconds(), fEngineControl->fBufferSize);
536 }
537
538 jack_nframes_t JackAlsaDriver::last_frame_time() const
539 {
540 JackTimer timer;
541 fEngineControl->ReadFrameTime(&timer);
542 return timer.CurFrame();
543 }
544
545 } // end of namespace
546
547
548 #ifdef __cplusplus
549 extern "C"
550 {
551 #endif
552
553 static
554 jack_driver_param_constraint_desc_t *
555 enum_alsa_devices()
556 {
557 snd_ctl_t * handle;
558 snd_ctl_card_info_t * info;
559 snd_pcm_info_t * pcminfo_capture;
560 snd_pcm_info_t * pcminfo_playback;
561 int card_no = -1;
562 jack_driver_param_value_t card_id;
563 jack_driver_param_value_t device_id;
564 char description[64];
565 int device_no;
566 bool has_capture;
567 bool has_playback;
568 jack_driver_param_constraint_desc_t * constraint_ptr;
569 uint32_t array_size = 0;
570
571 snd_ctl_card_info_alloca(&info);
572 snd_pcm_info_alloca(&pcminfo_capture);
573 snd_pcm_info_alloca(&pcminfo_playback);
574
575 constraint_ptr = NULL;
576
577 while(snd_card_next(&card_no) >= 0 && card_no >= 0)
578 {
579 snprintf(card_id.str, sizeof(card_id.str), "hw:%d", card_no);
580
581 if (snd_ctl_open(&handle, card_id.str, 0) >= 0 &&
582 snd_ctl_card_info(handle, info) >= 0)
583 {
584 snprintf(card_id.str, sizeof(card_id.str), "hw:%s", snd_ctl_card_info_get_id(info));
585 if (!jack_constraint_add_enum(
586 &constraint_ptr,
587 &array_size,
588 &card_id,
589 snd_ctl_card_info_get_name(info)))
590 goto fail;
591
592 device_no = -1;
593
594 while (snd_ctl_pcm_next_device(handle, &device_no) >= 0 && device_no != -1)
595 {
596 snprintf(device_id.str, sizeof(device_id.str), "%s,%d", card_id.str, device_no);
597
598 snd_pcm_info_set_device(pcminfo_capture, device_no);
599 snd_pcm_info_set_subdevice(pcminfo_capture, 0);
600 snd_pcm_info_set_stream(pcminfo_capture, SND_PCM_STREAM_CAPTURE);
601 has_capture = snd_ctl_pcm_info(handle, pcminfo_capture) >= 0;
602
603 snd_pcm_info_set_device(pcminfo_playback, device_no);
604 snd_pcm_info_set_subdevice(pcminfo_playback, 0);
605 snd_pcm_info_set_stream(pcminfo_playback, SND_PCM_STREAM_PLAYBACK);
606 has_playback = snd_ctl_pcm_info(handle, pcminfo_playback) >= 0;
607
608 if (has_capture && has_playback)
609 {
610 snprintf(description, sizeof(description),"%s (duplex)", snd_pcm_info_get_name(pcminfo_capture));
611 }
612 else if (has_capture)
613 {
614 snprintf(description, sizeof(description),"%s (capture)", snd_pcm_info_get_name(pcminfo_capture));
615 }
616 else if (has_playback)
617 {
618 snprintf(description, sizeof(description),"%s (playback)", snd_pcm_info_get_name(pcminfo_playback));
619 }
620 else
621 {
622 continue;
623 }
624
625 if (!jack_constraint_add_enum(
626 &constraint_ptr,
627 &array_size,
628 &device_id,
629 description))
630 goto fail;
631 }
632
633 snd_ctl_close(handle);
634 }
635 }
636
637 return constraint_ptr;
638 fail:
639 jack_constraint_free(constraint_ptr);
640 return NULL;
641 }
642
643 static int
644 dither_opt (char c, DitherAlgorithm* dither)
645 {
646 switch (c) {
647 case '-':
648 case 'n':
649 *dither = None;
650 break;
651
652 case 'r':
653 *dither = Rectangular;
654 break;
655
656 case 's':
657 *dither = Shaped;
658 break;
659
660 case 't':
661 *dither = Triangular;
662 break;
663
664 default:
665 fprintf (stderr, "ALSA driver: illegal dithering mode %c\n", c);
666 return -1;
667 }
668 return 0;
669 }
670
671 SERVER_EXPORT const jack_driver_desc_t* driver_get_descriptor ()
672 {
673 jack_driver_desc_t * desc;
674 jack_driver_desc_filler_t filler;
675 jack_driver_param_value_t value;
676
677 desc = jack_driver_descriptor_construct("alsa", JackDriverMaster, "Linux ALSA API based audio backend", &filler);
678
679 strcpy(value.str, "hw:0");
680 #ifdef __ANDROID__
681 jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "ALSA device name", NULL);
682 #else
683 jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, enum_alsa_devices(), "ALSA device name", NULL);
684 #endif
685
686 strcpy(value.str, "none");
687 jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set device", NULL);
688 jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set device", NULL);
689
690 value.ui = 48000U;
691 jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
692
693 value.ui = 1024U;
694 jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
695
696 value.ui = 2U;
697 jack_driver_descriptor_add_parameter(desc, &filler, "nperiods", 'n', JackDriverParamUInt, &value, NULL, "Number of periods of playback latency", NULL);
698
699 value.i = 0;
700 jack_driver_descriptor_add_parameter(desc, &filler, "hwmon", 'H', JackDriverParamBool, &value, NULL, "Hardware monitoring, if available", NULL);
701
702 value.i = 0;
703 jack_driver_descriptor_add_parameter(desc, &filler, "hwmeter", 'M', JackDriverParamBool, &value, NULL, "Hardware metering, if available", NULL);
704
705 value.i = 1;
706 jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
707
708 value.i = 0;
709 jack_driver_descriptor_add_parameter(desc, &filler, "softmode", 's', JackDriverParamBool, &value, NULL, "Soft-mode, no xrun handling", NULL);
710
711 value.i = 0;
712 jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
713
714 value.c = 'n';
715 jack_driver_descriptor_add_parameter(
716 desc,
717 &filler,
718 "dither",
719 'z',
720 JackDriverParamChar,
721 &value,
722 jack_constraint_compose_enum_char(
723 JACK_CONSTRAINT_FLAG_STRICT | JACK_CONSTRAINT_FLAG_FAKE_VALUE,
724 dither_constraint_descr_array),
725 "Dithering mode",
726 NULL);
727
728 value.ui = 0;
729 jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Number of capture channels (defaults to hardware max)", NULL);
730 jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Number of playback channels (defaults to hardware max)", NULL);
731
732 value.i = FALSE;
733 jack_driver_descriptor_add_parameter(desc, &filler, "shorts", 'S', JackDriverParamBool, &value, NULL, "Try 16-bit samples before 32-bit", NULL);
734
735 value.ui = 0;
736 jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency (frames)", NULL);
737 jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency (frames)", NULL);
738
739 strcpy(value.str, "none");
740 jack_driver_descriptor_add_parameter(
741 desc,
742 &filler,
743 "midi-driver",
744 'X',
745 JackDriverParamString,
746 &value,
747 jack_constraint_compose_enum_str(
748 JACK_CONSTRAINT_FLAG_STRICT | JACK_CONSTRAINT_FLAG_FAKE_VALUE,
749 midi_constraint_descr_array),
750 "ALSA MIDI driver",
751 NULL);
752
753 return desc;
754 }
755
756 static Jack::JackAlsaDriver* g_alsa_driver;
757
758 SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
759 {
760 jack_nframes_t srate = 48000;
761 jack_nframes_t frames_per_interrupt = 1024;
762 unsigned long user_nperiods = 2;
763 const char *playback_pcm_name = "hw:0";
764 const char *capture_pcm_name = "hw:0";
765 int hw_monitoring = FALSE;
766 int hw_metering = FALSE;
767 int capture = FALSE;
768 int playback = FALSE;
769 int soft_mode = FALSE;
770 int monitor = FALSE;
771 DitherAlgorithm dither = None;
772 int user_capture_nchnls = 0;
773 int user_playback_nchnls = 0;
774 int shorts_first = FALSE;
775 jack_nframes_t systemic_input_latency = 0;
776 jack_nframes_t systemic_output_latency = 0;
777 const JSList * node;
778 const jack_driver_param_t * param;
779 const char *midi_driver = "none";
780
781 for (node = params; node; node = jack_slist_next (node)) {
782 param = (const jack_driver_param_t *) node->data;
783
784 switch (param->character) {
785
786 case 'C':
787 capture = TRUE;
788 if (strcmp (param->value.str, "none") != 0) {
789 capture_pcm_name = strdup (param->value.str);
790 jack_log("capture device %s", capture_pcm_name);
791 }
792 break;
793
794 case 'P':
795 playback = TRUE;
796 if (strcmp (param->value.str, "none") != 0) {
797 playback_pcm_name = strdup (param->value.str);
798 jack_log("playback device %s", playback_pcm_name);
799 }
800 break;
801
802 case 'D':
803 playback = TRUE;
804 capture = TRUE;
805 break;
806
807 case 'd':
808 if (strcmp (param->value.str, "none") != 0) {
809 playback_pcm_name = strdup (param->value.str);
810 capture_pcm_name = strdup (param->value.str);
811 jack_log("playback device %s", playback_pcm_name);
812 jack_log("capture device %s", capture_pcm_name);
813 }
814 break;
815
816 case 'H':
817 hw_monitoring = param->value.i;
818 break;
819
820 case 'm':
821 monitor = param->value.i;
822 break;
823
824 case 'M':
825 hw_metering = param->value.i;
826 break;
827
828 case 'r':
829 srate = param->value.ui;
830 jack_log("apparent rate = %d", srate);
831 break;
832
833 case 'p':
834 frames_per_interrupt = param->value.ui;
835 jack_log("frames per period = %d", frames_per_interrupt);
836 break;
837
838 case 'n':
839 user_nperiods = param->value.ui;
840 if (user_nperiods < 2) { /* enforce minimum value */
841 user_nperiods = 2;
842 }
843 break;
844
845 case 's':
846 soft_mode = param->value.i;
847 break;
848
849 case 'z':
850 if (dither_opt (param->value.c, &dither)) {
851 return NULL;
852 }
853 break;
854
855 case 'i':
856 user_capture_nchnls = param->value.ui;
857 break;
858
859 case 'o':
860 user_playback_nchnls = param->value.ui;
861 break;
862
863 case 'S':
864 shorts_first = param->value.i;
865 break;
866
867 case 'I':
868 systemic_input_latency = param->value.ui;
869 break;
870
871 case 'O':
872 systemic_output_latency = param->value.ui;
873 break;
874
875 case 'X':
876 midi_driver = strdup(param->value.str);
877 break;
878 }
879 }
880
881 /* duplex is the default */
882 if (!capture && !playback) {
883 capture = TRUE;
884 playback = TRUE;
885 }
886
887 g_alsa_driver = new Jack::JackAlsaDriver("system", "alsa_pcm", engine, table);
888 Jack::JackDriverClientInterface* threaded_driver = new Jack::JackThreadedDriver(g_alsa_driver);
889 // Special open for ALSA driver...
890 if (g_alsa_driver->Open(frames_per_interrupt, user_nperiods, srate, hw_monitoring, hw_metering, capture, playback, dither, soft_mode, monitor,
891 user_capture_nchnls, user_playback_nchnls, shorts_first, capture_pcm_name, playback_pcm_name,
892 systemic_input_latency, systemic_output_latency, midi_driver) == 0) {
893 return threaded_driver;
894 } else {
895 delete threaded_driver; // Delete the decorated driver
896 return NULL;
897 }
898 }
899
900 // Code to be used in alsa_driver.c
901
902 void ReadInput(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nread)
903 {
904 g_alsa_driver->ReadInputAux(orig_nframes, contiguous, nread);
905 }
906 void MonitorInput()
907 {
908 g_alsa_driver->MonitorInputAux();
909 }
910 void ClearOutput()
911 {
912 g_alsa_driver->ClearOutputAux();
913 }
914 void WriteOutput(jack_nframes_t orig_nframes, snd_pcm_sframes_t contiguous, snd_pcm_sframes_t nwritten)
915 {
916 g_alsa_driver->WriteOutputAux(orig_nframes, contiguous, nwritten);
917 }
918 void SetTime(jack_time_t time)
919 {
920 g_alsa_driver->SetTimetAux(time);
921 }
922
923 int Restart()
924 {
925 int res;
926 if ((res = g_alsa_driver->Stop()) == 0) {
927 res = g_alsa_driver->Start();
928 }
929 return res;
930 }
931
932 #ifdef __cplusplus
933 }
934 #endif
935
936
jack_driver_param_constraint_desc_t
Definition: driver_interface.h:97
jack_constraint_enum_char_descriptor
Definition: driver_interface.h:87
Jack::JackThreadedDriver
The base class for threaded drivers using a "decorator" pattern. Threaded drivers are used with block...
Definition: JackThreadedDriver.h:34
Jack::JackLockedEngine
Locked Engine, access to methods is serialized using a mutex.
Definition: JackLockedEngine.h:78
jack_driver_param_value_t
Definition: driver_interface.h:64
Jack::JackPosixSemaphore
Inter process synchronization using POSIX semaphore.
Definition: JackPosixSemaphore.h:36
Jack::JackAlsaDriver
The ALSA driver.
Definition: JackAlsaDriver.h:36
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
jack_constraint_enum_str_descriptor
Definition: driver_interface.h:92
_jack_latency_range::min
jack_nframes_t min
Definition: types.h:270
_jack_latency_range
Definition: types.h:265
_jack_latency_range::max
jack_nframes_t max
Definition: types.h:274
alsa_midi_t
Definition: alsa_midi.h:30
jack_driver_desc_filler_t
Definition: driver_interface.h:142
_alsa_driver
Definition: alsa_driver.h:57
Jack::JackDriverClientInterface
The base interface for drivers clients.
Definition: JackDriver.h:114
jack_driver_param_t
Definition: driver_interface.h:125
_JSList
Definition: linux/alsa/jslist.h:35
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108
jack_driver_desc_t
Definition: driver_interface.h:132