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.

262 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. #include "JackAudioAdapter.h"
  16. #include "JackLibSampleRateResampler.h"
  17. #include "JackTime.h"
  18. #include <stdio.h>
  19. namespace Jack
  20. {
  21. #ifdef JACK_MONITOR
  22. void MeasureTable::Write(int time1, int time2, float r1, float r2, int pos1, int pos2)
  23. {
  24. int pos = (++fCount) % TABLE_MAX;
  25. fTable[pos].time1 = time1;
  26. fTable[pos].time2 = time2;
  27. fTable[pos].r1 = r1;
  28. fTable[pos].r2 = r2;
  29. fTable[pos].pos1 = pos1;
  30. fTable[pos].pos2 = pos2;
  31. }
  32. void MeasureTable::Save(unsigned int fHostBufferSize, unsigned int fHostSampleRate, unsigned int fAdaptedSampleRate, unsigned int fAdaptedBufferSize)
  33. {
  34. char buffer[1024];
  35. FILE* file = fopen("JackAudioAdapter.log", "w");
  36. int max = (fCount) % TABLE_MAX - 1;
  37. for (int i = 1; i < max; i++)
  38. {
  39. fprintf(file, "%d \t %d \t %d \t %f \t %f \t %d \t %d \n",
  40. fTable[i].delta, fTable[i+1].time1 - fTable[i].time1,
  41. fTable[i+1].time2 - fTable[i].time2,
  42. fTable[i].r1, fTable[i].r2, fTable[i].pos1, fTable[i].pos2);
  43. }
  44. fclose(file);
  45. /* No used for now
  46. // Adapter timing 1
  47. file = fopen("AdapterTiming1.plot", "w");
  48. fprintf(file, "set multiplot\n");
  49. fprintf(file, "set grid\n");
  50. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  51. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  52. fprintf(file, "set xlabel \"audio cycles\"\n");
  53. fprintf(file, "set ylabel \"frames\"\n");
  54. fprintf(file, "plot ");
  55. sprintf(buffer, "\"JackAudioAdapter.log\" using 2 title \"Consumer interrupt period\" with lines,");
  56. fprintf(file, buffer);
  57. sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Producer interrupt period\" with lines");
  58. fprintf(file, buffer);
  59. fprintf(file, "\n unset multiplot\n");
  60. fprintf(file, "set output 'AdapterTiming1.pdf\n");
  61. fprintf(file, "set terminal pdf\n");
  62. fprintf(file, "set multiplot\n");
  63. fprintf(file, "set grid\n");
  64. fprintf(file, "set title \"Audio adapter timing\"\n");
  65. fprintf(file, "set xlabel \"audio cycles\"\n");
  66. fprintf(file, "set ylabel \"frames\"\n");
  67. fprintf(file, "plot ");
  68. sprintf(buffer, "\"JackAudioAdapter.log\" using 2 title \"Consumer interrupt period\" with lines,");
  69. fprintf(file, buffer);
  70. sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Producer interrupt period\" with lines");
  71. fprintf(file, buffer);
  72. fclose(file);
  73. */
  74. // Adapter timing 2
  75. file = fopen("AdapterTiming2.plot", "w");
  76. fprintf(file, "set multiplot\n");
  77. fprintf(file, "set grid\n");
  78. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  79. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  80. fprintf(file, "set xlabel \"audio cycles\"\n");
  81. fprintf(file, "set ylabel \"resampling ratio\"\n");
  82. fprintf(file, "plot ");
  83. sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
  84. fprintf(file, buffer);
  85. sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
  86. fprintf(file, buffer);
  87. fprintf(file, "\n unset multiplot\n");
  88. fprintf(file, "set output 'AdapterTiming2.pdf\n");
  89. fprintf(file, "set terminal pdf\n");
  90. fprintf(file, "set multiplot\n");
  91. fprintf(file, "set grid\n");
  92. fprintf(file, "set title \"Audio adapter timing\"\n");
  93. fprintf(file, "set xlabel \"audio cycles\"\n");
  94. fprintf(file, "set ylabel \"resampling ratio\"\n");
  95. fprintf(file, "plot ");
  96. sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
  97. fprintf(file, buffer);
  98. sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
  99. fprintf(file, buffer);
  100. fclose(file);
  101. // Adapter timing 3
  102. file = fopen("AdapterTiming3.plot", "w");
  103. fprintf(file, "set multiplot\n");
  104. fprintf(file, "set grid\n");
  105. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  106. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  107. fprintf(file, "set xlabel \"audio cycles\"\n");
  108. fprintf(file, "set ylabel \"frames\"\n");
  109. fprintf(file, "plot ");
  110. sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
  111. fprintf(file, buffer);
  112. sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
  113. fprintf(file, buffer);
  114. fprintf(file, "\n unset multiplot\n");
  115. fprintf(file, "set output 'AdapterTiming3.pdf\n");
  116. fprintf(file, "set terminal pdf\n");
  117. fprintf(file, "set multiplot\n");
  118. fprintf(file, "set grid\n");
  119. fprintf(file, "set title \"Audio adapter timing\"\n");
  120. fprintf(file, "set xlabel \"audio cycles\"\n");
  121. fprintf(file, "set ylabel \"frames\"\n");
  122. fprintf(file, "plot ");
  123. sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
  124. fprintf(file, buffer);
  125. sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
  126. fprintf(file, buffer);
  127. fclose(file);
  128. }
  129. #endif
  130. void JackAudioAdapterInterface::ResetRingBuffers()
  131. {
  132. for (int i = 0; i < fCaptureChannels; i++)
  133. fCaptureRingBuffer[i]->Reset();
  134. for (int i = 0; i < fPlaybackChannels; i++)
  135. fPlaybackRingBuffer[i]->Reset();
  136. }
  137. void JackAudioAdapterInterface::Reset()
  138. {
  139. ResetRingBuffers();
  140. fRunning = false;
  141. }
  142. void JackAudioAdapterInterface::Create()
  143. {
  144. //ringbuffers
  145. fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
  146. fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
  147. for (int i = 0; i < fCaptureChannels; i++ )
  148. fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fQuality, fRingbufferSize);
  149. for (int i = 0; i < fPlaybackChannels; i++ )
  150. fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fQuality, fRingbufferSize);
  151. if (fCaptureChannels > 0)
  152. jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
  153. if (fPlaybackChannels > 0)
  154. jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
  155. }
  156. void JackAudioAdapterInterface::Destroy()
  157. {
  158. for (int i = 0; i < fCaptureChannels; i++ )
  159. delete ( fCaptureRingBuffer[i] );
  160. for (int i = 0; i < fPlaybackChannels; i++ )
  161. delete ( fPlaybackRingBuffer[i] );
  162. delete[] fCaptureRingBuffer;
  163. delete[] fPlaybackRingBuffer;
  164. }
  165. int JackAudioAdapterInterface::PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int inNumberFrames)
  166. {
  167. bool failure = false;
  168. fRunning = true;
  169. /*
  170. Finer estimation of the position in the ringbuffer ??
  171. int delta_frames = (int)(float(long(GetMicroSeconds() - fPullAndPushTime)) * float(fAdaptedSampleRate)) / 1000000.f;
  172. double ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetOffset() - delta_frames);
  173. */
  174. double ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetOffset());
  175. // Push/pull from ringbuffer
  176. for (int i = 0; i < fCaptureChannels; i++) {
  177. fCaptureRingBuffer[i]->SetRatio(ratio);
  178. if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], inNumberFrames) < inNumberFrames)
  179. failure = true;
  180. }
  181. for (int i = 0; i < fPlaybackChannels; i++) {
  182. fPlaybackRingBuffer[i]->SetRatio(1 / ratio);
  183. if (fPlaybackRingBuffer[i]->ReadResample(outputBuffer[i], inNumberFrames) < inNumberFrames)
  184. failure = true;
  185. }
  186. #ifdef JACK_MONITOR
  187. fTable.Write(0, 0, ratio, 1/ratio, fCaptureRingBuffer[0]->ReadSpace(), fPlaybackRingBuffer[0]->WriteSpace());
  188. #endif
  189. // Reset all ringbuffers in case of failure
  190. if (failure) {
  191. jack_error("JackAudioAdapterInterface::PushAndPull ringbuffer failure... reset");
  192. ResetRingBuffers();
  193. return -1;
  194. } else {
  195. return 0;
  196. }
  197. }
  198. int JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int inNumberFrames)
  199. {
  200. bool failure = false;
  201. fPullAndPushTime = GetMicroSeconds();
  202. // Push/pull from ringbuffer
  203. for (int i = 0; i < fCaptureChannels; i++) {
  204. if (fCaptureRingBuffer[i]->Read(inputBuffer[i], inNumberFrames) < inNumberFrames)
  205. failure = true;
  206. }
  207. for (int i = 0; i < fPlaybackChannels; i++) {
  208. if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], inNumberFrames) < inNumberFrames)
  209. failure = true;
  210. }
  211. // Reset all ringbuffers in case of failure
  212. if (failure) {
  213. jack_error("JackCallbackAudioAdapter::PullAndPush ringbuffer failure... reset");
  214. Reset();
  215. return -1;
  216. } else {
  217. return 0;
  218. }
  219. }
  220. } // namespace