2 Copyright (C) 2004-2008 Grame
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.
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.
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.
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"
27 #include "JackTools.h"
28 #include "JackCompilerDeps.h"
37 int JackPortAudioDriver::Render(const void* inputBuffer
, void* outputBuffer
,
38 unsigned long framesPerBuffer
,
39 const PaStreamCallbackTimeInfo
* timeInfo
,
40 PaStreamCallbackFlags statusFlags
,
43 JackPortAudioDriver
* driver
= (JackPortAudioDriver
*)userData
;
44 driver
->fInputBuffer
= (jack_default_audio_sample_t
**)inputBuffer
;
45 driver
->fOutputBuffer
= (jack_default_audio_sample_t
**)outputBuffer
;
47 //MMCSSAcquireRealTime(GetCurrentThread());
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");
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...
67 // Setup threadded based log function
68 set_threaded_log_function();
69 driver
->CycleTakeBeginTime();
70 return (driver
->Process() == 0) ? paContinue
: paAbort
;
73 int JackPortAudioDriver::Read()
75 for (int i
= 0; i
< fCaptureChannels
; i
++) {
76 memcpy(GetInputBuffer(i
), fInputBuffer
[i
], sizeof(jack_default_audio_sample_t
) * fEngineControl
->fBufferSize
);
81 int JackPortAudioDriver::Write()
83 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
84 memcpy(fOutputBuffer
[i
], GetOutputBuffer(i
), sizeof(jack_default_audio_sample_t
) * fEngineControl
->fBufferSize
);
89 PaError
JackPortAudioDriver::OpenStream(jack_nframes_t buffer_size
)
91 PaStreamParameters inputParameters
;
92 PaStreamParameters outputParameters
;
94 jack_log("JackPortAudioDriver::OpenStream buffer_size = %d", buffer_size
);
97 inputParameters
.device
= fInputDevice
;
98 inputParameters
.channelCount
= fCaptureChannels
;
99 inputParameters
.sampleFormat
= paFloat32
| paNonInterleaved
; // 32 bit floating point output
100 inputParameters
.suggestedLatency
= (fInputDevice
!= paNoDevice
) // TODO: check how to setup this on ASIO
101 ? (fPaDevices
->IsASIO(fInputDevice
) ? 0 : Pa_GetDeviceInfo(inputParameters
.device
)->defaultLowInputLatency
)
103 inputParameters
.hostApiSpecificStreamInfo
= NULL
;
105 outputParameters
.device
= fOutputDevice
;
106 outputParameters
.channelCount
= fPlaybackChannels
;
107 outputParameters
.sampleFormat
= paFloat32
| paNonInterleaved
; // 32 bit floating point output
108 outputParameters
.suggestedLatency
= (fOutputDevice
!= paNoDevice
) // TODO: check how to setup this on ASIO
109 ? (fPaDevices
->IsASIO(fOutputDevice
) ? 0 : Pa_GetDeviceInfo(outputParameters
.device
)->defaultLowOutputLatency
)
111 outputParameters
.hostApiSpecificStreamInfo
= NULL
;
113 return Pa_OpenStream(&fStream
,
114 (fInputDevice
== paNoDevice
) ? 0 : &inputParameters
,
115 (fOutputDevice
== paNoDevice
) ? 0 : &outputParameters
,
116 fEngineControl
->fSampleRate
,
118 paNoFlag
, // Clipping is on...
123 void JackPortAudioDriver::UpdateLatencies()
125 jack_latency_range_t input_range
;
126 jack_latency_range_t output_range
;
127 jack_latency_range_t monitor_range
;
129 const PaStreamInfo
* info
= Pa_GetStreamInfo(fStream
);
132 for (int i
= 0; i
< fCaptureChannels
; i
++) {
133 input_range
.max
= input_range
.min
= fEngineControl
->fBufferSize
+ (info
->inputLatency
* fEngineControl
->fSampleRate
) + fCaptureLatency
;
134 fGraphManager
->GetPort(fCapturePortList
[i
])->SetLatencyRange(JackCaptureLatency
, &input_range
);
137 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
138 output_range
.max
= output_range
.min
= (info
->outputLatency
* fEngineControl
->fSampleRate
) + fPlaybackLatency
;
139 if (fEngineControl
->fSyncMode
) {
140 output_range
.max
= output_range
.min
+= fEngineControl
->fBufferSize
;
142 output_range
.max
= output_range
.min
+= fEngineControl
->fBufferSize
* 2;
144 fGraphManager
->GetPort(fPlaybackPortList
[i
])->SetLatencyRange(JackPlaybackLatency
, &output_range
);
145 if (fWithMonitorPorts
) {
146 monitor_range
.min
= monitor_range
.max
= fEngineControl
->fBufferSize
;
147 fGraphManager
->GetPort(fMonitorPortList
[i
])->SetLatencyRange(JackCaptureLatency
, &monitor_range
);
152 int JackPortAudioDriver::Open(jack_nframes_t buffer_size
,
153 jack_nframes_t samplerate
,
159 const char* capture_driver_uid
,
160 const char* playback_driver_uid
,
161 jack_nframes_t capture_latency
,
162 jack_nframes_t playback_latency
)
166 PaError err
= paNoError
;
168 fCaptureLatency
= capture_latency
;
169 fPlaybackLatency
= playback_latency
;
171 jack_log("JackPortAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s samplerate = %ld",
172 buffer_size
, inchannels
, outchannels
, capture_driver_uid
, playback_driver_uid
, samplerate
);
176 if (fPaDevices
->GetInputDeviceFromName(capture_driver_uid
, fInputDevice
, in_max
) < 0) {
181 if (fPaDevices
->GetOutputDeviceFromName(playback_driver_uid
, fOutputDevice
, out_max
) < 0) {
186 // If ASIO, request for preferred size (assuming fInputDevice and fOutputDevice are the same)
187 if (buffer_size
== 0) {
188 buffer_size
= fPaDevices
->GetPreferredBufferSize(fInputDevice
);
189 jack_log("JackPortAudioDriver::Open preferred buffer_size = %d", buffer_size
);
192 // Generic JackAudioDriver Open
193 if (JackAudioDriver::Open(buffer_size
, samplerate
, capturing
, playing
, inchannels
, outchannels
, monitor
,
194 capture_driver_uid
, playback_driver_uid
, capture_latency
, playback_latency
) != 0) {
198 jack_log("JackPortAudioDriver::Open fInputDevice = %d, fOutputDevice %d", fInputDevice
, fOutputDevice
);
200 // Default channels number required
201 if (inchannels
== 0) {
202 jack_log("JackPortAudioDriver::Open setup max in channels = %ld", in_max
);
205 if (outchannels
== 0) {
206 jack_log("JackPortAudioDriver::Open setup max out channels = %ld", out_max
);
207 outchannels
= out_max
;
210 // Too many channels required, take max available
211 if (inchannels
> in_max
) {
212 jack_error("This device has only %d available input channels.", in_max
);
215 if (outchannels
> out_max
) {
216 jack_error("This device has only %d available output channels.", out_max
);
217 outchannels
= out_max
;
220 // Core driver may have changed the in/out values
221 fCaptureChannels
= inchannels
;
222 fPlaybackChannels
= outchannels
;
224 err
= OpenStream(buffer_size
);
225 if (err
!= paNoError
) {
226 jack_error("Pa_OpenStream error %d = %s", err
, Pa_GetErrorText(err
));
231 fEngineControl
->fPeriod
= fEngineControl
->fPeriodUsecs
* 1000;
232 fEngineControl
->fComputation
= JackTools::ComputationMicroSec(fEngineControl
->fBufferSize
) * 1000;
233 fEngineControl
->fConstraint
= fEngineControl
->fPeriodUsecs
* 1000;
236 assert(strlen(capture_driver_uid
) < JACK_CLIENT_NAME_SIZE
);
237 assert(strlen(playback_driver_uid
) < JACK_CLIENT_NAME_SIZE
);
239 strcpy(fCaptureDriverName
, capture_driver_uid
);
240 strcpy(fPlaybackDriverName
, playback_driver_uid
);
245 JackAudioDriver::Close();
246 jack_error("Can't open default PortAudio device");
250 int JackPortAudioDriver::Close()
252 // Generic audio driver close
253 jack_log("JackPortAudioDriver::Close");
254 int res
= JackAudioDriver::Close();
255 return (Pa_CloseStream(fStream
) != paNoError
) ? -1 : res
;
258 int JackPortAudioDriver::Attach()
260 if (JackAudioDriver::Attach() == 0) {
264 if (fInputDevice
!= paNoDevice
&& fPaDevices
->GetHostFromDevice(fInputDevice
) == "ASIO") {
265 for (int i
= 0; i
< fCaptureChannels
; i
++) {
266 PaError err
= PaAsio_GetInputChannelName(fInputDevice
, i
, &alias
);
267 if (err
== paNoError
) {
268 JackPort
* port
= fGraphManager
->GetPort(fCapturePortList
[i
]);
269 port
->SetAlias(alias
);
274 if (fOutputDevice
!= paNoDevice
&& fPaDevices
->GetHostFromDevice(fOutputDevice
) == "ASIO") {
275 for (int i
= 0; i
< fPlaybackChannels
; i
++) {
276 PaError err
= PaAsio_GetInputChannelName(fOutputDevice
, i
, &alias
);
277 if (err
== paNoError
) {
278 JackPort
* port
= fGraphManager
->GetPort(fPlaybackPortList
[i
]);
279 port
->SetAlias(alias
);
291 int JackPortAudioDriver::Start()
293 jack_log("JackPortAudioDriver::Start");
294 if (JackAudioDriver::Start() >= 0) {
295 if (Pa_StartStream(fStream
) == paNoError
) {
298 JackAudioDriver::Stop();
303 int JackPortAudioDriver::Stop()
305 jack_log("JackPortAudioDriver::Stop");
306 int res
= (Pa_StopStream(fStream
) == paNoError
) ? 0 : -1;
307 if (JackAudioDriver::Stop() < 0) {
313 int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size
)
317 if ((err
= Pa_CloseStream(fStream
)) != paNoError
) {
318 jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err
));
322 err
= OpenStream(buffer_size
);
323 if (err
!= paNoError
) {
324 jack_error("Pa_OpenStream error %d = %s", err
, Pa_GetErrorText(err
));
327 JackAudioDriver::SetBufferSize(buffer_size
); // Generic change, never fails
332 } // end of namespace
339 #include "JackCompilerDeps.h"
341 SERVER_EXPORT jack_driver_desc_t
* driver_get_descriptor()
343 jack_driver_desc_t
* desc
;
344 jack_driver_desc_filler_t filler
;
345 jack_driver_param_value_t value
;
347 desc
= jack_driver_descriptor_construct("portaudio", JackDriverMaster
, "PortAudio API based audio backend", &filler
);
350 jack_driver_descriptor_add_parameter(desc
, &filler
, "channels", 'c', JackDriverParamUInt
, &value
, NULL
, "Maximum number of channels", NULL
);
351 jack_driver_descriptor_add_parameter(desc
, &filler
, "inchannels", 'i', JackDriverParamUInt
, &value
, NULL
, "Maximum number of input channels", NULL
);
352 jack_driver_descriptor_add_parameter(desc
, &filler
, "outchannels", 'o', JackDriverParamUInt
, &value
, NULL
, "Maximum number of output channels", NULL
);
354 jack_driver_descriptor_add_parameter(desc
, &filler
, "capture", 'C', JackDriverParamString
, &value
, NULL
, "Provide capture ports. Optionally set PortAudio device name", NULL
);
356 jack_driver_descriptor_add_parameter(desc
, &filler
, "playback", 'P', JackDriverParamString
, &value
, NULL
, "Provide playback ports. Optionally set PortAudio device name", NULL
);
359 jack_driver_descriptor_add_parameter(desc
, &filler
, "monitor", 'm', JackDriverParamBool
, &value
, NULL
, "Provide monitor ports for the output", NULL
);
362 jack_driver_descriptor_add_parameter(desc
, &filler
, "duplex", 'D', JackDriverParamBool
, &value
, NULL
, "Provide both capture and playback ports", NULL
);
365 jack_driver_descriptor_add_parameter(desc
, &filler
, "rate", 'r', JackDriverParamUInt
, &value
, NULL
, "Sample rate", NULL
);
368 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");
370 jack_driver_descriptor_add_parameter(desc
, &filler
, "device", 'd', JackDriverParamString
, &value
, NULL
, "PortAudio device name", NULL
);
373 jack_driver_descriptor_add_parameter(desc
, &filler
, "input-latency", 'I', JackDriverParamUInt
, &value
, NULL
, "Extra input latency", NULL
);
374 jack_driver_descriptor_add_parameter(desc
, &filler
, "output-latency", 'O', JackDriverParamUInt
, &value
, NULL
, "Extra output latency", NULL
);
377 jack_driver_descriptor_add_parameter(desc
, &filler
, "list-devices", 'l', JackDriverParamBool
, &value
, NULL
, "Display available PortAudio devices", NULL
);
382 SERVER_EXPORT
Jack::JackDriverClientInterface
* driver_initialize(Jack::JackLockedEngine
* engine
, Jack::JackSynchro
* table
, const JSList
* params
)
384 jack_nframes_t srate
= 44100;
385 jack_nframes_t frames_per_interrupt
= 512;
386 const char* capture_pcm_name
= "";
387 const char* playback_pcm_name
= "";
388 bool capture
= false;
389 bool playback
= false;
392 bool monitor
= false;
394 const jack_driver_param_t
*param
;
395 jack_nframes_t systemic_input_latency
= 0;
396 jack_nframes_t systemic_output_latency
= 0;
397 PortAudioDevices
* pa_devices
= new PortAudioDevices();
399 for (node
= params
; node
; node
= jack_slist_next(node
))
401 param
= (const jack_driver_param_t
*) node
->data
;
403 switch (param
->character
) {
406 capture_pcm_name
= param
->value
.str
;
407 playback_pcm_name
= param
->value
.str
;
416 chan_in
= chan_out
= (int)param
->value
.ui
;
420 chan_in
= (int)param
->value
.ui
;
424 chan_out
= (int)param
->value
.ui
;
429 if (strcmp(param
->value
.str
, "none") != 0) {
430 capture_pcm_name
= param
->value
.str
;
436 if (strcmp(param
->value
.str
, "none") != 0) {
437 playback_pcm_name
= param
->value
.str
;
442 monitor
= param
->value
.i
;
446 srate
= param
->value
.ui
;
450 frames_per_interrupt
= (unsigned int)param
->value
.ui
;
454 systemic_input_latency
= param
->value
.ui
;
458 systemic_output_latency
= param
->value
.ui
;
462 pa_devices
->DisplayDevicesNames();
463 // Stops the server in this case
468 // duplex is the default
469 if (!capture
&& !playback
) {
474 Jack::JackDriverClientInterface
* driver
= new Jack::JackPortAudioDriver("system", "portaudio", engine
, table
, pa_devices
);
475 if (driver
->Open(frames_per_interrupt
, srate
, capture
, playback
,
476 chan_in
, chan_out
, monitor
, capture_pcm_name
,
477 playback_pcm_name
, systemic_input_latency
,
478 systemic_output_latency
) == 0) {