48 #include <jack/jack.h>
49 #include <jack/jack.h>
50 #include <jack/ringbuffer.h>
52 #define DEFAULT_DEVICE "system:capture_1"
53 #define BUFFER_SIZE 352800
56 jack_client_t *client;
57 jack_port_t *input_port;
58 jack_port_t *output_port;
59 jack_ringbuffer_t* rbuffer;
60 jack_default_audio_sample_t* sample_buffer;
68 const size_t sample_size =
sizeof(jack_default_audio_sample_t);
69 const size_t int16_range_over_two = (-SHRT_MIN + SHRT_MAX) / 2.0;
72 process (jack_nframes_t nframes,
void *arg)
76 size_t buffer_size = jack_ringbuffer_write_space (handle->rbuffer);
78 jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (handle->input_port, nframes);
80 if (buffer_size <= 0) {
81 fprintf(stderr,
"JACK: buffer is full. Deactivating JACK client.\n");
86 jack_ringbuffer_write (handle->rbuffer, (
char*) in, sample_size * nframes);
88 #ifdef MIC_SPEAKER_PASSTHROUGH_DEBUG
90 jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (handle->output_port, nframes);
93 memcpy (out, in, sample_size * nframes);
101 error (
const char *desc)
103 fprintf (stderr,
"JACK error: %s\n", desc);
107 jack_shutdown (
void *arg)
113 srate (jack_nframes_t nframes,
void *arg)
116 printf (
"JACK: The sample rate is now %u/sec\n", nframes);
127 dev = DEFAULT_DEVICE;
130 printf(
"JACK: Setting default device: %s\n", dev);
133 fprintf(stderr,
"calloc(%d) failed\n", (
int)
sizeof(
ad_rec_t));
140 jack_set_error_function (error);
143 if ((handle->client = jack_client_open (
"jack_ad", (jack_options_t)0, NULL)) == 0) {
144 fprintf (stderr,
"jack server not running?\n");
148 handle->rbuffer = jack_ringbuffer_create(BUFFER_SIZE);
149 handle->sample_buffer = malloc(BUFFER_SIZE);
151 if(handle->rbuffer == NULL) {
152 fprintf (stderr,
"Failed to create jack ringbuffer\n");
159 jack_set_process_callback (handle->client, process, handle);
164 jack_set_sample_rate_callback (handle->client, srate, 0);
170 jack_on_shutdown (handle->client, jack_shutdown, 0);
174 if((handle->input_port = jack_port_register (handle->client,
"jack_ad_input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
175 fprintf (stderr,
"cannot register input port!\n");
179 if((handle->output_port = jack_port_register (handle->client,
"jack_ad_output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsOutput, 0)) == 0) {
180 fprintf (stderr,
"cannot register output port!\n");
185 if (jack_activate (handle->client)) {
186 fprintf (stderr,
"cannot activate client");
196 if ((ports = jack_get_ports (handle->client, dev, NULL, JackPortIsOutput)) == NULL) {
197 fprintf(stderr,
"Cannot find any physical capture ports\n");
201 if (jack_connect (handle->client, ports[0], jack_port_name (handle->input_port))) {
202 fprintf (stderr,
"cannot connect input ports\n");
208 #ifdef MIC_SPEAKER_PASSTHROUGH_DEBUG
210 if ((ports = jack_get_ports (handle->client,
"system:playback", NULL,
211 JackPortIsPhysical|JackPortIsInput)) == NULL) {
212 fprintf(stderr,
"Cannot find any physical playback ports\n");
216 for (i = 0; ports[i] != NULL; i++) {
217 if (jack_connect (handle->client, jack_port_name (handle->output_port), ports[i])) {
218 fprintf (stderr,
"cannot connect output ports\n");
225 handle->recording = 0;
227 handle->bps =
sizeof(int16);
251 free (handle->sample_buffer);
252 jack_ringbuffer_free (handle->rbuffer);
253 jack_client_close (handle->client);
262 if (handle->recording)
265 handle->recording = 1;
273 handle->recording = 0;
279 ad_read(
ad_rec_t * handle, int16 * buf, int32 max)
282 if (!handle->recording)
285 size_t length = sample_size * max;
287 length = jack_ringbuffer_read (handle->rbuffer, (
char*) handle->sample_buffer, length);
288 size_t length_in_samples = length / sample_size;
290 for(i = 0; i < length_in_samples; i++) {
291 buf[i] = (int16) (int16_range_over_two * (handle->sample_buffer[i] + 1.0) + SHRT_MIN);
294 if (length == 0 && (!handle->recording)) {
298 return length_in_samples;
Audio recording structure.
Basic type definitions used in Sphinx.
SPHINXBASE_EXPORT ad_rec_t * ad_open(void)
Open the default audio device.
generic live audio interface for recording and playback
SPHINXBASE_EXPORT ad_rec_t * ad_open_dev(const char *dev, int32 samples_per_sec)
Open a specific audio device for recording.
SPHINXBASE_EXPORT ad_rec_t * ad_open_sps(int32 samples_per_sec)
Open the default audio device with a given sampling rate.
Audio recording structure.