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.

394 lines
14KB

  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 "JackError.h"
  24. #include <stdio.h>
  25. namespace Jack
  26. {
  27. #ifdef JACK_MONITOR
  28. void MeasureTable::Write(int time1, int time2, float r1, float r2, int pos1, int pos2)
  29. {
  30. int pos = (++fCount) % TABLE_MAX;
  31. fTable[pos].time1 = time1;
  32. fTable[pos].time2 = time2;
  33. fTable[pos].r1 = r1;
  34. fTable[pos].r2 = r2;
  35. fTable[pos].pos1 = pos1;
  36. fTable[pos].pos2 = pos2;
  37. }
  38. void MeasureTable::Save(unsigned int fHostBufferSize, unsigned int fHostSampleRate, unsigned int fAdaptedSampleRate, unsigned int fAdaptedBufferSize)
  39. {
  40. FILE* file = fopen("JackAudioAdapter.log", "w");
  41. int max = (fCount) % TABLE_MAX - 1;
  42. for (int i = 1; i < max; i++) {
  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. }
  141. void JackAudioAdapterInterface::ResetRingBuffers()
  142. {
  143. if (fRingbufferCurSize > DEFAULT_RB_SIZE) {
  144. fRingbufferCurSize = DEFAULT_RB_SIZE;
  145. }
  146. for (int i = 0; i < fCaptureChannels; i++) {
  147. fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
  148. }
  149. for (int i = 0; i < fPlaybackChannels; i++) {
  150. fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
  151. }
  152. }
  153. void JackAudioAdapterInterface::Reset()
  154. {
  155. ResetRingBuffers();
  156. fRunning = false;
  157. }
  158. #ifdef MY_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. }
  174. jack_info("Fixed ringbuffer size = %d frames", fRingbufferCurSize);
  175. }
  176. for (int i = 0; i < fCaptureChannels; i++ ) {
  177. fCaptureRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
  178. fCaptureRingBuffer[i]->Reset(fRingbufferCurSize);
  179. }
  180. for (int i = 0; i < fPlaybackChannels; i++ ) {
  181. fPlaybackRingBuffer[i] = new JackLibSampleRateResampler(fQuality);
  182. fPlaybackRingBuffer[i]->Reset(fRingbufferCurSize);
  183. }
  184. if (fCaptureChannels > 0) {
  185. jack_log("ReadSpace = %ld", fCaptureRingBuffer[0]->ReadSpace());
  186. }
  187. if (fPlaybackChannels > 0) {
  188. jack_log("WriteSpace = %ld", fPlaybackRingBuffer[0]->WriteSpace());
  189. }
  190. }
  191. #endif
  192. void JackAudioAdapterInterface::Destroy()
  193. {
  194. for (int i = 0; i < fCaptureChannels; i++) {
  195. delete(fCaptureRingBuffer[i]);
  196. }
  197. for (int i = 0; i < fPlaybackChannels; i++) {
  198. delete (fPlaybackRingBuffer[i]);
  199. }
  200. delete[] fCaptureRingBuffer;
  201. delete[] fPlaybackRingBuffer;
  202. }
  203. int JackAudioAdapterInterface::PushAndPull(float** inputBuffer, float** outputBuffer, unsigned int frames)
  204. {
  205. bool failure = false;
  206. fRunning = true;
  207. // Finer estimation of the position in the ringbuffer
  208. int delta_frames = (fPullAndPushTime > 0) ? (int)((float(long(GetMicroSeconds() - fPullAndPushTime)) * float(fAdaptedSampleRate)) / 1000000.f) : 0;
  209. double ratio = 1;
  210. // TODO : done like this just to avoid crash when input only or output only...
  211. if (fCaptureChannels > 0) {
  212. ratio = fPIControler.GetRatio(fCaptureRingBuffer[0]->GetError() - delta_frames);
  213. } else if (fPlaybackChannels > 0) {
  214. ratio = fPIControler.GetRatio(fPlaybackRingBuffer[0]->GetError() - delta_frames);
  215. }
  216. #ifdef JACK_MONITOR
  217. if (fCaptureRingBuffer && fCaptureRingBuffer[0] != NULL)
  218. fTable.Write(fCaptureRingBuffer[0]->GetError(), fCaptureRingBuffer[0]->GetError() - delta_frames, ratio, 1/ratio, fCaptureRingBuffer[0]->ReadSpace(), fCaptureRingBuffer[0]->ReadSpace());
  219. #endif
  220. // Push/pull from ringbuffer
  221. for (int i = 0; i < fCaptureChannels; i++) {
  222. fCaptureRingBuffer[i]->SetRatio(ratio);
  223. if (inputBuffer[i]) {
  224. if (fCaptureRingBuffer[i]->WriteResample(inputBuffer[i], frames) < frames) {
  225. failure = true;
  226. }
  227. }
  228. }
  229. for (int i = 0; i < fPlaybackChannels; i++) {
  230. fPlaybackRingBuffer[i]->SetRatio(1/ratio);
  231. if (outputBuffer[i]) {
  232. if (fPlaybackRingBuffer[i]->ReadResample(outputBuffer[i], frames) < frames) {
  233. failure = true;
  234. }
  235. }
  236. }
  237. // Reset all ringbuffers in case of failure
  238. if (failure) {
  239. jack_error("JackAudioAdapterInterface::PushAndPull ringbuffer failure... reset");
  240. if (fAdaptative) {
  241. GrowRingBufferSize();
  242. jack_info("Ringbuffer size = %d frames", fRingbufferCurSize);
  243. }
  244. ResetRingBuffers();
  245. return -1;
  246. } else {
  247. return 0;
  248. }
  249. }
  250. int JackAudioAdapterInterface::PullAndPush(float** inputBuffer, float** outputBuffer, unsigned int frames)
  251. {
  252. fPullAndPushTime = GetMicroSeconds();
  253. if (!fRunning) {
  254. return 0;
  255. }
  256. int res = 0;
  257. // Push/pull from ringbuffer
  258. for (int i = 0; i < fCaptureChannels; i++) {
  259. if (inputBuffer[i]) {
  260. if (fCaptureRingBuffer[i]->Read(inputBuffer[i], frames) < frames) {
  261. res = -1;
  262. }
  263. }
  264. }
  265. for (int i = 0; i < fPlaybackChannels; i++) {
  266. if (outputBuffer[i]) {
  267. if (fPlaybackRingBuffer[i]->Write(outputBuffer[i], frames) < frames) {
  268. res = -1;
  269. }
  270. }
  271. }
  272. return res;
  273. }
  274. int JackAudioAdapterInterface::SetHostBufferSize(jack_nframes_t buffer_size)
  275. {
  276. fHostBufferSize = buffer_size;
  277. if (fAdaptative) {
  278. AdaptRingBufferSize();
  279. }
  280. return 0;
  281. }
  282. int JackAudioAdapterInterface::SetAdaptedBufferSize(jack_nframes_t buffer_size)
  283. {
  284. fAdaptedBufferSize = buffer_size;
  285. if (fAdaptative) {
  286. AdaptRingBufferSize();
  287. }
  288. return 0;
  289. }
  290. int JackAudioAdapterInterface::SetBufferSize(jack_nframes_t buffer_size)
  291. {
  292. SetHostBufferSize(buffer_size);
  293. SetAdaptedBufferSize(buffer_size);
  294. return 0;
  295. }
  296. int JackAudioAdapterInterface::SetHostSampleRate(jack_nframes_t sample_rate)
  297. {
  298. fHostSampleRate = sample_rate;
  299. fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
  300. return 0;
  301. }
  302. int JackAudioAdapterInterface::SetAdaptedSampleRate(jack_nframes_t sample_rate)
  303. {
  304. fAdaptedSampleRate = sample_rate;
  305. fPIControler.Init(double(fHostSampleRate) / double(fAdaptedSampleRate));
  306. return 0;
  307. }
  308. int JackAudioAdapterInterface::SetSampleRate(jack_nframes_t sample_rate)
  309. {
  310. SetHostSampleRate(sample_rate);
  311. SetAdaptedSampleRate(sample_rate);
  312. return 0;
  313. }
  314. void JackAudioAdapterInterface::SetInputs(int inputs)
  315. {
  316. jack_log("JackAudioAdapterInterface::SetInputs %d", inputs);
  317. fCaptureChannels = inputs;
  318. }
  319. void JackAudioAdapterInterface::SetOutputs(int outputs)
  320. {
  321. jack_log("JackAudioAdapterInterface::SetOutputs %d", outputs);
  322. fPlaybackChannels = outputs;
  323. }
  324. int JackAudioAdapterInterface::GetInputs()
  325. {
  326. //jack_log("JackAudioAdapterInterface::GetInputs %d", fCaptureChannels);
  327. return fCaptureChannels;
  328. }
  329. int JackAudioAdapterInterface::GetOutputs()
  330. {
  331. //jack_log ("JackAudioAdapterInterface::GetOutputs %d", fPlaybackChannels);
  332. return fPlaybackChannels;
  333. }
  334. } // namespace