root / trunk / Linux / addons / ofxASR / libs / sphinx / include / sphinxbase / ad.h @ 59

View | Annotate | Download (12.6 KB)

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 ([email protected]) 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 ([email protected]) at Carnegie Mellon University
85
 *                 Added ad_open_play_sps().
86
 * 
87
 * 07-Mar-98        M K Ravishankar ([email protected]) at Carnegie Mellon University
88
 *                 Added ad_open_sps().
89
 * 
90
 * 10-Jun-96        M K Ravishankar ([email protected]) 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 ([email protected]) at Carnegie Mellon University
96
 *                Created.
97
 */
98
99
/** \file ad.h
100
 * \brief generic live audio interface for recording and playback
101
 */
102
103
#ifndef _AD_H_
104
#define _AD_H_
105
106
#include <sphinx_config.h>
107
108
#if defined (__CYGWIN__)
109
110
#include <w32api/windows.h>
111
#include <w32api/mmsystem.h>
112
113
#elif (defined(WIN32) && !defined(GNUWINCE)) || defined(_WIN32_WCE)
114
115
#include <windows.h>
116
#include <mmsystem.h>
117
118
#elif defined(AD_BACKEND_OSF) /* Not implemented, it seems */
119
120
#include <AF/AFlib.h>
121
122
#elif defined(AD_BACKEND_HPUX) /* Not implemented, it seems */
123
124
#include <audio/Alib.h>
125
126
#elif defined(AD_BACKEND_ALSA)
127
128
#include <alsa/asoundlib.h>
129
130
#elif defined(AD_BACKEND_IRIX)
131
132
#include <dmedia/audio.h>
133
134
#elif defined(AD_BACKEND_PORTAUDIO)
135
136
#include "portaudio.h"
137
#include "pablio.h"
138
139
#endif
140
141
/* Win32/WinCE DLL gunk */
142
#include <sphinxbase_export.h>
143
144
#include <prim_type.h>
145
146
147
#ifdef __cplusplus
148
extern "C" {
149
#endif
150
#if 0
151
/* Fool Emacs. */
152
}
153
#endif
154
155
#define AD_SAMPLE_SIZE                (sizeof(int16))
156
#define DEFAULT_SAMPLES_PER_SEC        16000
157
158
/* Return codes */
159
#define AD_OK                0
160
#define AD_EOF                -1
161
#define AD_ERR_GEN        -1
162
#define AD_ERR_NOT_OPEN        -2
163
#define AD_ERR_WAVE        -3
164
165
166
#if  (defined(WIN32) || defined(AD_BACKEND_WIN32)) && !defined(GNUWINCE)
167
typedef struct {
168
    HGLOBAL h_whdr;
169
    LPWAVEHDR p_whdr;
170
    HGLOBAL h_buf;
171
    LPSTR p_buf;
172
} ad_wbuf_t;
173
#endif
174
175
176
/* ------------ RECORDING -------------- */
177
178
/*
179
 * NOTE: ad_rec_t and ad_play_t are READ-ONLY structures for the user.
180
 */
181
182
#if (defined(WIN32) || defined(AD_BACKEND_WIN32)) && !defined(GNUWINCE)
183
184
#define DEFAULT_DEVICE (char*)DEV_MAPPER
185
186
/**
187
 * Audio recording structure. 
188
 */
189
typedef struct ad_rec_s {
190
    HWAVEIN h_wavein;        /* "HANDLE" to the audio input device */
191
    ad_wbuf_t *wi_buf;        /* Recording buffers provided to system */
192
    int32 n_buf;        /* #Recording buffers provided to system */
193
    int32 opened;        /* Flag; A/D opened for recording */
194
    int32 recording;
195
    int32 curbuf;        /* Current buffer with data for application */
196
    int32 curoff;        /* Start of data for application in curbuf */
197
    int32 curlen;        /* #samples of data from curoff in curbuf */
198
    int32 lastbuf;        /* Last buffer containing data after recording stopped */
199
    int32 sps;                /* Samples/sec */
200
    int32 bps;                /* Bytes/sample */
201
} ad_rec_t;
202
203
#elif defined(AD_BACKEND_OSF)
204
205
#define DEFAULT_DEVICE NULL
206
207
typedef struct {
208
    AFAudioConn *aud;
209
    AC ac;
210
    int32 recording;                /* flag; TRUE iff currently recording */
211
    ATime last_rec_time;        /* timestamp of last sample recorded in buffer */
212
    ATime end_rec_time;                /* time at which recording stopped */
213
    int32 sps;                /* Samples/sec */
214
    int32 bps;                /* Bytes/sample */
215
} ad_rec_t;
216
217
#elif defined(AD_BACKEND_SUNOS)
218
219
#define DEFAULT_DEVICE "/dev/audio"
220
221
typedef struct {
222
    int32 audio_fd;
223
    int32 recording;
224
    int32 sps;                /* Samples/sec */
225
    int32 bps;                /* Bytes/sample */
226
} ad_rec_t;
227
228
#elif defined(AD_BACKEND_OSS) || defined(AD_BACKEND_OSS_BSD)
229
230
#define DEFAULT_DEVICE "/dev/dsp"
231
232
/** \struct ad_rec_t
233
 *  \brief Audio recording structure. 
234
 */
