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.

247 lines
6.1KB

  1. /*
  2. * Copyright (C) 2001 Steve Harris
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. */
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <signal.h>
  24. #include <math.h>
  25. #include <getopt.h>
  26. #include <jack/jack.h>
  27. jack_port_t *input_port;
  28. jack_port_t *output_port;
  29. unsigned int impulse_sent = 0;
  30. float *response;
  31. unsigned long response_duration;
  32. unsigned long response_pos;
  33. int grab_finished = 0;
  34. jack_client_t *client;
  35. static void signal_handler(int sig)
  36. {
  37. jack_client_close(client);
  38. fprintf(stderr, "signal received, exiting ...\n");
  39. exit(0);
  40. }
  41. static int
  42. process (jack_nframes_t nframes, void *arg)
  43. {
  44. jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  45. jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
  46. unsigned int i;
  47. if (grab_finished) {
  48. return 0;
  49. } else if (impulse_sent) {
  50. for(i=0; i<nframes && response_pos < response_duration; i++) {
  51. response[response_pos++] = in[i];
  52. }
  53. if (response_pos >= response_duration) {
  54. grab_finished = 1;
  55. }
  56. for (i=0; i<nframes; i++) {
  57. out[i] = 0.0f;;
  58. }
  59. } else {
  60. out[0] = 1.0f;
  61. for (i=1; i<nframes; i++) {
  62. out[i] = 0.0f;
  63. }
  64. impulse_sent = 1;
  65. }
  66. return 0;
  67. }
  68. static void
  69. jack_shutdown (void *arg)
  70. {
  71. exit (1);
  72. }
  73. int
  74. main (int argc, char *argv[])
  75. {
  76. const char **ports;
  77. float fs; // The sample rate
  78. float peak;
  79. unsigned long peak_sample;
  80. unsigned int i;
  81. float duration = 0.0f;
  82. unsigned int c_format = 0;
  83. int longopt_index = 0;
  84. int c;
  85. extern int optind, opterr;
  86. int show_usage = 0;
  87. char *optstring = "d:f";
  88. struct option long_options[] = {
  89. { "help", 1, 0, 'h' },
  90. { "duration", 1, 0, 'd' },
  91. { "format", 1, 0, 'f' },
  92. { 0, 0, 0, 0 }
  93. };
  94. while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
  95. switch (c) {
  96. case 1:
  97. // end of opts, but don't care
  98. break;
  99. case 'h':
  100. show_usage++;
  101. break;
  102. case 'd':
  103. duration = (float)atof(optarg);
  104. break;
  105. case 'f':
  106. if (*optarg == 'c' || *optarg == 'C') {
  107. c_format = 1;
  108. }
  109. break;
  110. default:
  111. show_usage++;
  112. break;
  113. }
  114. }
  115. if (show_usage || duration <= 0.0f) {
  116. fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
  117. exit(1);
  118. }
  119. /* try to become a client of the JACK server */
  120. if ((client = jack_client_open("impulse_grabber", JackNullOption, NULL)) == 0) {
  121. fprintf (stderr, "jack server not running?\n");
  122. return 1;
  123. }
  124. /* tell the JACK server to call `process()' whenever
  125. there is work to be done.
  126. */
  127. jack_set_process_callback (client, process, 0);
  128. /* tell the JACK server to call `jack_shutdown()' if
  129. it ever shuts down, either entirely, or if it
  130. just decides to stop calling us.
  131. */
  132. jack_on_shutdown (client, jack_shutdown, 0);
  133. /* display the current sample rate. once the client is activated
  134. (see below), you should rely on your own sample rate
  135. callback (see above) for this value.
  136. */
  137. fs = jack_get_sample_rate(client);
  138. response_duration = (unsigned long) (fs * duration);
  139. response = malloc(response_duration * sizeof(float));
  140. fprintf(stderr,
  141. "Grabbing %f seconds (%lu samples) of impulse response\n",
  142. duration, response_duration);
  143. /* create two ports */
  144. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  145. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  146. /* tell the JACK server that we are ready to roll */
  147. if (jack_activate (client)) {
  148. fprintf (stderr, "cannot activate client");
  149. return 1;
  150. }
  151. /* connect the ports. Note: you can't do this before
  152. the client is activated (this may change in the future).
  153. */
  154. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  155. fprintf(stderr, "Cannot find any physical capture ports");
  156. exit(1);
  157. }
  158. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  159. fprintf (stderr, "cannot connect input ports\n");
  160. }
  161. free (ports);
  162. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
  163. fprintf(stderr, "Cannot find any physical playback ports");
  164. exit(1);
  165. }
  166. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  167. fprintf (stderr, "cannot connect output ports\n");
  168. }
  169. free (ports);
  170. /* install a signal handler to properly quits jack client */
  171. signal(SIGQUIT, signal_handler);
  172. signal(SIGTERM, signal_handler);
  173. signal(SIGHUP, signal_handler);
  174. signal(SIGINT, signal_handler);
  175. /* Wait for grab to finish */
  176. while (!grab_finished) {
  177. sleep (1);
  178. }
  179. jack_client_close (client);
  180. peak = response[0];
  181. peak_sample = 0;
  182. if (c_format) {
  183. printf("impulse[%lu] = {", response_duration);
  184. for (i=0; i<response_duration; i++) {
  185. if (i % 4 != 0) {
  186. printf(" ");
  187. } else {
  188. printf("\n\t");
  189. }
  190. printf("\"%+1.10f\"", response[i]);
  191. if (i < response_duration - 1) {
  192. printf(",");
  193. }
  194. if (fabs(response[i]) > peak) {
  195. peak = fabs(response[i]);
  196. peak_sample = i;
  197. }
  198. }
  199. printf("\n};\n");
  200. } else {
  201. for (i=0; i<response_duration; i++) {
  202. printf("%1.12f\n", response[i]);
  203. if (fabs(response[i]) > peak) {
  204. peak = fabs(response[i]);
  205. peak_sample = i;
  206. }
  207. }
  208. }
  209. fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
  210. exit (0);
  211. }