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.

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