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.

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