Jack2 1.9.12

JackPortAudioDriver.cpp
1 /*
2 Copyright (C) 2004-2008 Grame
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "JackDriverLoader.h"
21 #include "driver_interface.h"
22 #include "JackPortAudioDriver.h"
23 #include "JackEngineControl.h"
24 #include "JackGraphManager.h"
25 #include "JackError.h"
26 #include "JackTime.h"
27 #include "JackTools.h"
28 #include "JackCompilerDeps.h"
29 #include <iostream>
30 #include <assert.h>
31
32 using namespace std;
33
34 namespace Jack
35 {
36
37 int JackPortAudioDriver::Render(const void* inputBuffer, void* outputBuffer,
38 unsigned long framesPerBuffer,
39 const PaStreamCallbackTimeInfo* timeInfo,
40 PaStreamCallbackFlags statusFlags,
41 void* userData)
42 {
43 return static_cast<JackPortAudioDriver*>(userData)->Render(inputBuffer, outputBuffer, statusFlags);
44 }
45
46 int JackPortAudioDriver::Render(const void* inputBuffer, void* outputBuffer, PaStreamCallbackFlags statusFlags)
47 {
48 fInputBuffer = (jack_default_audio_sample_t**)inputBuffer;
49 fOutputBuffer = (jack_default_audio_sample_t**)outputBuffer;
50
51 if (statusFlags) {
52 if (statusFlags & paOutputUnderflow)
53 jack_error("JackPortAudioDriver::Render paOutputUnderflow");
54 if (statusFlags & paInputUnderflow)
55 jack_error("JackPortAudioDriver::Render paInputUnderflow");
56 if (statusFlags & paOutputOverflow)
57 jack_error("JackPortAudioDriver::Render paOutputOverflow");
58 if (statusFlags & paInputOverflow)
59 jack_error("JackPortAudioDriver::Render paInputOverflow");
60 if (statusFlags & paPrimingOutput)
61 jack_error("JackPortAudioDriver::Render paOutputUnderflow");
62
63 if (statusFlags != paPrimingOutput) {
64 jack_time_t cur_time = GetMicroSeconds();
65 NotifyXRun(cur_time, float(cur_time - fBeginDateUst)); // Better this value than nothing...
66 }
67 }
68
69 // Setup threaded based log function
70 set_threaded_log_function();
71 CycleTakeBeginTime();
72 return (Process() == 0) ? paContinue : paAbort;
73 }
74
75 int JackPortAudioDriver::Read()
76 {
77 for (int i = 0; i < fCaptureChannels; i++) {
78 memcpy(GetInputBuffer(i), fInputBuffer[i], sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
79 }
80 return 0;
81 }
82
83 int JackPortAudioDriver::Write()
84 {
85 for (int i = 0; i < fPlaybackChannels; i++) {
86 memcpy(fOutputBuffer[i], GetOutputBuffer(i), sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
87 }
88 return 0;
89 }
90
91 PaError JackPortAudioDriver::OpenStream(jack_nframes_t buffer_size)
92 {
93 PaStreamParameters inputParameters;
94 PaStreamParameters outputParameters;
95
96 jack_log("JackPortAudioDriver::OpenStream buffer_size = %d", buffer_size);
97
98 // Update parameters
99 inputParameters.device = fInputDevice;
100 inputParameters.channelCount = fCaptureChannels;
101 inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point input
102 inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
103 ? ((fPaDevices->GetHostFromDevice(fInputDevice) == "ASIO") ? 0 : Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency)
104 : 0;
105 inputParameters.hostApiSpecificStreamInfo = NULL;
106
107 outputParameters.device = fOutputDevice;
108 outputParameters.channelCount = fPlaybackChannels;
109 outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
110 outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
111 ? ((fPaDevices->GetHostFromDevice(fOutputDevice) == "ASIO") ? 0 : Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency)
112 : 0;
113 outputParameters.hostApiSpecificStreamInfo = NULL;
114
115 return Pa_OpenStream(&fStream,
116 (fInputDevice == paNoDevice) ? 0 : &inputParameters,
117 (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
118 fEngineControl->fSampleRate,
119 buffer_size,
120 paNoFlag, // Clipping is on...
121 Render,
122 this);
123 }
124
125 void JackPortAudioDriver::UpdateLatencies()
126 {
127 jack_latency_range_t input_range;
128 jack_latency_range_t output_range;
129 jack_latency_range_t monitor_range;
130
131 const PaStreamInfo* info = Pa_GetStreamInfo(fStream);
132 assert(info);
133
134 for (int i = 0; i < fCaptureChannels; i++) {
135 input_range.max = input_range.min = fEngineControl->fBufferSize + (info->inputLatency * fEngineControl->fSampleRate) + fCaptureLatency;
136 fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
137 }
138
139 for (int i = 0; i < fPlaybackChannels; i++) {
140 output_range.max = output_range.min = (info->outputLatency * fEngineControl->fSampleRate) + fPlaybackLatency;
141 if (fEngineControl->fSyncMode) {
142 output_range.max = output_range.min += fEngineControl->fBufferSize;
143 } else {
144 output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
145 }
146 fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
147 if (fWithMonitorPorts) {
148 monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
149 fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
150 }
151 }
152 }
153
154 int JackPortAudioDriver::Open(jack_nframes_t buffer_size,
155 jack_nframes_t samplerate,
156 bool capturing,
157 bool playing,
158 int inchannels,
159 int outchannels,
160 bool monitor,
161 const char* capture_driver_uid,
162 const char* playback_driver_uid,
163 jack_nframes_t capture_latency,
164 jack_nframes_t playback_latency)
165 {
166 int in_max = 0;
167 int out_max = 0;
168 PaError err = paNoError;
169
170 if (!fPaDevices) {
171 fPaDevices = new PortAudioDevices();
172 }
173
174 fCaptureLatency = capture_latency;
175 fPlaybackLatency = playback_latency;
176
177 jack_log("JackPortAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s samplerate = %ld",
178 buffer_size, inchannels, outchannels, capture_driver_uid, playback_driver_uid, samplerate);
179
180 // Get devices
181 if (capturing) {
182 if (fPaDevices->GetInputDeviceFromName(capture_driver_uid, fInputDevice, in_max) < 0) {
183 goto error;
184 }
185 }
186 if (playing) {
187 if (fPaDevices->GetOutputDeviceFromName(playback_driver_uid, fOutputDevice, out_max) < 0) {
188 goto error;
189 }
190 }
191
192 // If ASIO, request for preferred size (assuming fInputDevice and fOutputDevice are the same)
193 if (buffer_size == 0) {
194 buffer_size = fPaDevices->GetPreferredBufferSize(fInputDevice);
195 jack_log("JackPortAudioDriver::Open preferred buffer_size = %d", buffer_size);
196 }
197
198 // Generic JackAudioDriver Open
199 char capture_driver_name[JACK_CLIENT_NAME_SIZE];
200 char playback_driver_name[JACK_CLIENT_NAME_SIZE];
201 snprintf(capture_driver_name, sizeof(capture_driver_name), "%s", capture_driver_uid);
202 snprintf(playback_driver_name, sizeof(playback_driver_name), "%s", playback_driver_uid);
203 if (JackAudioDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor,
204 capture_driver_name, playback_driver_name, capture_latency, playback_latency) != 0) {
205 return -1;
206 }
207
208 jack_log("JackPortAudioDriver::Open fInputDevice = %d, fOutputDevice %d", fInputDevice, fOutputDevice);
209
210 // Default channels number required
211 if (inchannels == 0) {
212 jack_log("JackPortAudioDriver::Open setup max in channels = %ld", in_max);
213 inchannels = in_max;
214 }
215 if (outchannels == 0) {
216 jack_log("JackPortAudioDriver::Open setup max out channels = %ld", out_max);
217 outchannels = out_max;
218 }
219
220 // Too many channels required, take max available
221 if (inchannels > in_max) {
222 jack_error("This device has only %d available input channels.", in_max);
223 inchannels = in_max;
224 }
225 if (outchannels > out_max) {
226 jack_error("This device has only %d available output channels.", out_max);
227 outchannels = out_max;
228 }
229
230 // Core driver may have changed the in/out values
231 fCaptureChannels = inchannels;
232 fPlaybackChannels = outchannels;
233
234 err = OpenStream(buffer_size);
235 if (err != paNoError) {
236 jack_error("Pa_OpenStream error = %s", Pa_GetErrorText(err));
237 goto error;
238 }
239
240 #ifdef __APPLE__
241 fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000;
242 fEngineControl->fComputation = JackTools::ComputationMicroSec(fEngineControl->fBufferSize) * 1000;
243 fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
244 #endif
245
246 return 0;
247
248 error:
249 JackAudioDriver::Close();
250 jack_error("Can't open default PortAudio device");
251 return -1;
252 }
253
254 int JackPortAudioDriver::Close()
255 {
256 // Generic audio driver close
257 jack_log("JackPortAudioDriver::Close");
258 JackAudioDriver::Close();
259 PaError err = Pa_CloseStream(fStream);
260 if (err != paNoError) {
261 jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
262 }
263 delete fPaDevices;
264 fPaDevices = NULL;
265 return (err != paNoError) ? -1 : 0;
266 }
267
268 int JackPortAudioDriver::Attach()
269 {
270 if (JackAudioDriver::Attach() == 0) {
271
272 const char* alias;
273
274 #if defined(HAVE_ASIO)
275 if (fInputDevice != paNoDevice && fPaDevices->GetHostFromDevice(fInputDevice) == "ASIO") {
276 for (int i = 0; i < fCaptureChannels; i++) {
277 if (PaAsio_GetInputChannelName(fInputDevice, i, &alias) == paNoError) {
278 JackPort* port = fGraphManager->GetPort(fCapturePortList[i]);
279 port->SetAlias(alias);
280 }
281 }
282 }
283
284 if (fOutputDevice != paNoDevice && fPaDevices->GetHostFromDevice(fOutputDevice) == "ASIO") {
285 for (int i = 0; i < fPlaybackChannels; i++) {
286 if (PaAsio_GetOutputChannelName(fOutputDevice, i, &alias) == paNoError) {
287 JackPort* port = fGraphManager->GetPort(fPlaybackPortList[i]);
288 port->SetAlias(alias);
289 }
290 }
291 }
292 #endif
293 return 0;
294
295 } else {
296 return -1;
297 }
298 }
299
300 int JackPortAudioDriver::Start()
301 {
302 jack_log("JackPortAudioDriver::Start");
303 if (JackAudioDriver::Start() == 0) {
304 PaError err;
305 if ((err = Pa_StartStream(fStream)) == paNoError) {
306 return 0;
307 }
308 jack_error("Pa_StartStream error = %s", Pa_GetErrorText(err));
309 JackAudioDriver::Stop();
310 }
311 return -1;
312 }
313
314 int JackPortAudioDriver::Stop()
315 {
316 jack_log("JackPortAudioDriver::Stop");
317 PaError err;
318 if ((err = Pa_StopStream(fStream)) != paNoError) {
319 jack_error("Pa_StopStream error = %s", Pa_GetErrorText(err));
320 }
321 if (JackAudioDriver::Stop() < 0) {
322 return -1;
323 } else {
324 return (err == paNoError) ? 0 : -1;
325 }
326 }
327
328 int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
329 {
330 PaError err;
331
332 if (fStream && (err = Pa_CloseStream(fStream)) != paNoError) {
333 jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
334 goto error;
335 }
336
337 // It seems that some ASIO drivers (like ASIO4All) needs this to restart correctly;
338 delete fPaDevices;
339 fPaDevices = new PortAudioDevices();
340
341 err = OpenStream(buffer_size);
342 if (err != paNoError) {
343 jack_error("Pa_OpenStream error = %s", Pa_GetErrorText(err));
344 goto error;
345 } else {
346 JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
347 return 0;
348 }
349
350 error:
351 fStream = NULL;
352 return -1;
353 }
354
355 } // end of namespace
356
357 #ifdef __cplusplus
358 extern "C"
359 {
360 #endif
361
362 #include "JackCompilerDeps.h"
363
364 SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
365 {
366 jack_driver_desc_t * desc;
367 jack_driver_desc_filler_t filler;
368 jack_driver_param_value_t value;
369
370 desc = jack_driver_descriptor_construct("portaudio", JackDriverMaster, "PortAudio API based audio backend", &filler);
371
372 value.ui = 0;
373 jack_driver_descriptor_add_parameter(desc, &filler, "channels", 'c', JackDriverParamUInt, &value, NULL, "Maximum number of channels", NULL);
374 jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Maximum number of input channels", NULL);
375 jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Maximum number of output channels", NULL);
376
377 jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set PortAudio device name", NULL);
378
379 jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set PortAudio device name", NULL);
380
381 value.i = 0;
382 jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
383
384 value.i = true;
385 jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
386
387 value.ui = 44100U;
388 jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
389
390 value.ui = 512U;
391 jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", "Frames per period. If 0 and ASIO driver, will take preferred value");
392
393 jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "PortAudio device name", NULL);
394
395 value.ui = 0;
396 jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency", NULL);
397 jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency", NULL);
398
399 value.i = true;
400 jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available PortAudio devices", NULL);
401
402 return desc;
403 }
404
405 SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
406 {
407 jack_nframes_t srate = 44100;
408 jack_nframes_t frames_per_interrupt = 512;
409 const char* capture_pcm_name = "";
410 const char* playback_pcm_name = "";
411 bool capture = false;
412 bool playback = false;
413 int chan_in = 0;
414 int chan_out = 0;
415 bool monitor = false;
416 const JSList *node;
417 const jack_driver_param_t *param;
418 jack_nframes_t systemic_input_latency = 0;
419 jack_nframes_t systemic_output_latency = 0;
420 PortAudioDevices* pa_devices = new PortAudioDevices();
421
422 for (node = params; node; node = jack_slist_next(node))
423 {
424 param = (const jack_driver_param_t *) node->data;
425
426 switch (param->character) {
427
428 case 'd':
429 capture_pcm_name = param->value.str;
430 playback_pcm_name = param->value.str;
431 break;
432
433 case 'D':
434 capture = true;
435 playback = true;
436 break;
437
438 case 'c':
439 chan_in = chan_out = (int)param->value.ui;
440 break;
441
442 case 'i':
443 chan_in = (int)param->value.ui;
444 break;
445
446 case 'o':
447 chan_out = (int)param->value.ui;
448 break;
449
450 case 'C':
451 capture = true;
452 if (strcmp(param->value.str, "none") != 0) {
453 capture_pcm_name = param->value.str;
454 }
455 break;
456
457 case 'P':
458 playback = true;
459 if (strcmp(param->value.str, "none") != 0) {
460 playback_pcm_name = param->value.str;
461 }
462 break;
463
464 case 'm':
465 monitor = param->value.i;
466 break;
467
468 case 'r':
469 srate = param->value.ui;
470 break;
471
472 case 'p':
473 frames_per_interrupt = (unsigned int)param->value.ui;
474 break;
475
476 case 'I':
477 systemic_input_latency = param->value.ui;
478 break;
479
480 case 'O':
481 systemic_output_latency = param->value.ui;
482 break;
483
484 case 'l':
485 pa_devices->DisplayDevicesNames();
486 // Stops the server in this case
487 return NULL;
488 }
489 }
490
491 // duplex is the default
492 if (!capture && !playback) {
493 capture = true;
494 playback = true;
495 }
496
497 Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table, pa_devices);
498 if (driver->Open(frames_per_interrupt, srate, capture, playback,
499 chan_in, chan_out, monitor, capture_pcm_name,
500 playback_pcm_name, systemic_input_latency,
501 systemic_output_latency) == 0) {
502 return driver;
503 } else {
504 delete driver;
505 return NULL;
506 }
507 }
508
509 #ifdef __cplusplus
510 }
511 #endif
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
Pa_StopStream
PaError Pa_StopStream(PaStream *stream)
PaStreamInfo
Definition: portaudio.h:948
paOutputUnderflow
#define paOutputUnderflow
Definition: portaudio.h:670
Jack::JackPosixSemaphore
Inter process synchronization using POSIX semaphore.
Definition: JackPosixSemaphore.h:36
Pa_GetStreamInfo
const PaStreamInfo * Pa_GetStreamInfo(PaStream *stream)
jack_error
SERVER_EXPORT void jack_error(const char *fmt,...)
Definition: JackError.cpp:92
Pa_OpenStream
PaError Pa_OpenStream(PaStream **stream, const PaStreamParameters *inputParameters, const PaStreamParameters *outputParameters, double sampleRate, unsigned long framesPerBuffer, PaStreamFlags streamFlags, PaStreamCallback *streamCallback, void *userData)
PaAsio_GetOutputChannelName
PaError PaAsio_GetOutputChannelName(PaDeviceIndex device, int channelIndex, const char **channelName)
paInputUnderflow
#define paInputUnderflow
Definition: portaudio.h:655
_jack_latency_range::min
jack_nframes_t min
Definition: types.h:270
Jack::JackPortAudioDriver
The PortAudio driver.
Definition: JackPortAudioDriver.h:34
paNoFlag
#define paNoFlag
Definition: portaudio.h:593
_jack_latency_range
Definition: types.h:265
paInputOverflow
#define paInputOverflow
Definition: portaudio.h:664
Pa_StartStream
PaError Pa_StartStream(PaStream *stream)
PaStreamParameters::hostApiSpecificStreamInfo
void * hostApiSpecificStreamInfo
Definition: portaudio.h:515
_jack_latency_range::max
jack_nframes_t max
Definition: types.h:274
paFloat32
#define paFloat32
Definition: portaudio.h:424
PaAsio_GetInputChannelName
PaError PaAsio_GetInputChannelName(PaDeviceIndex device, int channelIndex, const char **channelName)
PaStreamInfo::inputLatency
PaTime inputLatency
Definition: portaudio.h:958
paOutputOverflow
#define paOutputOverflow
Definition: portaudio.h:675
PaStreamParameters::sampleFormat
PaSampleFormat sampleFormat
Definition: portaudio.h:495
PaError
int PaError
Definition: portaudio.h:63
PaStreamParameters::suggestedLatency
PaTime suggestedLatency
Definition: portaudio.h:508
PaStreamParameters
Definition: portaudio.h:475
PaStreamCallbackFlags
unsigned long PaStreamCallbackFlags
Definition: portaudio.h:646
paNoDevice
#define paNoDevice
Definition: portaudio.h:161
jack_driver_desc_filler_t
Definition: driver_interface.h:142
Jack::JackDriverClientInterface
The base interface for drivers clients.
Definition: JackDriver.h:114
Pa_GetDeviceInfo
const PaDeviceInfo * Pa_GetDeviceInfo(PaDeviceIndex device)
PaStreamParameters::device
PaDeviceIndex device
Definition: portaudio.h:482
PaStreamInfo::outputLatency
PaTime outputLatency
Definition: portaudio.h:966
jack_driver_param_t
Definition: driver_interface.h:125
Pa_GetErrorText
const char * Pa_GetErrorText(PaError errorCode)
_JSList
Definition: linux/alsa/jslist.h:35
PaStreamParameters::channelCount
int channelCount
Definition: portaudio.h:489
paPrimingOutput
#define paPrimingOutput
Definition: portaudio.h:681
jack_log
SERVER_EXPORT void jack_log(const char *fmt,...)
Definition: JackError.cpp:108
jack_driver_desc_t
Definition: driver_interface.h:132
Pa_CloseStream
PaError Pa_CloseStream(PaStream *stream)
PaStreamCallbackTimeInfo
Definition: portaudio.h:632
PortAudioDevices
A PortAudio Devices manager.
Definition: JackPortAudioDevices.h:33