SphinxBase  5prealpha
ad_openal.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2014 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 #include <config.h>
38 #include <stdlib.h>
39 #include <stdio.h>
40 
41 #include "ad.h"
42 
43 #include <al.h>
44 #include <alc.h>
45 
46 struct ad_rec_s {
47  ALCdevice * device;
48 };
49 
50 ad_rec_t *
51 ad_open_dev(const char * dev, int32 samples_per_sec)
52 {
53  ad_rec_t * handle = malloc(sizeof(ad_rec_t));
54 
55  if (handle == NULL) {
56  fprintf(stderr, "%s\n", "failed to allocate memory");
57  abort();
58  }
59 
60  handle -> device = alcCaptureOpenDevice(dev, samples_per_sec, AL_FORMAT_MONO16, samples_per_sec * 10);
61 
62  if (handle -> device == NULL) {
63  free(handle);
64  fprintf(stderr, "%s\n", "failed to open capture device");
65  abort();
66  }
67 
68  return handle;
69 }
70 
71 
72 ad_rec_t *
73 ad_open_sps(int32 samples_per_sec)
74 {
75  return ad_open_dev(NULL, samples_per_sec);
76 }
77 
78 ad_rec_t *
79 ad_open(void)
80 {
81  return ad_open_sps(DEFAULT_SAMPLES_PER_SEC);
82 }
83 
84 
85 int32
86 ad_start_rec(ad_rec_t * r)
87 {
88  alcCaptureStart(r -> device);
89  return 0;
90 }
91 
92 
93 int32
94 ad_stop_rec(ad_rec_t * r)
95 {
96  alcCaptureStop(r -> device);
97  return 0;
98 }
99 
100 
101 int32
102 ad_read(ad_rec_t * r, int16 * buf, int32 max)
103 {
104  ALCint number;
105 
106  alcGetIntegerv(r -> device, ALC_CAPTURE_SAMPLES, sizeof(number), &number);
107  if (number >= 0) {
108  number = (number < max ? number : max);
109  alcCaptureSamples(r -> device, buf, number);
110  }
111 
112  return number;
113 }
114 
115 
116 int32
117 ad_close(ad_rec_t * r)
118 {
119  ALCboolean isClosed;
120 
121  isClosed = alcCaptureCloseDevice(r -> device);
122 
123  if (isClosed) {
124  return 0;
125  } else {
126  return -1;
127  }
128 }
Audio recording structure.
SPHINXBASE_EXPORT ad_rec_t * ad_open(void)
Open the default audio device.
Definition: ad_alsa.c:228
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_sps(int32 samples_per_sec)
Open the default audio device with a given sampling rate.
Definition: ad_alsa.c:222
Audio recording structure.
Definition: ad_alsa.c:90