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.

160 lines
3.7KB

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