fix segfault, with -f option in jack_impulse_grabber
[jack.git] / example-clients / inprocess.c
blob93258cacae9d5f9439fd57a8cd90c4e9ea4b1767
1 /** @file inprocess.c
3 * @brief This demonstrates the basic concepts for writing a client
4 * that runs within the JACK server process.
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 */
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <memory.h>
14 #include <jack/jack.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().
21 typedef struct {
22 jack_port_t *input_port;
23 jack_port_t *output_port;
24 } port_pair_t;
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.
32 * @return 0 if successful; otherwise jack_finish() will be called and
33 * the client terminated immediately.
35 int
36 inprocess (jack_nframes_t nframes, void *arg)
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);
44 memcpy (out, in, sizeof (jack_default_audio_sample_t) * nframes);
46 return 0; /* continue */
49 /**
50 * This required entry point is called after the client is loaded by
51 * jack_internal_client_load().
53 * @param client pointer to JACK client structure.
54 * @param load_init character string passed to the load operation.
56 * @return 0 if successful; otherwise jack_finish() will be called and
57 * the client terminated immediately.
59 int
60 jack_initialize (jack_client_t *client, const char *load_init)
62 port_pair_t *pp = malloc (sizeof (port_pair_t));
64 if (pp == NULL)
65 return 1; /* heap exhausted */
67 jack_set_process_callback (client, inprocess, pp);
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);
77 /* join the process() cycle */
78 jack_activate (client);
80 /* try to connect to the first physical input & output ports */
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 */
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 */
94 return 0; /* success */
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().
103 * @param arg the same parameter provided to inprocess().
105 void
106 jack_finish (void *arg)
108 if (arg)
109 free ((port_pair_t *) arg);