📄 haudio.c
字号:
/* ----------------------------------------------------------- *//* *//* ___ *//* |_| | |_/ SPEECH *//* | | | | \ RECOGNITION *//* ========= SOFTWARE */ /* *//* *//* ----------------------------------------------------------- *//* Copyright: Microsoft Corporation *//* 1995-2000 Redmond, Washington USA *//* http://www.microsoft.com *//* *//* Use of this software is governed by a License Agreement *//* ** See the file License for the Conditions of Use ** *//* ** This banner notice must not be removed ** *//* *//* ----------------------------------------------------------- *//* File: HAudio.c: Audio Input/Output Interface *//* ----------------------------------------------------------- */char *haudio_version = "!HVER!HAudio: 3.3 [CUED 28/04/05]";char *haudio_vc_id = "$Id: HAudio.c,v 1.2 2005/05/12 15:51:23 jal58 Exp $";#include "HShell.h" /* HTK Libraries */#include "HMem.h"#include "HMath.h"#include "HWave.h"#include "HAudio.h"/* ----------------------------- Trace Flags ------------------------- */static int trace = 0;#define T_TOP 0001 /* Top Level tracing */#define T_STC 0002 /* Trace Audio State Changes */#define T_DET 0004 /* Trace Detector State Changes */#define T_AUD 0010 /* Trace device dependent audio code */#define T_RUN 0020 /* Trace audio read/status *//* -------------------- Configuration Parameters --------------------- */static ConfParam *cParm[MAXGLOBS]; /* config parameters */static int numParm = 0;/* ---------------------------------------------------------- *//* The type of audio device must be defined - choices are NO_AUDIO - none SGI_AUDIO - SGI Indigo SUN16_AUDIO - Sun 16-bit linear*/#ifdef USS_AUDIO#define OSS_AUDIO#endif#if !defined NO_AUDIO && !defined SGI_AUDIO && !defined SUN16_AUDIO && !defined SOLARIS16_AUDIO && !defined HPUX_AUDIO && !defined RS6000_AUDIO && !defined WIN95_AUDIO && !defined OSS_AUDIO && !defined HPRAW_AUDIO && !defined DEC_AUDIO && !defined WIN32_AUDIO && !defined EXT_FD_AUDIO #define NO_AUDIO#endif#ifdef WIN32_AUDIO#define MMAPI_AUDIO#include <windows.h>#include <mmsystem.h>#include <stdlib.h> #include <search.h> #endif#ifdef DEC_AUDIO#define MMAPI_AUDIO#include <mme/mme_api.h>#endif#ifdef RS6000_AUDIO#include <string.h>#include <fcntl.h>#include <UMSBAUDDevice.h>/* function return codes */#define rOK UMSAudioDevice_Success #define rUR UMSAudioDevice_UnderRun#define rOR UMSAudioDevice_OverRun#ifdef AIX_3_2_5#define AUDIO_DEV "/dev/baud0"#else#define AUDIO_DEV "/dev/paud0"#endif#endif#ifdef SGI_AUDIO#include <audio.h>#include <unistd.h>#endif#ifdef SUN16_AUDIO#include <fcntl.h>#include <stropts.h>#include <sys/filio.h>#include <sun/audioio.h> /* May be in sys in local configuration */#include <sys/ioctl.h>#define AUDIO_IODEV "/dev/audioctl"#define AUDIO_IO "/dev/audio"#endif#ifdef SOLARIS16_AUDIO#include <fcntl.h>#include <stropts.h>#include <sys/filio.h>#include <sys/audioio.h> /* May be in sys in local configuration */#include <sys/ioctl.h>#define AUDIO_IODEV "/dev/audioctl"#define AUDIO_IO "/dev/audio"#define SUN16_AUDIO#endif#ifdef HPUX_AUDIO#include <fcntl.h>#include <sys/socket.h>#include <time.h>#include <sys/audio.h>/* to include audio library compile with: -I/usr/include/audio for HPUX-9.x -I/opt/audio/include for HPUX-10.x */#include <Alib.h>static Audio *audio_dev=NULL;static int audio_cnt=0;#endif#ifdef OSS_AUDIO#include <unistd.h>#include <fcntl.h>#include <sys/ioctl.h>#include <sys/soundcard.h>#define AUDIO_DEV "/dev/dsp"#define MIXER_DEV "/dev/mixer"#define AUDIO_RD 0x01#define AUDIO_WR 0x02#define BUF_SIZE 4096static short zero_buf[BUF_SIZE];static int audio_fd;static int mixer_fd;static unsigned short audio_io = 0x00;static int frag_size;static audio_buf_info audio_info;#endif/* Define the Audio Stream Information Records */#define AUDBUFSIZE 640000 /* size of audio buffer */typedef enum { v_peak=1, v_rms} VolType;static VolType volType = v_peak;static Boolean lineOut = TRUE;static Boolean phonesOut = TRUE;static Boolean speakerOut = FALSE;static Boolean lineIn = TRUE;static Boolean micIn = FALSE;static volatile Boolean stopSignalled;typedef enum { ADS_INIT, ADS_OPEN, ADS_SAMPLING, ADS_STOPPED, ADS_CLOSED } AudioDevStatus;typedef struct { /* circular buffer */ Boolean isActive; /* true if in use */ short *data; /* actual data buffer */ int inx,outx; /* in/out indices - wrap modulo size */ int used,size; /* used in data, size of data */}ReplayBuf;#ifdef MMAPI_AUDIO#define MMAPI_BUFFER_DURATION 0.2#define MMAPI_BUFFER_COUNT 12static DWORD sMagic=-1;typedef struct mmapibuf{ int index; /* Index of buffer */ int size; /* Size of buffer */ int n; /* Number of valid samples in buffer */ int cur; /* Current sample index */ LPWAVEHDR waveHdr; /* Pointer to WAVEHDR */ LPSTR waveData; /* Data in buffer */ struct mmapibuf *next; struct mmapibuf *prev;} mmApiBuf;#endiftypedef struct _AudioIn { /* -- Machine Independent Part -- */ MemHeap *mem; /* memory heap for this audio rec */ HTime sampPeriod; /* sampling period in 100ns units */ int frSize; /* num samples per speech frame */ int frRate; /* num samples between speech frames */ short * frBuf; /* buffer for constructing frames */ Vector frOLap; /* frame overlap buffer used by GetAudio */ int inOLap; /* num samples in frOLap */ AudioInStatus status; /* current status of this audio stream */ short buffer[AUDBUFSIZE]; int bufferSize; /* Size of audio buffer */ int nInBuffer; /* Number of valid samples in buffer */ int inBufPos; /* Position to write in to buffer */ int outBufPos; /* Position to read out of buffer */ int sig; /* signal if any */ ReplayBuf rbuf; /* replay buffer (if needed) */ AudioDevStatus isActive; /* indicates when device active */ float curVol; /* Current volume of input speech */ /* -- Machine Dependent Part -- */#ifdef MMAPI_AUDIO MMRESULT mmError; DWORD magic; /* Magic number identifying this instance */ HWAVEIN waveIn; LPPCMWAVEFORMAT waveFmt; /* Pointer to PCMWAVEFORMAT */ LPMMTIME wavePos; /* Pointer to MMTIME */ int total; /* Total number of samples queued */ int current; /* Index of current buffer */ int bufSize; /* Block size for each buffer */ mmApiBuf *qHead; /* Head of buffer waiting to be filled list */ mmApiBuf *qTail; /* Tail of buffer waiting to be filled list */ mmApiBuf *fHead; /* Head of filled buffer list */ mmApiBuf *fTail; /* Tail of filled buffer list */#ifdef WIN32_AUDIO CRITICAL_SECTION c; HANDLE callBackEvent;#endif#endif#ifdef RS6000_AUDIO UMSBAUDDevice adevin; UMSAudioDevice_ReturnCode rc; Environment *evin; long sw; long osamples; /* Sample rate */ char *obyte_order; long lgain; long rgain; long channels ; long bits ; char inConn[30]; #endif#ifdef SGI_AUDIO long params[2]; /* parameter array */ ALport recPort; /* SGI audio port */ ALconfig config; /* configuration record */#endif#ifdef SUN16_AUDIO int numSamples; int audio_ctld; audio_info_t audio_info;#endif#ifdef HPUX_AUDIO Audio *audio; ATransID tid; AudioAttributes attr; SSRecordParams parms; AGainEntry gains[4]; SStream stream; int socket;#endif}AudioInRec;typedef struct _AudioOut { /* -- Machine Independent Part -- */ MemHeap *mem; /* memory heap for this audio rec */ float vol; /* current volume */ Boolean isActive; /* true when device active */ /* -- Machine Dependent Part -- */#ifdef MMAPI_AUDIO MMRESULT mmError; DWORD magic; /* Magic number identifying this instance */ HWAVEOUT waveOut; UINT waveOutDev; /* Device being used for wave output */ LPPCMWAVEFORMAT waveFmt; /* Pointer to PCMWAVEFORMAT */ LPMMTIME wavePos; /* Pointer to MMTIME */ int total; /* Total number of samples queued */ int current; /* Index of current buffer */ mmApiBuf *pHead; /* Head of buffer list */ mmApiBuf *pTail; /* Tail of buffer list */#ifdef WIN32_AUDIO CRITICAL_SECTION c; HANDLE callBackEvent;#endif#endif#ifdef RS6000_AUDIO UMSBAUDDevice adevout; UMSAudioDevice_ReturnCode rc; Environment *evout; long sw; long osamples; /* Sample rate */ char *obyte_order; long lgain; long rgain; long channels ; long bits ; char outConn[30]; #endif#ifdef SGI_AUDIO long params[6]; /* parameter array */ ALport playPort; /* SGI audio port */ ALconfig config; /* configuration record */#endif#ifdef SUN16_AUDIO int numSamples; int numWrites; int audio_ctld; audio_info_t audio_info;#endif#ifdef HPUX_AUDIO Audio *audio; ATransID tid; int nToPlay; AudioAttributes attr; SSPlayParams parms; AGainEntry gains[4]; SStream stream; int socket;#endif}AudioOutRec;/* ------------------ Device Dependent Routines ----------------- *//* All device dependent parts of this module are in this section *//* -------------------------------------------------------------- */#ifdef SUN16_AUDIO#define NUM_SAMP_FREQS 10static float sampFreqs[NUM_SAMP_FREQS] = { 8000, 9600, 11025, 16000, 18900, 22050, 32000, 37800, 44100, 48000};/* TrimSampFreq: find the nearest available sampling frequency */static int TrimSampFreq(int f){ int i, d; int min, mi; min = (int) fabs((double)(sampFreqs[0]-f)); mi = 0; for (i=0; i<NUM_SAMP_FREQS; i++) { d = (int) fabs((double)(sampFreqs[i]-f)); if (d < min) { min = d; mi = i; } } return (int) sampFreqs[mi];}#endif#ifdef OSS_AUDIO/* IsVAXOrder: returns true if machine has VAX ordered bytes */static Boolean IsVAXOrder(void){ short x, *px; unsigned char *pc; px = &x; pc = (unsigned char *) px; *pc = 1; *(pc+1) = 0; /* store bytes 1 0 */ return x==1; /* does it read back as 1? */}#endif/* CalcVolume: calculate volume of data */static float CalcVolume(short *data, int len){ float vol; double sum, sqr; int i, minSamp, maxSamp; switch (volType) { case v_rms: sum=sqr=0.0; for(i = 0; i < len; i++) { sum += (double)data[i]; sqr += ((double)data[i]) * ((double)data[i]); } sum/=len; sqr/=len; vol = sqrt(sqr-sum*sum); break; case v_peak: default: minSamp =maxSamp = data[0]; for(i = 0; i < len; i++) { if ( data[i] > maxSamp ) maxSamp = data[i]; else if ( data[i] < minSamp ) minSamp = data[i]; } vol = (maxSamp-minSamp)/2.0; break; } return vol;}#ifdef MMAPI_AUDIO#ifdef WIN32_AUDIOvoid *mmeAllocMem(size_t size){ void *ptr; ptr=GlobalAlloc(GMEM_FIXED,size); if (ptr==NULL) HError(6006,"StartAudi: Cannot allocate memory for mme structure"); return(ptr);}void *mmeAllocBuffer(size_t size){ void *ptr; ptr=GlobalAlloc(GMEM_FIXED,size); if (ptr==NULL) HError(6006,"StartAudi: Cannot allocate memory for mme structure"); return(ptr);}Boolean mmeFreeMem(void *ptr){ ptr=GlobalFree(ptr); return(TRUE);}Boolean mmeFreeBuffer(void *ptr){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -