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.

673 lines
25KB

  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. #ifdef WIN32
  16. #pragma warning (disable : 4786)
  17. #endif
  18. #include "pa_asio.h"
  19. #include "JackDriverLoader.h"
  20. #include "driver_interface.h"
  21. #include "JackPortAudioDriver.h"
  22. #include "JackEngineControl.h"
  23. #include "JackError.h"
  24. #include "JackTime.h"
  25. #include <iostream>
  26. #include <assert.h>
  27. namespace Jack
  28. {
  29. void JackPortAudioDriver::PrintSupportedStandardSampleRates(const PaStreamParameters* inputParameters, const PaStreamParameters* outputParameters)
  30. {
  31. static double standardSampleRates[] = {
  32. 8000.0, 9600.0, 11025.0, 12000.0, 16000.0, 22050.0, 24000.0, 32000.0,
  33. 44100.0, 48000.0, 88200.0, 96000.0, 192000.0, -1 /* negative terminated list */
  34. };
  35. int i, printCount;
  36. PaError err;
  37. printCount = 0;
  38. for (i = 0; standardSampleRates[i] > 0; i++) {
  39. err = Pa_IsFormatSupported(inputParameters, outputParameters, standardSampleRates[i]);
  40. if (err == paFormatIsSupported) {
  41. if (printCount == 0) {
  42. printf("\t%8.2f", standardSampleRates[i]);
  43. printCount = 1;
  44. } else if (printCount == 4) {
  45. printf(",\n\t%8.2f", standardSampleRates[i]);
  46. printCount = 1;
  47. } else {
  48. printf(", %8.2f", standardSampleRates[i]);
  49. ++printCount;
  50. }
  51. }
  52. }
  53. if (!printCount)
  54. printf("None\n");
  55. else
  56. printf("\n");
  57. }
  58. bool JackPortAudioDriver::GetInputDeviceFromName(const char* name, PaDeviceIndex* device, int* in_max)
  59. {
  60. const PaDeviceInfo* deviceInfo;
  61. PaDeviceIndex numDevices = Pa_GetDeviceCount();
  62. for (int i = 0; i < numDevices; i++) {
  63. deviceInfo = Pa_GetDeviceInfo(i);
  64. if (strcmp(name, deviceInfo->name) == 0) {
  65. *device = i;
  66. *in_max = deviceInfo->maxInputChannels;
  67. return true;
  68. }
  69. }
  70. return false;
  71. }
  72. bool JackPortAudioDriver::GetOutputDeviceFromName(const char* name, PaDeviceIndex* device, int* out_max)
  73. {
  74. const PaDeviceInfo* deviceInfo;
  75. PaDeviceIndex numDevices = Pa_GetDeviceCount();
  76. for (int i = 0; i < numDevices; i++) {
  77. deviceInfo = Pa_GetDeviceInfo(i);
  78. if (strcmp(name, deviceInfo->name) == 0) {
  79. *device = i;
  80. *out_max = deviceInfo->maxOutputChannels;
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. static void DisplayDeviceNames()
  87. {
  88. PaError err;
  89. const PaDeviceInfo* deviceInfo;
  90. PaStreamParameters inputParameters, outputParameters;
  91. int defaultDisplayed;
  92. err = Pa_Initialize();
  93. if (err != paNoError)
  94. return ;
  95. PaDeviceIndex numDevices = Pa_GetDeviceCount();
  96. printf("Number of devices = %d\n", numDevices);
  97. for (int i = 0; i < numDevices; i++) {
  98. deviceInfo = Pa_GetDeviceInfo(i);
  99. printf( "--------------------------------------- device #%d\n", i );
  100. /* Mark global and API specific default devices */
  101. defaultDisplayed = 0;
  102. if (i == Pa_GetDefaultInputDevice()) {
  103. printf("[ Default Input");
  104. defaultDisplayed = 1;
  105. } else if (i == Pa_GetHostApiInfo(deviceInfo->hostApi)->defaultInputDevice) {
  106. const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo(deviceInfo->hostApi);
  107. printf("[ Default %s Input", hostInfo->name);
  108. defaultDisplayed = 1;
  109. }
  110. if (i == Pa_GetDefaultOutputDevice()) {
  111. printf((defaultDisplayed ? "," : "["));
  112. printf(" Default Output");
  113. defaultDisplayed = 1;
  114. } else if (i == Pa_GetHostApiInfo(deviceInfo->hostApi)->defaultOutputDevice) {
  115. const PaHostApiInfo *hostInfo = Pa_GetHostApiInfo(deviceInfo->hostApi);
  116. printf((defaultDisplayed ? "," : "["));
  117. printf(" Default %s Output", hostInfo->name);
  118. defaultDisplayed = 1;
  119. }
  120. if (defaultDisplayed)
  121. printf(" ]\n");
  122. /* print device info fields */
  123. printf("Name = %s\n", deviceInfo->name);
  124. printf("Host API = %s\n", Pa_GetHostApiInfo(deviceInfo->hostApi)->name);
  125. printf("Max inputs = %d", deviceInfo->maxInputChannels);
  126. printf(", Max outputs = %d\n", deviceInfo->maxOutputChannels);
  127. /*
  128. printf("Default low input latency = %8.3f\n", deviceInfo->defaultLowInputLatency);
  129. printf("Default low output latency = %8.3f\n", deviceInfo->defaultLowOutputLatency);
  130. printf("Default high input latency = %8.3f\n", deviceInfo->defaultHighInputLatency);
  131. printf("Default high output latency = %8.3f\n", deviceInfo->defaultHighOutputLatency);
  132. */
  133. #ifdef WIN32
  134. #ifndef PA_NO_ASIO
  135. /* ASIO specific latency information */
  136. if (Pa_GetHostApiInfo(deviceInfo->hostApi)->type == paASIO) {
  137. long minLatency, maxLatency, preferredLatency, granularity;
  138. err = PaAsio_GetAvailableLatencyValues(i, &minLatency, &maxLatency, &preferredLatency, &granularity);
  139. printf("ASIO minimum buffer size = %ld\n", minLatency);
  140. printf("ASIO maximum buffer size = %ld\n", maxLatency);
  141. printf("ASIO preferred buffer size = %ld\n", preferredLatency);
  142. if (granularity == -1)
  143. printf("ASIO buffer granularity = power of 2\n");
  144. else
  145. printf("ASIO buffer granularity = %ld\n", granularity);
  146. }
  147. #endif /* !PA_NO_ASIO */
  148. #endif /* WIN32 */
  149. printf("Default sample rate = %8.2f\n", deviceInfo->defaultSampleRate);
  150. /* poll for standard sample rates */
  151. inputParameters.device = i;
  152. inputParameters.channelCount = deviceInfo->maxInputChannels;
  153. inputParameters.sampleFormat = paInt16;
  154. inputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */
  155. inputParameters.hostApiSpecificStreamInfo = NULL;
  156. outputParameters.device = i;
  157. outputParameters.channelCount = deviceInfo->maxOutputChannels;
  158. outputParameters.sampleFormat = paInt16;
  159. outputParameters.suggestedLatency = 0; /* ignored by Pa_IsFormatSupported() */
  160. outputParameters.hostApiSpecificStreamInfo = NULL;
  161. /*
  162. if (inputParameters.channelCount > 0) {
  163. printf("Supported standard sample rates\n for half-duplex 16 bit %d channel input = \n", inputParameters.channelCount);
  164. PrintSupportedStandardSampleRates(&inputParameters, NULL);
  165. }
  166. if (outputParameters.channelCount > 0) {
  167. printf("Supported standard sample rates\n for half-duplex 16 bit %d channel output = \n", outputParameters.channelCount);
  168. PrintSupportedStandardSampleRates(NULL, &outputParameters);
  169. }
  170. if (inputParameters.channelCount > 0 && outputParameters.channelCount > 0) {
  171. printf("Supported standard sample rates\n for full-duplex 16 bit %d channel input, %d channel output = \n",
  172. inputParameters.channelCount, outputParameters.channelCount );
  173. PrintSupportedStandardSampleRates(&inputParameters, &outputParameters);
  174. }
  175. */
  176. }
  177. Pa_Terminate();
  178. }
  179. int JackPortAudioDriver::Render(const void* inputBuffer, void* outputBuffer,
  180. unsigned long framesPerBuffer,
  181. const PaStreamCallbackTimeInfo* timeInfo,
  182. PaStreamCallbackFlags statusFlags,
  183. void* userData)
  184. {
  185. JackPortAudioDriver* driver = (JackPortAudioDriver*)userData;
  186. driver->fLastWaitUst = GetMicroSeconds(); // Take callback date here
  187. driver->fInputBuffer = (float**)inputBuffer;
  188. driver->fOutputBuffer = (float**)outputBuffer;
  189. // Setup threadded based log function
  190. set_threaded_log_function();
  191. return (driver->Process() == 0) ? paContinue : paAbort;
  192. }
  193. int JackPortAudioDriver::Read()
  194. {
  195. for (int i = 0; i < fCaptureChannels; i++) {
  196. memcpy(GetInputBuffer(i), fInputBuffer[i], sizeof(float) * fEngineControl->fBufferSize);
  197. }
  198. return 0;
  199. }
  200. int JackPortAudioDriver::Write()
  201. {
  202. for (int i = 0; i < fPlaybackChannels; i++) {
  203. memcpy(fOutputBuffer[i], GetOutputBuffer(i), sizeof(float) * fEngineControl->fBufferSize);
  204. }
  205. return 0;
  206. }
  207. int JackPortAudioDriver::Open(jack_nframes_t nframes,
  208. jack_nframes_t samplerate,
  209. bool capturing,
  210. bool playing,
  211. int inchannels,
  212. int outchannels,
  213. bool monitor,
  214. const char* capture_driver_uid,
  215. const char* playback_driver_uid,
  216. jack_nframes_t capture_latency,
  217. jack_nframes_t playback_latency)
  218. {
  219. PaError err;
  220. PaStreamParameters inputParameters;
  221. PaStreamParameters outputParameters;
  222. const PaDeviceInfo* deviceInfo;
  223. int in_max = 0;
  224. int out_max = 0;
  225. jack_log("JackPortAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s samplerate = %ld",
  226. nframes, inchannels, outchannels, capture_driver_uid, playback_driver_uid, samplerate);
  227. // Generic JackAudioDriver Open
  228. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
  229. return -1;
  230. }
  231. err = Pa_Initialize();
  232. if (err != paNoError) {
  233. jack_error("JackPortAudioDriver::Pa_Initialize error = %s\n", Pa_GetErrorText(err));
  234. goto error;
  235. }
  236. jack_log("JackPortAudioDriver::Pa_GetDefaultInputDevice %ld", Pa_GetDefaultInputDevice());
  237. jack_log("JackPortAudioDriver::Pa_GetDefaultOutputDevice %ld", Pa_GetDefaultOutputDevice());
  238. if (capturing) {
  239. if (!GetInputDeviceFromName(capture_driver_uid, &fInputDevice, &in_max)) {
  240. jack_log("JackPortAudioDriver::GetInputDeviceFromName cannot open %s", capture_driver_uid);
  241. fInputDevice = Pa_GetDefaultInputDevice();
  242. if (fInputDevice == paNoDevice)
  243. goto error;
  244. deviceInfo = Pa_GetDeviceInfo(fInputDevice);
  245. in_max = deviceInfo->maxInputChannels;
  246. capture_driver_uid = strdup(deviceInfo->name);
  247. }
  248. }
  249. if (inchannels > in_max) {
  250. jack_error("This device hasn't required input channels inchannels = %ld in_max = %ld", inchannels, in_max);
  251. goto error;
  252. }
  253. if (playing) {
  254. if (!GetOutputDeviceFromName(playback_driver_uid, &fOutputDevice, &out_max)) {
  255. jack_log("JackPortAudioDriver::GetOutputDeviceFromName cannot open %s", playback_driver_uid);
  256. fOutputDevice = Pa_GetDefaultOutputDevice();
  257. if (fOutputDevice == paNoDevice)
  258. goto error;
  259. deviceInfo = Pa_GetDeviceInfo(fOutputDevice);
  260. out_max = deviceInfo->maxOutputChannels;
  261. playback_driver_uid = strdup(deviceInfo->name);
  262. }
  263. }
  264. if (outchannels > out_max) {
  265. jack_error("This device hasn't required output channels outchannels = %ld out_max = %ld", outchannels, out_max);
  266. goto error;
  267. }
  268. if (inchannels == 0) {
  269. jack_log("JackPortAudioDriver::Open setup max in channels = %ld", in_max);
  270. inchannels = in_max;
  271. }
  272. if (outchannels == 0) {
  273. jack_log("JackPortAudioDriver::Open setup max out channels = %ld", out_max);
  274. outchannels = out_max;
  275. }
  276. inputParameters.device = fInputDevice;
  277. inputParameters.channelCount = inchannels;
  278. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  279. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  280. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  281. : 0;
  282. inputParameters.hostApiSpecificStreamInfo = NULL;
  283. outputParameters.device = fOutputDevice;
  284. outputParameters.channelCount = outchannels;
  285. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  286. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  287. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  288. : 0;
  289. outputParameters.hostApiSpecificStreamInfo = NULL;
  290. err = Pa_OpenStream(&fStream,
  291. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  292. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  293. samplerate,
  294. nframes,
  295. paNoFlag, // Clipping is on...
  296. Render,
  297. this);
  298. if (err != paNoError) {
  299. jack_error("Pa_OpenStream error = %s\n", Pa_GetErrorText(err));
  300. goto error;
  301. }
  302. #ifdef __APPLE__
  303. fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000;
  304. fEngineControl->fComputation = 500 * 1000;
  305. fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
  306. #endif
  307. // Core driver may have changed the in/out values
  308. fCaptureChannels = inchannels;
  309. fPlaybackChannels = outchannels;
  310. assert(strlen(capture_driver_uid) < JACK_CLIENT_NAME_SIZE);
  311. assert(strlen(playback_driver_uid) < JACK_CLIENT_NAME_SIZE);
  312. strcpy(fCaptureDriverName, capture_driver_uid);
  313. strcpy(fPlaybackDriverName, playback_driver_uid);
  314. return 0;
  315. error:
  316. Pa_Terminate();
  317. return -1;
  318. }
  319. int JackPortAudioDriver::Close()
  320. {
  321. JackAudioDriver::Close();
  322. jack_log("JackPortAudioDriver::Close");
  323. Pa_CloseStream(fStream);
  324. Pa_Terminate();
  325. return 0;
  326. }
  327. int JackPortAudioDriver::Start()
  328. {
  329. jack_log("JackPortAudioDriver::Start");
  330. JackAudioDriver::Start();
  331. PaError err = Pa_StartStream(fStream);
  332. return (err == paNoError) ? 0 : -1;
  333. }
  334. int JackPortAudioDriver::Stop()
  335. {
  336. jack_log("JackPortAudioDriver::Stop");
  337. PaError err = Pa_StopStream(fStream);
  338. return (err == paNoError) ? 0 : -1;
  339. }
  340. int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  341. {
  342. PaError err;
  343. PaStreamParameters inputParameters;
  344. PaStreamParameters outputParameters;
  345. if ((err = Pa_CloseStream(fStream)) != paNoError) {
  346. jack_error("Pa_CloseStream error = %s\n", Pa_GetErrorText(err));
  347. return -1;
  348. }
  349. inputParameters.device = fInputDevice;
  350. inputParameters.channelCount = fCaptureChannels;
  351. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  352. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  353. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  354. : 0;
  355. inputParameters.hostApiSpecificStreamInfo = NULL;
  356. outputParameters.device = fOutputDevice;
  357. outputParameters.channelCount = fPlaybackChannels;
  358. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  359. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  360. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  361. : 0;
  362. outputParameters.hostApiSpecificStreamInfo = NULL;
  363. err = Pa_OpenStream(&fStream,
  364. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  365. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  366. fEngineControl->fSampleRate,
  367. buffer_size,
  368. paNoFlag, // Clipping is on...
  369. Render,
  370. this);
  371. if (err != paNoError) {
  372. jack_error("Pa_OpenStream error = %s\n", Pa_GetErrorText(err));
  373. return -1;
  374. } else {
  375. // Only done when success
  376. return JackAudioDriver::SetBufferSize(buffer_size); // never fails
  377. }
  378. }
  379. } // end of namespace
  380. #ifdef __cplusplus
  381. extern "C"
  382. {
  383. #endif
  384. #include "JackExports.h"
  385. EXPORT jack_driver_desc_t* driver_get_descriptor() {
  386. jack_driver_desc_t *desc;
  387. unsigned int i;
  388. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  389. strcpy(desc->name, "portaudio");
  390. desc->nparams = 13;
  391. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  392. i = 0;
  393. strcpy(desc->params[i].name, "channels");
  394. desc->params[i].character = 'c';
  395. desc->params[i].type = JackDriverParamInt;
  396. desc->params[i].value.ui = 0;
  397. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  398. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  399. i++;
  400. strcpy(desc->params[i].name, "inchannels");
  401. desc->params[i].character = 'i';
  402. desc->params[i].type = JackDriverParamInt;
  403. desc->params[i].value.ui = 0;
  404. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  405. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  406. i++;
  407. strcpy(desc->params[i].name, "outchannels");
  408. desc->params[i].character = 'o';
  409. desc->params[i].type = JackDriverParamInt;
  410. desc->params[i].value.ui = 0;
  411. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  412. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  413. i++;
  414. strcpy(desc->params[i].name, "capture");
  415. desc->params[i].character = 'C';
  416. desc->params[i].type = JackDriverParamString;
  417. strcpy(desc->params[i].value.str, "will take default PortAudio input device");
  418. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set PortAudio device name");
  419. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  420. i++;
  421. strcpy(desc->params[i].name, "playback");
  422. desc->params[i].character = 'P';
  423. desc->params[i].type = JackDriverParamString;
  424. strcpy(desc->params[i].value.str, "will take default PortAudio output device");
  425. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set PortAudio device name");
  426. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  427. i++;
  428. strcpy (desc->params[i].name, "monitor");
  429. desc->params[i].character = 'm';
  430. desc->params[i].type = JackDriverParamBool;
  431. desc->params[i].value.i = 0;
  432. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  433. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  434. i++;
  435. strcpy(desc->params[i].name, "duplex");
  436. desc->params[i].character = 'D';
  437. desc->params[i].type = JackDriverParamBool;
  438. desc->params[i].value.i = TRUE;
  439. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  440. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  441. i++;
  442. strcpy(desc->params[i].name, "rate");
  443. desc->params[i].character = 'r';
  444. desc->params[i].type = JackDriverParamUInt;
  445. desc->params[i].value.ui = 44100U;
  446. strcpy(desc->params[i].short_desc, "Sample rate");
  447. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  448. i++;
  449. strcpy(desc->params[i].name, "period");
  450. desc->params[i].character = 'p';
  451. desc->params[i].type = JackDriverParamUInt;
  452. desc->params[i].value.ui = 128U;
  453. strcpy(desc->params[i].short_desc, "Frames per period");
  454. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  455. i++;
  456. strcpy(desc->params[i].name, "device");
  457. desc->params[i].character = 'd';
  458. desc->params[i].type = JackDriverParamString;
  459. desc->params[i].value.ui = 128U;
  460. strcpy(desc->params[i].value.str, "will take default PortAudio device name");
  461. strcpy(desc->params[i].short_desc, "PortAudio device name");
  462. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  463. i++;
  464. strcpy(desc->params[i].name, "input-latency");
  465. desc->params[i].character = 'I';
  466. desc->params[i].type = JackDriverParamUInt;
  467. desc->params[i].value.i = 0;
  468. strcpy(desc->params[i].short_desc, "Extra input latency");
  469. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  470. i++;
  471. strcpy(desc->params[i].name, "output-latency");
  472. desc->params[i].character = 'O';
  473. desc->params[i].type = JackDriverParamUInt;
  474. desc->params[i].value.i = 0;
  475. strcpy(desc->params[i].short_desc, "Extra output latency");
  476. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  477. i++;
  478. strcpy(desc->params[i].name, "list-devices");
  479. desc->params[i].character = 'l';
  480. desc->params[i].type = JackDriverParamBool;
  481. desc->params[i].value.i = TRUE;
  482. strcpy(desc->params[i].short_desc, "Display available PortAudio devices");
  483. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  484. return desc;
  485. }
  486. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngineInterface* engine, Jack::JackSynchro** table, const JSList* params) {
  487. jack_nframes_t srate = 44100;
  488. jack_nframes_t frames_per_interrupt = 512;
  489. int capture = FALSE;
  490. int playback = FALSE;
  491. int chan_in = 0;
  492. int chan_out = 0;
  493. bool monitor = false;
  494. char* capture_pcm_name = "winmme";
  495. char* playback_pcm_name = "winmme";
  496. const JSList *node;
  497. const jack_driver_param_t *param;
  498. jack_nframes_t systemic_input_latency = 0;
  499. jack_nframes_t systemic_output_latency = 0;
  500. for (node = params; node; node = jack_slist_next(node)) {
  501. param = (const jack_driver_param_t *) node->data;
  502. switch (param->character) {
  503. case 'd':
  504. capture_pcm_name = strdup(param->value.str);
  505. playback_pcm_name = strdup(param->value.str);
  506. break;
  507. case 'D':
  508. capture = TRUE;
  509. playback = TRUE;
  510. break;
  511. case 'c':
  512. chan_in = chan_out = (int) param->value.ui;
  513. break;
  514. case 'i':
  515. chan_in = (int) param->value.ui;
  516. break;
  517. case 'o':
  518. chan_out = (int) param->value.ui;
  519. break;
  520. case 'C':
  521. capture = TRUE;
  522. if (strcmp(param->value.str, "none") != 0) {
  523. capture_pcm_name = strdup(param->value.str);
  524. }
  525. break;
  526. case 'P':
  527. playback = TRUE;
  528. if (strcmp(param->value.str, "none") != 0) {
  529. playback_pcm_name = strdup(param->value.str);
  530. }
  531. break;
  532. case 'm':
  533. monitor = param->value.i;
  534. break;
  535. case 'r':
  536. srate = param->value.ui;
  537. break;
  538. case 'p':
  539. frames_per_interrupt = (unsigned int) param->value.ui;
  540. break;
  541. case 'I':
  542. systemic_input_latency = param->value.ui;
  543. break;
  544. case 'O':
  545. systemic_output_latency = param->value.ui;
  546. break;
  547. case 'l':
  548. Jack::DisplayDeviceNames();
  549. break;
  550. }
  551. }
  552. // duplex is the default
  553. if (!capture && !playback) {
  554. capture = TRUE;
  555. playback = TRUE;
  556. }
  557. Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table);
  558. 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) {
  559. return driver;
  560. } else {
  561. delete driver;
  562. return NULL;
  563. }
  564. }
  565. #ifdef __cplusplus
  566. }
  567. #endif