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.

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