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.

248 lines
6.2KB

  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. fprintf(stderr, "JACK shut down, exiting ...\n");
  72. exit (1);
  73. }
  74. int
  75. main (int argc, char *argv[])
  76. {
  77. const char **ports;
  78. float fs; // The sample rate
  79. float peak;
  80. unsigned long peak_sample;
  81. unsigned int i;
  82. float duration = 0.0f;
  83. unsigned int c_format = 0;
  84. int longopt_index = 0;
  85. int c;
  86. extern int optind, opterr;
  87. int show_usage = 0;
  88. char *optstring = "d:f:h";
  89. struct option long_options[] = {
  90. { "help", 1, 0, 'h' },
  91. { "duration", 1, 0, 'd' },
  92. { "format", 1, 0, 'f' },
  93. { 0, 0, 0, 0 }
  94. };
  95. while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
  96. switch (c) {
  97. case 1:
  98. // end of opts, but don't care
  99. break;
  100. case 'h':
  101. show_usage++;
  102. break;
  103. case 'd':
  104. duration = (float)atof(optarg);
  105. break;
  106. case 'f':
  107. if (*optarg == 'c' || *optarg == 'C') {
  108. c_format = 1;
  109. }
  110. break;
  111. default:
  112. show_usage++;
  113. break;
  114. }
  115. }
  116. if (show_usage || duration <= 0.0f) {
  117. fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
  118. exit(1);
  119. }
  120. /* try to become a client of the JACK server */
  121. if ((client = jack_client_open("impulse_grabber", JackNullOption, NULL)) == 0) {
  122. fprintf (stderr, "JACK server not running?\n");
  123. return 1;
  124. }
  125. /* tell the JACK server to call `process()' whenever
  126. there is work to be done.
  127. */
  128. jack_set_process_callback (client, process, 0);
  129. /* tell the JACK server to call `jack_shutdown()' if
  130. it ever shuts down, either entirely, or if it
  131. just decides to stop calling us.
  132. */
  133. jack_on_shutdown (client, jack_shutdown, 0);
  134. /* display the current sample rate. once the client is activated
  135. (see below), you should rely on your own sample rate
  136. callback (see above) for this value.
  137. */
  138. fs = jack_get_sample_rate(client);
  139. response_duration = (unsigned long) (fs * duration);
  140. response = malloc(response_duration * sizeof(float));
  141. fprintf(stderr,
  142. "Grabbing %f seconds (%lu samples) of impulse response\n",
  143. duration, response_duration);
  144. /* create two ports */
  145. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  146. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  147. /* tell the JACK server that we are ready to roll */
  148. if (jack_activate (client)) {
  149. fprintf (stderr, "cannot activate client");
  150. return 1;
  151. }
  152. /* connect the ports. Note: you can't do this before
  153. the client is activated (this may change in the future).
  154. */
  155. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  156. fprintf(stderr, "Cannot find any physical capture ports");
  157. exit(1);
  158. }
  159. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  160. fprintf (stderr, "cannot connect input ports\n");
  161. }
  162. free (ports);
  163. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
  164. fprintf(stderr, "Cannot find any physical playback ports");
  165. exit(1);
  166. }
  167. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  168. fprintf (stderr, "cannot connect output ports\n");
  169. }
  170. free (ports);
  171. /* install a signal handler to properly quits jack client */
  172. signal(SIGQUIT, signal_handler);
  173. signal(SIGTERM, signal_handler);
  174. signal(SIGHUP, signal_handler);
  175. signal(SIGINT, signal_handler);
  176. /* Wait for grab to finish */
  177. while (!grab_finished) {
  178. sleep (1);
  179. }
  180. jack_client_close (client);
  181. peak = response[0];
  182. peak_sample = 0;
  183. if (c_format) {
  184. printf("impulse[%lu] = {", response_duration);
  185. for (i=0; i<response_duration; i++) {
  186. if (i % 4 != 0) {
  187. printf(" ");
  188. } else {
  189. printf("\n\t");
  190. }
  191. printf("\"%+1.10f\"", response[i]);
  192. if (i < response_duration - 1) {
  193. printf(",");
  194. }
  195. if (fabs(response[i]) > peak) {
  196. peak = fabs(response[i]);
  197. peak_sample = i;
  198. }
  199. }
  200. printf("\n};\n");
  201. } else {
  202. for (i=0; i<response_duration; i++) {
  203. printf("%1.12f\n", response[i]);
  204. if (fabs(response[i]) > peak) {
  205. peak = fabs(response[i]);
  206. peak_sample = i;
  207. }
  208. }
  209. }
  210. fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
  211. exit (0);
  212. }