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.

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