jack2 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.

124 lines
3.3KB

  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. const char **ports;
  58. if (pp == NULL)
  59. return 1; /* heap exhausted */
  60. jack_set_process_callback (client, inprocess, pp);
  61. /* create a pair of ports */
  62. pp->input_port = jack_port_register (client, "input",
  63. JACK_DEFAULT_AUDIO_TYPE,
  64. JackPortIsInput, 0);
  65. pp->output_port = jack_port_register (client, "output",
  66. JACK_DEFAULT_AUDIO_TYPE,
  67. JackPortIsOutput, 0);
  68. /* join the process() cycle */
  69. jack_activate (client);
  70. ports = jack_get_ports (client, NULL, NULL,
  71. JackPortIsPhysical|JackPortIsOutput);
  72. if (ports == NULL) {
  73. fprintf(stderr, "no physical capture ports\n");
  74. return 1; /* terminate client */
  75. }
  76. if (jack_connect (client, ports[0], jack_port_name (pp->input_port))) {
  77. fprintf (stderr, "cannot connect input ports\n");
  78. }
  79. jack_free (ports);
  80. ports = jack_get_ports (client, NULL, NULL,
  81. JackPortIsPhysical|JackPortIsInput);
  82. if (ports == NULL) {
  83. fprintf(stderr, "no physical playback ports\n");
  84. return 1; /* terminate client */
  85. }
  86. if (jack_connect (client, jack_port_name (pp->output_port), ports[0])) {
  87. fprintf (stderr, "cannot connect output ports\n");
  88. }
  89. jack_free (ports);
  90. return 0; /* success */
  91. }
  92. /**
  93. * This required entry point is called immediately before the client
  94. * is unloaded, which could happen due to a call to
  95. * jack_internal_client_unload(), or a nonzero return from either
  96. * jack_initialize() or inprocess().
  97. *
  98. * @param arg the same parameter provided to inprocess().
  99. */
  100. void
  101. jack_finish (void *arg)
  102. {
  103. if (arg)
  104. free ((port_pair_t *) arg);
  105. }