SphinxBase  0.6
ad_jack.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 2011 Glenn Pierce. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY GLENN PIERCE ``AS IS'' AND
18  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
21  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * ====================================================================
30  *
31  */
32 /* Sphinx II libad (Linux)
33  * ^^^^^^^^^^^^^^^^^^^^^^^
34  *
35  *
36  * Glenn Pierce (glennpierce@gmail.com)
37  * ********************************************************************
38  */
39 
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <config.h>
47 #include <limits.h>
48 
49 #include "prim_type.h"
50 #include "ad.h"
51 
52 #include <jack/jack.h>
53 
54 #define DEFAULT_DEVICE "system:capture_1"
55 #define BUFFER_SIZE 352800
56 
57 //#define MIC_SPEAKER_PASSTHROUGH_DEBUG
58 
59 const size_t sample_size = sizeof(jack_default_audio_sample_t);
60 const size_t int16_range_over_two = (-SHRT_MIN + SHRT_MAX) / 2.0;
61 
62 int
63 process (jack_nframes_t nframes, void *arg)
64 {
65  ad_rec_t *handle = (ad_rec_t *) arg;
66 
67  size_t buffer_size = jack_ringbuffer_write_space (handle->rbuffer);
68 
69  jack_default_audio_sample_t *in = (jack_default_audio_sample_t *) jack_port_get_buffer (handle->input_port, nframes);
70 
71  if (buffer_size <= 0) {
72  fprintf(stderr, "JACK: buffer is full. Deactivating JACK client.\n");
73  return 1;
74  }
75 
76  // Write to jack ringbuffer which should be thread safe
77  jack_ringbuffer_write (handle->rbuffer, (char*) in, sample_size * nframes);
78 
79 #ifdef MIC_SPEAKER_PASSTHROUGH_DEBUG
80 
81  jack_default_audio_sample_t *out = (jack_default_audio_sample_t *) jack_port_get_buffer (handle->output_port, nframes);
82 
83  // Output mic output to speakers (Just for testing)
84  memcpy (out, in, sample_size * nframes);
85 
86 #endif
87 
88  return 0;
89 }
90 
91 void
92 error (const char *desc)
93 {
94  fprintf (stderr, "JACK error: %s\n", desc);
95 }
96 
97 void
98 jack_shutdown (void *arg)
99 {
100  exit(1);
101 }
102 
103 int
104 srate (jack_nframes_t nframes, void *arg)
105 
106 {
107  printf ("JACK: The sample rate is now %u/sec\n", nframes);
108  return 0;
109 }
110 
111 ad_rec_t *
112 ad_open_dev(const char *dev, int32 sps)
113 {
114  ad_rec_t *handle;
115  const char **ports;
116 
117 #ifdef HAVE_SAMPLERATE_H
118  int resample_error;
119  double samplerates_ratio;
120  jack_nframes_t jack_samplerate;
121 #endif
122 
123  if (dev == NULL) {
124  dev = DEFAULT_DEVICE;
125  }
126 
127  printf("JACK: Setting default device: %s\n", dev);
128 
129  if ((handle = (ad_rec_t *) calloc(1, sizeof(ad_rec_t))) == NULL) {
130  fprintf(stderr, "calloc(%d) failed\n", (int)sizeof(ad_rec_t));
131  abort();
132  }
133 
134  /* Tell the JACK server to call error() whenever it
135  experiences an error.
136  */
137  jack_set_error_function (error);
138 
139  /* Try to become a client of the JACK server */
140  if ((handle->client = jack_client_open ("jack_ad", (jack_options_t)0, NULL)) == 0) {
141  fprintf (stderr, "jack server not running?\n");
142  return NULL;
143  }
144 
145 #ifdef HAVE_SAMPLERATE_H
146  handle->resample_buffer = malloc(BUFFER_SIZE);
147 
148  jack_samplerate = jack_get_sample_rate(handle->client);
149  samplerates_ratio = (double)((double)jack_samplerate / (double)sps);
150 
151  handle->rbuffer = jack_ringbuffer_create((int)((double)BUFFER_SIZE * samplerates_ratio));
152  handle->sample_buffer = malloc((int)((double)BUFFER_SIZE * samplerates_ratio));
153 #else
154  handle->rbuffer = jack_ringbuffer_create(BUFFER_SIZE);
155  handle->sample_buffer = malloc(BUFFER_SIZE);
156 #endif
157 
158  if(handle->rbuffer == NULL) {
159  fprintf (stderr, "Failed to create jack ringbuffer\n");
160  return NULL;
161  }
162 
163  /* Tell the JACK server to call `process()' whenever
164  there is work to be done.
165  */
166  jack_set_process_callback (handle->client, process, handle);
167 
168  /* Tell the JACK server to call `srate()' whenever
169  the sample rate of the system changes.
170  */
171  jack_set_sample_rate_callback (handle->client, srate, 0);
172 
173  /* Tell the JACK server to call `jack_shutdown()' if
174  it ever shuts down, either entirely, or if it
175  just decides to stop calling us.
176  */
177  jack_on_shutdown (handle->client, jack_shutdown, 0);
178 
179 
180  /* Create the input port */
181  if((handle->input_port = jack_port_register (handle->client, "jack_ad_input", JACK_DEFAULT_AUDIO_TYPE, JackPortIsInput, 0)) == 0) {
182  fprintf (stderr, "cannot register input port!\n");
183  return NULL;
184  }
185 
186  if((handle->output_port = jack_port_register (handle->client, "jack_ad_output", JACK_DEFAULT_AUDIO_TYPE, JackPortIsPhysical|JackPortIsOutput, 0)) == 0) {
187  fprintf (stderr, "cannot register output port!\n");
188  return NULL;
189  }
190 
191  /* Tell the JACK server that we are ready to start */
192  if (jack_activate (handle->client)) {
193  fprintf (stderr, "cannot activate client");
194  return NULL;
195  }
196 
197  /* Connect the ports. Note: you can't do this before
198  the client is activated, because we can't allow
199  connections to be made to clients that aren't
200  running.
201  */
202 
203  if ((ports = jack_get_ports (handle->client, dev, NULL, JackPortIsOutput)) == NULL) {
204  fprintf(stderr, "Cannot find any physical capture ports\n");
205  return NULL;
206  }
207 
208  if (jack_connect (handle->client, ports[0], jack_port_name (handle->input_port))) {
209  fprintf (stderr, "cannot connect input ports\n");
210  return NULL;
211  }
212 
213  free (ports);
214 
215 #ifdef MIC_SPEAKER_PASSTHROUGH_DEBUG
216  int i;
217  if ((ports = jack_get_ports (handle->client, "system:playback", NULL,
218  JackPortIsPhysical|JackPortIsInput)) == NULL) {
219  fprintf(stderr, "Cannot find any physical playback ports\n");
220  return NULL;
221  }
222 
223  for (i = 0; ports[i] != NULL; i++) {
224  if (jack_connect (handle->client, jack_port_name (handle->output_port), ports[i])) {
225  fprintf (stderr, "cannot connect output ports\n");
226  }
227  }
228 
229  free (ports);
230 #endif
231 
232 #ifdef HAVE_SAMPLERATE_H
233  // Initialize the sample rate converter.
234  if ((handle->resample_state = src_new (SRC_SINC_BEST_QUALITY, 1, &resample_error)) == NULL) {
235  fprintf (stderr, "Error : src_new() failed: %s\n", src_strerror(resample_error));
236  return NULL;
237  }
238 #endif
239 
240  handle->recording = 0;
241  handle->sps = sps;
242  handle->bps = sizeof(int16);
243 
244  // Give the jack process callback time to run ?
245  sleep (1);
246 
247  return (ad_rec_t *) handle;
248 }
249 
250 ad_rec_t *
251 ad_open_sps(int32 sps)
252 {
253  // Ignored the sps has to set for the jackd server
254  return ad_open_dev(DEFAULT_DEVICE, sps);
255 }
256 
257 ad_rec_t *
258 ad_open(void)
259 {
260  return ad_open_sps(DEFAULT_SAMPLES_PER_SEC);
261 }
262 
263 int32
264 ad_close(ad_rec_t * handle)
265 {
266  free (handle->sample_buffer);
267 #ifdef HAVE_SAMPLERATE_H
268  free (handle->resample_buffer);
269 #endif
270  jack_ringbuffer_free (handle->rbuffer);
271  jack_client_close (handle->client);
272  free(handle);
273 
274  return 0;
275 }
276 
277 int32
278 ad_start_rec(ad_rec_t * handle)
279 {
280  if (handle->recording)
281  return AD_ERR_GEN;
282 
283  handle->recording = 1;
284 
285  return 0;
286 }
287 
288 int32
289 ad_stop_rec(ad_rec_t * handle)
290 {
291  handle->recording = 0;
292 
293  return 0;
294 }
295 
296 int32
297 ad_read(ad_rec_t * handle, int16 * buf, int32 max)
298 {
299 #ifdef HAVE_SAMPLERATE_H
300  int resample_error;
301 #endif
302 
303  if (!handle->recording)
304  return AD_EOF;
305 
306  size_t length = sample_size * max;
307 
308 #ifdef HAVE_SAMPLERATE_H
309 
310  // Resample the data from the sample rate set in the jack server to that required
311  // by sphinx
312 
313  length = jack_ringbuffer_peek (handle->rbuffer, (char*) handle->sample_buffer, length);
314  size_t length_in_samples = length / sample_size;
315 
316  if(handle->resample_state == NULL)
317  return AD_EOF;
318 
319  // We will try to downsample if jackd is running at a higher sample rate
320  jack_nframes_t jack_samplerate = jack_get_sample_rate(handle->client);
321 
322  SRC_DATA data;
323 
324  data.data_in = handle->sample_buffer;
325  data.input_frames = length_in_samples;
326  data.data_out = handle->resample_buffer;
327  data.output_frames = BUFFER_SIZE / sample_size;
328  data.src_ratio = (float) handle->sps / jack_samplerate;
329  data.end_of_input = 0;
330 
331  if ((resample_error = src_process(handle->resample_state, &data)) != 0) {
332  fprintf (stderr, "resample error %s\n", src_strerror (resample_error));
333  return 1;
334  }
335 
336  for(int i=0; i<data.output_frames_gen; i++) {
337  buf[i] = (int16) (int16_range_over_two * (handle->resample_buffer[i] + 1.0) + SHRT_MIN);
338  }
339 
340  jack_ringbuffer_read_advance(handle->rbuffer, data.input_frames_used * sample_size);
341 
342  if(length == 0 && (!handle->recording)) {
343  return AD_EOF;
344  }
345 
346  return data.output_frames_gen;
347 
348 #else
349 
350  length = jack_ringbuffer_read (handle->rbuffer, (char*) handle->sample_buffer, length);
351  size_t length_in_samples = length / sample_size;
352 
353  for(int i=0; i<length_in_samples; i++) {
354  buf[i] = (int16) (int16_range_over_two * (handle->sample_buffer[i] + 1.0) + SHRT_MIN);
355  }
356 
357  if(length == 0 && (!handle->recording)) {
358  return AD_EOF;
359  }
360 
361  return length_in_samples;
362 
363 #endif
364 }
Definition: ad.h:255
int32 sps
Samples/sec.
Definition: ad.h:256
Basic type definitions used in Sphinx.
int32 bps
Bytes/sample.
Definition: ad.h:257
SPHINXBASE_EXPORT ad_rec_t * ad_open(void)
Open the default audio device.
Definition: ad_alsa.c:296
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.
Definition: ad_alsa.c:252
SPHINXBASE_EXPORT ad_rec_t * ad_open_sps(int32 samples_per_sec)
Open the default audio device with a given sampling rate.
Definition: ad_alsa.c:290