1 /** @file thru_client.c
3 * @brief This simple through client demonstrates the basic features of JACK
4 * as they would be used by many applications.
16 #include <jack/jack.h>
18 jack_port_t
**input_ports
;
19 jack_port_t
**output_ports
;
20 jack_client_t
*client
;
22 static void signal_handler ( int sig
)
24 jack_client_close ( client
);
25 fprintf ( stderr
, "signal received, exiting ...\n" );
30 * The process callback for this JACK application is called in a
31 * special realtime thread once for each audio cycle.
33 * This client follows a simple rule: when the JACK transport is
34 * running, copy the input port to the output. When it stops, exit.
38 process ( jack_nframes_t nframes
, void *arg
)
41 jack_default_audio_sample_t
*in
, *out
;
42 for ( i
= 0; i
< 2; i
++ )
44 in
= jack_port_get_buffer ( input_ports
[i
], nframes
);
45 out
= jack_port_get_buffer ( output_ports
[i
], nframes
);
46 memcpy ( out
, in
, nframes
* sizeof ( jack_default_audio_sample_t
) );
52 * JACK calls this shutdown_callback if the server ever shuts down or
53 * decides to disconnect the client.
56 jack_shutdown ( void *arg
)
59 free ( output_ports
);
64 main ( int argc
, char *argv
[] )
68 const char *client_name
;
69 const char *server_name
= NULL
;
70 jack_options_t options
= JackNullOption
;
73 if ( argc
>= 2 ) /* client name specified? */
75 client_name
= argv
[1];
76 if ( argc
>= 3 ) /* server name specified? */
78 server_name
= argv
[2];
79 options
|= JackServerName
;
82 else /* use basename of argv[0] */
84 client_name
= strrchr ( argv
[0], '/' );
85 if ( client_name
== 0 )
87 client_name
= argv
[0];
95 /* open a client connection to the JACK server */
97 client
= jack_client_open ( client_name
, options
, &status
, server_name
);
100 fprintf ( stderr
, "jack_client_open() failed, "
101 "status = 0x%2.0x\n", status
);
102 if ( status
& JackServerFailed
)
104 fprintf ( stderr
, "Unable to connect to JACK server\n" );
108 if ( status
& JackServerStarted
)
110 fprintf ( stderr
, "JACK server started\n" );
112 if ( status
& JackNameNotUnique
)
114 client_name
= jack_get_client_name ( client
);
115 fprintf ( stderr
, "unique name `%s' assigned\n", client_name
);
118 /* tell the JACK server to call `process()' whenever
119 there is work to be done.
122 jack_set_process_callback ( client
, process
, 0 );
124 /* tell the JACK server to call `jack_shutdown()' if
125 it ever shuts down, either entirely, or if it
126 just decides to stop calling us.
129 jack_on_shutdown ( client
, jack_shutdown
, 0 );
131 /* create two ports pairs*/
132 input_ports
= ( jack_port_t
** ) calloc ( 2, sizeof ( jack_port_t
* ) );
133 output_ports
= ( jack_port_t
** ) calloc ( 2, sizeof ( jack_port_t
* ) );
136 for ( i
= 0; i
< 2; i
++ )
138 sprintf ( port_name
, "input_%d", i
+ 1 );
139 input_ports
[i
] = jack_port_register ( client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0 );
140 sprintf ( port_name
, "output_%d", i
+ 1 );
141 output_ports
[i
] = jack_port_register ( client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0 );
142 if ( ( input_ports
[i
] == NULL
) || ( output_ports
[i
] == NULL
) )
144 fprintf ( stderr
, "no more JACK ports available\n" );
149 /* Tell the JACK server that we are ready to roll. Our
150 * process() callback will start running now. */
152 if ( jack_activate ( client
) )
154 fprintf ( stderr
, "cannot activate client" );
158 /* Connect the ports. You can't do this before the client is
159 * activated, because we can't make connections to clients
160 * that aren't running. Note the confusing (but necessary)
161 * orientation of the driver backend ports: playback ports are
162 * "input" to the backend, and capture ports are "output" from
166 ports
= jack_get_ports ( client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsOutput
);
169 fprintf ( stderr
, "no physical capture ports\n" );
173 for ( i
= 0; i
< 2; i
++ )
174 if ( jack_connect ( client
, ports
[i
], jack_port_name ( input_ports
[i
] ) ) )
175 fprintf ( stderr
, "cannot connect input ports\n" );
179 ports
= jack_get_ports ( client
, NULL
, NULL
, JackPortIsPhysical
|JackPortIsInput
);
182 fprintf ( stderr
, "no physical playback ports\n" );
186 for ( i
= 0; i
< 2; i
++ )
187 if ( jack_connect ( client
, jack_port_name ( output_ports
[i
] ), ports
[i
] ) )
188 fprintf ( stderr
, "cannot connect input ports\n" );
192 /* install a signal handler to properly quits jack client */
194 signal ( SIGINT
, signal_handler
);
195 signal ( SIGABRT
, signal_handler
);
196 signal ( SIGTERM
, signal_handler
);
198 signal ( SIGQUIT
, signal_handler
);
199 signal ( SIGTERM
, signal_handler
);
200 signal ( SIGHUP
, signal_handler
);
201 signal ( SIGINT
, signal_handler
);
204 /* keep running until the transport stops */
215 jack_client_close ( client
);