JACK example clients
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.

111 lines
3.0KB

  1. /** @file inprocess.c
  2. *
  3. * @brief This demonstrates the basic concepts for writing a client
  4. * that runs within the JACK server process.
  5. *
  6. * For the sake of example, a port_pair_t is allocated in
  7. * jack_initialize(), passed to inprocess() as an argument, then freed
  8. * in jack_finish().
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <memory.h>
  13. #include <jack/jack.h>
  14. /**
  15. * For the sake of example, an instance of this struct is allocated in
  16. * jack_initialize(), passed to inprocess() as an argument, then freed
  17. * in jack_finish().
  18. */
  19. typedef struct {
  20. jack_port_t *input_port;
  21. jack_port_t *output_port;
  22. } port_pair_t;
  23. /**
  24. * Called in the realtime thread on every process cycle. The entry
  25. * point name was passed to jack_set_process_callback() from
  26. * jack_initialize(). Although this is an internal client, its
  27. * process() interface is identical to @ref simple_client.c.
  28. *
  29. * @return 0 if successful; otherwise jack_finish() will be called and
  30. * the client terminated immediately.
  31. */
  32. int
  33. inprocess (jack_nframes_t nframes, void *arg)
  34. {
  35. port_pair_t *pp = arg;
  36. jack_default_audio_sample_t *out =
  37. jack_port_get_buffer (pp->output_port, nframes);
  38. jack_default_audio_sample_t *in =
  39. jack_port_get_buffer (pp->input_port, nframes);
  40. memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
  41. return 0; /* continue */
  42. }
  43. /**
  44. * This required entry point is called after the client is loaded by
  45. * jack_internal_client_load().
  46. *
  47. * @param client pointer to JACK client structure.
  48. * @param load_init character string passed to the load operation.
  49. *
  50. * @return 0 if successful; otherwise jack_finish() will be called and
  51. * the client terminated immediately.
  52. */
  53. int
  54. jack_initialize (jack_client_t *client, const char *load_init)
  55. {
  56. port_pair_t *pp = malloc (sizeof (port_pair_t));
  57. if (pp == NULL)
  58. return 1; /* heap exhausted */
  59. jack_set_process_callback (client, inprocess, pp);
  60. /* create a pair of ports */
  61. pp->input_port = jack_port_register (client, "input",
  62. JACK_DEFAULT_AUDIO_TYPE,
  63. JackPortIsInput, 0);
  64. pp->output_port = jack_port_register (client, "output",
  65. JACK_DEFAULT_AUDIO_TYPE,
  66. JackPortIsOutput, 0);
  67. /* join the process() cycle */
  68. jack_activate (client);
  69. /* try to connect to the first physical input & output ports */
  70. if (jack_connect (client, "system:capture_1",
  71. jack_port_name (pp->input_port))) {
  72. fprintf (stderr, "cannot connect input port\n");
  73. return 1; /* terminate client */
  74. }
  75. if (jack_connect (client, jack_port_name (pp->output_port),
  76. "system:playback_1")) {
  77. fprintf (stderr, "cannot connect output port\n");
  78. return 1; /* terminate client */
  79. }
  80. return 0; /* success */
  81. }
  82. /**
  83. * This required entry point is called immediately before the client
  84. * is unloaded, which could happen due to a call to
  85. * jack_internal_client_unload(), or a nonzero return from either
  86. * jack_initialize() or inprocess().
  87. *
  88. * @param arg the same parameter provided to inprocess().
  89. */
  90. void
  91. jack_finish (void *arg)
  92. {
  93. if (arg)
  94. free ((port_pair_t *) arg);
  95. }