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.

235 lines
5.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 <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. int jack_callback (jack_nframes_t nframes, void *arg)
  138. {
  139. float *ip, *op;
  140. ip = (float *)(jack_port_get_buffer (jack_capt, nframes));
  141. op = (float *)(jack_port_get_buffer (jack_play, nframes));
  142. mtdm.process (nframes, ip, op);
  143. return 0;
  144. }
  145. int main (int ac, char *av [])
  146. {
  147. float t;
  148. jack_status_t s;
  149. jack_handle = jack_client_open ("jack_delay", JackNoStartServer, &s);
  150. if (jack_handle == 0)
  151. {
  152. fprintf (stderr, "Can't connect to Jack, is the server running ?\n");
  153. exit (1);
  154. }
  155. jack_set_process_callback (jack_handle, jack_callback, 0);
  156. jack_capt = jack_port_register (jack_handle, "in", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  157. jack_play = jack_port_register (jack_handle, "out", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  158. printf ("capture latency = %d\n",
  159. jack_port_get_latency (jack_port_by_name (jack_handle, "system:capture_1")));
  160. printf ("playback_latency = %d\n",
  161. jack_port_get_latency (jack_port_by_name (jack_handle, "system:playback_1")));
  162. t = 1000.0f / jack_get_sample_rate (jack_handle);
  163. if (jack_activate (jack_handle))
  164. {
  165. fprintf(stderr, "Can't activate Jack");
  166. return 1;
  167. }
  168. while (1)
  169. {
  170. usleep (250000);
  171. if (mtdm.resolve () < 0) printf ("Signal below threshold...\n");
  172. else
  173. {
  174. if (mtdm.err () > 0.3)
  175. {
  176. mtdm.invert ();
  177. mtdm.resolve ();
  178. }
  179. printf ("%10.3lf frames %10.3lf ms", mtdm.del (), mtdm.del () * t);
  180. if (mtdm.err () > 0.2) printf (" ??");
  181. if (mtdm.inv ()) printf (" Inv");
  182. printf ("\n");
  183. }
  184. }
  185. return 0;
  186. }
  187. // --------------------------------------------------------------------------------