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.

287 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+1].time1 - fTable[i].time1,
  46. fTable[i+1].time2 - fTable[i].time2,
  47. fTable[i].r1, fTable[i].r2, fTable[i].pos1, fTable[i].pos2);
  48. }
  49. fclose(file);
  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 \"Consumer interrupt period\" with lines,");
  60. fprintf(file, buffer);
  61. sprintf(buffer, "\"JackAudioAdapter.log\" using 3 title \"Producer interrupt period\" 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::ResetRingBuffers()
  134. {
  135. for (int i = 0; i < fCaptureChannels; i++)
  136. fCaptureRingBuffer[i]->Reset();
  137. for (int i = 0; i < fPlaybackChannels; i++)
  138. fPlaybackRingBuffer[i]->Reset();
  139. }
  140. void JackAudioAdapterInterface::ResampleFactor ( jack_time_t& frame1, jack_time_t& frame2 )
  141. {
  142. jack_time_t time = GetMicroSeconds();
  143. if (!fRunning) {
  144. // Init DLL
  145. fRunning = true;
  146. fHostDLL.Init(time);
  147. fAdaptedDLL.Init(time);
  148. frame1 = 1;
  149. frame2 = 1;
  150. } else {
  151. // DLL
  152. fAdaptedDLL.IncFrame(time);
  153. jack_nframes_t time1 = fHostDLL.Time2Frames(time);
  154. jack_nframes_t time2 = fAdaptedDLL.Time2Frames(time);
  155. frame1 = time1;
  156. frame2 = time2;
  157. jack_log("JackAudioAdapterInterface::ResampleFactor time1 = %ld time2 = %ld src_ratio_input = %f src_ratio_output = %f",
  158. long(time1), long(time2), double(time1) / double(time2), double(time2) / double(time1));
  159. }
  160. }
  161. void JackAudioAdapterInterface::Reset()
  162. {
  163. ResetRingBuffers();
  164. fRunning = false;
  165. }
  166. #ifdef TARGET_OS_IPHONE
  167. void JackAudioAdapterInterface::Create()
  168. {}
  169. #else
  170. void JackAudioAdapterInterface::Create()
  171. {
  172. //ringbuffers
  173. fCaptureRingBuffer = new JackResampler*[fCaptureChannels];
  174. fPlaybackRingBuffer = new JackResampler*[fPlaybackChannels];
  175. for (int i = 0; i < fCaptureChannels; i++ )
  176. fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fQuality, fRingbufferSize);
  177. for (int i = 0; i < fPlaybackChannels; i++ )
  178. fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fQuality, fRingbufferSize);
  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. jack_time_t time1, time2;
  198. ResampleFactor(time1, time2);
  199. // Push/pull from ringbuffer
  200. for (int i = 0; i < fCaptureChannels; i++) {
  201. fCaptureRingBuffer[i]->SetRatio(time1, time2);
  202. if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], frames) < frames)
  203. failure = true;
  204. }
  205. for (int i = 0; i < fPlaybackChannels; i++) {
  206. fPlaybackRingBuffer[i]->SetRatio(time2, time1);
  207. if (fPlaybackRingBuffer[i]->ReadResample(outputBuffer[i], frames) < frames)
  208. failure = true;
  209. }
  210. #ifdef JACK_MONITOR
  211. fTable.Write(time1, time2, double(time1) / double(time2), double(time2) / double(time1),
  212. fCaptureRingBuffer[0]->ReadSpace(), fPlaybackRingBuffer[0]->WriteSpace());
  213. #endif
  214. // Reset all ringbuffers in case of failure
  215. if (failure) {
  216. jack_error("JackAudioAdapterInterface::PushAndPull ringbuffer failure... reset");
  217. ResetRingBuffers();
  218. return -1;
  219. } else {
  220. return 0;
  221. }
  222. }
  223. int JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int frames)
  224. {
  225. bool failure = false;
  226. fHostDLL.IncFrame(GetMicroSeconds());
  227. // Push/pull from ringbuffer
  228. for (int i = 0; i < fCaptureChannels; i++) {
  229. if (fCaptureRingBuffer[i]->Read(inputBuffer[i], frames) < frames)
  230. failure = true;
  231. }
  232. for (int i = 0; i < fPlaybackChannels; i++) {
  233. if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], frames) < frames)
  234. failure = true;
  235. }
  236. // Reset all ringbuffers in case of failure
  237. if (failure) {
  238. jack_error("JackCallbackAudioAdapter::PullAndPush ringbuffer failure... reset");
  239. Reset();
  240. return -1;
  241. } else {
  242. return 0;
  243. }
  244. }
  245. } // namespace