235
236
/* Added by [email protected], 10/3/1997: */
237
typedef struct {
238
    int32 dspFD;        /* Audio device descriptor */
239
    int32 recording;
240
    int32 sps;                /* Samples/sec */
241
    int32 bps;                /* Bytes/sample */
242
} ad_rec_t;
243
244
#elif defined(AD_BACKEND_ESD)
245
246
#define DEFAULT_DEVICE NULL
247
typedef struct {
248
    int32 fd;
249
    int32 recording;
250
    int32 sps;
251
    int32 bps;
252
} ad_rec_t;
253
254
#elif defined(AD_BACKEND_ALSA)
255
256
#define DEFAULT_DEVICE "default"
257
typedef struct {
258
    snd_pcm_t *dspH;
259
    int32 recording;
260
    int32 sps;
261
    int32 bps;
262
} ad_rec_t;
263
264
#elif defined(AD_BACKEND_HPUX)
265
266
#define DEFAULT_DEVICE NULL
267
typedef struct {
268
    Audio *audio;        /* The main audio handle */
269
    ATransID xid;        /* The current transaction ID */
270
    int32 streamSocket;        /* Connection socket */
271
    int32 recording;        /* TRUE iff currently recording */
272
    int32 sps;                /* Samples/sec */
273
    int32 bps;                /* Bytes/sample */
274
} ad_rec_t;
275
276
#elif defined(AD_BACKEND_IRIX)
277
#define DEFAULT_DEVICE NULL
278
typedef struct {
279
    ALport audio;        /* The main audio handle */
280
    int32 recording;        /* TRUE iff currently recording */
281
    int32 sps;                /* Samples/sec */
282
    int32 bps;                /* Bytes/sample */
283
} ad_rec_t;
284
285
#elif defined(AD_BACKEND_PORTAUDIO)
286
#define DEFAULT_DEVICE NULL /* FIXME */
287
typedef struct {
288
  PABLIO_Stream *astream;
289
  int32 sps;
290
  int32 bps;
291
  int32 recording;
292
} ad_rec_t;
293
294
#elif defined(AD_BACKEND_S60)
295
296
typedef struct ad_rec_s {
297
    void* recorder;
298
    int32 recording;
299
    int32 sps;
300
    int32 bps;
301
} ad_rec_t;
302
303
SPHINXBASE_EXPORT
304
ad_rec_t *ad_open_sps_bufsize (int32 samples_per_sec, int32 bufsize_msec);
305
306
#else
307
308
#define DEFAULT_DEVICE NULL
309
typedef struct {
310
    int32 sps;                /**< Samples/sec */
311
    int32 bps;                /**< Bytes/sample */
312
} ad_rec_t;        
313
314
315
#endif
316
317
318
/**
319
 * Open a specific audio device for recording.
320
 *
321
 * The device is opened in non-blocking mode and placed in idle state.
322
 *
323
 * @return pointer to read-only ad_rec_t structure if successful, NULL
324
 * otherwise.  The return value to be used as the first argument to
325
 * other recording functions.
326
 */
327
SPHINXBASE_EXPORT
328
ad_rec_t *ad_open_dev (
329
        const char *dev, /**< Device name (platform-specific) */
330
        int32 samples_per_sec /**< Samples per second */
331
        );
332
333
/**
334
 * Open the default audio device with a given sampling rate.
335
 */
