SphinxBase 0.6
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
51ad_open_dev(const char *dev, int32 samples_per_sec)
52{
53 ad_rec_t *handle;
54 pa_simple *pa;
55 pa_sample_spec ss;
56 int error;
57
58 ss.format = PA_SAMPLE_S16LE;
59 ss.channels = 1;
60 ss.rate = 16000; //samples_per_sec;
61
62 pa = pa_simple_new(NULL, "ASR", PA_STREAM_RECORD, dev, "Speech", &ss, NULL, NULL, &error);
63 if (pa == NULL) {
64 fprintf(stderr, "Error opening audio device %s for capture: %s\n", dev, pa_strerror(error));
65 return NULL;
66 }
67
68 if ((handle = (ad_rec_t *) calloc(1, sizeof(ad_rec_t))) == NULL) {
69 fprintf(stderr, "Failed to allocate memory for ad device\n");
70 return NULL;
71 }
72
73 handle->pa = pa;
74 handle->recording = 0;
75 handle->sps = samples_per_sec;
76 handle->bps = sizeof(int16);
77
78 return handle;
79}
80
81
83ad_open_sps(int32 samples_per_sec)
84{
85 return ad_open_dev(DEFAULT_DEVICE, samples_per_sec);
86}
87
89ad_open(void)
90{
91 return ad_open_sps(DEFAULT_SAMPLES_PER_SEC);
92}
93
94
95int32
96ad_start_rec(ad_rec_t * r)
97{
98 if (r->recording)
99 return AD_ERR_GEN;
100
101 r->recording = 1;
102
103 return 0;
104}
105
106
107int32
108ad_stop_rec(ad_rec_t * r)
109{
110 if (!r->recording)
111 return AD_ERR_GEN;
112
113 r->recording = 0;
114
115 return 0;
116}
117
118
119int32
120ad_read(ad_rec_t * r, int16 * buf, int32 max)
121{
122 int error;
123
124 if (!r->recording)
125 return AD_EOF;
126
127 if (pa_simple_read(r->pa, (void*)buf, max * 2, &error) < 0) {
128 fprintf(stderr, "Failed to read speech: %s\n", pa_strerror(error));
129 }
130
131 return max;
132}
133
134
135int32
136ad_close(ad_rec_t * r)
137{
138 if (r->pa == NULL)
139 return AD_ERR_NOT_OPEN;
140
141 if (r->recording) {
142 if (ad_stop_rec(r) < 0)
143 return AD_ERR_GEN;
144 }
145 pa_simple_free(r->pa);
146 free(r);
147
148 return 0;
149}
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(void)
Open the default audio device.
Definition ad_alsa.c:296
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
Basic type definitions used in Sphinx.
Definition ad.h:255
int32 sps
Samples/sec.
Definition ad.h:256
int32 bps
Bytes/sample.
Definition ad.h:257