1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2001 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
37 /* -*- mode:c; indent-tabs-mode:t; c-basic-offset:4; comment-column:40 -*-
39 * Sphinx II libad (Linux)
40 * ^^^^^^^^^^^^^^^^^^^^^^^
41 * $Id: ad_alsa.c,v 1.6 2001/12/11 00:24:48 lenzo Exp $
43 * John G. Dorsey (jd5q+@andrew.cmu.edu)
44 * Engineering Design Research Center
45 * Carnegie Mellon University
46 * ***************************************************************************
50 * 18-Mar-2006 David Huggins-Daines <dhuggins@cs.cmu.edu>
51 * Update this to the ALSA 1.0 API.
53 * 12-Dec-2000 David Huggins-Daines <dhd@cepstral.com> at Cepstral LLC
54 * Make this at least compile with the new ALSA API.
56 * 05-Nov-1999 Sean Levy (snl@stalphonsos.com) at St. Alphonsos, LLC.
57 * Ported to ALSA so I can actually get working full-duplex.
59 * 09-Aug-1999 Kevin Lenzo (lenzo@cs.cmu.edu) at Cernegie Mellon University.
60 * Incorporated nickr@cs.cmu.edu's changes (marked below) and
61 * SPS_EPSILON to allow for sample rates that are "close enough".
63 * 15-Jun-1999 M. K. Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon Univ.
64 * Consolidated all ad functions into
65 * this one file. Added ad_open_sps().
66 * Other cosmetic changes for consistency (e.g., use of err.h).
68 * 18-May-1999 Kevin Lenzo (lenzo@cs.cmu.edu) added <errno.h>.
76 #include <alsa/asoundlib.h>
81 #include "prim_type.h"
85 #define AUDIO_FORMAT SND_PCM_SFMT_S16_LE /* 16-bit signed, little endian */
86 #define INPUT_GAIN (85)
87 #define SPS_EPSILON 200
90 setparams(int32 sps, snd_pcm_t * handle)
92 snd_pcm_hw_params_t *hwparams;
93 unsigned int out_sps, buffer_time, period_time;
96 snd_pcm_hw_params_alloca(&hwparams);
97 err = snd_pcm_hw_params_any(handle, hwparams);
99 fprintf(stderr, "Can not configure this PCM device: %s\n",
105 snd_pcm_hw_params_set_access(handle, hwparams,
106 SND_PCM_ACCESS_RW_INTERLEAVED);
109 "Failed to set PCM device to interleaved: %s\n",
115 snd_pcm_hw_params_set_format(handle, hwparams, SND_PCM_FORMAT_S16);
118 "Failed to set PCM device to 16-bit signed PCM: %s\n",
123 err = snd_pcm_hw_params_set_channels(handle, hwparams, 1);
125 fprintf(stderr, "Failed to set PCM device to mono: %s\n",
132 snd_pcm_hw_params_set_rate_near(handle, hwparams, &out_sps, NULL);
134 fprintf(stderr, "Failed to set sampling rate: %s\n",
138 if (abs(out_sps - sps) > SPS_EPSILON) {
140 "Available samping rate %d is too far from requested %d\n",
145 /* Set buffer time to the maximum. */
146 err = snd_pcm_hw_params_get_buffer_time_max(hwparams, &buffer_time, 0);
147 period_time = buffer_time / 4;
148 err = snd_pcm_hw_params_set_period_time_near(handle, hwparams,
151 fprintf(stderr, "Failed to set period time to %u: %s\n",
152 period_time, snd_strerror(err));
155 err = snd_pcm_hw_params_set_buffer_time_near(handle, hwparams,
158 fprintf(stderr, "Failed to set buffer time to %u: %s\n",
159 buffer_time, snd_strerror(err));
163 err = snd_pcm_hw_params(handle, hwparams);
165 fprintf(stderr, "Failed to set hwparams: %s\n", snd_strerror(err));
169 err = snd_pcm_nonblock(handle, 1);
171 fprintf(stderr, "Failed to set non-blocking mode: %s\n",
179 setlevels(const char *dev)
182 snd_mixer_selem_id_t *sid;
183 snd_mixer_elem_t *elem;
187 /* Basically we just want to turn on Mic capture. */
188 if ((err = snd_mixer_open(&handle, 0)) < 0) {
189 fprintf(stderr, "Mixer open failed: %s\n", snd_strerror(err));
193 mixer_dev = strdup(dev);
194 if (strncmp(mixer_dev, "plug", 4) == 0)
195 memmove(mixer_dev, mixer_dev + 4, strlen(mixer_dev) - 4 + 1);
196 if ((c = strchr(mixer_dev, ',')))
198 if ((err = snd_mixer_attach(handle, mixer_dev)) < 0) {
199 fprintf(stderr, "Mixer attach to %s failed: %s\n",
200 mixer_dev, snd_strerror(err));
202 snd_mixer_close(handle);
206 if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
207 fprintf(stderr, "Mixer register failed: %s\n", snd_strerror(err));
208 snd_mixer_close(handle);
211 if ((err = snd_mixer_load(handle)) < 0) {
212 fprintf(stderr, "Mixer load failed: %s\n", snd_strerror(err));
213 snd_mixer_close(handle);
216 snd_mixer_selem_id_alloca(&sid);
217 snd_mixer_selem_id_set_name(sid, "Mic");
218 if ((elem = snd_mixer_find_selem(handle, sid)) == NULL) {
219 fprintf(stderr, "Warning: Could not find Mic element\n");
222 if (snd_mixer_selem_has_capture_switch(elem)) {
223 if ((err = snd_mixer_selem_set_capture_switch_all(elem, 1)) < 0) {
225 "Failed to enable microphone capture: %s\n",
227 snd_mixer_close(handle);
232 snd_mixer_selem_id_set_name(sid, "Capture");
233 if ((elem = snd_mixer_find_selem(handle, sid)) == NULL) {
234 fprintf(stderr, "Warning: Could not find Capture element\n");
237 if (snd_mixer_selem_has_capture_switch(elem)) {
238 if ((err = snd_mixer_selem_set_capture_switch_all(elem, 1)) < 0) {
240 "Failed to enable microphone capture: %s\n",
242 snd_mixer_close(handle);
252 ad_open_dev(const char *dev, int32 sps)
260 dev = DEFAULT_DEVICE;
262 err = snd_pcm_open(&dspH, dev, SND_PCM_STREAM_CAPTURE, 0);
265 "Error opening audio device %s for capture: %s\n",
266 dev, snd_strerror(err));
270 if (setparams(sps, dspH) < 0) {
273 if (setlevels(dev) < 0) {
276 if ((handle = (ad_rec_t *) calloc(1, sizeof(ad_rec_t))) == NULL) {
277 fprintf(stderr, "calloc(%d) failed\n", (int)sizeof(ad_rec_t));
282 handle->recording = 0;
284 handle->bps = sizeof(int16);
290 ad_open_sps(int32 sps)
292 return ad_open_dev(DEFAULT_DEVICE, sps);
298 return ad_open_sps(DEFAULT_SAMPLES_PER_SEC);
303 ad_close(ad_rec_t * handle)
305 if (handle->dspH == NULL)
306 return AD_ERR_NOT_OPEN;
308 if (handle->recording) {
309 if (ad_stop_rec(handle) < 0)
312 snd_pcm_close(handle->dspH);
320 ad_start_rec(ad_rec_t * handle)
324 if (handle->dspH == NULL)
325 return AD_ERR_NOT_OPEN;
327 if (handle->recording)
330 err = snd_pcm_prepare(handle->dspH);
332 fprintf(stderr, "snd_pcm_prepare failed: %s\n", snd_strerror(err));
335 err = snd_pcm_start(handle->dspH);
337 fprintf(stderr, "snd_pcm_start failed: %s\n", snd_strerror(err));
340 handle->recording = 1;
347 ad_stop_rec(ad_rec_t * handle)
351 if (handle->dspH == NULL)
352 return AD_ERR_NOT_OPEN;
354 if (!handle->recording)
357 err = snd_pcm_drop(handle->dspH);
359 fprintf(stderr, "snd_pcm_drop failed: %s\n", snd_strerror(err));
362 handle->recording = 0;
369 ad_read(ad_rec_t * handle, int16 * buf, int32 max)
373 if (!handle->recording) {
374 fprintf(stderr, "Recording is stopped, start recording with ad_start_rec\n");
378 length = snd_pcm_readi(handle->dspH, buf, max);
379 if (length == -EAGAIN) {
382 else if (length == -EPIPE) {
383 fprintf(stderr, "Input overrun, read calls are too rare (non-fatal)\n");
384 err = snd_pcm_prepare(handle->dspH);
386 fprintf(stderr, "Can't recover from underrun: %s\n",
392 else if (length == -ESTRPIPE) {
393 fprintf(stderr, "Resuming sound driver (non-fatal)\n");
394 while ((err = snd_pcm_resume(handle->dspH)) == -EAGAIN)
395 usleep(10000); /* Wait for the driver to wake up */
397 err = snd_pcm_prepare(handle->dspH);
399 fprintf(stderr, "Can't recover from underrun: %s\n",
406 else if (length < 0) {
407 fprintf(stderr, "Audio read error: %s\n",
408 snd_strerror(length));