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.

136 lines
3.9KB

  1. /** @file inprocess_s32.cpp
  2. *
  3. * @brief This demonstrates the basic concepts for writing a client
  4. * that runs within the JACK server process for s32 format.
  5. *
  6. * For the sake of example, a port_converter_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 <typeinfo>
  14. #include <jack/jack.h>
  15. #include <jack/format_converter.h>
  16. /**
  17. * For the sake of example, an instance of this struct is allocated in
  18. * jack_initialize(), passed to inprocess() as an argument, then freed
  19. * in jack_finish().
  20. */
  21. typedef struct {
  22. IJackPortConverter* input_port_converter;
  23. IJackPortConverter* output_port_converter;
  24. } port_converter_pair_t;
  25. /**
  26. * Called in the realtime thread on every process cycle. The entry
  27. * point name was passed to jack_set_process_callback() from
  28. * jack_initialize(). Although this is an internal client, its
  29. * process() interface is identical to @ref simple_client.c.
  30. *
  31. * @return 0 if successful; otherwise jack_finish() will be called and
  32. * the client terminated immediately.
  33. */
  34. int
  35. inprocess (jack_nframes_t nframes, void *arg)
  36. {
  37. port_converter_pair_t *pp = (port_converter_pair_t *)arg;
  38. int32_t *in_buffer;
  39. int32_t *out_buffer;
  40. in_buffer = (int32_t *) pp->input_port_converter->get(nframes);
  41. out_buffer = (int32_t *) pp->output_port_converter->get(nframes);
  42. /* This memcpy needs to be replaced with the actual processing */
  43. memcpy (out_buffer, in_buffer, sizeof(int32_t) * nframes);
  44. pp->output_port_converter->set(out_buffer, nframes);
  45. return 0;
  46. }
  47. /**
  48. * This required entry point is called after the client is loaded by
  49. * jack_internal_client_load().
  50. *
  51. * @param client pointer to JACK client structure.
  52. * @param load_init character string passed to the load operation.
  53. *
  54. * @return 0 if successful; otherwise jack_finish() will be called and
  55. * the client terminated immediately.
  56. */
  57. extern "C" int
  58. jack_initialize (jack_client_t *client, const char *load_init)
  59. {
  60. jack_port_t *input_port;
  61. jack_port_t *output_port;
  62. port_converter_pair_t *pp = (port_converter_pair_t *) malloc (sizeof (port_converter_pair_t));
  63. const char **ports;
  64. if (pp == NULL)
  65. return 1; /* heap exhausted */
  66. jack_set_process_callback (client, inprocess, pp);
  67. /* create a pair of ports */
  68. input_port = jack_port_register (client, "input",
  69. JACK_DEFAULT_AUDIO_TYPE,
  70. JackPortIsInput, 0);
  71. output_port = jack_port_register (client, "output",
  72. JACK_DEFAULT_AUDIO_TYPE,
  73. JackPortIsOutput, 0);
  74. pp->input_port_converter = jack_port_create_converter(input_port, typeid(int32_t), false);
  75. pp->output_port_converter = jack_port_create_converter(output_port, typeid(int32_t), false);
  76. /* join the process() cycle */
  77. jack_activate (client);
  78. ports = jack_get_ports (client, NULL, NULL,
  79. JackPortIsPhysical|JackPortIsOutput);
  80. if (ports == NULL) {
  81. fprintf(stderr, "no physical capture ports\n");
  82. return 1; /* terminate client */
  83. }
  84. if (jack_connect (client, ports[0], jack_port_name (input_port))) {
  85. fprintf (stderr, "cannot connect input ports\n");
  86. }
  87. jack_free (ports);
  88. ports = jack_get_ports (client, NULL, NULL,
  89. JackPortIsPhysical|JackPortIsInput);
  90. if (ports == NULL) {
  91. fprintf(stderr, "no physical playback ports\n");
  92. return 1; /* terminate client */
  93. }
  94. if (jack_connect (client, jack_port_name (output_port), ports[0])) {
  95. fprintf (stderr, "cannot connect output ports\n");
  96. }
  97. jack_free (ports);
  98. return 0; /* success */
  99. }
  100. /**
  101. * This required entry point is called immediately before the client
  102. * is unloaded, which could happen due to a call to
  103. * jack_internal_client_unload(), or a nonzero return from either
  104. * jack_initialize() or inprocess().
  105. *
  106. * @param arg the same parameter provided to inprocess().
  107. */
  108. extern "C" void
  109. jack_finish (void *arg)
  110. {
  111. if (arg)
  112. free ((port_converter_pair_t *) arg);
  113. }