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.

161 lines
3.7KB

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <jack/jack.h>
  7. jack_port_t *input_port;
  8. jack_port_t *output_port;
  9. int
  10. process (jack_nframes_t nframes, void *arg)
  11. {
  12. jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (output_port, nframes);
  13. jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (input_port, nframes);
  14. memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
  15. return 0;
  16. }
  17. int
  18. bufsize (jack_nframes_t nframes, void *arg)
  19. {
  20. printf ("the maximum buffer size is now %lu\n", nframes);
  21. return 0;
  22. }
  23. int
  24. srate (jack_nframes_t nframes, void *arg)
  25. {
  26. printf ("the sample rate is now %lu/sec\n", nframes);
  27. return 0;
  28. }
  29. void
  30. error (const char *desc)
  31. {
  32. printf ("JACK experienced an error: %s\n", desc);
  33. }
  34. void
  35. jack_shutdown (void *arg)
  36. {
  37. exit (1);
  38. }
  39. int
  40. main (int argc, char *argv[])
  41. {
  42. jack_client_t *client;
  43. const char **ports;
  44. if (argc < 2) {
  45. fprintf (stderr, "usage: jack_simple_client <name>\n");
  46. return 1;
  47. }
  48. /* tell the JACK server to call error() whenever it
  49. experiences an error. Notice that this callback is
  50. global to this process, not specific to each client.
  51. This is set here so that it can catch errors in the
  52. connection process
  53. */
  54. jack_set_error_function (error);
  55. /* try to become a client of the JACK server */
  56. if ((client = jack_client_new (argv[1])) == 0) {
  57. fprintf (stderr, "jack server not running?\n");
  58. return 1;
  59. }
  60. /* tell the JACK server to call `process()' whenever
  61. there is work to be done.
  62. */
  63. jack_set_process_callback (client, process, 0);
  64. /* tell the JACK server to call `bufsize()' whenever
  65. the maximum number of frames that will be passed
  66. to `process()' changes
  67. */
  68. jack_set_buffer_size_callback (client, bufsize, 0);
  69. /* tell the JACK server to call `srate()' whenever
  70. the sample rate of the system changes.
  71. */
  72. jack_set_sample_rate_callback (client, srate, 0);
  73. /* tell the JACK server to call `jack_shutdown()' if
  74. it ever shuts down, either entirely, or if it
  75. just decides to stop calling us.
  76. */
  77. jack_on_shutdown (client, jack_shutdown, 0);
  78. /* display the current sample rate. once the client is activated
  79. (see below), you should rely on your own sample rate
  80. callback (see above) for this value.
  81. */
  82. printf ("engine sample rate: %lu\n", jack_get_sample_rate (client));
  83. /* create two ports */
  84. input_port = jack_port_register (client, "input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0);
  85. output_port = jack_port_register (client, "output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0);
  86. /* tell the JACK server that we are ready to roll */
  87. if (jack_activate (client)) {
  88. fprintf (stderr, "cannot activate client");
  89. return 1;
  90. }
  91. /* connect the ports. Note: you can't do this before
  92. the client is activated, because we can't allow
  93. connections to be made to clients that aren't
  94. running.
  95. */
  96. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsOutput)) == NULL) {
  97. fprintf(stderr, "Cannot find any physical capture ports");
  98. exit(1);
  99. }
  100. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  101. fprintf (stderr, "cannot connect input ports\n");
  102. }
  103. free (ports);
  104. if ((ports = jack_get_ports (client, NULL, NULL, JackPortIsPhysical|JackPortIsInput)) == NULL) {
  105. fprintf(stderr, "Cannot find any physical playback ports");
  106. exit(1);
  107. }
  108. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  109. fprintf (stderr, "cannot connect output ports\n");
  110. }
  111. free (ports);
  112. /* Since this is just a toy, run for a few seconds, then finish */
  113. sleep (10);
  114. jack_client_close (client);
  115. exit (0);
  116. }