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.

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