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.

496 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 "JackExports.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 "JackExports.h"
  229. 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");
  235. desc->nparams = 13;
  236. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  237. i = 0;
  238. strcpy(desc->params[i].name, "channels");
  239. desc->params[i].character = 'c';
  240. desc->params[i].type = JackDriverParamInt;
  241. desc->params[i].value.ui = 0;
  242. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  243. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  244. i++;
  245. strcpy(desc->params[i].name, "inchannels");
  246. desc->params[i].character = 'i';
  247. desc->params[i].type = JackDriverParamInt;
  248. desc->params[i].value.ui = 0;
  249. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  250. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  251. i++;
  252. strcpy(desc->params[i].name, "outchannels");
  253. desc->params[i].character = 'o';
  254. desc->params[i].type = JackDriverParamInt;
  255. desc->params[i].value.ui = 0;
  256. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  257. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  258. i++;
  259. strcpy(desc->params[i].name, "capture");
  260. desc->params[i].character = 'C';
  261. desc->params[i].type = JackDriverParamString;
  262. strcpy(desc->params[i].value.str, "will take default PortAudio input device");
  263. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set PortAudio device name");
  264. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  265. i++;
  266. strcpy(desc->params[i].name, "playback");
  267. desc->params[i].character = 'P';
  268. desc->params[i].type = JackDriverParamString;
  269. strcpy(desc->params[i].value.str, "will take default PortAudio output device");
  270. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set PortAudio device name");
  271. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  272. i++;
  273. strcpy (desc->params[i].name, "monitor");
  274. desc->params[i].character = 'm';
  275. desc->params[i].type = JackDriverParamBool;
  276. desc->params[i].value.i = 0;
  277. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  278. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  279. i++;
  280. strcpy(desc->params[i].name, "duplex");
  281. desc->params[i].character = 'D';
  282. desc->params[i].type = JackDriverParamBool;
  283. desc->params[i].value.i = TRUE;
  284. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  285. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  286. i++;
  287. strcpy(desc->params[i].name, "rate");
  288. desc->params[i].character = 'r';
  289. desc->params[i].type = JackDriverParamUInt;
  290. desc->params[i].value.ui = 44100U;
  291. strcpy(desc->params[i].short_desc, "Sample rate");
  292. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  293. i++;
  294. strcpy(desc->params[i].name, "period");
  295. desc->params[i].character = 'p';
  296. desc->params[i].type = JackDriverParamUInt;
  297. desc->params[i].value.ui = 128U;
  298. strcpy(desc->params[i].short_desc, "Frames per period");
  299. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  300. i++;
  301. strcpy(desc->params[i].name, "device");
  302. desc->params[i].character = 'd';
  303. desc->params[i].type = JackDriverParamString;
  304. desc->params[i].value.ui = 128U;
  305. strcpy(desc->params[i].value.str, "will take default PortAudio device name");
  306. strcpy(desc->params[i].short_desc, "PortAudio device name");
  307. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  308. i++;
  309. strcpy(desc->params[i].name, "input-latency");
  310. desc->params[i].character = 'I';
  311. desc->params[i].type = JackDriverParamUInt;
  312. desc->params[i].value.i = 0;
  313. strcpy(desc->params[i].short_desc, "Extra input latency");
  314. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  315. i++;
  316. strcpy(desc->params[i].name, "output-latency");
  317. desc->params[i].character = 'O';
  318. desc->params[i].type = JackDriverParamUInt;
  319. desc->params[i].value.i = 0;
  320. strcpy(desc->params[i].short_desc, "Extra output latency");
  321. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  322. i++;
  323. strcpy(desc->params[i].name, "list-devices");
  324. desc->params[i].character = 'l';
  325. desc->params[i].type = JackDriverParamBool;
  326. desc->params[i].value.i = TRUE;
  327. strcpy(desc->params[i].short_desc, "Display available PortAudio devices");
  328. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  329. return desc;
  330. }
  331. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  332. {
  333. jack_nframes_t srate = 44100;
  334. jack_nframes_t frames_per_interrupt = 512;
  335. const char* capture_pcm_name = "";
  336. const char* playback_pcm_name = "";
  337. int capture = FALSE;
  338. int playback = FALSE;
  339. int chan_in = 0;
  340. int chan_out = 0;
  341. bool monitor = false;
  342. const JSList *node;
  343. const jack_driver_param_t *param;
  344. jack_nframes_t systemic_input_latency = 0;
  345. jack_nframes_t systemic_output_latency = 0;
  346. PortAudioDevices* pa_devices = new PortAudioDevices();
  347. for (node = params; node; node = jack_slist_next(node))
  348. {
  349. param = (const jack_driver_param_t *) node->data;
  350. switch (param->character)
  351. {
  352. case 'd':
  353. capture_pcm_name = strdup(param->value.str);
  354. playback_pcm_name = strdup(param->value.str);
  355. break;
  356. case 'D':
  357. capture = TRUE;
  358. playback = TRUE;
  359. break;
  360. case 'c':
  361. chan_in = chan_out = (int) param->value.ui;
  362. break;
  363. case 'i':
  364. chan_in = (int) param->value.ui;
  365. break;
  366. case 'o':
  367. chan_out = (int) param->value.ui;
  368. break;
  369. case 'C':
  370. capture = TRUE;
  371. if (strcmp(param->value.str, "none") != 0)
  372. {
  373. capture_pcm_name = strdup(param->value.str);
  374. }
  375. break;
  376. case 'P':
  377. playback = TRUE;
  378. if (strcmp(param->value.str, "none") != 0)
  379. {
  380. playback_pcm_name = strdup(param->value.str);
  381. }
  382. break;
  383. case 'm':
  384. monitor = param->value.i;
  385. break;
  386. case 'r':
  387. srate = param->value.ui;
  388. break;
  389. case 'p':
  390. frames_per_interrupt = (unsigned int) param->value.ui;
  391. break;
  392. case 'I':
  393. systemic_input_latency = param->value.ui;
  394. break;
  395. case 'O':
  396. systemic_output_latency = param->value.ui;
  397. break;
  398. case 'l':
  399. pa_devices->DisplayDevicesNames();
  400. break;
  401. }
  402. }
  403. // duplex is the default
  404. if (!capture && !playback)
  405. {
  406. capture = TRUE;
  407. playback = TRUE;
  408. }
  409. Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table, pa_devices);
  410. 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)
  411. {
  412. return driver;
  413. }
  414. else
  415. {
  416. delete driver;
  417. return NULL;
  418. }
  419. }
  420. #ifdef __cplusplus
  421. }
  422. #endif