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.

411 lines
15KB

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