SphinxBase 5prealpha
ad_pulse.c
1/* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* ====================================================================
3 * Copyright (c) 1999-2011 Carnegie Mellon University. All rights
4 * reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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
16 * distribution.
17 *
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.
21 *
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.
33 *
34 * ====================================================================
35 *
36 */
37
38/* Input for Pulseaudio */
39
40#include <stdio.h>
41#include <string.h>
42#include <config.h>
43
44#include <pulse/pulseaudio.h>
45#include <pulse/simple.h>
46
47#include "prim_type.h"
48#include "ad.h"
49
50#define DEFAULT_DEVICE NULL
51
52struct ad_rec_s {
53 pa_simple* pa;
54 int32 recording;
55 int32 sps;
56 int32 bps;
57};
58
60ad_open_dev(const char *dev, int32 samples_per_sec)
61{
62 ad_rec_t *handle;
63 pa_simple *pa;
64 pa_sample_spec ss;
65 int error;
66
67 ss.format = PA_SAMPLE_S16LE;
68 ss.channels = 1;
69 ss.rate = 16000;
70
71 pa = pa_simple_new(NULL, "ASR", PA_STREAM_RECORD, dev, "Speech", &ss, NULL, NULL, &error);
72 if (pa == NULL) {
73 fprintf(stderr, "Error opening audio device %s for capture: %s\n", dev, pa_strerror(error));
74 return NULL;
75 }
76
77 if ((handle = (ad_rec_t *) calloc(1, sizeof(ad_rec_t))) == NULL) {
78 fprintf(stderr, "Failed to allocate memory for ad device\n");
79 return NULL;
80 }
81
82 handle->pa = pa;
83 handle->recording = 0;
84 handle->sps = samples_per_sec;
85 handle->bps = sizeof(int16);
86
87 return handle;
88}
89
90
92ad_open_sps(int32 samples_per_sec)
93{
94 return ad_open_dev(DEFAULT_DEVICE, samples_per_sec);
95}
96
98ad_open(void)
99{
100 return ad_open_sps(DEFAULT_SAMPLES_PER_SEC);
101}
102
103
104int32
105ad_start_rec(ad_rec_t * r)
106{
107 if (r->recording)
108 return AD_ERR_GEN;
109
110 r->recording = 1;
111
112 return 0;
113}
114
115
116int32
117ad_stop_rec(ad_rec_t * r)
118{
119 if (!r->recording)
120 return AD_ERR_GEN;
121
122 r->recording = 0;
123
124 return 0;
125}
126
127
128int32
129ad_read(ad_rec_t * r, int16 * buf, int32 max)
130{
131 int error;
132
133 if (!r->recording)
134 return AD_EOF;
135
136 if (max > 2048) {
137 max = 2048;
138 }
139
140 if (pa_simple_read(r->pa, (void*)buf, max * 2, &error) < 0) {
141 fprintf(stderr, "Failed to read speech: %s\n", pa_strerror(error));
142 }
143
144 return max;
145}
146
147
148int32
149ad_close(ad_rec_t * r)
150{
151 if (r->pa == NULL)
152 return AD_ERR_NOT_OPEN;
153
154 if (r->recording) {
155 if (ad_stop_rec(r) < 0)
156 return AD_ERR_GEN;
157 }
158 pa_simple_free(r->pa);
159 free(r);
160
161 return 0;
162}
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:187
SPHINXBASE_EXPORT ad_rec_t * ad_open(void)
Open the default audio device.
Definition ad_alsa.c:228
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:222
Basic type definitions used in Sphinx.
Audio recording structure.
Definition ad_alsa.c:90
int32 sps
Samples/sec.
Definition ad_alsa.c:93
int32 bps
Bytes/sample.
Definition ad_alsa.c:94
Audio recording structure.