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.

674 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. driver->fEngineControl->CycleIncTime(driver->fLastWaitUst);
  192. return (driver->Process() == 0) ? paContinue : paAbort;
  193. }
  194. int JackPortAudioDriver::Read()
  195. {
  196. for (int i = 0; i < fCaptureChannels; i++) {
  197. memcpy(GetInputBuffer(i), fInputBuffer[i], sizeof(float) * fEngineControl->fBufferSize);
  198. }
  199. return 0;
  200. }
  201. int JackPortAudioDriver::Write()
  202. {
  203. for (int i = 0; i < fPlaybackChannels; i++) {
  204. memcpy(fOutputBuffer[i], GetOutputBuffer(i), sizeof(float) * fEngineControl->fBufferSize);
  205. }
  206. return 0;
  207. }
  208. int JackPortAudioDriver::Open(jack_nframes_t nframes,
  209. jack_nframes_t samplerate,
  210. bool capturing,
  211. bool playing,
  212. int inchannels,
  213. int outchannels,
  214. bool monitor,
  215. const char* capture_driver_uid,
  216. const char* playback_driver_uid,
  217. jack_nframes_t capture_latency,
  218. jack_nframes_t playback_latency)
  219. {
  220. PaError err;
  221. PaStreamParameters inputParameters;
  222. PaStreamParameters outputParameters;
  223. const PaDeviceInfo* deviceInfo;
  224. int in_max = 0;
  225. int out_max = 0;
  226. jack_log("JackPortAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s samplerate = %ld",
  227. nframes, inchannels, outchannels, capture_driver_uid, playback_driver_uid, samplerate);
  228. // Generic JackAudioDriver Open
  229. if (JackAudioDriver::Open(nframes, samplerate, capturing, playing, inchannels, outchannels, monitor, capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
  230. return -1;
  231. }
  232. err = Pa_Initialize();
  233. if (err != paNoError) {
  234. jack_error("JackPortAudioDriver::Pa_Initialize error = %s\n", Pa_GetErrorText(err));
  235. goto error;
  236. }
  237. jack_log("JackPortAudioDriver::Pa_GetDefaultInputDevice %ld", Pa_GetDefaultInputDevice());
  238. jack_log("JackPortAudioDriver::Pa_GetDefaultOutputDevice %ld", Pa_GetDefaultOutputDevice());
  239. if (capturing) {
  240. if (!GetInputDeviceFromName(capture_driver_uid, &fInputDevice, &in_max)) {
  241. jack_log("JackPortAudioDriver::GetInputDeviceFromName cannot open %s", capture_driver_uid);
  242. fInputDevice = Pa_GetDefaultInputDevice();
  243. if (fInputDevice == paNoDevice)
  244. goto error;
  245. deviceInfo = Pa_GetDeviceInfo(fInputDevice);
  246. in_max = deviceInfo->maxInputChannels;
  247. capture_driver_uid = strdup(deviceInfo->name);
  248. }
  249. }
  250. if (inchannels > in_max) {
  251. jack_error("This device hasn't required input channels inchannels = %ld in_max = %ld", inchannels, in_max);
  252. goto error;
  253. }
  254. if (playing) {
  255. if (!GetOutputDeviceFromName(playback_driver_uid, &fOutputDevice, &out_max)) {
  256. jack_log("JackPortAudioDriver::GetOutputDeviceFromName cannot open %s", playback_driver_uid);
  257. fOutputDevice = Pa_GetDefaultOutputDevice();
  258. if (fOutputDevice == paNoDevice)
  259. goto error;
  260. deviceInfo = Pa_GetDeviceInfo(fOutputDevice);
  261. out_max = deviceInfo->maxOutputChannels;
  262. playback_driver_uid = strdup(deviceInfo->name);
  263. }
  264. }
  265. if (outchannels > out_max) {
  266. jack_error("This device hasn't required output channels outchannels = %ld out_max = %ld", outchannels, out_max);
  267. goto error;
  268. }
  269. if (inchannels == 0) {
  270. jack_log("JackPortAudioDriver::Open setup max in channels = %ld", in_max);
  271. inchannels = in_max;
  272. }
  273. if (outchannels == 0) {
  274. jack_log("JackPortAudioDriver::Open setup max out channels = %ld", out_max);
  275. outchannels = out_max;
  276. }
  277. inputParameters.device = fInputDevice;
  278. inputParameters.channelCount = inchannels;
  279. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  280. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  281. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  282. : 0;
  283. inputParameters.hostApiSpecificStreamInfo = NULL;
  284. outputParameters.device = fOutputDevice;
  285. outputParameters.channelCount = outchannels;
  286. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  287. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  288. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  289. : 0;
  290. outputParameters.hostApiSpecificStreamInfo = NULL;
  291. err = Pa_OpenStream(&fStream,
  292. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  293. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  294. samplerate,
  295. nframes,
  296. paNoFlag, // Clipping is on...
  297. Render,
  298. this);
  299. if (err != paNoError) {
  300. jack_error("Pa_OpenStream error = %s\n", Pa_GetErrorText(err));
  301. goto error;
  302. }
  303. #ifdef __APPLE__
  304. fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000;
  305. fEngineControl->fComputation = 500 * 1000;
  306. fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
  307. #endif
  308. // Core driver may have changed the in/out values
  309. fCaptureChannels = inchannels;
  310. fPlaybackChannels = outchannels;
  311. assert(strlen(capture_driver_uid) < JACK_CLIENT_NAME_SIZE);
  312. assert(strlen(playback_driver_uid) < JACK_CLIENT_NAME_SIZE);
  313. strcpy(fCaptureDriverName, capture_driver_uid);
  314. strcpy(fPlaybackDriverName, playback_driver_uid);
  315. return 0;
  316. error:
  317. Pa_Terminate();
  318. return -1;
  319. }
  320. int JackPortAudioDriver::Close()
  321. {
  322. JackAudioDriver::Close();
  323. jack_log("JackPortAudioDriver::Close");
  324. Pa_CloseStream(fStream);
  325. Pa_Terminate();
  326. return 0;
  327. }
  328. int JackPortAudioDriver::Start()
  329. {
  330. jack_log("JackPortAudioDriver::Start");
  331. JackAudioDriver::Start();
  332. PaError err = Pa_StartStream(fStream);
  333. return (err == paNoError) ? 0 : -1;
  334. }
  335. int JackPortAudioDriver::Stop()
  336. {
  337. jack_log("JackPortAudioDriver::Stop");
  338. PaError err = Pa_StopStream(fStream);
  339. return (err == paNoError) ? 0 : -1;
  340. }
  341. int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  342. {
  343. PaError err;
  344. PaStreamParameters inputParameters;
  345. PaStreamParameters outputParameters;
  346. if ((err = Pa_CloseStream(fStream)) != paNoError) {
  347. jack_error("Pa_CloseStream error = %s\n", Pa_GetErrorText(err));
  348. return -1;
  349. }
  350. inputParameters.device = fInputDevice;
  351. inputParameters.channelCount = fCaptureChannels;
  352. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  353. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  354. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  355. : 0;
  356. inputParameters.hostApiSpecificStreamInfo = NULL;
  357. outputParameters.device = fOutputDevice;
  358. outputParameters.channelCount = fPlaybackChannels;
  359. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  360. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  361. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  362. : 0;
  363. outputParameters.hostApiSpecificStreamInfo = NULL;
  364. err = Pa_OpenStream(&fStream,
  365. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  366. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  367. fEngineControl->fSampleRate,
  368. buffer_size,
  369. paNoFlag, // Clipping is on...
  370. Render,
  371. this);
  372. if (err != paNoError) {
  373. jack_error("Pa_OpenStream error = %s\n", Pa_GetErrorText(err));
  374. return -1;
  375. } else {
  376. // Only done when success
  377. return JackAudioDriver::SetBufferSize(buffer_size); // never fails
  378. }
  379. }
  380. } // end of namespace
  381. #ifdef __cplusplus
  382. extern "C"
  383. {
  384. #endif
  385. #include "JackExports.h"
  386. EXPORT jack_driver_desc_t* driver_get_descriptor() {
  387. jack_driver_desc_t *desc;
  388. unsigned int i;
  389. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  390. strcpy(desc->name, "portaudio");
  391. desc->nparams = 13;
  392. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  393. i = 0;
  394. strcpy(desc->params[i].name, "channels");
  395. desc->params[i].character = 'c';
  396. desc->params[i].type = JackDriverParamInt;
  397. desc->params[i].value.ui = 0;
  398. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  399. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  400. i++;
  401. strcpy(desc->params[i].name, "inchannels");
  402. desc->params[i].character = 'i';
  403. desc->params[i].type = JackDriverParamInt;
  404. desc->params[i].value.ui = 0;
  405. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  406. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  407. i++;
  408. strcpy(desc->params[i].name, "outchannels");
  409. desc->params[i].character = 'o';
  410. desc->params[i].type = JackDriverParamInt;
  411. desc->params[i].value.ui = 0;
  412. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  413. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  414. i++;
  415. strcpy(desc->params[i].name, "capture");
  416. desc->params[i].character = 'C';
  417. desc->params[i].type = JackDriverParamString;
  418. strcpy(desc->params[i].value.str, "will take default PortAudio input device");
  419. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set PortAudio device name");
  420. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  421. i++;
  422. strcpy(desc->params[i].name, "playback");
  423. desc->params[i].character = 'P';
  424. desc->params[i].type = JackDriverParamString;
  425. strcpy(desc->params[i].value.str, "will take default PortAudio output device");
  426. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set PortAudio device name");
  427. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  428. i++;
  429. strcpy (desc->params[i].name, "monitor");
  430. desc->params[i].character = 'm';
  431. desc->params[i].type = JackDriverParamBool;
  432. desc->params[i].value.i = 0;
  433. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  434. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  435. i++;
  436. strcpy(desc->params[i].name, "duplex");
  437. desc->params[i].character = 'D';
  438. desc->params[i].type = JackDriverParamBool;
  439. desc->params[i].value.i = TRUE;
  440. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  441. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  442. i++;
  443. strcpy(desc->params[i].name, "rate");
  444. desc->params[i].character = 'r';
  445. desc->params[i].type = JackDriverParamUInt;
  446. desc->params[i].value.ui = 44100U;
  447. strcpy(desc->params[i].short_desc, "Sample rate");
  448. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  449. i++;
  450. strcpy(desc->params[i].name, "period");
  451. desc->params[i].character = 'p';
  452. desc->params[i].type = JackDriverParamUInt;
  453. desc->params[i].value.ui = 128U;
  454. strcpy(desc->params[i].short_desc, "Frames per period");
  455. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  456. i++;
  457. strcpy(desc->params[i].name, "device");
  458. desc->params[i].character = 'd';
  459. desc->params[i].type = JackDriverParamString;
  460. desc->params[i].value.ui = 128U;
  461. strcpy(desc->params[i].value.str, "will take default PortAudio device name");
  462. strcpy(desc->params[i].short_desc, "PortAudio device name");
  463. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  464. i++;
  465. strcpy(desc->params[i].name, "input-latency");
  466. desc->params[i].character = 'I';
  467. desc->params[i].type = JackDriverParamUInt;
  468. desc->params[i].value.i = 0;
  469. strcpy(desc->params[i].short_desc, "Extra input latency");
  470. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  471. i++;
  472. strcpy(desc->params[i].name, "output-latency");
  473. desc->params[i].character = 'O';
  474. desc->params[i].type = JackDriverParamUInt;
  475. desc->params[i].value.i = 0;
  476. strcpy(desc->params[i].short_desc, "Extra output latency");
  477. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  478. i++;
  479. strcpy(desc->params[i].name, "list-devices");
  480. desc->params[i].character = 'l';
  481. desc->params[i].type = JackDriverParamBool;
  482. desc->params[i].value.i = TRUE;
  483. strcpy(desc->params[i].short_desc, "Display available PortAudio devices");
  484. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  485. return desc;
  486. }
  487. EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackEngineInterface* engine, Jack::JackSynchro** table, const JSList* params) {
  488. jack_nframes_t srate = 44100;
  489. jack_nframes_t frames_per_interrupt = 512;
  490. int capture = FALSE;
  491. int playback = FALSE;
  492. int chan_in = 0;
  493. int chan_out = 0;
  494. bool monitor = false;
  495. char* capture_pcm_name = "winmme";
  496. char* playback_pcm_name = "winmme";
  497. const JSList *node;
  498. const jack_driver_param_t *param;
  499. jack_nframes_t systemic_input_latency = 0;
  500. jack_nframes_t systemic_output_latency = 0;
  501. for (node = params; node; node = jack_slist_next(node)) {
  502. param = (const jack_driver_param_t *) node->data;
  503. switch (param->character) {
  504. case 'd':
  505. capture_pcm_name = strdup(param->value.str);
  506. playback_pcm_name = strdup(param->value.str);
  507. break;
  508. case 'D':
  509. capture = TRUE;
  510. playback = TRUE;
  511. break;
  512. case 'c':
  513. chan_in = chan_out = (int) param->value.ui;
  514. break;
  515. case 'i':
  516. chan_in = (int) param->value.ui;
  517. break;
  518. case 'o':
  519. chan_out = (int) param->value.ui;
  520. break;
  521. case 'C':
  522. capture = TRUE;
  523. if (strcmp(param->value.str, "none") != 0) {
  524. capture_pcm_name = strdup(param->value.str);
  525. }
  526. break;
  527. case 'P':
  528. playback = TRUE;
  529. if (strcmp(param->value.str, "none") != 0) {
  530. playback_pcm_name = strdup(param->value.str);
  531. }
  532. break;
  533. case 'm':
  534. monitor = param->value.i;
  535. break;
  536. case 'r':
  537. srate = param->value.ui;
  538. break;
  539. case 'p':
  540. frames_per_interrupt = (unsigned int) param->value.ui;
  541. break;
  542. case 'I':
  543. systemic_input_latency = param->value.ui;
  544. break;
  545. case 'O':
  546. systemic_output_latency = param->value.ui;
  547. break;
  548. case 'l':
  549. Jack::DisplayDeviceNames();
  550. break;
  551. }
  552. }
  553. // duplex is the default
  554. if (!capture && !playback) {
  555. capture = TRUE;
  556. playback = TRUE;
  557. }
  558. Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table);
  559. 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) {
  560. return driver;
  561. } else {
  562. delete driver;
  563. return NULL;
  564. }
  565. }
  566. #ifdef __cplusplus
  567. }
  568. #endif