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.

129 lines
2.9KB

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