SphinxBase  0.6
ad.h
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2004 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  * ad.h -- generic live audio interface for recording and playback
39  *
40  * **********************************************
41  * CMU ARPA Speech Project
42  *
43  * Copyright (c) 1996 Carnegie Mellon University.
44  * ALL RIGHTS RESERVED.
45  * **********************************************
46  *
47  * HISTORY
48  *
49  * $Log: ad.h,v $
50  * Revision 1.8 2005/06/22 08:00:06 arthchan2003
51  * Completed all doxygen documentation on file description for libs3decoder/libutil/libs3audio and programs.
52  *
53  * Revision 1.7 2004/12/14 00:39:49 arthchan2003
54  * add <s3types.h> to the code, change some comments to doxygen style
55  *
56  * Revision 1.6 2004/12/06 11:17:55 arthchan2003
57  * Update the copyright information of ad.h, *sigh* start to feel tired of updating documentation system. Anyone who has time, please take up libs3audio. That is the last place which is undocumented
58  *
59  * Revision 1.5 2004/07/23 23:44:46 egouvea
60  * Changed the cygwin code to use the same audio files as the MS Visual code, removed unused variables from fe_interface.c
61  *
62  * Revision 1.4 2004/02/29 23:48:31 egouvea
63  * Updated configure.in to the recent automake/autoconf, fixed win32
64  * references in audio files.
65  *
66  * Revision 1.3 2002/11/10 19:27:38 egouvea
67  * Fixed references to sun's implementation of audio interface,
68  * referring to the correct .h file, and replacing sun4 with sunos.
69  *
70  * Revision 1.2 2001/12/11 04:40:55 lenzo
71  * License cleanup.
72  *
73  * Revision 1.1.1.1 2001/12/03 16:01:45 egouvea
74  * Initial import of sphinx3
75  *
76  * Revision 1.1.1.1 2001/01/17 05:17:14 ricky
77  * Initial Import of the s3.3 decoder, has working decodeaudiofile, s3.3_live
78  *
79  *
80  * 19-Jan-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
81  * Added AD_ return codes. Added ad_open_sps_bufsize(), and
82  * ad_rec_t.n_buf.
83  *
84  * 17-Apr-98 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
85  * Added ad_open_play_sps().
86  *
87  * 07-Mar-98 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
88  * Added ad_open_sps().
89  *
90  * 10-Jun-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
91  * Added ad_wbuf_t, ad_rec_t, and ad_play_t types, and augmented all
92  * recording functions with ad_rec_t, and playback functions with
93  * ad_play_t.
94  *
95  * 06-Jun-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
96  * Created.
97  */
98 
103 #ifndef _AD_H_
104 #define _AD_H_
105 
106 #include <sphinx_config.h>
107 
108 #if defined (__CYGWIN__)
109 #include <w32api/windows.h>
110 #include <w32api/mmsystem.h>
111 #elif (defined(WIN32) && !defined(GNUWINCE)) || defined(_WIN32_WCE)
112 #include <windows.h>
113 #include <mmsystem.h>
114 #elif defined(AD_BACKEND_ALSA)
115 #include <alsa/asoundlib.h>
116 #endif
117 
118 /* Win32/WinCE DLL gunk */
119 #include <sphinxbase/sphinxbase_export.h>
120 
121 #include <sphinxbase/prim_type.h>
122 
123 #ifdef __cplusplus
124 extern "C" {
125 #endif
126 #if 0
127 /* Fool Emacs. */
128 }
129 #endif
130 
131 #define AD_SAMPLE_SIZE (sizeof(int16))
132 #define DEFAULT_SAMPLES_PER_SEC 16000
133 
134 /* Return codes */
135 #define AD_OK 0
136 #define AD_EOF -1
137 #define AD_ERR_GEN -1
138 #define AD_ERR_NOT_OPEN -2
139 #define AD_ERR_WAVE -3
140 
141 
142 #if (defined(WIN32) || defined(AD_BACKEND_WIN32)) && !defined(GNUWINCE)
143 typedef struct {
144  HGLOBAL h_whdr;
145  LPWAVEHDR p_whdr;
146  HGLOBAL h_buf;
147  LPSTR p_buf;
148 } ad_wbuf_t;
149 #endif
150 
151 
152 /* ------------ RECORDING -------------- */
153 
154 /*
155  * NOTE: ad_rec_t and ad_play_t are READ-ONLY structures for the user.
156  */
157 
158 #if (defined(WIN32) || defined(AD_BACKEND_WIN32)) && !defined(GNUWINCE)
159 
160 #define DEFAULT_DEVICE (char*)DEV_MAPPER
161 
165 typedef struct ad_rec_s {
166  HWAVEIN h_wavein; /* "HANDLE" to the audio input device */
167  ad_wbuf_t *wi_buf; /* Recording buffers provided to system */
168  int32 n_buf; /* #Recording buffers provided to system */
169  int32 opened; /* Flag; A/D opened for recording */
170  int32 recording;
171  int32 curbuf; /* Current buffer with data for application */
172  int32 curoff; /* Start of data for application in curbuf */
173  int32 curlen; /* #samples of data from curoff in curbuf */
174  int32 lastbuf; /* Last buffer containing data after recording stopped */
175  int32 sps; /* Samples/sec */
176  int32 bps; /* Bytes/sample */
177 } ad_rec_t;
178 
179 #elif defined(AD_BACKEND_OSS)
180 
181 #define DEFAULT_DEVICE "/dev/dsp"
182 
187 /* Added by jd5q+@andrew.cmu.edu, 10/3/1997: */
188 typedef struct {
189  int32 dspFD; /* Audio device descriptor */
190  int32 recording;
191  int32 sps; /* Samples/sec */
192  int32 bps; /* Bytes/sample */
193 } ad_rec_t;
194 
195 #elif defined(AD_BACKEND_ESD)
196 
197 #define DEFAULT_DEVICE NULL
198 typedef struct {
199  int32 fd;
200  int32 recording;
201  int32 sps;
202  int32 bps;
203 } ad_rec_t;
204 
205 #elif defined(AD_BACKEND_ALSA)
206 
207 #define DEFAULT_DEVICE "default"
208 typedef struct {
209  snd_pcm_t *dspH;
210  int32 recording;
211  int32 sps;
212  int32 bps;
213 } ad_rec_t;
214 
215 #elif defined(AD_BACKEND_S60)
216 
217 typedef struct ad_rec_s {
218  void* recorder;
219  int32 recording;
220  int32 sps;
221  int32 bps;
222 } ad_rec_t;
223 
224 SPHINXBASE_EXPORT
225 ad_rec_t *ad_open_sps_bufsize (int32 samples_per_sec, int32 bufsize_msec);
226 
227 #else
228 
229 #define DEFAULT_DEVICE NULL
230 typedef struct {
231  int32 sps;
232  int32 bps;
233 } ad_rec_t;
234 
235 
236 #endif
237 
238 
248 SPHINXBASE_EXPORT
250  const char *dev,
251  int32 samples_per_sec
252  );
253 
257 SPHINXBASE_EXPORT
259  int32 samples_per_sec
260  );
261 
262 
266 SPHINXBASE_EXPORT
267 ad_rec_t *ad_open ( void );
268 
269 
270 #if defined(WIN32) && !defined(GNUWINCE)
271 /*
272  * Like ad_open_sps but specifies buffering required within driver. This function is
273  * useful if the default (5000 msec worth) is too small and results in loss of data.
274  */
275 SPHINXBASE_EXPORT
276 ad_rec_t *ad_open_sps_bufsize (int32 samples_per_sec, int32 bufsize_msec);
277 #endif
278 
279 
280 /* Start audio recording. Return value: 0 if successful, <0 otherwise */
281 SPHINXBASE_EXPORT
282 int32 ad_start_rec (ad_rec_t *);
283 
284 
285 /* Stop audio recording. Return value: 0 if successful, <0 otherwise */
286 SPHINXBASE_EXPORT
287 int32 ad_stop_rec (ad_rec_t *);
288 
289 
290 /* Close the recording device. Return value: 0 if successful, <0 otherwise */
291 SPHINXBASE_EXPORT
292 int32 ad_close (ad_rec_t *);
293 
294 
295 /*
296  * Read next block of audio samples while recording; read upto max samples into buf.
297  * Return value: # samples actually read (could be 0 since non-blocking); -1 if not
298  * recording and no more samples remaining to be read from most recent recording.
299  */
300 SPHINXBASE_EXPORT
301 int32 ad_read (ad_rec_t *, int16 *buf, int32 max);
302 
303 
304 /* ------ PLAYBACK; SIMILAR TO RECORDING ------- */
305 
306 #if defined(WIN32) && !defined(GNUWINCE)
307 
308 typedef struct {
309  HWAVEOUT h_waveout; /* "HANDLE" to the audio output device */
310  ad_wbuf_t *wo_buf; /* Playback buffers given to the system */
311  int32 opened; /* Flag; A/D opened for playback */
312  int32 playing;
313  char *busy; /* flags [N_WO_BUF] indicating whether given to system */
314  int32 nxtbuf; /* Next buffer [0..N_WO_BUF-1] to be used for playback data */
315  int32 sps; /* Samples/sec */
316  int32 bps; /* Bytes/sample */
317 } ad_play_t;
318 
319 #else
320 
321 typedef struct {
322  int32 sps; /* Samples/sec */
323  int32 bps; /* Bytes/sample */
324 } ad_play_t; /* Dummy definition for systems without A/D stuff */
325 
326 #endif
327 
328 
329 SPHINXBASE_EXPORT
330 ad_play_t *ad_open_play_sps (int32 samples_per_sec);
331 
332 SPHINXBASE_EXPORT
333 ad_play_t *ad_open_play ( void );
334 
335 SPHINXBASE_EXPORT
336 int32 ad_start_play (ad_play_t *);
337 
338 SPHINXBASE_EXPORT
339 int32 ad_stop_play (ad_play_t *);
340 
341 SPHINXBASE_EXPORT
342 int32 ad_close_play (ad_play_t *);
343 
344 
353 SPHINXBASE_EXPORT
354 int32 ad_write (ad_play_t *, int16 *buf, int32 len);
355 
356 
357 /* ------ MISCELLANEOUS ------- */
358 
362 SPHINXBASE_EXPORT
363 void ad_mu2li (int16 *outbuf, /* Out: PCM data placed here (allocated by user) */
364  unsigned char *inbuf, /* In: Input buffer with mulaw data */
365  int32 n_samp); /* In: #Samples in inbuf */
366 
367 #ifdef __cplusplus
368 }
369 #endif
370 
371 
372 #endif