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.

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