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.

198 lines
4.5KB

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