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.

184 lines
5.3KB

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