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.

491 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 = (jack_default_audio_sample_t**)inputBuffer;
  35. driver->fOutputBuffer = (jack_default_audio_sample_t**)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(jack_default_audio_sample_t) * 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(jack_default_audio_sample_t) * 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. // Generic audio driver close
  158. int res = JackAudioDriver::Close();
  159. jack_log("JackPortAudioDriver::Close");
  160. Pa_CloseStream(fStream);
  161. return res;
  162. }
  163. int JackPortAudioDriver::Start()
  164. {
  165. jack_log("JackPortAudioDriver::Start");
  166. JackAudioDriver::Start();
  167. PaError err = Pa_StartStream(fStream);
  168. return (err == paNoError) ? 0 : -1;
  169. }
  170. int JackPortAudioDriver::Stop()
  171. {
  172. jack_log("JackPortAudioDriver::Stop");
  173. PaError err = Pa_StopStream(fStream);
  174. return (err == paNoError) ? 0 : -1;
  175. }
  176. int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  177. {
  178. PaError err;
  179. PaStreamParameters inputParameters;
  180. PaStreamParameters outputParameters;
  181. if ((err = Pa_CloseStream(fStream)) != paNoError)
  182. {
  183. jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
  184. return -1;
  185. }
  186. //change parametering
  187. inputParameters.device = fInputDevice;
  188. inputParameters.channelCount = fCaptureChannels;
  189. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  190. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  191. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  192. : 0;
  193. inputParameters.hostApiSpecificStreamInfo = NULL;
  194. outputParameters.device = fOutputDevice;
  195. outputParameters.channelCount = fPlaybackChannels;
  196. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  197. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  198. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  199. : 0;
  200. outputParameters.hostApiSpecificStreamInfo = NULL;
  201. err = Pa_OpenStream(&fStream,
  202. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  203. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  204. fEngineControl->fSampleRate,
  205. buffer_size,
  206. paNoFlag, // Clipping is on...
  207. Render,
  208. this);
  209. if (err != paNoError)
  210. {
  211. jack_error("Pa_OpenStream error = %s", Pa_GetErrorText(err));
  212. return -1;
  213. }
  214. else
  215. {
  216. // Only done when success
  217. return JackAudioDriver::SetBufferSize(buffer_size); // never fails
  218. }
  219. }
  220. } // end of namespace
  221. #ifdef __cplusplus
  222. extern "C"
  223. {
  224. #endif
  225. #include "JackCompilerDeps.h"
  226. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  227. {
  228. jack_driver_desc_t *desc;
  229. unsigned int i;
  230. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  231. strcpy(desc->name, "portaudio"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  232. strcpy(desc->desc, "PortAudio API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  233. desc->nparams = 13;
  234. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  235. i = 0;
  236. strcpy(desc->params[i].name, "channels");
  237. desc->params[i].character = 'c';
  238. desc->params[i].type = JackDriverParamInt;
  239. desc->params[i].value.ui = 0;
  240. strcpy(desc->params[i].short_desc, "Maximum number of channels");
  241. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  242. i++;
  243. strcpy(desc->params[i].name, "inchannels");
  244. desc->params[i].character = 'i';
  245. desc->params[i].type = JackDriverParamInt;
  246. desc->params[i].value.ui = 0;
  247. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  248. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  249. i++;
  250. strcpy(desc->params[i].name, "outchannels");
  251. desc->params[i].character = 'o';
  252. desc->params[i].type = JackDriverParamInt;
  253. desc->params[i].value.ui = 0;
  254. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  255. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  256. i++;
  257. strcpy(desc->params[i].name, "capture");
  258. desc->params[i].character = 'C';
  259. desc->params[i].type = JackDriverParamString;
  260. strcpy(desc->params[i].value.str, "will take default PortAudio input device");
  261. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set PortAudio device name");
  262. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  263. i++;
  264. strcpy(desc->params[i].name, "playback");
  265. desc->params[i].character = 'P';
  266. desc->params[i].type = JackDriverParamString;
  267. strcpy(desc->params[i].value.str, "will take default PortAudio output device");
  268. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set PortAudio device name");
  269. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  270. i++;
  271. strcpy (desc->params[i].name, "monitor");
  272. desc->params[i].character = 'm';
  273. desc->params[i].type = JackDriverParamBool;
  274. desc->params[i].value.i = 0;
  275. strcpy(desc->params[i].short_desc, "Provide monitor ports for the output");
  276. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  277. i++;
  278. strcpy(desc->params[i].name, "duplex");
  279. desc->params[i].character = 'D';
  280. desc->params[i].type = JackDriverParamBool;
  281. desc->params[i].value.i = TRUE;
  282. strcpy(desc->params[i].short_desc, "Provide both capture and playback ports");
  283. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  284. i++;
  285. strcpy(desc->params[i].name, "rate");
  286. desc->params[i].character = 'r';
  287. desc->params[i].type = JackDriverParamUInt;
  288. desc->params[i].value.ui = 44100U;
  289. strcpy(desc->params[i].short_desc, "Sample rate");
  290. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  291. i++;
  292. strcpy(desc->params[i].name, "period");
  293. desc->params[i].character = 'p';
  294. desc->params[i].type = JackDriverParamUInt;
  295. desc->params[i].value.ui = 128U;
  296. strcpy(desc->params[i].short_desc, "Frames per period");
  297. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  298. i++;
  299. strcpy(desc->params[i].name, "device");
  300. desc->params[i].character = 'd';
  301. desc->params[i].type = JackDriverParamString;
  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. bool capture = false;
  335. bool 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 = param->value.str;
  351. playback_pcm_name = 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. capture_pcm_name = param->value.str;
  370. }
  371. break;
  372. case 'P':
  373. playback = TRUE;
  374. if (strcmp(param->value.str, "none") != 0) {
  375. playback_pcm_name = param->value.str;
  376. }
  377. break;
  378. case 'm':
  379. monitor = param->value.i;
  380. break;
  381. case 'r':
  382. srate = param->value.ui;
  383. break;
  384. case 'p':
  385. frames_per_interrupt = (unsigned int)param->value.ui;
  386. break;
  387. case 'I':
  388. systemic_input_latency = param->value.ui;
  389. break;
  390. case 'O':
  391. systemic_output_latency = param->value.ui;
  392. break;
  393. case 'l':
  394. pa_devices->DisplayDevicesNames();
  395. break;
  396. }
  397. }
  398. // duplex is the default
  399. if (!capture && !playback) {
  400. capture = true;
  401. playback = true;
  402. }
  403. Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table, pa_devices);
  404. 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)
  405. {
  406. return driver;
  407. }
  408. else
  409. {
  410. delete driver;
  411. return NULL;
  412. }
  413. }
  414. #ifdef __cplusplus
  415. }
  416. #endif