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.

451 lines
16KB

  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. MMCSSAcquireRealTime(GetCurrentThread());
  37. if (statusFlags) {
  38. if (statusFlags & paOutputUnderflow)
  39. jack_error("JackPortAudioDriver::Render paOutputUnderflow");
  40. if (statusFlags & paInputUnderflow)
  41. jack_error("JackPortAudioDriver::Render paInputUnderflow");
  42. if (statusFlags & paOutputOverflow)
  43. jack_error("JackPortAudioDriver::Render paOutputOverflow");
  44. if (statusFlags & paInputOverflow)
  45. jack_error("JackPortAudioDriver::Render paInputOverflow");
  46. if (statusFlags & paPrimingOutput)
  47. jack_error("JackPortAudioDriver::Render paOutputUnderflow");
  48. if (statusFlags != paPrimingOutput) {
  49. jack_time_t cur_time = GetMicroSeconds();
  50. driver->NotifyXRun(cur_time, float(cur_time - driver->fBeginDateUst)); // Better this value than nothing...
  51. }
  52. }
  53. // Setup threadded based log function
  54. set_threaded_log_function();
  55. driver->CycleTakeBeginTime();
  56. return (driver->Process() == 0) ? paContinue : paAbort;
  57. }
  58. int JackPortAudioDriver::Read()
  59. {
  60. for (int i = 0; i < fCaptureChannels; i++) {
  61. memcpy(GetInputBuffer(i), fInputBuffer[i], sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
  62. }
  63. return 0;
  64. }
  65. int JackPortAudioDriver::Write()
  66. {
  67. for (int i = 0; i < fPlaybackChannels; i++) {
  68. memcpy(fOutputBuffer[i], GetOutputBuffer(i), sizeof(jack_default_audio_sample_t) * fEngineControl->fBufferSize);
  69. }
  70. return 0;
  71. }
  72. PaError JackPortAudioDriver::OpenStream(jack_nframes_t buffer_size)
  73. {
  74. PaStreamParameters inputParameters;
  75. PaStreamParameters outputParameters;
  76. // Update parameters
  77. inputParameters.device = fInputDevice;
  78. inputParameters.channelCount = fCaptureChannels;
  79. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  80. inputParameters.suggestedLatency = (fInputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  81. ? Pa_GetDeviceInfo(inputParameters.device)->defaultLowInputLatency
  82. : 0;
  83. inputParameters.hostApiSpecificStreamInfo = NULL;
  84. outputParameters.device = fOutputDevice;
  85. outputParameters.channelCount = fPlaybackChannels;
  86. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  87. outputParameters.suggestedLatency = (fOutputDevice != paNoDevice) // TODO: check how to setup this on ASIO
  88. ? Pa_GetDeviceInfo(outputParameters.device)->defaultLowOutputLatency
  89. : 0;
  90. outputParameters.hostApiSpecificStreamInfo = NULL;
  91. return Pa_OpenStream(&fStream,
  92. (fInputDevice == paNoDevice) ? 0 : &inputParameters,
  93. (fOutputDevice == paNoDevice) ? 0 : &outputParameters,
  94. fEngineControl->fSampleRate,
  95. buffer_size,
  96. paNoFlag, // Clipping is on...
  97. Render,
  98. this);
  99. }
  100. void JackPortAudioDriver::UpdateLatencies()
  101. {
  102. jack_latency_range_t input_range;
  103. jack_latency_range_t output_range;
  104. jack_latency_range_t monitor_range;
  105. const PaStreamInfo* info = Pa_GetStreamInfo(fStream);
  106. assert(info);
  107. for (int i = 0; i < fCaptureChannels; i++) {
  108. input_range.max = input_range.min = fEngineControl->fBufferSize + (info->inputLatency * fEngineControl->fSampleRate) + fCaptureLatency;
  109. fGraphManager->GetPort(fCapturePortList[i])->SetLatencyRange(JackCaptureLatency, &input_range);
  110. }
  111. for (int i = 0; i < fPlaybackChannels; i++) {
  112. output_range.max = output_range.min = (info->outputLatency * fEngineControl->fSampleRate) + fPlaybackLatency;
  113. if (fEngineControl->fSyncMode) {
  114. output_range.max = output_range.min += fEngineControl->fBufferSizey;
  115. } else {
  116. output_range.max = output_range.min += fEngineControl->fBufferSize * 2;
  117. }
  118. fGraphManager->GetPort(fPlaybackPortList[i])->SetLatencyRange(JackPlaybackLatency, &output_range);
  119. if (fWithMonitorPorts) {
  120. monitor_range.min = monitor_range.max = fEngineControl->fBufferSize;
  121. fGraphManager->GetPort(fMonitorPortList[i])->SetLatencyRange(JackCaptureLatency, &monitor_range);
  122. }
  123. }
  124. }
  125. int JackPortAudioDriver::Open(jack_nframes_t buffer_size,
  126. jack_nframes_t samplerate,
  127. bool capturing,
  128. bool playing,
  129. int inchannels,
  130. int outchannels,
  131. bool monitor,
  132. const char* capture_driver_uid,
  133. const char* playback_driver_uid,
  134. jack_nframes_t capture_latency,
  135. jack_nframes_t playback_latency)
  136. {
  137. int in_max = 0;
  138. int out_max = 0;
  139. PaError err = paNoError;
  140. fCaptureLatency = capture_latency;
  141. fPlaybackLatency = playback_latency;
  142. jack_log("JackPortAudioDriver::Open nframes = %ld in = %ld out = %ld capture name = %s playback name = %s samplerate = %ld",
  143. buffer_size, inchannels, outchannels, capture_driver_uid, playback_driver_uid, samplerate);
  144. // Generic JackAudioDriver Open
  145. if (JackAudioDriver::Open(buffer_size, samplerate, capturing, playing, inchannels, outchannels, monitor,
  146. capture_driver_uid, playback_driver_uid, capture_latency, playback_latency) != 0) {
  147. return -1;
  148. }
  149. //get devices
  150. if (capturing) {
  151. if (fPaDevices->GetInputDeviceFromName(capture_driver_uid, fInputDevice, in_max) < 0) {
  152. goto error;
  153. }
  154. }
  155. if (playing) {
  156. if (fPaDevices->GetOutputDeviceFromName(playback_driver_uid, fOutputDevice, out_max) < 0) {
  157. goto error;
  158. }
  159. }
  160. jack_log("JackPortAudioDriver::Open fInputDevice = %d, fOutputDevice %d", fInputDevice, fOutputDevice);
  161. //default channels number required
  162. if (inchannels == 0) {
  163. jack_log("JackPortAudioDriver::Open setup max in channels = %ld", in_max);
  164. inchannels = in_max;
  165. }
  166. if (outchannels == 0) {
  167. jack_log("JackPortAudioDriver::Open setup max out channels = %ld", out_max);
  168. outchannels = out_max;
  169. }
  170. //too many channels required, take max available
  171. if (inchannels > in_max) {
  172. jack_error("This device has only %d available input channels.", in_max);
  173. inchannels = in_max;
  174. }
  175. if (outchannels > out_max) {
  176. jack_error("This device has only %d available output channels.", out_max);
  177. outchannels = out_max;
  178. }
  179. // Core driver may have changed the in/out values
  180. fCaptureChannels = inchannels;
  181. fPlaybackChannels = outchannels;
  182. err = OpenStream(buffer_size);
  183. if (err != paNoError) {
  184. jack_error("Pa_OpenStream error %d = %s", err, Pa_GetErrorText(err));
  185. goto error;
  186. }
  187. #ifdef __APPLE__
  188. fEngineControl->fPeriod = fEngineControl->fPeriodUsecs * 1000;
  189. fEngineControl->fComputation = 500 * 1000;
  190. fEngineControl->fConstraint = fEngineControl->fPeriodUsecs * 1000;
  191. #endif
  192. assert(strlen(capture_driver_uid) < JACK_CLIENT_NAME_SIZE);
  193. assert(strlen(playback_driver_uid) < JACK_CLIENT_NAME_SIZE);
  194. strcpy(fCaptureDriverName, capture_driver_uid);
  195. strcpy(fPlaybackDriverName, playback_driver_uid);
  196. return 0;
  197. error:
  198. JackAudioDriver::Close();
  199. jack_error("Can't open default PortAudio device");
  200. return -1;
  201. }
  202. int JackPortAudioDriver::Close()
  203. {
  204. // Generic audio driver close
  205. int res = JackAudioDriver::Close();
  206. jack_log("JackPortAudioDriver::Close");
  207. Pa_CloseStream(fStream);
  208. return res;
  209. }
  210. int JackPortAudioDriver::Start()
  211. {
  212. jack_log("JackPortAudioDriver::Start");
  213. if (JackAudioDriver::Start() >= 0) {
  214. PaError err = Pa_StartStream(fStream);
  215. if (err == paNoError) {
  216. return 0;
  217. }
  218. JackAudioDriver::Stop();
  219. }
  220. return -1;
  221. }
  222. int JackPortAudioDriver::Stop()
  223. {
  224. jack_log("JackPortAudioDriver::Stop");
  225. PaError err = Pa_StopStream(fStream);
  226. int res = (err == paNoError) ? 0 : -1;
  227. if (JackAudioDriver::Stop() < 0) {
  228. res = -1;
  229. }
  230. return res;
  231. }
  232. int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  233. {
  234. PaError err;
  235. if ((err = Pa_CloseStream(fStream)) != paNoError) {
  236. jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
  237. return -1;
  238. }
  239. err = OpenStream(buffer_size);
  240. if (err != paNoError) {
  241. jack_error("Pa_OpenStream error %d = %s", err, Pa_GetErrorText(err));
  242. return -1;
  243. } else {
  244. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  245. return 0;
  246. }
  247. }
  248. } // end of namespace
  249. #ifdef __cplusplus
  250. extern "C"
  251. {
  252. #endif
  253. #include "JackCompilerDeps.h"
  254. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  255. {
  256. jack_driver_desc_t * desc;
  257. jack_driver_desc_filler_t filler;
  258. jack_driver_param_value_t value;
  259. desc = jack_driver_descriptor_construct("portaudio", JackDriverMaster, "PortAudio API based audio backend", &filler);
  260. value.ui = 0;
  261. jack_driver_descriptor_add_parameter(desc, &filler, "channels", 'c', JackDriverParamUInt, &value, NULL, "Maximum number of channels", NULL);
  262. jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Maximum number of input channels", NULL);
  263. jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Maximum number of output channels", NULL);
  264. strcpy(value.str, "will take default PortAudio input device");
  265. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set PortAudio device name", NULL);
  266. strcpy(value.str, "will take default PortAudio output device");
  267. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set PortAudio device name", NULL);
  268. value.i = 0;
  269. jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
  270. value.i = TRUE;
  271. jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
  272. value.ui = 44100U;
  273. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  274. value.ui = 512U;
  275. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  276. strcpy(value.str, "will take default PortAudio device name");
  277. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "PortAudio device name", NULL);
  278. value.ui = 0;
  279. jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency", NULL);
  280. jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency", NULL);
  281. value.i = TRUE;
  282. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available PortAudio devices", NULL);
  283. return desc;
  284. }
  285. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  286. {
  287. jack_nframes_t srate = 44100;
  288. jack_nframes_t frames_per_interrupt = 512;
  289. const char* capture_pcm_name = "";
  290. const char* playback_pcm_name = "";
  291. bool capture = false;
  292. bool playback = false;
  293. int chan_in = 0;
  294. int chan_out = 0;
  295. bool monitor = false;
  296. const JSList *node;
  297. const jack_driver_param_t *param;
  298. jack_nframes_t systemic_input_latency = 0;
  299. jack_nframes_t systemic_output_latency = 0;
  300. PortAudioDevices* pa_devices = new PortAudioDevices();
  301. for (node = params; node; node = jack_slist_next(node))
  302. {
  303. param = (const jack_driver_param_t *) node->data;
  304. switch (param->character) {
  305. case 'd':
  306. capture_pcm_name = param->value.str;
  307. playback_pcm_name = param->value.str;
  308. break;
  309. case 'D':
  310. capture = true;
  311. playback = true;
  312. break;
  313. case 'c':
  314. chan_in = chan_out = (int)param->value.ui;
  315. break;
  316. case 'i':
  317. chan_in = (int)param->value.ui;
  318. break;
  319. case 'o':
  320. chan_out = (int)param->value.ui;
  321. break;
  322. case 'C':
  323. capture = true;
  324. if (strcmp(param->value.str, "none") != 0) {
  325. capture_pcm_name = param->value.str;
  326. }
  327. break;
  328. case 'P':
  329. playback = TRUE;
  330. if (strcmp(param->value.str, "none") != 0) {
  331. playback_pcm_name = param->value.str;
  332. }
  333. break;
  334. case 'm':
  335. monitor = param->value.i;
  336. break;
  337. case 'r':
  338. srate = param->value.ui;
  339. break;
  340. case 'p':
  341. frames_per_interrupt = (unsigned int)param->value.ui;
  342. break;
  343. case 'I':
  344. systemic_input_latency = param->value.ui;
  345. break;
  346. case 'O':
  347. systemic_output_latency = param->value.ui;
  348. break;
  349. case 'l':
  350. pa_devices->DisplayDevicesNames();
  351. break;
  352. }
  353. }
  354. // duplex is the default
  355. if (!capture && !playback) {
  356. capture = true;
  357. playback = true;
  358. }
  359. Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table, pa_devices);
  360. if (driver->Open(frames_per_interrupt, srate, capture, playback,
  361. chan_in, chan_out, monitor, capture_pcm_name,
  362. playback_pcm_name, systemic_input_latency,
  363. systemic_output_latency) == 0) {
  364. return driver;
  365. } else {
  366. delete driver;
  367. return NULL;
  368. }
  369. }
  370. #ifdef __cplusplus
  371. }
  372. #endif