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.

236 lines
6.8KB

  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. #include "JackAlsaAdapter.h"
  16. namespace Jack
  17. {
  18. JackAlsaAdapter::JackAlsaAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params)
  19. :JackAudioAdapterInterface(buffer_size, sample_rate)
  20. ,fThread(this), fAudioInterface(GetInputs(), GetOutputs(), buffer_size, sample_rate)
  21. {
  22. const JSList* node;
  23. const jack_driver_param_t* param;
  24. fCaptureChannels = 2;
  25. fPlaybackChannels = 2;
  26. for (node = params; node; node = jack_slist_next(node)) {
  27. param = (const jack_driver_param_t*) node->data;
  28. switch (param->character) {
  29. case 'i':
  30. fCaptureChannels = param->value.ui;
  31. break;
  32. case 'o':
  33. fPlaybackChannels = param->value.ui;
  34. break;
  35. case 'C':
  36. break;
  37. case 'P':
  38. break;
  39. case 'D':
  40. break;
  41. case 'n':
  42. break;
  43. case 'd':
  44. break;
  45. case 'r':
  46. SetAudioSampleRate(param->value.ui);
  47. break;
  48. }
  49. }
  50. }
  51. int JackAlsaAdapter::Open()
  52. {
  53. if (fAudioInterface.open() == 0) {
  54. fAudioInterface.longinfo();
  55. fThread.AcquireRealTime(85);
  56. return fThread.StartSync();
  57. } else {
  58. return -1;
  59. }
  60. }
  61. int JackAlsaAdapter::Close()
  62. {
  63. #ifdef DEBUG
  64. fTable.Save();
  65. #endif
  66. fThread.Stop();
  67. return fAudioInterface.close();
  68. }
  69. bool JackAlsaAdapter::Init()
  70. {
  71. fAudioInterface.write();
  72. fAudioInterface.write();
  73. return true;
  74. }
  75. bool JackAlsaAdapter::Execute()
  76. {
  77. if (fAudioInterface.read() < 0)
  78. return false;
  79. bool failure = false;
  80. jack_nframes_t time1, time2;
  81. ResampleFactor(time1, time2);
  82. for (int i = 0; i < fCaptureChannels; i++) {
  83. fCaptureRingBuffer[i]->SetRatio(time1, time2);
  84. if (fCaptureRingBuffer[i]->WriteResample(fAudioInterface.fInputSoftChannels[i], fBufferSize) < fBufferSize)
  85. failure = true;
  86. }
  87. for (int i = 0; i < fPlaybackChannels; i++) {
  88. fPlaybackRingBuffer[i]->SetRatio(time2, time1);
  89. if (fPlaybackRingBuffer[i]->ReadResample(fAudioInterface.fOutputSoftChannels[i], fBufferSize) < fBufferSize)
  90. failure = true;
  91. }
  92. #ifdef DEBUG
  93. fTable.Write(time1, time2, double(time1) / double(time2), double(time2) / double(time1),
  94. fCaptureRingBuffer[0]->ReadSpace(), fPlaybackRingBuffer[0]->WriteSpace());
  95. #endif
  96. if (fAudioInterface.write() < 0)
  97. return false;
  98. // Reset all ringbuffers in case of failure
  99. if (failure) {
  100. jack_error("JackAlsaAdapter::Execute ringbuffer failure... reset");
  101. ResetRingBuffers();
  102. }
  103. return true;
  104. }
  105. int JackAlsaAdapter::SetBufferSize(jack_nframes_t buffer_size)
  106. {
  107. JackAudioAdapterInterface::SetBufferSize(buffer_size);
  108. Close();
  109. return Open();
  110. }
  111. } // namespace
  112. #ifdef __cplusplus
  113. extern "C"
  114. {
  115. #endif
  116. EXPORT jack_driver_desc_t* jack_get_descriptor()
  117. {
  118. jack_driver_desc_t *desc;
  119. jack_driver_param_desc_t * params;
  120. unsigned int i;
  121. desc = (jack_driver_desc_t*)calloc(1, sizeof(jack_driver_desc_t));
  122. strcpy (desc->name, "alsa-adapter");
  123. desc->nparams = 8;
  124. params = (jack_driver_param_desc_t*)calloc(desc->nparams, sizeof(jack_driver_param_desc_t));
  125. i = 0;
  126. strcpy(params[i].name, "capture");
  127. params[i].character = 'C';
  128. params[i].type = JackDriverParamString;
  129. strcpy (params[i].value.str, "none");
  130. strcpy (params[i].short_desc,
  131. "Provide capture ports. Optionally set device");
  132. strcpy (params[i].long_desc, params[i].short_desc);
  133. i++;
  134. strcpy(params[i].name, "playback");
  135. params[i].character = 'P';
  136. params[i].type = JackDriverParamString;
  137. strcpy(params[i].value.str, "none");
  138. strcpy(params[i].short_desc,
  139. "Provide playback ports. Optionally set device");
  140. strcpy(params[i].long_desc, params[i].short_desc);
  141. i++;
  142. strcpy(params[i].name, "device");
  143. params[i].character = 'd';
  144. params[i].type = JackDriverParamString;
  145. strcpy(params[i].value.str, "hw:0");
  146. strcpy(params[i].short_desc, "ALSA device name");
  147. strcpy(params[i].long_desc, params[i].short_desc);
  148. i++;
  149. strcpy (params[i].name, "rate");
  150. params[i].character = 'r';
  151. params[i].type = JackDriverParamUInt;
  152. params[i].value.ui = 48000U;
  153. strcpy(params[i].short_desc, "Sample rate");
  154. strcpy(params[i].long_desc, params[i].short_desc);
  155. i++;
  156. strcpy(params[i].name, "nperiods");
  157. params[i].character = 'n';
  158. params[i].type = JackDriverParamUInt;
  159. params[i].value.ui = 2U;
  160. strcpy(params[i].short_desc, "Number of periods of playback latency");
  161. strcpy(params[i].long_desc, params[i].short_desc);
  162. i++;
  163. strcpy(params[i].name, "duplex");
  164. params[i].character = 'D';
  165. params[i].type = JackDriverParamBool;
  166. params[i].value.i = 1;
  167. strcpy(params[i].short_desc,
  168. "Provide both capture and playback ports");
  169. strcpy(params[i].long_desc, params[i].short_desc);
  170. i++;
  171. strcpy(params[i].name, "inchannels");
  172. params[i].character = 'i';
  173. params[i].type = JackDriverParamUInt;
  174. params[i].value.i = 0;
  175. strcpy(params[i].short_desc,
  176. "Number of capture channels (defaults to hardware max)");
  177. strcpy(params[i].long_desc, params[i].short_desc);
  178. i++;
  179. strcpy(params[i].name, "outchannels");
  180. params[i].character = 'o';
  181. params[i].type = JackDriverParamUInt;
  182. params[i].value.i = 0;
  183. strcpy(params[i].short_desc,
  184. "Number of playback channels (defaults to hardware max)");
  185. strcpy(params[i].long_desc, params[i].short_desc);
  186. desc->params = params;
  187. return desc;
  188. }
  189. #ifdef __cplusplus
  190. }
  191. #endif