JACK tools
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.

270 lines
6.8KB

  1. /*
  2. Copyright (C) 2003-2008 Fons Adriaensen <fons@kokkinizita.net>
  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. // --------------------------------------------------------------------------------
  16. #include <stdlib.h>
  17. #include <stdio.h>
  18. #include <math.h>
  19. #include <unistd.h>
  20. #include <jack/jack.h>
  21. struct Freq
  22. {
  23. int p;
  24. int f;
  25. float xa;
  26. float ya;
  27. float x1;
  28. float y1;
  29. float x2;
  30. float y2;
  31. };
  32. struct MTDM
  33. {
  34. double _del;
  35. double _err;
  36. float _wlp;
  37. int _cnt;
  38. int _inv;
  39. struct Freq _freq [13];
  40. };
  41. struct MTDM * mtdm_new (double fsamp)
  42. {
  43. int i;
  44. struct Freq *F;
  45. struct MTDM *retval = malloc( sizeof(struct MTDM) );
  46. if (retval==NULL)
  47. return NULL;
  48. retval->_cnt = 0;
  49. retval->_inv = 0;
  50. retval->_freq [0].f = 4096;
  51. retval->_freq [1].f = 2048;
  52. retval->_freq [2].f = 3072;
  53. retval->_freq [3].f = 2560;
  54. retval->_freq [4].f = 2304;
  55. retval->_freq [5].f = 2176;
  56. retval->_freq [6].f = 1088;
  57. retval->_freq [7].f = 1312;
  58. retval->_freq [8].f = 1552;
  59. retval->_freq [9].f = 1800;
  60. retval->_freq [10].f = 3332;
  61. retval->_freq [11].f = 3586;
  62. retval->_freq [12].f = 3841;
  63. retval->_wlp = 200.0f / fsamp;
  64. for (i = 0, F = retval->_freq; i < 13; i++, F++) {
  65. F->p = 128;
  66. F->xa = F->ya = 0.0f;
  67. F->x1 = F->y1 = 0.0f;
  68. F->x2 = F->y2 = 0.0f;
  69. }
  70. return retval;
  71. }
  72. int mtdm_process (struct MTDM *self, size_t len, float *ip, float *op)
  73. {
  74. int i;
  75. float vip, vop, a, c, s;
  76. struct Freq *F;
  77. while (len--)
  78. {
  79. vop = 0.0f;
  80. vip = *ip++;
  81. for (i = 0, F = self->_freq; i < 13; i++, F++)
  82. {
  83. a = 2 * (float) M_PI * (F->p & 65535) / 65536.0;
  84. F->p += F->f;
  85. c = cosf (a);
  86. s = -sinf (a);
  87. vop += (i ? 0.01f : 0.20f) * s;
  88. F->xa += s * vip;
  89. F->ya += c * vip;
  90. }
  91. *op++ = vop;
  92. if (++self->_cnt == 16)
  93. {
  94. for (i = 0, F = self->_freq; i < 13; i++, F++)
  95. {
  96. F->x1 += self->_wlp * (F->xa - F->x1 + 1e-20);
  97. F->y1 += self->_wlp * (F->ya - F->y1 + 1e-20);
  98. F->x2 += self->_wlp * (F->x1 - F->x2 + 1e-20);
  99. F->y2 += self->_wlp * (F->y1 - F->y2 + 1e-20);
  100. F->xa = F->ya = 0.0f;
  101. }
  102. self->_cnt = 0;
  103. }
  104. }
  105. return 0;
  106. }
  107. int mtdm_resolve (struct MTDM *self)
  108. {
  109. int i, k, m;
  110. double d, e, f0, p;
  111. struct Freq *F = self->_freq;
  112. if (hypot (F->x2, F->y2) < 0.001) return -1;
  113. d = atan2 (F->y2, F->x2) / (2 * M_PI);
  114. if (self->_inv) d += 0.5;
  115. if (d > 0.5) d -= 1.0;
  116. f0 = self->_freq [0].f;
  117. m = 1;
  118. self->_err = 0.0;
  119. for (i = 0; i < 12; i++)
  120. {
  121. F++;
  122. p = atan2 (F->y2, F->x2) / (2 * M_PI) - d * F->f / f0;
  123. if (self->_inv) p += 0.5;
  124. p -= floor (p);
  125. p *= 2;
  126. k = (int)(floor (p + 0.5));
  127. e = fabs (p - k);
  128. if (e > self->_err) self->_err = e;
  129. if (e > 0.4) return 1;
  130. d += m * (k & 1);
  131. m *= 2;
  132. }
  133. self->_del = 16 * d;
  134. return 0;
  135. }
  136. void mtdm_invert (struct MTDM *self)
  137. {
  138. self->_inv ^= 1;
  139. }
  140. // --------------------------------------------------------------------------------
  141. static struct MTDM *mtdm;
  142. static jack_client_t *jack_handle;
  143. static jack_port_t *jack_capt;
  144. static jack_port_t *jack_play;
  145. jack_latency_range_t capture_latency = {-1, -1};
  146. jack_latency_range_t playback_latency = {-1, -1};
  147. void
  148. latency_cb (jack_latency_callback_mode_t mode, void *arg)
  149. {
  150. jack_latency_range_t range;
  151. range.min = range.max = 0;
  152. if (mode == JackCaptureLatency) {
  153. jack_port_set_latency_range (jack_play, mode, &range);
  154. jack_port_get_latency_range (jack_capt, mode, &range);
  155. if ((range.min != capture_latency.min) || (range.max != capture_latency.max)) {
  156. capture_latency = range;
  157. printf ("new capture latency: [%d, %d]\n", range.min, range.max);
  158. }
  159. } else {
  160. jack_port_set_latency_range (jack_capt, mode, &range);
  161. jack_port_get_latency_range (jack_play, mode, &range);
  162. if ((range.min != playback_latency.min) || (range.max != playback_latency.max)) {
  163. playback_latency = range;
  164. printf ("new playback latency: [%d, %d]\n", range.min, range.max);
  165. }
  166. }
  167. }
  168. int jack_callback (jack_nframes_t nframes, void *arg)
  169. {
  170. float *ip, *op;
  171. ip = (float *)(jack_port_get_buffer (jack_capt, nframes));
  172. op = (float *)(jack_port_get_buffer (jack_play, nframes));
  173. mtdm_process (mtdm, nframes, ip, op);
  174. return 0;
  175. }
  176. int main (int ac, char *av [])
  177. {
  178. float t;
  179. jack_status_t s;
  180. jack_handle = jack_client_open ("jack_delay", JackNoStartServer, &s);
  181. if (jack_handle == 0)
  182. {
  183. fprintf (stderr, "Can't connect to Jack, is the server running ?\n");
  184. exit (1);
  185. }
  186. mtdm = mtdm_new(jack_get_sample_rate(jack_handle));
  187. jack_set_process_callback (jack_handle, jack_callback, 0);
  188. if (jack_set_latency_callback)
  189. jack_set_latency_callback (jack_handle, latency_cb, 0);
  190. jack_capt = jack_port_register (jack_handle, "in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  191. jack_play = jack_port_register (jack_handle, "out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  192. t = 1000.0f / jack_get_sample_rate (jack_handle);
  193. if (jack_activate (jack_handle))
  194. {
  195. fprintf(stderr, "Can't activate Jack");
  196. return 1;
  197. }
  198. while (1)
  199. {
  200. #ifdef WIN32
  201. Sleep (250);
  202. #else
  203. usleep (250000);
  204. #endif
  205. if (mtdm_resolve (mtdm) < 0) printf ("Signal below threshold...\n");
  206. else
  207. {
  208. jack_nframes_t systemic_latency;
  209. if (mtdm->_err > 0.3)
  210. {
  211. mtdm_invert ( mtdm );
  212. mtdm_resolve ( mtdm );
  213. }
  214. systemic_latency = (jack_nframes_t) floor (mtdm->_del - (capture_latency.max + playback_latency.max));
  215. printf ("%10.3lf frames %10.3lf ms total roundtrip latency\n\textra loopback latency: %u frames\n\tuse %u for the backend arguments -I and -O", mtdm->_del, mtdm->_del * t,
  216. systemic_latency, systemic_latency/2);
  217. if (mtdm->_err > 0.2) printf (" ??");
  218. if (mtdm->_inv) printf (" Inv");
  219. printf ("\n");
  220. }
  221. }
  222. return 0;
  223. }
  224. // --------------------------------------------------------------------------------