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.

178 lines
4.0KB

  1. /** @file simple_client.c
  2. *
  3. * @brief This simple client demonstrates the basic features of JACK
  4. * as they would be used by many applications.
  5. */
  6. #include <stdio.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <jack/jack.h>
  12. jack_port_t *input_port;
  13. jack_port_t *output_port;
  14. jack_client_t *client;
  15. /* a simple state machine for this client */
  16. volatile enum {
  17. Init,
  18. Run,
  19. Exit
  20. } client_state = Init;
  21. /**
  22. * The process callback for this JACK application is called in a
  23. * special realtime thread once for each audio cycle.
  24. *
  25. * This client follows a simple rule: when the JACK transport is
  26. * running, copy the input port to the output. When it stops, exit.
  27. */
  28. int
  29. process (jack_nframes_t nframes, void *arg)
  30. {
  31. jack_default_audio_sample_t *in, *out;
  32. jack_transport_state_t ts = jack_transport_query(client, NULL);
  33. if (ts == JackTransportRolling) {
  34. if (client_state == Init)
  35. client_state = Run;
  36. in = jack_port_get_buffer (input_port, nframes);
  37. out = jack_port_get_buffer (output_port, nframes);
  38. memcpy (out, in,
  39. sizeof (jack_default_audio_sample_t) * nframes);
  40. } else if (ts == JackTransportStopped) {
  41. if (client_state == Run)
  42. client_state = Exit;
  43. }
  44. return 0;
  45. }
  46. /**
  47. * JACK calls this shutdown_callback if the server ever shuts down or
  48. * decides to disconnect the client.
  49. */
  50. void
  51. jack_shutdown (void *arg)
  52. {
  53. exit (1);
  54. }
  55. int
  56. main (int argc, char *argv[])
  57. {
  58. const char **ports;
  59. const char *client_name;
  60. jack_status_t status;
  61. if (argc >= 2) { /* session name specified? */
  62. client_name = argv[1];
  63. } else { /* use basename of argv[0] */
  64. client_name = strrchr(argv[0], '/');
  65. if (client_name == 0) {
  66. client_name = argv[0];
  67. } else {
  68. client_name++;
  69. }
  70. }
  71. /* open a client connection to the JACK server */
  72. client = jack_client_open (client_name, 0, &status, NULL, NULL);
  73. if (client == NULL) {
  74. fprintf (stderr, "jack_client_open() failed, status = %d\n",
  75. status);
  76. exit (1);
  77. }
  78. if (status & JackServerStarted) {
  79. fprintf (stderr, "JACK server started\n");
  80. }
  81. if (status & JackNameNotUnique) {
  82. client_name = jack_get_client_name(client);
  83. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  84. }
  85. /* tell the JACK server to call `process()' whenever
  86. there is work to be done.
  87. */
  88. jack_set_process_callback (client, process, 0);
  89. /* tell the JACK server to call `jack_shutdown()' if
  90. it ever shuts down, either entirely, or if it
  91. just decides to stop calling us.
  92. */
  93. jack_on_shutdown (client, jack_shutdown, 0);
  94. /* display the current sample rate.
  95. */
  96. printf ("engine sample rate: %" PRIu32 "\n",
  97. jack_get_sample_rate (client));
  98. /* create two ports */
  99. input_port = jack_port_register (client, "input",
  100. JACK_DEFAULT_AUDIO_TYPE,
  101. JackPortIsInput, 0);
  102. output_port = jack_port_register (client, "output",
  103. JACK_DEFAULT_AUDIO_TYPE,
  104. JackPortIsOutput, 0);
  105. /* Tell the JACK server that we are ready to roll. Our
  106. * process() callback will start running now. */
  107. if (jack_activate (client)) {
  108. fprintf (stderr, "cannot activate client");
  109. exit (1);
  110. }
  111. /* connect the ports. Note: you can't do this before the
  112. client is activated, because we can't allow connections to
  113. be made to clients that aren't running.
  114. */
  115. ports = jack_get_ports (client, NULL, NULL,
  116. JackPortIsPhysical|JackPortIsOutput);
  117. if (ports == NULL) {
  118. fprintf(stderr, "no physical capture ports\n");
  119. exit (1);
  120. }
  121. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  122. fprintf (stderr, "cannot connect input ports\n");
  123. }
  124. free (ports);
  125. ports = jack_get_ports (client, NULL, NULL,
  126. JackPortIsPhysical|JackPortIsInput);
  127. if (ports == NULL) {
  128. fprintf(stderr, "no physical playback ports\n");
  129. exit (1);
  130. }
  131. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  132. fprintf (stderr, "cannot connect output ports\n");
  133. }
  134. free (ports);
  135. /* keep running until the transport stops */
  136. while (client_state != Exit) {
  137. sleep (1);
  138. }
  139. jack_client_close (client);
  140. exit (0);
  141. }