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.

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