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.

109 lines
2.9KB

  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 process() 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 process() 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() by
  26. * jack_initialize().
  27. *
  28. * @return 0 if successful; otherwise jack_finish() will be called and
  29. * the client terminated immediately.
  30. */
  31. int
  32. process (jack_nframes_t nframes, void *arg)
  33. {
  34. port_pair_t *pp = arg;
  35. jack_default_audio_sample_t *out =
  36. jack_port_get_buffer (pp->output_port, nframes);
  37. jack_default_audio_sample_t *in =
  38. jack_port_get_buffer (pp->input_port, nframes);
  39. memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
  40. return 0; /* continue */
  41. }
  42. /**
  43. * This required entry point is called after the client is loaded by
  44. * jack_internal_client_new().
  45. *
  46. * @param client pointer to JACK client structure.
  47. * @param so_data character string passed from jack_internal_client_new().
  48. *
  49. * @return 0 if successful; otherwise jack_finish() will be called and
  50. * the client terminated immediately.
  51. */
  52. int
  53. jack_initialize (jack_client_t *client, const char *so_data)
  54. {
  55. port_pair_t *pp = malloc (sizeof (port_pair_t));
  56. if (pp == NULL)
  57. return 1; /* heap exhausted */
  58. jack_set_process_callback (client, process, pp);
  59. /* create a pair of ports */
  60. pp->input_port = jack_port_register (client, "input",
  61. JACK_DEFAULT_AUDIO_TYPE,
  62. JackPortIsInput, 0);
  63. pp->output_port = jack_port_register (client, "output",
  64. JACK_DEFAULT_AUDIO_TYPE,
  65. JackPortIsOutput, 0);
  66. /* join the process() cycle */
  67. jack_activate (client);
  68. /* connect the ports */
  69. if (jack_connect (client, "alsa_pcm:capture_1",
  70. jack_port_name (pp->input_port))) {
  71. fprintf (stderr, "cannot connect input port\n");
  72. return 1; /* terminate client */
  73. }
  74. if (jack_connect (client, jack_port_name (pp->output_port),
  75. "alsa_pcm:playback_1")) {
  76. fprintf (stderr, "cannot connect output port\n");
  77. return 1; /* terminate client */
  78. }
  79. return 0; /* success */
  80. }
  81. /**
  82. * This required entry point is called immediately before the client
  83. * is unloaded, which could happen due to a call to
  84. * jack_internal_client_close(), or a nonzero return from either
  85. * jack_initialize() or process().
  86. *
  87. * @param arg the same parameter provided to process().
  88. */
  89. void
  90. jack_finish (void *arg)
  91. {
  92. if (arg)
  93. free ((port_pair_t *) arg);
  94. }