00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */ 00002 /* ==================================================================== 00003 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights 00004 * reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 00013 * 2. Redistributions in binary form must reproduce the above copyright 00014 * notice, this list of conditions and the following disclaimer in 00015 * the documentation and/or other materials provided with the 00016 * distribution. 00017 * 00018 * This work was supported in part by funding from the Defense Advanced 00019 * Research Projects Agency and the National Science Foundation of the 00020 * United States of America, and the CMU Sphinx Speech Consortium. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 00023 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 00024 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00025 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY 00026 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00027 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00028 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00029 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00030 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00031 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00032 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 * 00034 * ==================================================================== 00035 * 00036 */ 00037 /* 00038 * ad.h -- generic live audio interface for recording and playback 00039 * 00040 * ********************************************** 00041 * CMU ARPA Speech Project 00042 * 00043 * Copyright (c) 1996 Carnegie Mellon University. 00044 * ALL RIGHTS RESERVED. 00045 * ********************************************** 00046 * 00047 * HISTORY 00048 * 00049 * $Log: ad.h,v $ 00050 * Revision 1.8 2005/06/22 08:00:06 arthchan2003 00051 * Completed all doxygen documentation on file description for libs3decoder/libutil/libs3audio and programs. 00052 * 00053 * Revision 1.7 2004/12/14 00:39:49 arthchan2003 00054 * add <s3types.h> to the code, change some comments to doxygen style 00055 * 00056 * Revision 1.6 2004/12/06 11:17:55 arthchan2003 00057 * 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 00058 * 00059 * Revision 1.5 2004/07/23 23:44:46 egouvea 00060 * Changed the cygwin code to use the same audio files as the MS Visual code, removed unused variables from fe_interface.c 00061 * 00062 * Revision 1.4 2004/02/29 23:48:31 egouvea 00063 * Updated configure.in to the recent automake/autoconf, fixed win32 00064 * references in audio files. 00065 * 00066 * Revision 1.3 2002/11/10 19:27:38 egouvea 00067 * Fixed references to sun's implementation of audio interface, 00068 * referring to the correct .h file, and replacing sun4 with sunos. 00069 * 00070 * Revision 1.2 2001/12/11 04:40:55 lenzo 00071 * License cleanup. 00072 * 00073 * Revision 1.1.1.1 2001/12/03 16:01:45 egouvea 00074 * Initial import of sphinx3 00075 * 00076 * Revision 1.1.1.1 2001/01/17 05:17:14 ricky 00077 * Initial Import of the s3.3 decoder, has working decodeaudiofile, s3.3_live 00078 * 00079 * 00080 * 19-Jan-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University 00081 * Added AD_ return codes. Added ad_open_sps_bufsize(), and 00082 * ad_rec_t.n_buf. 00083 * 00084 * 17-Apr-98 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University 00085 * Added ad_open_play_sps(). 00086 * 00087 * 07-Mar-98 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University 00088 * Added ad_open_sps(). 00089 * 00090 * 10-Jun-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University 00091 * Added ad_wbuf_t, ad_rec_t, and ad_play_t types, and augmented all 00092 * recording functions with ad_rec_t, and playback functions with 00093 * ad_play_t. 00094 * 00095 * 06-Jun-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University 00096 * Created. 00097 */ 00098 00103 #ifndef _AD_H_ 00104 #define _AD_H_ 00105 00106 #include <sphinx_config.h> 00107 00108 #if defined (__CYGWIN__) 00109 00110 #include <w32api/windows.h> 00111 #include <w32api/mmsystem.h> 00112 00113 #elif (defined(WIN32) && !defined(GNUWINCE)) || defined(_WIN32_WCE) 00114 00115 #include <windows.h> 00116 #include <mmsystem.h> 00117 00118 #elif defined(AD_BACKEND_OSF) /* Not implemented, it seems */ 00119 00120 #include <AF/AFlib.h> 00121 00122 #elif defined(AD_BACKEND_HPUX) /* Not implemented, it seems */ 00123 00124 #include <audio/Alib.h> 00125 00126 #elif defined(AD_BACKEND_ALSA) 00127 00128 #include <alsa/asoundlib.h> 00129 00130 #elif defined(AD_BACKEND_IRIX) 00131 00132 #include <dmedia/audio.h> 00133 00134 #elif defined(AD_BACKEND_PORTAUDIO) 00135 00136 #include "portaudio.h" 00137 #include "pablio.h" 00138 00139 #endif 00140 00141 /* Win32/WinCE DLL gunk */ 00142 #include <sphinxbase_export.h> 00143 00144 #include <prim_type.h> 00145 00146 00147 #ifdef __cplusplus 00148 extern "C" { 00149 #endif 00150 #if 0 00151 /* Fool Emacs. */ 00152 } 00153 #endif 00154 00155 #define AD_SAMPLE_SIZE (sizeof(int16)) 00156 #define DEFAULT_SAMPLES_PER_SEC 16000 00157 00158 /* Return codes */ 00159 #define AD_OK 0 00160 #define AD_EOF -1 00161 #define AD_ERR_GEN -1 00162 #define AD_ERR_NOT_OPEN -2 00163 #define AD_ERR_WAVE -3 00164 00165 00166 #if (defined(WIN32) || defined(AD_BACKEND_WIN32)) && !defined(GNUWINCE) 00167 typedef struct { 00168 HGLOBAL h_whdr; 00169 LPWAVEHDR p_whdr; 00170 HGLOBAL h_buf; 00171 LPSTR p_buf; 00172 } ad_wbuf_t; 00173 #endif 00174 00175 00176 /* ------------ RECORDING -------------- */ 00177 00178 /* 00179 * NOTE: ad_rec_t and ad_play_t are READ-ONLY structures for the user. 00180 */ 00181 00182 #if (defined(WIN32) || defined(AD_BACKEND_WIN32)) && !defined(GNUWINCE) 00183 00184 #define DEFAULT_DEVICE (char*)DEV_MAPPER 00185 00189 typedef struct ad_rec_s { 00190 HWAVEIN h_wavein; /* "HANDLE" to the audio input device */ 00191 ad_wbuf_t *wi_buf; /* Recording buffers provided to system */ 00192 int32 n_buf; /* #Recording buffers provided to system */ 00193 int32 opened; /* Flag; A/D opened for recording */ 00194 int32 recording; 00195 int32 curbuf; /* Current buffer with data for application */ 00196 int32 curoff; /* Start of data for application in curbuf */ 00197 int32 curlen; /* #samples of data from curoff in curbuf */ 00198 int32 lastbuf; /* Last buffer containing data after recording stopped */ 00199 int32 sps; /* Samples/sec */ 00200 int32 bps; /* Bytes/sample */ 00201 } ad_rec_t; 00202 00203 #elif defined(AD_BACKEND_OSF) 00204 00205 #define DEFAULT_DEVICE NULL 00206 00207 typedef struct { 00208 AFAudioConn *aud; 00209 AC ac; 00210 int32 recording; /* flag; TRUE iff currently recording */ 00211 ATime last_rec_time; /* timestamp of last sample recorded in buffer */ 00212 ATime end_rec_time; /* time at which recording stopped */ 00213 int32 sps; /* Samples/sec */ 00214 int32 bps; /* Bytes/sample */ 00215 } ad_rec_t; 00216 00217 #elif defined(AD_BACKEND_SUNOS) 00218 00219 #define DEFAULT_DEVICE "/dev/audio" 00220 00221 typedef struct { 00222 int32 audio_fd; 00223 int32 recording; 00224 int32 sps; /* Samples/sec */ 00225 int32 bps; /* Bytes/sample */ 00226 } ad_rec_t; 00227 00228 #elif defined(AD_BACKEND_OSS) || defined(AD_BACKEND_OSS_BSD) 00229 00230 #define DEFAULT_DEVICE "/dev/dsp" 00231 00236 /* Added by jd5q+@andrew.cmu.edu, 10/3/1997: */ 00237 typedef struct { 00238 int32 dspFD; /* Audio device descriptor */ 00239 int32 recording; 00240 int32 sps; /* Samples/sec */ 00241 int32 bps; /* Bytes/sample */ 00242 } ad_rec_t; 00243 00244 #elif defined(AD_BACKEND_ESD) 00245 00246 #define DEFAULT_DEVICE NULL 00247 typedef struct { 00248 int32 fd; 00249 int32 recording; 00250 int32 sps; 00251 int32 bps; 00252 } ad_rec_t; 00253 00254 #elif defined(AD_BACKEND_ALSA) 00255 00256 #define DEFAULT_DEVICE "default" 00257 typedef struct { 00258 snd_pcm_t *dspH; 00259 int32 recording; 00260 int32 sps; 00261 int32 bps; 00262 } ad_rec_t; 00263 00264 #elif defined(AD_BACKEND_HPUX) 00265 00266 #define DEFAULT_DEVICE NULL 00267 typedef struct { 00268 Audio *audio; /* The main audio handle */ 00269 ATransID xid; /* The current transaction ID */ 00270 int32 streamSocket; /* Connection socket */ 00271 int32 recording; /* TRUE iff currently recording */ 00272 int32 sps; /* Samples/sec */ 00273 int32 bps; /* Bytes/sample */ 00274 } ad_rec_t; 00275 00276 #elif defined(AD_BACKEND_IRIX) 00277 #define DEFAULT_DEVICE NULL 00278 typedef struct { 00279 ALport audio; /* The main audio handle */ 00280 int32 recording; /* TRUE iff currently recording */ 00281 int32 sps; /* Samples/sec */ 00282 int32 bps; /* Bytes/sample */ 00283 } ad_rec_t; 00284 00285 #elif defined(AD_BACKEND_PORTAUDIO) 00286 #define DEFAULT_DEVICE NULL /* FIXME */ 00287 typedef struct { 00288 PABLIO_Stream *astream; 00289 int32 sps; 00290 int32 bps; 00291 int32 recording; 00292 } ad_rec_t; 00293 00294 #else 00295 00296 #define DEFAULT_DEVICE NULL 00297 typedef struct { 00298 int32 sps; 00299 int32 bps; 00300 } ad_rec_t; 00301 00302 00303 #endif 00304 00305 00315 SPHINXBASE_EXPORT 00316 ad_rec_t *ad_open_dev ( 00317 const char *dev, 00318 int32 samples_per_sec 00319 ); 00320 00324 SPHINXBASE_EXPORT 00325 ad_rec_t *ad_open_sps ( 00326 int32 samples_per_sec 00327 ); 00328 00329 00333 SPHINXBASE_EXPORT 00334 ad_rec_t *ad_open ( void ); 00335 00336 00337 #if defined(WIN32) && !defined(GNUWINCE) 00338 /* 00339 * Like ad_open_sps but specifies buffering required within driver. This function is 00340 * useful if the default (5000 msec worth) is too small and results in loss of data. 00341 */ 00342 SPHINXBASE_EXPORT 00343 ad_rec_t *ad_open_sps_bufsize (int32 samples_per_sec, int32 bufsize_msec); 00344 #endif 00345 00346 00347 /* Start audio recording. Return value: 0 if successful, <0 otherwise */ 00348 SPHINXBASE_EXPORT 00349 int32 ad_start_rec (ad_rec_t *); 00350 00351 00352 /* Stop audio recording. Return value: 0 if successful, <0 otherwise */ 00353 SPHINXBASE_EXPORT 00354 int32 ad_stop_rec (ad_rec_t *); 00355 00356 00357 /* Close the recording device. Return value: 0 if successful, <0 otherwise */ 00358 SPHINXBASE_EXPORT 00359 int32 ad_close (ad_rec_t *); 00360 00361 00362 /* 00363 * Read next block of audio samples while recording; read upto max samples into buf. 00364 * Return value: # samples actually read (could be 0 since non-blocking); -1 if not 00365 * recording and no more samples remaining to be read from most recent recording. 00366 */ 00367 SPHINXBASE_EXPORT 00368 int32 ad_read (ad_rec_t *, int16 *buf, int32 max); 00369 00370 00371 /* ------ PLAYBACK; SIMILAR TO RECORDING ------- */ 00372 00373 #if defined(WIN32) && !defined(GNUWINCE) 00374 00375 typedef struct { 00376 HWAVEOUT h_waveout; /* "HANDLE" to the audio output device */ 00377 ad_wbuf_t *wo_buf; /* Playback buffers given to the system */ 00378 int32 opened; /* Flag; A/D opened for playback */ 00379 int32 playing; 00380 char *busy; /* flags [N_WO_BUF] indicating whether given to system */ 00381 int32 nxtbuf; /* Next buffer [0..N_WO_BUF-1] to be used for playback data */ 00382 int32 sps; /* Samples/sec */ 00383 int32 bps; /* Bytes/sample */ 00384 } ad_play_t; 00385 00386 #else 00387 00388 typedef struct { 00389 int32 sps; /* Samples/sec */ 00390 int32 bps; /* Bytes/sample */ 00391 } ad_play_t; /* Dummy definition for systems without A/D stuff */ 00392 00393 #endif 00394 00395 00396 SPHINXBASE_EXPORT 00397 ad_play_t *ad_open_play_sps (int32 samples_per_sec); 00398 00399 SPHINXBASE_EXPORT 00400 ad_play_t *ad_open_play ( void ); 00401 00402 SPHINXBASE_EXPORT 00403 int32 ad_start_play (ad_play_t *); 00404 00405 SPHINXBASE_EXPORT 00406 int32 ad_stop_play (ad_play_t *); 00407 00408 SPHINXBASE_EXPORT 00409 int32 ad_close_play (ad_play_t *); 00410 00411 00420 SPHINXBASE_EXPORT 00421 int32 ad_write (ad_play_t *, int16 *buf, int32 len); 00422 00423 00424 /* ------ MISCELLANEOUS ------- */ 00425 00429 SPHINXBASE_EXPORT 00430 void ad_mu2li (int16 *outbuf, /* Out: PCM data placed here (allocated by user) */ 00431 unsigned char *inbuf, /* In: Input buffer with mulaw data */ 00432 int32 n_samp); /* In: #Samples in inbuf */ 00433 00434 #ifdef __cplusplus 00435 } 00436 #endif 00437 00438 00439 #endif