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.

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