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