JACK example clients
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.

234 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. */
  19. #include <stdio.h>
  20. #include <errno.h>
  21. #include <unistd.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24. #include <getopt.h>
  25. #include <jack/jack.h>
  26. jack_port_t *input_port;
  27. jack_port_t *output_port;
  28. unsigned int impulse_sent = 0;
  29. float *response;
  30. unsigned long response_duration;
  31. unsigned long response_pos;
  32. int grab_finished = 0;
  33. int
  34. process (jack_nframes_t nframes, void *arg)
  35. {
  36. jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  37. jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
  38. unsigned int i;
  39. if (grab_finished) {
  40. return 0;
  41. } else if (impulse_sent) {
  42. for(i=0; i<nframes && response_pos < response_duration; i++) {
  43. response[response_pos++] = in[i];
  44. }
  45. if (response_pos >= response_duration) {
  46. grab_finished = 1;
  47. }
  48. for (i=0; i<nframes; i++) {
  49. out[i] = 0.0f;;
  50. }
  51. } else {
  52. out[0] = 1.0f;
  53. for (i=1; i<nframes; i++) {
  54. out[i] = 0.0f;
  55. }
  56. impulse_sent = 1;
  57. }
  58. return 0;
  59. }
  60. void
  61. jack_shutdown (void *arg)
  62. {
  63. exit (1);
  64. }
  65. int
  66. main (int argc, char *argv[])
  67. {
  68. jack_client_t *client;
  69. const char **ports;
  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:h";
  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_open("impulse_grabber", JackNullOption, NULL)) == 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 = (unsigned long) (fs * duration);
  132. response = malloc(response_duration * sizeof(float));
  133. fprintf(stderr,
  134. "Grabbing %f seconds (%lu samples) of impulse response\n",
  135. duration, response_duration);
  136. /* create two ports */
  137. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  138. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  139. /* tell the JACK server that we are ready to roll */
  140. if (jack_activate (client)) {
  141. fprintf (stderr, "cannot activate client");
  142. return 1;
  143. }
  144. /* connect the ports. Note: you can't do this before
  145. the client is activated (this may change in the future).
  146. */
  147. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  148. fprintf(stderr, "Cannot find any physical capture ports");
  149. exit(1);
  150. }
  151. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  152. fprintf (stderr, "cannot connect input ports\n");
  153. }
  154. free (ports);
  155. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
  156. fprintf(stderr, "Cannot find any physical playback ports");
  157. exit(1);
  158. }
  159. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  160. fprintf (stderr, "cannot connect output ports\n");
  161. }
  162. free (ports);
  163. /* Wait for grab to finish */
  164. while (!grab_finished) {
  165. sleep (1);
  166. }
  167. jack_client_close (client);
  168. peak = response[0];
  169. peak_sample = 0;
  170. if (c_format) {
  171. printf("impulse[%lu] = {", response_duration);
  172. for (i=0; i<response_duration; i++) {
  173. if (i % 4 != 0) {
  174. printf(" ");
  175. } else {
  176. printf("\n\t");
  177. }
  178. printf("\"%+1.10f\"", response[i]);
  179. if (i < response_duration - 1) {
  180. printf(",");
  181. }
  182. if (fabs(response[i]) > peak) {
  183. peak = fabs(response[i]);
  184. peak_sample = i;
  185. }
  186. }
  187. printf("\n};\n");
  188. } else {
  189. for (i=0; i<response_duration; i++) {
  190. printf("%1.12f\n", response[i]);
  191. if (fabs(response[i]) > peak) {
  192. peak = fabs(response[i]);
  193. peak_sample = i;
  194. }
  195. }
  196. }
  197. fprintf(stderr, "Peak value was %f at sample %lu\n", peak, peak_sample);
  198. exit (0);
  199. }