jack1 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.8KB

  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. * $Id$
  19. */
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23. #include <stdlib.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. int
  35. process (jack_nframes_t nframes, void *arg)
  36. {
  37. jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  38. jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
  39. unsigned int i;
  40. if (grab_finished) {
  41. return 0;
  42. } else if (impulse_sent) {
  43. for(i=0; i<nframes && response_pos < response_duration; i++) {
  44. response[response_pos++] = in[i];
  45. }
  46. if (response_pos >= response_duration) {
  47. grab_finished = 1;
  48. }
  49. for (i=0; i<nframes; i++) {
  50. out[i] = 0.0f;;
  51. }
  52. } else {
  53. out[0] = 1.0f;
  54. for (i=1; i<nframes; i++) {
  55. out[i] = 0.0f;
  56. }
  57. impulse_sent = 1;
  58. }
  59. return 0;
  60. }
  61. void
  62. jack_shutdown (void *arg)
  63. {
  64. exit (1);
  65. }
  66. int
  67. main (int argc, char *argv[])
  68. {
  69. jack_client_t *client;
  70. const char **ports;
  71. float fs; // The sample rate
  72. float peak;
  73. unsigned long peak_sample;
  74. unsigned int i;
  75. float duration = 0.0f;
  76. unsigned int c_format = 0;
  77. int longopt_index = 0;
  78. int c;
  79. extern int optind, opterr;
  80. int show_usage = 0;
  81. char *optstring = "d:f";
  82. struct option long_options[] = {
  83. { "help", 1, 0, 'h' },
  84. { "duration", 1, 0, 'd' },
  85. { "format", 1, 0, 'f' },
  86. { 0, 0, 0, 0 }
  87. };
  88. while ((c = getopt_long (argc, argv, optstring, long_options, &longopt_index)) != -1) {
  89. switch (c) {
  90. case 1:
  91. // end of opts, but don't care
  92. break;
  93. case 'h':
  94. show_usage++;
  95. break;
  96. case 'd':
  97. duration = (float)atof(optarg);
  98. break;
  99. case 'f':
  100. if (*optarg == 'c' || *optarg == 'C') {
  101. c_format = 1;
  102. }
  103. break;
  104. default:
  105. show_usage++;
  106. break;
  107. }
  108. }
  109. if (show_usage || duration <= 0.0f) {
  110. fprintf(stderr, "usage: jack_impulse_grab -d duration [-f (C|gnuplot)]\n");
  111. exit(1);
  112. }
  113. /* try to become a client of the JACK server */
  114. if ((client = jack_client_new("impulse_grabber")) == 0) {
  115. fprintf (stderr, "jack server not running?\n");
  116. return 1;
  117. }
  118. /* tell the JACK server to call `process()' whenever
  119. there is work to be done.
  120. */
  121. jack_set_process_callback (client, process, 0);
  122. /* tell the JACK server to call `jack_shutdown()' if
  123. it ever shuts down, either entirely, or if it
  124. just decides to stop calling us.
  125. */
  126. jack_on_shutdown (client, jack_shutdown, 0);
  127. /* display the current sample rate. once the client is activated
  128. (see below), you should rely on your own sample rate
  129. callback (see above) for this value.
  130. */
  131. fs = jack_get_sample_rate(client);
  132. response_duration = (unsigned long) (fs * duration);
  133. response = malloc(response_duration * sizeof(float));
  134. fprintf(stderr,
  135. "Grabbing %f seconds (%lu samples) of impulse response\n",
  136. duration, response_duration);
  137. /* create two ports */
  138. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  139. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  140. /* tell the JACK server that we are ready to roll */
  141. if (jack_activate (client)) {
  142. fprintf (stderr, "cannot activate client");
  143. return 1;
  144. }
  145. /* connect the ports. Note: you can't do this before
  146. the client is activated (this may change in the future).
  147. */
  148. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  149. fprintf(stderr, "Cannot find any physical capture ports");
  150. exit(1);
  151. }
  152. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  153. fprintf (stderr, "cannot connect input ports\n");
  154. }
  155. free (ports);
  156. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
  157. fprintf(stderr, "Cannot find any physical playback ports");
  158. exit(1);
  159. }
  160. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  161. fprintf (stderr, "cannot connect output ports\n");
  162. }
  163. free (ports);
  164. /* Wait for grab to finish */
  165. while (!grab_finished) {
  166. sleep (1);
  167. }
  168. jack_client_close (client);
  169. peak = response[0];
  170. peak_sample = 0;
  171. if (c_format) {
  172. printf("impulse[%lu] = {", response_duration);
  173. for (i=0; i<response_duration; i++) {
  174. if (i % 4 != 0) {
  175. printf(" ");
  176. } else {
  177. printf("\n\t");
  178. }
  179. printf("\"%+1.10f\"", response[i]);
  180. if (i < response_duration - 1) {
  181. printf(",");
  182. }
  183. if (fabs(response[i]) > peak) {
  184. peak = fabs(response[i]);
  185. peak_sample = i;
  186. }
  187. }
  188. printf("\n};\n");
  189. } else {
  190. for (i=0; i<response_duration; i++) {
  191. printf("%1.12f\n", response[i]);
  192. if (fabs(response[i]) > peak) {
  193. peak = fabs(response[i]);
  194. peak_sample = i;
  195. }
  196. }
  197. }
  198. fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
  199. exit (0);
  200. }