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.

500 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. if (JackAudioDriver::Start() >= 0) {
  167. PaError err = Pa_StartStream(fStream);
  168. if (err == paNoError) {
  169. return 0;
  170. }
  171. JackAudioDriver::Stop();
  172. }
  173. return -1;
  174. }
  175. int JackPortAudioDriver::Stop()
  176. {
  177. jack_log("JackPortAudioDriver::Stop");
  178. PaError err = Pa_StopStream(fStream);
  179. int res = (err == paNoError) ? 0 : -1;
  180. if (JackAudioDriver::Stop() < 0) {
  181. res = -1;
  182. }
  183. return res;
  184. }
  185. int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  186. {
  187. PaError err;
  188. PaStreamParameters inputParameters;
  189. PaStreamParameters outputParameters;
  190. if ((err = Pa_CloseStream(fStream)) != paNoError)
  191. {
  192. jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
  193. return -1;
  194. }
  195. //change parametering
  196. inputParameters.device = fInputDevice;
  197. inputParameters.channelCount = fCaptureChannels;
  198. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  199. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  200. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  201. : 0;
  202. inputParameters.hostApiSpecificStreamInfo = NULL;
  203. outputParameters.device = fOutputDevice;
  204. outputParameters.channelCount = fPlaybackChannels;
  205. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  206. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  207. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  208. : 0;
  209. outputParameters.hostApiSpecificStreamInfo = NULL;
  210. err = Pa_OpenStream(&fStream,
  211. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  212. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  213. fEngineControl->fSampleRate,
  214. buffer_size,
  215. paNoFlag, // Clipping is on...
  216. Render,
  217. this);
  218. if (err != paNoError)
  219. {
  220. jack_error("Pa_OpenStream error = %s", Pa_GetErrorText(err));
  221. return -1;
  222. }
  223. else
  224. {
  225. // Only done when success
  226. return JackAudioDriver::SetBufferSize(buffer_size); // never fails
  227. }
  228. }
  229. } // end of namespace
  230. #ifdef __cplusplus
  231. extern "C"
  232. {
  233. #endif
  234. #include "JackCompilerDeps.h"
  235. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  236. {
  237. jack_driver_desc_t *desc;
  238. unsigned int i;
  239. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  240. strcpy(desc->name, "portaudio"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  241. strcpy(desc->desc, "PortAudio API based audio backend"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  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. strcpy(desc->params[i].value.str, "will take default PortAudio device name");
  312. strcpy(desc->params[i].short_desc, "PortAudio device name");
  313. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  314. i++;
  315. strcpy(desc->params[i].name, "input-latency");
  316. desc->params[i].character = 'I';
  317. desc->params[i].type = JackDriverParamUInt;
  318. desc->params[i].value.i = 0;
  319. strcpy(desc->params[i].short_desc, "Extra input latency");
  320. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  321. i++;
  322. strcpy(desc->params[i].name, "output-latency");
  323. desc->params[i].character = 'O';
  324. desc->params[i].type = JackDriverParamUInt;
  325. desc->params[i].value.i = 0;
  326. strcpy(desc->params[i].short_desc, "Extra output latency");
  327. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  328. i++;
  329. strcpy(desc->params[i].name, "list-devices");
  330. desc->params[i].character = 'l';
  331. desc->params[i].type = JackDriverParamBool;
  332. desc->params[i].value.i = TRUE;
  333. strcpy(desc->params[i].short_desc, "Display available PortAudio devices");
  334. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  335. return desc;
  336. }
  337. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  338. {
  339. jack_nframes_t srate = 44100;
  340. jack_nframes_t frames_per_interrupt = 512;
  341. const char* capture_pcm_name = "";
  342. const char* playback_pcm_name = "";
  343. bool capture = false;
  344. bool playback = false;
  345. int chan_in = 0;
  346. int chan_out = 0;
  347. bool monitor = false;
  348. const JSList *node;
  349. const jack_driver_param_t *param;
  350. jack_nframes_t systemic_input_latency = 0;
  351. jack_nframes_t systemic_output_latency = 0;
  352. PortAudioDevices* pa_devices = new PortAudioDevices();
  353. for (node = params; node; node = jack_slist_next(node))
  354. {
  355. param = (const jack_driver_param_t *) node->data;
  356. switch (param->character)
  357. {
  358. case 'd':
  359. capture_pcm_name = param->value.str;
  360. playback_pcm_name = param->value.str;
  361. break;
  362. case 'D':
  363. capture = true;
  364. playback = true;
  365. break;
  366. case 'c':
  367. chan_in = chan_out = (int)param->value.ui;
  368. break;
  369. case 'i':
  370. chan_in = (int)param->value.ui;
  371. break;
  372. case 'o':
  373. chan_out = (int)param->value.ui;
  374. break;
  375. case 'C':
  376. capture = true;
  377. if (strcmp(param->value.str, "none") != 0) {
  378. capture_pcm_name = param->value.str;
  379. }
  380. break;
  381. case 'P':
  382. playback = TRUE;
  383. if (strcmp(param->value.str, "none") != 0) {
  384. playback_pcm_name = param->value.str;
  385. }
  386. break;
  387. case 'm':
  388. monitor = param->value.i;
  389. break;
  390. case 'r':
  391. srate = param->value.ui;
  392. break;
  393. case 'p':
  394. frames_per_interrupt = (unsigned int)param->value.ui;
  395. break;
  396. case 'I':
  397. systemic_input_latency = param->value.ui;
  398. break;
  399. case 'O':
  400. systemic_output_latency = param->value.ui;
  401. break;
  402. case 'l':
  403. pa_devices->DisplayDevicesNames();
  404. break;
  405. }
  406. }
  407. // duplex is the default
  408. if (!capture && !playback) {
  409. capture = true;
  410. playback = true;
  411. }
  412. Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table, pa_devices);
  413. 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)
  414. {
  415. return driver;
  416. }
  417. else
  418. {
  419. delete driver;
  420. return NULL;
  421. }
  422. }
  423. #ifdef __cplusplus
  424. }
  425. #endif