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.

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