336
SPHINXBASE_EXPORT
337
ad_rec_t *ad_open_sps (
338
                       int32 samples_per_sec /**< Samples per second */
339
                       );
340
341
342
/**
343
 * Open the default audio device.
344
 */
345
SPHINXBASE_EXPORT
346
ad_rec_t *ad_open ( void );
347
348
349
#if defined(WIN32) && !defined(GNUWINCE)
350
/*
351
 * Like ad_open_sps but specifies buffering required within driver.  This function is
352
 * useful if the default (5000 msec worth) is too small and results in loss of data.
353
 */
354
SPHINXBASE_EXPORT
355
ad_rec_t *ad_open_sps_bufsize (int32 samples_per_sec, int32 bufsize_msec);
356
#endif
357
358
359
/* Start audio recording.  Return value: 0 if successful, <0 otherwise */
360
SPHINXBASE_EXPORT
361
int32 ad_start_rec (ad_rec_t *);
362
363
364
/* Stop audio recording.  Return value: 0 if successful, <0 otherwise */
365
SPHINXBASE_EXPORT
366
int32 ad_stop_rec (ad_rec_t *);
367
368
369
/* Close the recording device.  Return value: 0 if successful, <0 otherwise */
370
SPHINXBASE_EXPORT
371
int32 ad_close (ad_rec_t *);
372
373
374
/*
375
 * Read next block of audio samples while recording; read upto max samples into buf.
376
 * Return value: # samples actually read (could be 0 since non-blocking); -1 if not
377
 * recording and no more samples remaining to be read from most recent recording.
378
 */
379
SPHINXBASE_EXPORT
380
int32 ad_read (ad_rec_t *, int16 *buf, int32 max);
381
382
383
/* ------ PLAYBACK; SIMILAR TO RECORDING ------- */
384
385
#if defined(WIN32) && !defined(GNUWINCE)
386
387
typedef struct {
388
    HWAVEOUT h_waveout;        /* "HANDLE" to the audio output device */
389
    ad_wbuf_t *wo_buf;        /* Playback buffers given to the system */
390
    int32 opened;        /* Flag; A/D opened for playback */
391
    int32 playing;
392
    char *busy;                /* flags [N_WO_BUF] indicating whether given to system */
393
    int32 nxtbuf;        /* Next buffer [0..N_WO_BUF-1] to be used for playback data */
394
    int32 sps;                /* Samples/sec */
395
    int32 bps;                /* Bytes/sample */
396
} ad_play_t;
397
398
#else
399
400
typedef struct {
401
    int32 sps;                /* Samples/sec */
402
    int32 bps;                /* Bytes/sample */
403
} ad_play_t;        /* Dummy definition for systems without A/D stuff */
404
405
#endif
406
407
408
SPHINXBASE_EXPORT
409
ad_play_t *ad_open_play_sps (int32 samples_per_sec);
410
411
SPHINXBASE_EXPORT
412
ad_play_t *ad_open_play ( void );
413
414
SPHINXBASE_EXPORT
415
int32 ad_start_play (ad_play_t *);
416
417
SPHINXBASE_EXPORT
418
int32 ad_stop_play (ad_play_t *);
419
420
SPHINXBASE_EXPORT
421
int32 ad_close_play (ad_play_t *);
422
423
424
/**
425
 * Queue a block of audio samples for playback.
426
 *
427
 * Write the next block of [len] samples from rawbuf to the A/D device for playback.
428
 * The device may queue less than len samples, possibly 0, since it is non-blocking.
429
 * The application should resubmit the remaining data to be played.
430
 * Return value: # samples accepted for playback; -1 if error.
431
 */
432
SPHINXBASE_EXPORT
433
int32 ad_write (ad_play_t *, int16 *buf, int32 len);
434
435
436
/* ------ MISCELLANEOUS ------- */
437
438
/**
439
 * Convert mu-law data to int16 linear PCM format.
440
 */
441
SPHINXBASE_EXPORT
442
void ad_mu2li (int16 *outbuf,                /* Out: PCM data placed here (allocated by user) */
443
               unsigned char *inbuf,        /* In: Input buffer with mulaw data */
444
               int32 n_samp);                /* In: #Samples in inbuf */
445
446
#ifdef __cplusplus
447
}
448
#endif
449
450
451
#endif