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.

258 lines
10KB

  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((jack_default_audio_sample_t**)inputBuffer, (jack_default_audio_sample_t**)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 && fInputDevice != paNoDevice)
  94. in_max = fPaDevices.GetDeviceInfo ( fInputDevice )->maxInputChannels;
  95. if ( out_max == 0 && fOutputDevice != paNoDevice)
  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 (fInputDevice == paNoDevice && fOutputDevice == paNoDevice) {
  112. jack_error("No input and output device!!");
  113. return -1;
  114. }
  115. jack_log("JackPortAudioAdapter::Open fInputDevice = %d DeviceName %s", fInputDevice, fPaDevices.GetFullName(fInputDevice).c_str());
  116. jack_log("JackPortAudioAdapter::Open fOutputDevice = %d DeviceName %s", fOutputDevice, fPaDevices.GetFullName(fOutputDevice).c_str());
  117. jack_log("JackPortAudioAdapter::Open fAdaptedBufferSize = %u fAdaptedSampleRate %u", fAdaptedBufferSize, fAdaptedSampleRate);
  118. inputParameters.device = fInputDevice;
  119. inputParameters.channelCount = fCaptureChannels;
  120. inputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  121. inputParameters.suggestedLatency = ( fInputDevice != paNoDevice ) // TODO: check how to setup this on ASIO
  122. ? fPaDevices.GetDeviceInfo(fInputDevice)->defaultLowInputLatency
  123. : 0;
  124. inputParameters.hostApiSpecificStreamInfo = NULL;
  125. outputParameters.device = fOutputDevice;
  126. outputParameters.channelCount = fPlaybackChannels;
  127. outputParameters.sampleFormat = paFloat32 | paNonInterleaved; // 32 bit floating point output
  128. outputParameters.suggestedLatency = ( fOutputDevice != paNoDevice ) // TODO: check how to setup this on ASIO
  129. ? fPaDevices.GetDeviceInfo(fOutputDevice)->defaultLowOutputLatency
  130. : 0;
  131. outputParameters.hostApiSpecificStreamInfo = NULL;
  132. err = Pa_OpenStream( &fStream,
  133. ( fInputDevice == paNoDevice ) ? 0 : &inputParameters,
  134. ( fOutputDevice == paNoDevice ) ? 0 : &outputParameters,
  135. fAdaptedSampleRate,
  136. fAdaptedBufferSize,
  137. paNoFlag, // Clipping is on...
  138. Render,
  139. this );
  140. if ( err != paNoError )
  141. {
  142. jack_error ( "Pa_OpenStream error = %s", Pa_GetErrorText ( err ) );
  143. return -1;
  144. }
  145. err = Pa_StartStream ( fStream );
  146. if ( err != paNoError )
  147. {
  148. jack_error ( "Pa_StartStream error = %s", Pa_GetErrorText ( err ) );
  149. return -1;
  150. }
  151. jack_log ( "JackPortAudioAdapter::Open OK" );
  152. return 0;
  153. }
  154. int JackPortAudioAdapter::Close()
  155. {
  156. #ifdef JACK_MONITOR
  157. fTable.Save(fHostBufferSize, fHostSampleRate, fAdaptedSampleRate, fAdaptedBufferSize);
  158. #endif
  159. jack_log ( "JackPortAudioAdapter::Close" );
  160. Pa_StopStream ( fStream );
  161. jack_log ( "JackPortAudioAdapter:: Pa_StopStream" );
  162. Pa_CloseStream ( fStream );
  163. jack_log ( "JackPortAudioAdapter:: Pa_CloseStream" );
  164. return 0;
  165. }
  166. int JackPortAudioAdapter::SetSampleRate ( jack_nframes_t sample_rate )
  167. {
  168. JackAudioAdapterInterface::SetHostSampleRate ( sample_rate );
  169. Close();
  170. return Open();
  171. }
  172. int JackPortAudioAdapter::SetBufferSize ( jack_nframes_t buffer_size )
  173. {
  174. JackAudioAdapterInterface::SetHostBufferSize ( buffer_size );
  175. Close();
  176. return Open();
  177. }
  178. } // namespace
  179. #ifdef __cplusplus
  180. extern "C"
  181. {
  182. #endif
  183. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor()
  184. {
  185. jack_driver_desc_t * desc;
  186. jack_driver_desc_filler_t filler;
  187. jack_driver_param_value_t value;
  188. desc = jack_driver_descriptor_construct("audioadapter", "netjack audio <==> net backend adapter", &filler);
  189. value.ui = 0;
  190. jack_driver_descriptor_add_parameter(desc, &filler, "inchannels", 'i', JackDriverParamInt, &value, NULL, "Maximum number of input channels", NULL);
  191. jack_driver_descriptor_add_parameter(desc, &filler, "outchannels", 'o', JackDriverParamInt, &value, NULL, "Maximum number of output channels", NULL);
  192. strcpy(value.str, "default input device");
  193. jack_driver_descriptor_add_parameter(desc, &filler, "capture", 'C', JackDriverParamString, &value, NULL, "Provide capture ports. Optionally set PortAudio device name", NULL);
  194. strcpy(value.str, "default output device");
  195. jack_driver_descriptor_add_parameter(desc, &filler, "playback", 'P', JackDriverParamString, &value, NULL, "Provide playback ports. Optionally set PortAudio device name", NULL);
  196. value.ui = 44100U;
  197. jack_driver_descriptor_add_parameter(desc, &filler, "rate", 'r', JackDriverParamUInt, &value, NULL, "Sample rate", NULL);
  198. value.ui = 512U;
  199. jack_driver_descriptor_add_parameter(desc, &filler, "periodsize", 'p', JackDriverParamUInt, &value, NULL, "Period size", NULL);
  200. strcpy(value.str, "default device");
  201. jack_driver_descriptor_add_parameter(desc, &filler, "device", 'd', JackDriverParamString, &value, NULL, "PortAudio device name", NULL);
  202. value.i = true;
  203. jack_driver_descriptor_add_parameter(desc, &filler, "list-devices", 'l', JackDriverParamBool, &value, NULL, "Display available PortAudio devices", NULL);
  204. value.ui = 0;
  205. jack_driver_descriptor_add_parameter(desc, &filler, "quality", 'q', JackDriverParamInt, &value, NULL, "Resample algorithm quality (0 - 4)", NULL);
  206. value.ui = 32768;
  207. jack_driver_descriptor_add_parameter(desc, &filler, "ring-buffer", 'g', JackDriverParamInt, &value, NULL, "Fixed ringbuffer size", "Fixed ringbuffer size (if not set => automatic adaptative)");
  208. return desc;
  209. }
  210. #ifdef __cplusplus
  211. }
  212. #endif