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.

261 lines
6.4KB

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