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.

295 lines
11KB

  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\"\n");
  69. fprintf(file, "set xlabel \"audio cycles\"\n");
  70. fprintf(file, "set ylabel \"frames\"\n");
  71. fprintf(file, "plot ");
  72. sprintf(buffer, "\"JackAudioAdapter.log\" using 2 title \"Consumer interrupt period\" with lines,");
  73. fprintf(file, buffer);
  74. sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Producer interrupt period\" with lines");
  75. fprintf(file, buffer);
  76. fclose(file);
  77. // Adapter timing 2
  78. file = fopen("AdapterTiming2.plot", "w");
  79. fprintf(file, "set multiplot\n");
  80. fprintf(file, "set grid\n");
  81. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  82. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  83. fprintf(file, "set xlabel \"audio cycles\"\n");
  84. fprintf(file, "set ylabel \"resampling ratio\"\n");
  85. fprintf(file, "plot ");
  86. sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
  87. fprintf(file, buffer);
  88. sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
  89. fprintf(file, buffer);
  90. fprintf(file, "\n unset multiplot\n");
  91. fprintf(file, "set output 'AdapterTiming2.pdf\n");
  92. fprintf(file, "set terminal pdf\n");
  93. fprintf(file, "set multiplot\n");
  94. fprintf(file, "set grid\n");
  95. fprintf(file, "set title \"Audio adapter timing\"\n");
  96. fprintf(file, "set xlabel \"audio cycles\"\n");
  97. fprintf(file, "set ylabel \"resampling ratio\"\n");
  98. fprintf(file, "plot ");
  99. sprintf(buffer, "\"JackAudioAdapter.log\" using 4 title \"Ratio 1\" with lines,");
  100. fprintf(file, buffer);
  101. sprintf(buffer, "\"JackAudioAdapter.log\" using 5 title \"Ratio 2\" with lines");
  102. fprintf(file, buffer);
  103. fclose(file);
  104. // Adapter timing 3
  105. file = fopen("AdapterTiming3.plot", "w");
  106. fprintf(file, "set multiplot\n");
  107. fprintf(file, "set grid\n");
  108. fprintf(file, "set title \"Audio adapter timing: host [rate = %.1f kHz buffer = %d frames] adapter [rate = %.1f kHz buffer = %d frames] \"\n"
  109. ,float(fHostSampleRate)/1000.f, fHostBufferSize, float(fAdaptedSampleRate)/1000.f, fAdaptedBufferSize);
  110. fprintf(file, "set xlabel \"audio cycles\"\n");
  111. fprintf(file, "set ylabel \"frames\"\n");
  112. fprintf(file, "plot ");
  113. sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
  114. fprintf(file, buffer);
  115. sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
  116. fprintf(file, buffer);
  117. fprintf(file, "\n unset multiplot\n");
  118. fprintf(file, "set output 'AdapterTiming3.pdf\n");
  119. fprintf(file, "set terminal pdf\n");
  120. fprintf(file, "set multiplot\n");
  121. fprintf(file, "set grid\n");
  122. fprintf(file, "set title \"Audio adapter timing\"\n");
  123. fprintf(file, "set xlabel \"audio cycles\"\n");
  124. fprintf(file, "set ylabel \"frames\"\n");
  125. fprintf(file, "plot ");
  126. sprintf(buffer, "\"JackAudioAdapter.log\" using 6 title \"Frames position in consumer ringbuffer\" with lines,");
  127. fprintf(file, buffer);
  128. sprintf(buffer, "\"JackAudioAdapter.log\" using 7 title \"Frames position in producer ringbuffer\" with lines");
  129. fprintf(file, buffer);
  130. fclose(file);
  131. }
  132. #endif
  133. void JackAudioAdapterInterface::GrowRingBufferSize()
  134. {
  135. fRingbufferCurSize *= 2;
  136. }
  137. void JackAudioAdapterInterface::AdaptRingBufferSize()
  138. {
  139. if (fHostBufferSize > fAdaptedBufferSize)
  140. fRingbufferCurSize = 4 * fHostBufferSize;
  141. else
  142. fRingbufferCurSize = 4 * fAdaptedBufferSize;
  143. }
  144. void JackAudioAdapterInterface::ResetRingBuffers()
  145. {
  146. if (fRingbufferCurSize > DEFAULT_RB_SIZE)
  147. fRingbufferCurSize = DEFAULT_RB_SIZE;
  148. for (int i = 0; i < fCaptureChannels; i++)
  149. fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
  150. for (int i = 0; i < fPlaybackChannels; i++)
  151. fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
  152. }
  153. void JackAudioAdapterInterface::Reset()
  154. {
  155. ResetRingBuffers();
  156. fRunning = false;
  157. }
  158. #ifdef TARGET_OS_IPHONE
  159. void JackAudioAdapterInterface::Create()
  160. {}
  161. #else
  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. #endif
  189. void JackAudioAdapterInterface::Destroy()
  190. {
  191. for (int i = 0; i < fCaptureChannels; i++ )
  192. delete ( fCaptureRingBuffer[i] );
  193. for (int i = 0; i < fPlaybackChannels; i++ )
  194. delete ( fPlaybackRingBuffer[i] );
  195. delete[] fCaptureRingBuffer;
  196. delete[] fPlaybackRingBuffer;
  197. }
  198. int JackAudioAdapterInterface::PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int frames)
  199. {
  200. bool failure = false;
  201. fRunning = true;
  202. // Finer estimation of the position in the ringbuffer
  203. int delta_frames = (fPullAndPushTime > 0) ? (int)((float(long(GetMicroSeconds() - fPullAndPushTime)) * float(fAdaptedSampleRate)) / 1000000.f) : 0;
  204. double ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetError() - delta_frames);
  205. #ifdef JACK_MONITOR
  206. fTable.Write(fCaptureRingBuffer[0]->GetError(), fCaptureRingBuffer[0]->GetError() - delta_frames, ratio, 1/ratio, fCaptureRingBuffer[0]->ReadSpace(), fCaptureRingBuffer[0]->ReadSpace());
  207. #endif
  208. // Push/pull from ringbuffer
  209. for (int i = 0; i < fCaptureChannels; i++) {
  210. fCaptureRingBuffer[i]->SetRatio(ratio);
  211. if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], frames) < frames)
  212. failure = true;
  213. }
  214. for (int i = 0; i < fPlaybackChannels; i++) {
  215. fPlaybackRingBuffer[i]->SetRatio(1/ratio);
  216. if (fPlaybackRingBuffer[i]->ReadResample(outputBuffer[i], frames) < frames)
  217. failure = true;
  218. }
  219. // Reset all ringbuffers in case of failure
  220. if (failure) {
  221. jack_error("JackAudioAdapterInterface::PushAndPull ringbuffer failure... reset");
  222. if (fAdaptative) {
  223. GrowRingBufferSize();
  224. jack_info("Ringbuffer size = %d frames", fRingbufferCurSize);
  225. }
  226. ResetRingBuffers();
  227. return -1;
  228. } else {
  229. return 0;
  230. }
  231. }
  232. int JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int frames)
  233. {
  234. fPullAndPushTime = GetMicroSeconds();
  235. if (!fRunning)
  236. return 0;
  237. int res = 0;
  238. // Push/pull from ringbuffer
  239. for (int i = 0; i < fCaptureChannels; i++) {
  240. if (fCaptureRingBuffer[i]->Read(inputBuffer[i], frames) < frames)
  241. res = -1;
  242. }
  243. for (int i = 0; i < fPlaybackChannels; i++) {
  244. if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], frames) < frames)
  245. res = -1;
  246. }
  247. return res;
  248. }
  249. } // namespace