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.

486 lines
17KB

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