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.

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