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.

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