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.

218 lines
5.4KB

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