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.

233 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 = (int)(fs * duration);
  133. response = malloc(response_duration * sizeof(float));
  134. fprintf(stderr, "Grabbing %f seconds (%lu samples) of impulse response\n", duration, response_duration);
  135. /* create two ports */
  136. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  137. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  138. /* tell the JACK server that we are ready to roll */
  139. if (jack_activate (client)) {
  140. fprintf (stderr, "cannot activate client");
  141. return 1;
  142. }
  143. /* connect the ports. Note: you can't do this before
  144. the client is activated (this may change in the future).
  145. */
  146. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  147. fprintf(stderr, "Cannot find any physical capture ports");
  148. exit(1);
  149. }
  150. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  151. fprintf (stderr, "cannot connect input ports\n");
  152. }
  153. free (ports);
  154. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
  155. fprintf(stderr, "Cannot find any physical playback ports");
  156. exit(1);
  157. }
  158. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  159. fprintf (stderr, "cannot connect output ports\n");
  160. }
  161. free (ports);
  162. /* Wait for grab to finish */
  163. while (!grab_finished) {
  164. sleep (1);
  165. }
  166. jack_client_close (client);
  167. peak = response[0];
  168. peak_sample = 0;
  169. if (c_format) {
  170. printf("impulse[%lu] = {", response_duration);
  171. for (i=0; i<response_duration; i++) {
  172. if (i % 4 != 0) {
  173. printf(" ");
  174. } else {
  175. printf("\n\t");
  176. }
  177. printf("\"%+1.10f\"", response[i]);
  178. if (i < response_duration - 1) {
  179. printf(",");
  180. }
  181. if (fabs(response[i]) > peak) {
  182. peak = fabs(response[i]);
  183. peak_sample = i;
  184. }
  185. }
  186. printf("\n};\n");
  187. } else {
  188. for (i=0; i<response_duration; i++) {
  189. printf("%1.12f\n", response[i]);
  190. if (fabs(response[i]) > peak) {
  191. peak = fabs(response[i]);
  192. peak_sample = i;
  193. }
  194. }
  195. }
  196. fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
  197. exit (0);
  198. }