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.

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