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.

332 lines
13KB

  1. /*
  2. Copyright (C) 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. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include "JackPortAudioAdapter.h"
  19. #include "JackError.h"
  20. namespace Jack
  21. {
  22. int JackPortAudioAdapter::Render ( const void* inputBuffer,
  23. void* outputBuffer,
  24. unsigned long framesPerBuffer,
  25. const PaStreamCallbackTimeInfo* timeInfo,
  26. PaStreamCallbackFlags statusFlags,
  27. void* userData)
  28. {
  29. JackPortAudioAdapter* adapter = static_cast<JackPortAudioAdapter*>(userData);
  30. float** paBuffer;
  31. bool failure = false;
  32. jack_nframes_t time1, time2;
  33. adapter->ResampleFactor ( time1, time2 );
  34. paBuffer = (float**)inputBuffer;
  35. for ( int i = 0; i < adapter->fCaptureChannels; i++ )
  36. {
  37. adapter->fCaptureRingBuffer[i]->SetRatio ( time1, time2 );
  38. if (adapter->fCaptureRingBuffer[i]->WriteResample ( (float*)paBuffer[i], framesPerBuffer ) < framesPerBuffer )
  39. failure = true;
  40. }
  41. paBuffer = (float**)outputBuffer;
  42. for ( int i = 0; i < adapter->fPlaybackChannels; i++ )
  43. {
  44. adapter->fPlaybackRingBuffer[i]->SetRatio ( time2, time1 );
  45. if ( adapter->fPlaybackRingBuffer[i]->ReadResample ( (float*)paBuffer[i], framesPerBuffer ) < framesPerBuffer )
  46. failure = true;
  47. }
  48. #ifdef JACK_MONITOR
  49. adapter->fTable.Write ( time1, time2, double(time1) / double(time2), double(time2) / double(time1),
  50. adapter->fCaptureRingBuffer[0]->ReadSpace(), adapter->fPlaybackRingBuffer[0]->WriteSpace() );
  51. #endif
  52. // Reset all ringbuffers in case of failure
  53. if ( failure )
  54. {
  55. jack_error ( "JackPortAudioAdapter::Render ringbuffer failure... reset" );
  56. adapter->ResetRingBuffers();
  57. }
  58. return paContinue;
  59. }
  60. JackPortAudioAdapter::JackPortAudioAdapter ( jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params )
  61. : JackAudioAdapterInterface ( buffer_size, sample_rate )
  62. {
  63. jack_log ( "JackPortAudioAdapter::JackPortAudioAdapter buffer_size = %d, sample_rate = %d", buffer_size, sample_rate );
  64. const JSList* node;
  65. const jack_driver_param_t* param;
  66. int in_max = 0;
  67. int out_max = 0;
  68. fInputDevice = Pa_GetDefaultInputDevice();
  69. fOutputDevice = Pa_GetDefaultOutputDevice();
  70. for (node = params; node; node = jack_slist_next(node))
  71. {
  72. param = (const jack_driver_param_t*) node->data;
  73. switch (param->character)
  74. {
  75. case 'i' :
  76. fCaptureChannels = param->value.ui;
  77. break;
  78. case 'o' :
  79. fPlaybackChannels = param->value.ui;
  80. break;
  81. case 'C' :
  82. if ( fPaDevices.GetInputDeviceFromName(param->value.str, fInputDevice, in_max) < 0 )
  83. {
  84. jack_error ( "Can't use %s, taking default input device", param->value.str );
  85. fInputDevice = Pa_GetDefaultInputDevice();
  86. }
  87. break;
  88. case 'P' :
  89. if ( fPaDevices.GetOutputDeviceFromName(param->value.str, fOutputDevice, out_max) < 0 )
  90. {
  91. jack_error ( "Can't use %s, taking default output device", param->value.str );
  92. fOutputDevice = Pa_GetDefaultOutputDevice();
  93. }
  94. break;
  95. case 'r' :
  96. SetAdaptedSampleRate ( param->value.ui );
  97. break;
  98. case 'p' :
  99. SetAdaptedBufferSize ( param->value.ui );
  100. break;
  101. case 'd' :
  102. if ( fPaDevices.GetInputDeviceFromName ( param->value.str, fInputDevice, in_max ) < 0 )
  103. jack_error ( "Can't use %s, taking default input device", param->value.str );
  104. if ( fPaDevices.GetOutputDeviceFromName ( param->value.str, fOutputDevice, out_max ) < 0 )
  105. jack_error ( "Can't use %s, taking default output device", param->value.str );
  106. break;
  107. case 'l' :
  108. fPaDevices.DisplayDevicesNames();
  109. break;
  110. case 'q':
  111. fQuality = param->value.ui;
  112. break;
  113. }
  114. }
  115. //max channels
  116. if ( in_max == 0 )
  117. in_max = fPaDevices.GetDeviceInfo ( fInputDevice )->maxInputChannels;
  118. if ( out_max == 0 )
  119. out_max = fPaDevices.GetDeviceInfo ( fOutputDevice )->maxOutputChannels;
  120. //effective channels
  121. if ( ( fCaptureChannels == 0 ) || ( fCaptureChannels > in_max ) )
  122. fCaptureChannels = in_max;
  123. if ( ( fPlaybackChannels == 0 ) || ( fPlaybackChannels > out_max ) )
  124. fPlaybackChannels = out_max;
  125. //set adapter interface channels
  126. SetInputs ( fCaptureChannels );
  127. SetOutputs ( fPlaybackChannels );
  128. }
  129. int JackPortAudioAdapter::Open()
  130. {
  131. PaError err;
  132. PaStreamParameters inputParameters;
  133. PaStreamParameters outputParameters;
  134. if ( JackAudioAdapterInterface::Open() < 0 )
  135. return -1;
  136. jack_log("JackPortAudioAdapter::Open fInputDevice = %d DeviceName %s", fInputDevice, fPaDevices.GetFullName(fInputDevice).c_str());
  137. jack_log("JackPortAudioAdapter::Open fOutputDevice = %d DeviceName %s", fOutputDevice, fPaDevices.GetFullName(fOutputDevice).c_str());
  138. jack_log("JackPortAudioAdapter::Open fAdaptedBufferSize = %u fAdaptedSampleRate %u", fAdaptedBufferSize, fAdaptedSampleRate);
  139. inputParameters.device = fInputDevice;
  140. inputParameters.channelCount = fCaptureChannels;
  141. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  142. inputParameters.suggestedLatency = ( fInputDevice != paNoDevice ) // TODO: check how to setup this on ASIO
  143. ? fPaDevices.GetDeviceInfo(fInputDevice)->defaultLowInputLatency
  144. : 0;
  145. inputParameters.hostApiSpecificStreamInfo = NULL;
  146. outputParameters.device = fOutputDevice;
  147. outputParameters.channelCount = fPlaybackChannels;
  148. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  149. outputParameters.suggestedLatency = ( fOutputDevice != paNoDevice ) // TODO: check how to setup this on ASIO
  150. ? fPaDevices.GetDeviceInfo(fOutputDevice)->defaultLowOutputLatency
  151. : 0;
  152. outputParameters.hostApiSpecificStreamInfo = NULL;
  153. err = Pa_OpenStream( &fStream,
  154. ( fInputDevice == paNoDevice ) ? 0 : &inputParameters,
  155. ( fOutputDevice == paNoDevice ) ? 0 : &outputParameters,
  156. fAdaptedSampleRate,
  157. fAdaptedBufferSize,
  158. paNoFlag, // Clipping is on...
  159. Render,
  160. this );
  161. if ( err != paNoError )
  162. {
  163. jack_error ( "Pa_OpenStream error = %s", Pa_GetErrorText ( err ) );
  164. return -1;
  165. }
  166. err = Pa_StartStream ( fStream );
  167. if ( err != paNoError )
  168. {
  169. jack_error ( "Pa_StartStream error = %s", Pa_GetErrorText ( err ) );
  170. return -1;
  171. }
  172. jack_log ( "JackPortAudioAdapter::Open OK" );
  173. return 0;
  174. }
  175. int JackPortAudioAdapter::Close()
  176. {
  177. #ifdef JACK_MONITOR
  178. fTable.Save();
  179. #endif
  180. jack_log ( "JackPortAudioAdapter::Close" );
  181. Pa_StopStream ( fStream );
  182. jack_log ( "JackPortAudioAdapter:: Pa_StopStream" );
  183. Pa_CloseStream ( fStream );
  184. jack_log ( "JackPortAudioAdapter:: Pa_CloseStream" );
  185. return JackAudioAdapterInterface::Close();
  186. }
  187. int JackPortAudioAdapter::SetSampleRate ( jack_nframes_t sample_rate )
  188. {
  189. JackAudioAdapterInterface::SetHostSampleRate ( sample_rate );
  190. Close();
  191. return Open();
  192. }
  193. int JackPortAudioAdapter::SetBufferSize ( jack_nframes_t buffer_size )
  194. {
  195. JackAudioAdapterInterface::SetHostBufferSize ( buffer_size );
  196. Close();
  197. return Open();
  198. }
  199. } // namespace
  200. #ifdef __cplusplus
  201. extern "C"
  202. {
  203. #endif
  204. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
  205. {
  206. jack_driver_desc_t *desc;
  207. unsigned int i;
  208. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  209. strcpy(desc->name, "audioadapter"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  210. strcpy(desc->desc, "netjack audio <==> net backend adapter"); // size MUST be less then JACK_DRIVER_PARAM_DESC + 1
  211. desc->nparams = 9;
  212. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  213. i = 0;
  214. strcpy(desc->params[i].name, "inchannels");
  215. desc->params[i].character = 'i';
  216. desc->params[i].type = JackDriverParamInt;
  217. desc->params[i].value.ui = 0;
  218. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  219. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  220. i++;
  221. strcpy(desc->params[i].name, "outchannels");
  222. desc->params[i].character = 'o';
  223. desc->params[i].type = JackDriverParamInt;
  224. desc->params[i].value.ui = 0;
  225. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  226. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  227. i++;
  228. strcpy(desc->params[i].name, "capture");
  229. desc->params[i].character = 'C';
  230. desc->params[i].type = JackDriverParamString;
  231. strcpy(desc->params[i].value.str, "default input device");
  232. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set PortAudio device name");
  233. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  234. i++;
  235. strcpy(desc->params[i].name, "playback");
  236. desc->params[i].character = 'P';
  237. desc->params[i].type = JackDriverParamString;
  238. strcpy(desc->params[i].value.str, "default output device");
  239. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set PortAudio device name");
  240. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  241. i++;
  242. strcpy(desc->params[i].name, "rate");
  243. desc->params[i].character = 'r';
  244. desc->params[i].type = JackDriverParamUInt;
  245. desc->params[i].value.ui = 44100U;
  246. strcpy(desc->params[i].short_desc, "Sample rate");
  247. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  248. i++;
  249. strcpy(desc->params[i].name, "periodsize");
  250. desc->params[i].character = 'p';
  251. desc->params[i].type = JackDriverParamUInt;
  252. desc->params[i].value.ui = 512U;
  253. strcpy(desc->params[i].short_desc, "Period size");
  254. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  255. i++;
  256. strcpy(desc->params[i].name, "device");
  257. desc->params[i].character = 'd';
  258. desc->params[i].type = JackDriverParamString;
  259. desc->params[i].value.ui = 128U;
  260. strcpy(desc->params[i].value.str, "default device");
  261. strcpy(desc->params[i].short_desc, "PortAudio device name");
  262. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  263. i++;
  264. strcpy(desc->params[i].name, "list-devices");
  265. desc->params[i].character = 'l';
  266. desc->params[i].type = JackDriverParamBool;
  267. desc->params[i].value.i = TRUE;
  268. strcpy(desc->params[i].short_desc, "Display available PortAudio devices");
  269. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  270. i++;
  271. strcpy(desc->params[i].name, "quality");
  272. desc->params[i].character = 'q';
  273. desc->params[i].type = JackDriverParamInt;
  274. desc->params[i].value.ui = 0;
  275. strcpy(desc->params[i].short_desc, "Resample algorithm quality (0 - 4)");
  276. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  277. return desc;
  278. }
  279. #ifdef __cplusplus
  280. }
  281. #endif