Jack2  1.9.8
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  JackPortAudioDriver* driver = (JackPortAudioDriver*)userData;
44  driver->fInputBuffer = (jack_default_audio_sample_t**)inputBuffer;
45  driver->fOutputBuffer = (jack_default_audio_sample_t**)outputBuffer;
46 
47  MMCSSAcquireRealTime(GetCurrentThread());
48 
49  if (statusFlags) {
50  if (statusFlags & paOutputUnderflow)
51  jack_error("JackPortAudioDriver::Render paOutputUnderflow");
52  if (statusFlags & paInputUnderflow)
53  jack_error("JackPortAudioDriver::Render paInputUnderflow");
54  if (statusFlags & paOutputOverflow)
55  jack_error("JackPortAudioDriver::Render paOutputOverflow");
56  if (statusFlags & paInputOverflow)
57  jack_error("JackPortAudioDriver::Render paInputOverflow");
58  if (statusFlags & paPrimingOutput)
59  jack_error("JackPortAudioDriver::Render paOutputUnderflow");
60 
61  if (statusFlags != paPrimingOutput) {
62  jack_time_t cur_time = GetMicroSeconds();
63  driver->NotifyXRun(cur_time, float(cur_time - driver->fBeginDateUst)); // Better this value than nothing...
64  }
65  }
66 
67  // Setup threadded based log function
68  set_threaded_log_function();
69  driver->CycleTakeBeginTime();
70  return (driver->Process() == 0) ? paContinue : paAbort;
71 }
72 
73 int JackPortAudioDriver::Read()
74 {
75  for (int i = 0; i < fCaptureChannels; i++) {
76  memcpy(GetInputBuffer(i), fInputBuffer[i], sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
77  }
78  return 0;
79 }
80 
81 int JackPortAudioDriver::Write()
82 {
83  for (int i = 0; i < fPlaybackChannels; i++) {
84  memcpy(fOutputBuffer[i], GetOutputBuffer(i), sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
85  }
86  return 0;
87 }
88 
89 PaError JackPortAudioDriver::OpenStream(jack_nframes_t buffer_size)
90 {
91  PaStreamParameters inputParameters;
92  PaStreamParameters outputParameters;
93 
94  // Update parameters
95  inputParameters.device = fInputDevice;
96  inputParameters.channelCount = fCaptureChannels;
97  inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
98  inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
99  ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
100  : 0;
101  inputParameters.hostApiSpecificStreamInfo = NULL;
102 
103  outputParameters.device = fOutputDevice;
104  outputParameters.channelCount = fPlaybackChannels;
105  outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
106  outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
107  ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
108  : 0;
109  outputParameters.hostApiSpecificStreamInfo = NULL;
110 
111  return Pa_OpenStream(&fStream,
112  (fInputDevice == paNoDevice) ? 0 : &inputParameters,
113  (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
114  fEngineControl->fSampleRate,
115  buffer_size,
116  paNoFlag, // Clipping is on...
117  Render,
118  this);
119 }
120 
121 void JackPortAudioDriver::UpdateLatencies()
122 {
123  jack_latency_range_t input_range;
124  jack_latency_range_t output_range;
125  jack_latency_range_t monitor_range;
126 
127  const PaStreamInfo* info = Pa_GetStreamInfo(fStream);
128  assert(info);
129 
130  for (int i = 0; i < fCaptureChannels; i++) {
131  input_range.max = input_range.min = fEngineControl->fBufferSize + (info->inputLatency * fEngineControl->fSampleRate) + fCaptureLatency;
132  fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
133  }
134 
135  for (int i = 0; i < fPlaybackChannels; i++) {
136  output_range.max = output_range.min = (info->outputLatency * fEngineControl->fSampleRate) + fPlaybackLatency;
137  if (fEngineControl->fSyncMode) {
138  output_range.max = output_range.min += fEngineControl->fBufferSize;
139  } else {
140  output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
141  }
142  fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
143  if (fWithMonitorPorts) {
144  monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
145  fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
146  }
147  }
148 }
149 
150 int JackPortAudioDriver::Open(jack_nframes_t buffer_size,
151  jack_nframes_t samplerate,
152  bool capturing,
153  bool playing,
154  int inchannels,
155  int outchannels,
156  bool monitor,
157  const char* capture_driver_uid,
158  const char* playback_driver_uid,
159  jack_nframes_t capture_latency,
160  jack_nframes_t playback_latency)
161 {
162  int in_max = 0;
163  int out_max = 0;
164  PaError err = paNoError;
165 
166  fCaptureLatency = capture_latency;
167  fPlaybackLatency = playback_latency;
168 
169  jack_log("JackPortAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s samplerate = %ld",
170  buffer_size, inchannels, outchannels, capture_driver_uid, playback_driver_uid, samplerate);
171 
172  // Generic JackAudioDriver Open
173  if (JackAudioDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor,
174  capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
175  return -1;
176  }
177 
178  //get devices
179  if (capturing) {
180  if (fPaDevices->GetInputDeviceFromName(capture_driver_uid, fInputDevice, in_max) < 0) {
181  goto error;
182  }
183  }
184  if (playing) {
185  if (fPaDevices->GetOutputDeviceFromName(playback_driver_uid, fOutputDevice, out_max) < 0) {
186  goto error;
187  }
188  }
189 
190  jack_log("JackPortAudioDriver::Open fInputDevice = %d, fOutputDevice %d", fInputDevice, fOutputDevice);
191 
192  //default channels number required
193  if (inchannels == 0) {
194  jack_log("JackPortAudioDriver::Open setup max in channels = %ld", in_max);
195  inchannels = in_max;
196  }
197  if (outchannels == 0) {
198  jack_log("JackPortAudioDriver::Open setup max out channels = %ld", out_max);
199  outchannels = out_max;
200  }
201 
202  //too many channels required, take max available
203  if (inchannels > in_max) {
204  jack_error("This device has only %d available input channels.", in_max);
205  inchannels = in_max;
206  }
207  if (outchannels > out_max) {
208  jack_error("This device has only %d available output channels.", out_max);
209  outchannels = out_max;
210  }
211 
212  // Core driver may have changed the in/out values
213  fCaptureChannels = inchannels;
214  fPlaybackChannels = outchannels;
215 
216  err = OpenStream(buffer_size);
217  if (err != paNoError) {
218  jack_error("Pa_OpenStream error %d = %s", err, Pa_GetErrorText(err));
219  goto error;
220  }
221 
222 #ifdef __APPLE__
223  fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000;
224  fEngineControl->fComputation = JackTools::ComputationMicroSec(fEngineControl->fBufferSize) * 1000;
225  fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
226 #endif
227 
228  assert(strlen(capture_driver_uid) < JACK_CLIENT_NAME_SIZE);
229  assert(strlen(playback_driver_uid) < JACK_CLIENT_NAME_SIZE);
230 
231  strcpy(fCaptureDriverName, capture_driver_uid);
232  strcpy(fPlaybackDriverName, playback_driver_uid);
233 
234  return 0;
235 
236 error:
237  JackAudioDriver::Close();
238  jack_error("Can't open default PortAudio device");
239  return -1;
240 }
241 
242 int JackPortAudioDriver::Close()
243 {
244  // Generic audio driver close
245  int res = JackAudioDriver::Close();
246  jack_log("JackPortAudioDriver::Close");
247  Pa_CloseStream(fStream);
248  return res;
249 }
250 
251 int JackPortAudioDriver::Start()
252 {
253  jack_log("JackPortAudioDriver::Start");
254  if (JackAudioDriver::Start() >= 0) {
255  PaError err = Pa_StartStream(fStream);
256  if (err == paNoError) {
257  return 0;
258  }
259  JackAudioDriver::Stop();
260  }
261  return -1;
262 }
263 
264 int JackPortAudioDriver::Stop()
265 {
266  jack_log("JackPortAudioDriver::Stop");
267  PaError err = Pa_StopStream(fStream);
268  int res = (err == paNoError) ? 0 : -1;
269  if (JackAudioDriver::Stop() < 0) {
270  res = -1;
271  }
272  return res;
273 }
274 
275 int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
276 {
277  PaError err;
278 
279  if ((err = Pa_CloseStream(fStream)) != paNoError) {
280  jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
281  return -1;
282  }
283 
284  err = OpenStream(buffer_size);
285  if (err != paNoError) {
286  jack_error("Pa_OpenStream error %d = %s", err, Pa_GetErrorText(err));
287  return -1;
288  } else {
289  JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
290  return 0;
291  }
292 }
293 
294 } // end of namespace
295 
296 #ifdef __cplusplus
297 extern "C"
298 {
299 #endif
300 
301 #include "JackCompilerDeps.h"
302 
303  SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
304  {
305  jack_driver_desc_t * desc;
308 
309  desc = jack_driver_descriptor_construct("portaudio", JackDriverMaster, "PortAudio API based audio backend", &filler);
310 
311  value.ui = 0;
312  jack_driver_descriptor_add_parameter(desc, &filler, "channels", 'c', JackDriverParamUInt, &value, NULL, "Maximum number of channels", NULL);
313  jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Maximum number of input channels", NULL);
314  jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Maximum number of output channels", NULL);
315 
316  strcpy(value.str, "will take default PortAudio input device");
317  jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set PortAudio device name", NULL);
318 
319  strcpy(value.str, "will take default PortAudio output device");
320  jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set PortAudio device name", NULL);
321 
322  value.i = 0;
323  jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
324 
325  value.i = TRUE;
326  jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
327 
328  value.ui = 44100U;
329  jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
330 
331  value.ui = 512U;
332  jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
333 
334  strcpy(value.str, "will take default PortAudio device name");
335  jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "PortAudio device name", NULL);
336 
337  value.ui = 0;
338  jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency", NULL);
339  jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency", NULL);
340 
341  value.i = TRUE;
342  jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available PortAudio devices", NULL);
343 
344  return desc;
345  }
346 
347  SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
348  {
349  jack_nframes_t srate = 44100;
350  jack_nframes_t frames_per_interrupt = 512;
351  const char* capture_pcm_name = "";
352  const char* playback_pcm_name = "";
353  bool capture = false;
354  bool playback = false;
355  int chan_in = 0;
356  int chan_out = 0;
357  bool monitor = false;
358  const JSList *node;
359  const jack_driver_param_t *param;
360  jack_nframes_t systemic_input_latency = 0;
361  jack_nframes_t systemic_output_latency = 0;
362  PortAudioDevices* pa_devices = new PortAudioDevices();
363 
364  for (node = params; node; node = jack_slist_next(node))
365  {
366  param = (const jack_driver_param_t *) node->data;
367 
368  switch (param->character) {
369 
370  case 'd':
371  capture_pcm_name = param->value.str;
372  playback_pcm_name = param->value.str;
373  break;
374 
375  case 'D':
376  capture = true;
377  playback = true;
378  break;
379 
380  case 'c':
381  chan_in = chan_out = (int)param->value.ui;
382  break;
383 
384  case 'i':
385  chan_in = (int)param->value.ui;
386  break;
387 
388  case 'o':
389  chan_out = (int)param->value.ui;
390  break;
391 
392  case 'C':
393  capture = true;
394  if (strcmp(param->value.str, "none") != 0) {
395  capture_pcm_name = param->value.str;
396  }
397  break;
398 
399  case 'P':
400  playback = TRUE;
401  if (strcmp(param->value.str, "none") != 0) {
402  playback_pcm_name = param->value.str;
403  }
404  break;
405 
406  case 'm':
407  monitor = param->value.i;
408  break;
409 
410  case 'r':
411  srate = param->value.ui;
412  break;
413 
414  case 'p':
415  frames_per_interrupt = (unsigned int)param->value.ui;
416  break;
417 
418  case 'I':
419  systemic_input_latency = param->value.ui;
420  break;
421 
422  case 'O':
423  systemic_output_latency = param->value.ui;
424  break;
425 
426  case 'l':
427  pa_devices->DisplayDevicesNames();
428  break;
429  }
430  }
431 
432  // duplex is the default
433  if (!capture && !playback) {
434  capture = true;
435  playback = true;
436  }
437 
438  Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table, pa_devices);
439  if (driver->Open(frames_per_interrupt, srate, capture, playback,
440  chan_in, chan_out, monitor, capture_pcm_name,
441  playback_pcm_name, systemic_input_latency,
442  systemic_output_latency) == 0) {
443  return driver;
444  } else {
445  delete driver;
446  return NULL;
447  }
448  }
449 
450 #ifdef __cplusplus
451 }
452 #endif