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.

487 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. // 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::Attach()
  213. {
  214. if (JackAudioDriver::Attach() == 0) {
  215. const char* alias;
  216. if (fPaDevices->GetHostFromDevice(fInputDevice) == "ASIO") {
  217. for (int i = 0; i < fCaptureChannels; i++) {
  218. PaError err = PaAsio_GetInputChannelName(fInputDevice, i, &alias);
  219. if (err == paNoError) {
  220. JackPort* port = fGraphManager->GetPort(fCapturePortList[i]);
  221. port->SetAlias(alias);
  222. }
  223. }
  224. }
  225. if (fPaDevices->GetHostFromDevice(fOutputDevice) == "ASIO") {
  226. for (int i = 0; i < fPlaybackChannels; i++) {
  227. PaError err = PaAsio_GetInputChannelName(fOutputDevice, i, &alias);
  228. if (err == paNoError) {
  229. JackPort* port = fGraphManager->GetPort(fPlaybackPortList[i]);
  230. port->SetAlias(alias);
  231. }
  232. }
  233. }
  234. return 0;
  235. } else {
  236. return -1;
  237. }
  238. }
  239. int JackPortAudioDriver::Start()
  240. {
  241. jack_log("JackPortAudioDriver::Start");
  242. if (JackAudioDriver::Start() >= 0) {
  243. PaError err = Pa_StartStream(fStream);
  244. if (err == paNoError) {
  245. return 0;
  246. }
  247. JackAudioDriver::Stop();
  248. }
  249. return -1;
  250. }
  251. int JackPortAudioDriver::Stop()
  252. {
  253. jack_log("JackPortAudioDriver::Stop");
  254. PaError err = Pa_StopStream(fStream);
  255. int res = (err == paNoError) ? 0 : -1;
  256. if (JackAudioDriver::Stop() < 0) {
  257. res = -1;
  258. }
  259. return res;
  260. }
  261. int JackPortAudioDriver::SetBufferSize(jack_nframes_t buffer_size)
  262. {
  263. PaError err;
  264. if ((err = Pa_CloseStream(fStream)) != paNoError) {
  265. jack_error("Pa_CloseStream error = %s", Pa_GetErrorText(err));
  266. return -1;
  267. }
  268. err = OpenStream(buffer_size);
  269. if (err != paNoError) {
  270. jack_error("Pa_OpenStream error %d = %s", err, Pa_GetErrorText(err));
  271. return -1;
  272. } else {
  273. JackAudioDriver::SetBufferSize(buffer_size); // Generic change, never fails
  274. return 0;
  275. }
  276. }
  277. } // end of namespace
  278. #ifdef __cplusplus
  279. extern "C"
  280. {
  281. #endif
  282. #include "JackCompilerDeps.h"
  283. SERVER_EXPORT jack_driver_desc_t* driver_get_descriptor()
  284. {
  285. jack_driver_desc_t * desc;
  286. jack_driver_desc_filler_t filler;
  287. jack_driver_param_value_t value;
  288. desc = jack_driver_descriptor_construct("portaudio", JackDriverMaster, "PortAudio API based audio backend", &filler);
  289. value.ui = 0;
  290. jack_driver_descriptor_add_parameter(desc, &filler, "channels", 'c', JackDriverParamUInt, &value, NULL, "Maximum number of channels", NULL);
  291. jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamUInt, &value, NULL, "Maximum number of input channels", NULL);
  292. jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamUInt, &value, NULL, "Maximum number of output channels", NULL);
  293. strcpy(value.str, "will take default PortAudio input device");
  294. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set PortAudio device name", NULL);
  295. strcpy(value.str, "will take default PortAudio output device");
  296. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set PortAudio device name", NULL);
  297. value.i = 0;
  298. jack_driver_descriptor_add_parameter(desc, &filler, "monitor", 'm', JackDriverParamBool, &value, NULL, "Provide monitor ports for the output", NULL);
  299. value.i = TRUE;
  300. jack_driver_descriptor_add_parameter(desc, &filler, "duplex", 'D', JackDriverParamBool, &value, NULL, "Provide both capture and playback ports", NULL);
  301. value.ui = 44100U;
  302. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  303. value.ui = 512U;
  304. jack_driver_descriptor_add_parameter(desc, &filler, "period", 'p', JackDriverParamUInt, &value, NULL, "Frames per period", NULL);
  305. strcpy(value.str, "will take default PortAudio device name");
  306. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "PortAudio device name", NULL);
  307. value.ui = 0;
  308. jack_driver_descriptor_add_parameter(desc, &filler, "input-latency", 'I', JackDriverParamUInt, &value, NULL, "Extra input latency", NULL);
  309. jack_driver_descriptor_add_parameter(desc, &filler, "output-latency", 'O', JackDriverParamUInt, &value, NULL, "Extra output latency", NULL);
  310. value.i = TRUE;
  311. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available PortAudio devices", NULL);
  312. return desc;
  313. }
  314. SERVER_EXPORT Jack::JackDriverClientInterface* driver_initialize(Jack::JackLockedEngine* engine, Jack::JackSynchro* table, const JSList* params)
  315. {
  316. jack_nframes_t srate = 44100;
  317. jack_nframes_t frames_per_interrupt = 512;
  318. const char* capture_pcm_name = "";
  319. const char* playback_pcm_name = "";
  320. bool capture = false;
  321. bool playback = false;
  322. int chan_in = 0;
  323. int chan_out = 0;
  324. bool monitor = false;
  325. const JSList *node;
  326. const jack_driver_param_t *param;
  327. jack_nframes_t systemic_input_latency = 0;
  328. jack_nframes_t systemic_output_latency = 0;
  329. PortAudioDevices* pa_devices = new PortAudioDevices();
  330. for (node = params; node; node = jack_slist_next(node))
  331. {
  332. param = (const jack_driver_param_t *) node->data;
  333. switch (param->character) {
  334. case 'd':
  335. capture_pcm_name = param->value.str;
  336. playback_pcm_name = param->value.str;
  337. break;
  338. case 'D':
  339. capture = true;
  340. playback = true;
  341. break;
  342. case 'c':
  343. chan_in = chan_out = (int)param->value.ui;
  344. break;
  345. case 'i':
  346. chan_in = (int)param->value.ui;
  347. break;
  348. case 'o':
  349. chan_out = (int)param->value.ui;
  350. break;
  351. case 'C':
  352. capture = true;
  353. if (strcmp(param->value.str, "none") != 0) {
  354. capture_pcm_name = param->value.str;
  355. }
  356. break;
  357. case 'P':
  358. playback = TRUE;
  359. if (strcmp(param->value.str, "none") != 0) {
  360. playback_pcm_name = param->value.str;
  361. }
  362. break;
  363. case 'm':
  364. monitor = param->value.i;
  365. break;
  366. case 'r':
  367. srate = param->value.ui;
  368. break;
  369. case 'p':
  370. frames_per_interrupt = (unsigned int)param->value.ui;
  371. break;
  372. case 'I':
  373. systemic_input_latency = param->value.ui;
  374. break;
  375. case 'O':
  376. systemic_output_latency = param->value.ui;
  377. break;
  378. case 'l':
  379. pa_devices->DisplayDevicesNames();
  380. // Stops the server in this case
  381. return NULL;
  382. }
  383. }
  384. // duplex is the default
  385. if (!capture && !playback) {
  386. capture = true;
  387. playback = true;
  388. }
  389. Jack::JackDriverClientInterface* driver = new Jack::JackPortAudioDriver("system", "portaudio", engine, table, pa_devices);
  390. if (driver->Open(frames_per_interrupt, srate, capture, playback,
  391. chan_in, chan_out, monitor, capture_pcm_name,
  392. playback_pcm_name, systemic_input_latency,
  393. systemic_output_latency) == 0) {
  394. return driver;
  395. } else {
  396. delete driver;
  397. return NULL;
  398. }
  399. }
  400. #ifdef __cplusplus
  401. }
  402. #endif