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.

146 lines
3.4KB

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