jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

502 lines
18KB

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