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.

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