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.

302 lines
12KB

  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].time1, fTable[i].time2,
  41. fTable[i].r1, fTable[i].r2, fTable[i].pos1, fTable[i].pos2);
  42. }
  43. fclose(file);
  44. // No used for now
  45. // Adapter timing 1
  46. file = fopen("AdapterTiming1.plot", "w");
  47. fprintf(file, "set multiplot\n");
  48. fprintf(file, "set grid\n");
  49. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  50. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  51. fprintf(file, "set xlabel \"audio cycles\"\n");
  52. fprintf(file, "set ylabel \"frames\"\n");
  53. fprintf(file, "plot ");
  54. sprintf(buffer, "\"JackAudioAdapter.log\" using 2 title \"Ringbuffer error\" with lines,");
  55. fprintf(file, buffer);
  56. sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Ringbuffer error with timing correction\" with lines");
  57. fprintf(file, buffer);
  58. fprintf(file, "\n unset multiplot\n");
  59. fprintf(file, "set output 'AdapterTiming1.svg\n");
  60. fprintf(file, "set terminal svg\n");
  61. fprintf(file, "set multiplot\n");
  62. fprintf(file, "set grid\n");
  63. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  64. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  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\n");
  71. fprintf(file, buffer);
  72. fprintf(file, "unset multiplot\n");
  73. fprintf(file, "unset output\n");
  74. fclose(file);
  75. // Adapter timing 2
  76. file = fopen("AdapterTiming2.plot", "w");
  77. fprintf(file, "set multiplot\n");
  78. fprintf(file, "set grid\n");
  79. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  80. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  81. fprintf(file, "set xlabel \"audio cycles\"\n");
  82. fprintf(file, "set ylabel \"resampling ratio\"\n");
  83. fprintf(file, "plot ");
  84. sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
  85. fprintf(file, buffer);
  86. sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
  87. fprintf(file, buffer);
  88. fprintf(file, "\n unset multiplot\n");
  89. fprintf(file, "set output 'AdapterTiming2.svg\n");
  90. fprintf(file, "set terminal svg\n");
  91. fprintf(file, "set multiplot\n");
  92. fprintf(file, "set grid\n");
  93. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  94. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  95. fprintf(file, "set xlabel \"audio cycles\"\n");
  96. fprintf(file, "set ylabel \"resampling ratio\"\n");
  97. fprintf(file, "plot ");
  98. sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
  99. fprintf(file, buffer);
  100. sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines\n");
  101. fprintf(file, buffer);
  102. fprintf(file, "unset multiplot\n");
  103. fprintf(file, "unset output\n");
  104. fclose(file);
  105. // Adapter timing 3
  106. file = fopen("AdapterTiming3.plot", "w");
  107. fprintf(file, "set multiplot\n");
  108. fprintf(file, "set grid\n");
  109. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  110. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  111. fprintf(file, "set xlabel \"audio cycles\"\n");
  112. fprintf(file, "set ylabel \"frames\"\n");
  113. fprintf(file, "plot ");
  114. sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
  115. fprintf(file, buffer);
  116. sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
  117. fprintf(file, buffer);
  118. fprintf(file, "\n unset multiplot\n");
  119. fprintf(file, "set output 'AdapterTiming3.svg\n");
  120. fprintf(file, "set terminal svg\n");
  121. fprintf(file, "set multiplot\n");
  122. fprintf(file, "set grid\n");
  123. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  124. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  125. fprintf(file, "set xlabel \"audio cycles\"\n");
  126. fprintf(file, "set ylabel \"frames\"\n");
  127. fprintf(file, "plot ");
  128. sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
  129. fprintf(file, buffer);
  130. sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines\n");
  131. fprintf(file, buffer);
  132. fprintf(file, "unset multiplot\n");
  133. fprintf(file, "unset output\n");
  134. fclose(file);
  135. }
  136. #endif
  137. void JackAudioAdapterInterface::GrowRingBufferSize()
  138. {
  139. fRingbufferCurSize *= 2;
  140. }
  141. void JackAudioAdapterInterface::AdaptRingBufferSize()
  142. {
  143. if (fHostBufferSize > fAdaptedBufferSize)
  144. fRingbufferCurSize = 4 * fHostBufferSize;
  145. else
  146. fRingbufferCurSize = 4 * fAdaptedBufferSize;
  147. }
  148. void JackAudioAdapterInterface::ResetRingBuffers()
  149. {
  150. if (fRingbufferCurSize > DEFAULT_RB_SIZE)
  151. fRingbufferCurSize = DEFAULT_RB_SIZE;
  152. for (int i = 0; i < fCaptureChannels; i++)
  153. fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
  154. for (int i = 0; i < fPlaybackChannels; i++)
  155. fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
  156. }
  157. void JackAudioAdapterInterface::Reset()
  158. {
  159. ResetRingBuffers();
  160. fRunning = false;
  161. }
  162. void JackAudioAdapterInterface::Create()
  163. {
  164. //ringbuffers
  165. fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
  166. fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
  167. if (fAdaptative) {
  168. AdaptRingBufferSize();
  169. jack_info("Ringbuffer automatic adaptative mode size = %d frames", fRingbufferCurSize);
  170. } else {
  171. if (fRingbufferCurSize > DEFAULT_RB_SIZE)
  172. fRingbufferCurSize = DEFAULT_RB_SIZE;
  173. jack_info("Fixed ringbuffer size = %d frames", fRingbufferCurSize);
  174. }
  175. for (int i = 0; i < fCaptureChannels; i++ ) {
  176. fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
  177. fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
  178. }
  179. for (int i = 0; i < fPlaybackChannels; i++ ) {
  180. fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
  181. fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
  182. }
  183. if (fCaptureChannels > 0)
  184. jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
  185. if (fPlaybackChannels > 0)
  186. jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
  187. }
  188. void JackAudioAdapterInterface::Destroy()
  189. {
  190. for (int i = 0; i < fCaptureChannels; i++ )
  191. delete ( fCaptureRingBuffer[i] );
  192. for (int i = 0; i < fPlaybackChannels; i++ )
  193. delete ( fPlaybackRingBuffer[i] );
  194. delete[] fCaptureRingBuffer;
  195. delete[] fPlaybackRingBuffer;
  196. }
  197. int JackAudioAdapterInterface::PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int frames)
  198. {
  199. bool failure = false;
  200. fRunning = true;
  201. // Finer estimation of the position in the ringbuffer
  202. int delta_frames = (fPullAndPushTime > 0) ? (int)((float(long(GetMicroSeconds() - fPullAndPushTime)) * float(fAdaptedSampleRate)) / 1000000.f) : 0;
  203. double ratio = 1;
  204. // TODO : done like this just to avoid crash when input only or output only...
  205. if (fCaptureChannels > 0)
  206. ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetError() - delta_frames);
  207. else if (fPlaybackChannels > 0)
  208. ratio = fPIControler.GetRatio(fPlaybackRingBuffer[0]->GetError() - delta_frames);
  209. #ifdef JACK_MONITOR
  210. if (fCaptureRingBuffer[0] != NULL)
  211. fTable.Write(fCaptureRingBuffer[0]->GetError(), fCaptureRingBuffer[0]->GetError() - delta_frames, ratio, 1/ratio, fCaptureRingBuffer[0]->ReadSpace(), fCaptureRingBuffer[0]->ReadSpace());
  212. #endif
  213. // Push/pull from ringbuffer
  214. for (int i = 0; i < fCaptureChannels; i++) {
  215. fCaptureRingBuffer[i]->SetRatio(ratio);
  216. if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], frames) < frames)
  217. failure = true;
  218. }
  219. for (int i = 0; i < fPlaybackChannels; i++) {
  220. fPlaybackRingBuffer[i]->SetRatio(1/ratio);
  221. if (fPlaybackRingBuffer[i]->ReadResample(outputBuffer[i], frames) < frames)
  222. failure = true;
  223. }
  224. // Reset all ringbuffers in case of failure
  225. if (failure) {
  226. jack_error("JackAudioAdapterInterface::PushAndPull ringbuffer failure... reset");
  227. if (fAdaptative) {
  228. GrowRingBufferSize();
  229. jack_info("Ringbuffer size = %d frames", fRingbufferCurSize);
  230. }
  231. ResetRingBuffers();
  232. return -1;
  233. } else {
  234. return 0;
  235. }
  236. }
  237. int JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int frames)
  238. {
  239. fPullAndPushTime = GetMicroSeconds();
  240. if (!fRunning)
  241. return 0;
  242. int res = 0;
  243. // Push/pull from ringbuffer
  244. for (int i = 0; i < fCaptureChannels; i++) {
  245. if (fCaptureRingBuffer[i]->Read(inputBuffer[i], frames) < frames)
  246. res = -1;
  247. }
  248. for (int i = 0; i < fPlaybackChannels; i++) {
  249. if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], frames) < frames)
  250. res = -1;
  251. }
  252. return res;
  253. }
  254. } // namespace