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.

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