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.

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