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