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. if (fPaDevices->GetInputDeviceFromName(capture_driver_uid, fInputDevice, in_max) < 0)
  78. goto error;
  79. }
  80. if (playing) {
  81. if (fPaDevices->GetOutputDeviceFromName(playback_driver_uid, fOutputDevice, out_max) < 0)
  82. goto error;
  83. }
  84. jack_log("JackPortAudioDriver::Open fInputDevice = %d, fOutputDevice %d", fInputDevice, fOutputDevice);
  85. //default channels number required
  86. if (inchannels == 0) {
  87. jack_log("JackPortAudioDriver::Open setup max in channels = %ld", in_max);
  88. inchannels = in_max;
  89. }
  90. if (outchannels == 0) {
  91. jack_log("JackPortAudioDriver::Open setup max out channels = %ld", out_max);
  92. outchannels = out_max;
  93. }
  94. //too many channels required, take max available
  95. if (inchannels > in_max) {
  96. jack_error("This device has only %d available input channels.", in_max);
  97. inchannels = in_max;
  98. }
  99. if (outchannels > out_max) {
  100. jack_error("This device has only %d available output channels.", out_max);
  101. outchannels = out_max;
  102. }
  103. //in/out streams parametering
  104. inputParameters.device = fInputDevice;
  105. inputParameters.channelCount = inchannels;
  106. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  107. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  108. ? fPaDevices->GetDeviceInfo(fInputDevice)->defaultLowInputLatency
  109. : 0;
  110. inputParameters.hostApiSpecificStreamInfo = NULL;
  111. outputParameters.device = fOutputDevice;
  112. outputParameters.channelCount = outchannels;
  113. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  114. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  115. ? fPaDevices->GetDeviceInfo(fOutputDevice)->defaultLowOutputLatency
  116. : 0;
  117. outputParameters.hostApiSpecificStreamInfo = NULL;
  118. err = Pa_OpenStream(&fStream,
  119. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  120. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  121. samplerate,
  122. buffer_size,
  123. paNoFlag, // Clipping is on...
  124. Render,
  125. this);
  126. if (err != paNoError) {
  127. jack_error("Pa_OpenStream error = %s", Pa_GetErrorText(err));
  128. goto error;
  129. }
  130. #ifdef __APPLE__
  131. fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000;
  132. fEngineControl->fComputation = 500 * 1000;
  133. fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
  134. #endif
  135. // Core driver may have changed the in/out values
  136. fCaptureChannels = inchannels;
  137. fPlaybackChannels = outchannels;
  138. assert(strlen(capture_driver_uid) < JACK_CLIENT_NAME_SIZE);
  139. assert(strlen(playback_driver_uid) < JACK_CLIENT_NAME_SIZE);
  140. strcpy(fCaptureDriverName, capture_driver_uid);
  141. strcpy(fPlaybackDriverName, playback_driver_uid);
  142. return 0;
  143. error:
  144. JackAudioDriver::Close();
  145. jack_error("Can't open default PortAudio device : %s", Pa_GetErrorText(err));
  146. return -1;
  147. }
  148. int JackPortAudioDriver::Close()
  149. {
  150. // Generic audio driver close
  151. int res = JackAudioDriver::Close();
  152. jack_log("JackPortAudioDriver::Close");
  153. Pa_CloseStream(fStream);
  154. return res;
  155. }
  156. int JackPortAudioDriver::Start()
  157. {
  158. jack_log("JackPortAudioDriver::Start");
  159. if (JackAudioDriver::Start() >= 0) {
  160. PaError err = Pa_StartStream(fStream);
  161. if (err == paNoError) {
  162. return 0;
  163. }
  164. JackAudioDriver::Stop();
  165. }
  166. return -1;
  167. }
  168. int JackPortAudioDriver::Stop()
  169. {
  170. jack_log("JackPortAudioDriver::Stop");
  171. PaError err = Pa_StopStream(fStream);
  172. int res = (err == paNoError) ? 0 : -1;
  173. if (JackAudioDriver::Stop() < 0) {
  174. res = -1;
  175. }
  176. return res;
  177. }
  178. int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  179. {
  180. PaError err;
  181. PaStreamParameters inputParameters;
  182. PaStreamParameters outputParameters;
  183. if ((err = Pa_CloseStream(fStream)) != paNoError) {
  184. jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
  185. return -1;
  186. }
  187. // Update parameters
  188. inputParameters.device = fInputDevice;
  189. inputParameters.channelCount = fCaptureChannels;
  190. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  191. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  192. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  193. : 0;
  194. inputParameters.hostApiSpecificStreamInfo = NULL;
  195. outputParameters.device = fOutputDevice;
  196. outputParameters.channelCount = fPlaybackChannels;
  197. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  198. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  199. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  200. : 0;
  201. outputParameters.hostApiSpecificStreamInfo = NULL;
  202. err = Pa_OpenStream(&fStream,
  203. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  204. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  205. fEngineControl->fSampleRate,
  206. buffer_size,
  207. paNoFlag, // Clipping is on...
  208. Render,
  209. this);
  210. if (err != paNoError) {
  211. jack_error("Pa_OpenStream error = %s", Pa_GetErrorText(err));
  212. return -1;
  213. } else {
  214. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  215. // PortAudio specific
  216. JackAudioDriver::UpdateLatencies();
  217. return 0;
  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