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.

254 lines
5.9KB

  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. class Freq
  22. {
  23. public:
  24. int p;
  25. int f;
  26. float a;
  27. float xa;
  28. float ya;
  29. float xf;
  30. float yf;
  31. };
  32. class MTDM
  33. {
  34. public:
  35. MTDM (void);
  36. int process (size_t len, float *inp, float *out);
  37. int resolve (void);
  38. void invert (void) { _inv ^= 1; }
  39. int inv (void) { return _inv; }
  40. double del (void) { return _del; }
  41. double err (void) { return _err; }
  42. private:
  43. double _del;
  44. double _err;
  45. int _cnt;
  46. int _inv;
  47. Freq _freq [5];
  48. };
  49. MTDM::MTDM (void) : _cnt (0), _inv (0)
  50. {
  51. int i;
  52. Freq *F;
  53. _freq [0].f = 4096;
  54. _freq [1].f = 512;
  55. _freq [2].f = 1088;
  56. _freq [3].f = 1544;
  57. _freq [4].f = 2049;
  58. _freq [0].a = 0.2f;
  59. _freq [1].a = 0.1f;
  60. _freq [2].a = 0.1f;
  61. _freq [3].a = 0.1f;
  62. _freq [4].a = 0.1f;
  63. for (i = 0, F = _freq; i < 5; i++, F++)
  64. {
  65. F->p = 128;
  66. F->xa = F->ya = 0.0f;
  67. F->xf = F->yf = 0.0f;
  68. }
  69. }
  70. int MTDM::process (size_t len, float *ip, float *op)
  71. {
  72. int i;
  73. float vip, vop, a, c, s;
  74. Freq *F;
  75. while (len--)
  76. {
  77. vop = 0.0f;
  78. vip = *ip++;
  79. for (i = 0, F = _freq; i < 5; i++, F++)
  80. {
  81. a = 2 * (float) M_PI * (F->p & 65535) / 65536.0;
  82. F->p += F->f;
  83. c = cosf (a);
  84. s = -sinf (a);
  85. vop += F->a * s;
  86. F->xa += s * vip;
  87. F->ya += c * vip;
  88. }
  89. *op++ = vop;
  90. if (++_cnt == 16)
  91. {
  92. for (i = 0, F = _freq; i < 5; i++, F++)
  93. {
  94. F->xf += 1e-3f * (F->xa - F->xf + 1e-20);
  95. F->yf += 1e-3f * (F->ya - F->yf + 1e-20);
  96. F->xa = F->ya = 0.0f;
  97. }
  98. _cnt = 0;
  99. }
  100. }
  101. return 0;
  102. }
  103. int MTDM::resolve (void)
  104. {
  105. int i, k, m;
  106. double d, e, f0, p;
  107. Freq *F = _freq;
  108. if (hypot (F->xf, F->yf) < 0.01) return -1;
  109. d = atan2 (F->yf, F->xf) / (2 * M_PI);
  110. if (_inv) d += 0.5f;
  111. if (d > 0.5f) d -= 1.0f;
  112. f0 = _freq [0].f;
  113. m = 1;
  114. _err = 0.0;
  115. for (i = 0; i < 4; i++)
  116. {
  117. F++;
  118. p = atan2 (F->yf, F->xf) / (2 * M_PI) - d * F->f / f0;
  119. if (_inv) p += 0.5f;
  120. p -= floor (p);
  121. p *= 8;
  122. k = (int)(floor (p + 0.5));
  123. e = fabs (p - k);
  124. if (e > _err) _err = e;
  125. if (e > 0.4) return 1;
  126. d += m * (k & 7);
  127. m *= 8;
  128. }
  129. _del = 16 * d;
  130. return 0;
  131. }
  132. // --------------------------------------------------------------------------------
  133. static 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 (nframes, ip, op);
  166. return 0;
  167. }
  168. int main (int ac, char *av [])
  169. {
  170. float t;
  171. jack_status_t s;
  172. jack_handle = jack_client_open ("jack_delay", JackNoStartServer, &s);
  173. if (jack_handle == 0)
  174. {
  175. fprintf (stderr, "Can't connect to Jack, is the server running ?\n");
  176. exit (1);
  177. }
  178. jack_set_process_callback (jack_handle, jack_callback, 0);
  179. if (jack_set_latency_callback)
  180. jack_set_latency_callback (jack_handle, latency_cb, 0);
  181. jack_capt = jack_port_register (jack_handle, "in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  182. jack_play = jack_port_register (jack_handle, "out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  183. t = 1000.0f / jack_get_sample_rate (jack_handle);
  184. if (jack_activate (jack_handle))
  185. {
  186. fprintf(stderr, "Can't activate Jack");
  187. return 1;
  188. }
  189. while (1)
  190. {
  191. #ifdef WIN32
  192. Sleep (250);
  193. #else
  194. usleep (250000);
  195. #endif
  196. if (mtdm.resolve () < 0) printf ("Signal below threshold...\n");
  197. else
  198. {
  199. if (mtdm.err () > 0.3)
  200. {
  201. mtdm.invert ();
  202. mtdm.resolve ();
  203. }
  204. printf ("%10.3lf frames %10.3lf ms", mtdm.del (), mtdm.del () * t);
  205. if (mtdm.err () > 0.2) printf (" ??");
  206. if (mtdm.inv ()) printf (" Inv");
  207. printf ("\n");
  208. }
  209. }
  210. return 0;
  211. }
  212. // --------------------------------------------------------------------------------