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.

272 lines
7.1KB

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