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.

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