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.

319 lines
12KB

  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. }
  111. }
  112. //max channels
  113. if ( in_max == 0 )
  114. in_max = fPaDevices.GetDeviceInfo ( fInputDevice )->maxInputChannels;
  115. if ( out_max == 0 )
  116. out_max = fPaDevices.GetDeviceInfo ( fOutputDevice )->maxOutputChannels;
  117. //effective channels
  118. if ( ( fCaptureChannels == 0 ) || ( fCaptureChannels > in_max ) )
  119. fCaptureChannels = in_max;
  120. if ( ( fPlaybackChannels == 0 ) || ( fPlaybackChannels > out_max ) )
  121. fPlaybackChannels = out_max;
  122. //set adapter interface channels
  123. SetInputs ( fCaptureChannels );
  124. SetOutputs ( fPlaybackChannels );
  125. }
  126. int JackPortAudioAdapter::Open()
  127. {
  128. PaError err;
  129. PaStreamParameters inputParameters;
  130. PaStreamParameters outputParameters;
  131. if ( JackAudioAdapterInterface::Open() < 0 )
  132. return -1;
  133. jack_log("JackPortAudioAdapter::Open fInputDevice = %d DeviceName %s", fInputDevice, fPaDevices.GetFullName(fInputDevice).c_str());
  134. jack_log("JackPortAudioAdapter::Open fOutputDevice = %d DeviceName %s", fOutputDevice, fPaDevices.GetFullName(fOutputDevice).c_str());
  135. jack_log("JackPortAudioAdapter::Open fAdaptedBufferSize = %u fAdaptedSampleRate %u", fAdaptedBufferSize, fAdaptedSampleRate);
  136. inputParameters.device = fInputDevice;
  137. inputParameters.channelCount = fCaptureChannels;
  138. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  139. inputParameters.suggestedLatency = ( fInputDevice != paNoDevice ) // TODO: check how to setup this on ASIO
  140. ? fPaDevices.GetDeviceInfo(fInputDevice)->defaultLowInputLatency
  141. : 0;
  142. inputParameters.hostApiSpecificStreamInfo = NULL;
  143. outputParameters.device = fOutputDevice;
  144. outputParameters.channelCount = fPlaybackChannels;
  145. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  146. outputParameters.suggestedLatency = ( fOutputDevice != paNoDevice ) // TODO: check how to setup this on ASIO
  147. ? fPaDevices.GetDeviceInfo(fOutputDevice)->defaultLowOutputLatency
  148. : 0;
  149. outputParameters.hostApiSpecificStreamInfo = NULL;
  150. err = Pa_OpenStream( &fStream,
  151. ( fInputDevice == paNoDevice ) ? 0 : &inputParameters,
  152. ( fOutputDevice == paNoDevice ) ? 0 : &outputParameters,
  153. fAdaptedSampleRate,
  154. fAdaptedBufferSize,
  155. paNoFlag, // Clipping is on...
  156. Render,
  157. this );
  158. if ( err != paNoError )
  159. {
  160. jack_error ( "Pa_OpenStream error = %s", Pa_GetErrorText ( err ) );
  161. return -1;
  162. }
  163. err = Pa_StartStream ( fStream );
  164. if ( err != paNoError )
  165. {
  166. jack_error ( "Pa_StartStream error = %s", Pa_GetErrorText ( err ) );
  167. return -1;
  168. }
  169. jack_log ( "JackPortAudioAdapter::Open OK" );
  170. return 0;
  171. }
  172. int JackPortAudioAdapter::Close()
  173. {
  174. #ifdef JACK_MONITOR
  175. fTable.Save();
  176. #endif
  177. jack_log ( "JackPortAudioAdapter::Close" );
  178. Pa_StopStream ( fStream );
  179. jack_log ( "JackPortAudioAdapter:: Pa_StopStream" );
  180. Pa_CloseStream ( fStream );
  181. jack_log ( "JackPortAudioAdapter:: Pa_CloseStream" );
  182. return JackAudioAdapterInterface::Close();
  183. }
  184. int JackPortAudioAdapter::SetSampleRate ( jack_nframes_t sample_rate )
  185. {
  186. JackAudioAdapterInterface::SetHostSampleRate ( sample_rate );
  187. Close();
  188. return Open();
  189. }
  190. int JackPortAudioAdapter::SetBufferSize ( jack_nframes_t buffer_size )
  191. {
  192. JackAudioAdapterInterface::SetHostBufferSize ( buffer_size );
  193. Close();
  194. return Open();
  195. }
  196. } // namespace
  197. #ifdef __cplusplus
  198. extern "C"
  199. {
  200. #endif
  201. EXPORT jack_driver_desc_t* jack_get_descriptor()
  202. {
  203. jack_driver_desc_t *desc;
  204. unsigned int i;
  205. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  206. strcpy(desc->name, "audioadapter"); // size MUST be less then JACK_DRIVER_NAME_MAX + 1
  207. desc->nparams = 8;
  208. desc->params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  209. i = 0;
  210. strcpy(desc->params[i].name, "inchannels");
  211. desc->params[i].character = 'i';
  212. desc->params[i].type = JackDriverParamInt;
  213. desc->params[i].value.ui = 0;
  214. strcpy(desc->params[i].short_desc, "Maximum number of input channels");
  215. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  216. i++;
  217. strcpy(desc->params[i].name, "outchannels");
  218. desc->params[i].character = 'o';
  219. desc->params[i].type = JackDriverParamInt;
  220. desc->params[i].value.ui = 0;
  221. strcpy(desc->params[i].short_desc, "Maximum number of output channels");
  222. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  223. i++;
  224. strcpy(desc->params[i].name, "capture");
  225. desc->params[i].character = 'C';
  226. desc->params[i].type = JackDriverParamString;
  227. strcpy(desc->params[i].value.str, "default input device");
  228. strcpy(desc->params[i].short_desc, "Provide capture ports. Optionally set PortAudio device name");
  229. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  230. i++;
  231. strcpy(desc->params[i].name, "playback");
  232. desc->params[i].character = 'P';
  233. desc->params[i].type = JackDriverParamString;
  234. strcpy(desc->params[i].value.str, "default output device");
  235. strcpy(desc->params[i].short_desc, "Provide playback ports. Optionally set PortAudio device name");
  236. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  237. i++;
  238. strcpy(desc->params[i].name, "rate");
  239. desc->params[i].character = 'r';
  240. desc->params[i].type = JackDriverParamUInt;
  241. desc->params[i].value.ui = 44100U;
  242. strcpy(desc->params[i].short_desc, "Sample rate");
  243. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  244. i++;
  245. strcpy(desc->params[i].name, "periodsize");
  246. desc->params[i].character = 'p';
  247. desc->params[i].type = JackDriverParamUInt;
  248. desc->params[i].value.ui = 512U;
  249. strcpy(desc->params[i].short_desc, "Period size");
  250. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  251. i++;
  252. strcpy(desc->params[i].name, "device");
  253. desc->params[i].character = 'd';
  254. desc->params[i].type = JackDriverParamString;
  255. desc->params[i].value.ui = 128U;
  256. strcpy(desc->params[i].value.str, "default device");
  257. strcpy(desc->params[i].short_desc, "PortAudio device name");
  258. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  259. i++;
  260. strcpy(desc->params[i].name, "list-devices");
  261. desc->params[i].character = 'l';
  262. desc->params[i].type = JackDriverParamBool;
  263. desc->params[i].value.i = TRUE;
  264. strcpy(desc->params[i].short_desc, "Display available PortAudio devices");
  265. strcpy(desc->params[i].long_desc, desc->params[i].short_desc);
  266. return desc;
  267. }
  268. #ifdef __cplusplus
  269. }
  270. #endif