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.

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