audio_sun4.c
来自「speech signal process tools」· C语言 代码 · 共 682 行 · 第 1/2 页
C
682 行
/* * This material contains unpublished, proprietary software of * Entropic Research Laboratory, Inc. Any reproduction, distribution, * or publication of this work must be authorized in writing by Entropic * Research Laboratory, Inc., and must bear the notice: * * "Copyright (c) 1995-1996 Entropic Research Laboratory, Inc. * All rights reserved" * * The copyright notice above does not evidence any actual or intended * publication of this source code. * * Written by: * Checked by: * Revised by: * * Brief description: * */static char *sccs_id = "@(#)audio_sun4.c 1.5 12/18/96 ERL";#include <stdio.h>#include <math.h>#include <fcntl.h>#include <stropts.h>#include <sys/ioctl.h>#include <errno.h>#ifdef OS5#include <sys/audioio.h>#else#include <sun/audioio.h>#endif/* * for select */#include <sys/time.h>#include <sys/types.h>/* * --------------------------------------------------------------- * Machine-dependent declaration * --------------------------------------------------------------- */static int recordPort = -1;/* audio port */static char *audio_dev = "/dev/audio";static char *audio_ctl = "/dev/audioctl";FILE *playPortFp = NULL;static int playPort = -1; /* audio port */static int playChan = 1;static int amdDeviceMu = 0;static char *muPlayData, *muRecordData;static short *resampBuffer;static struct timeval playBgnTime;static long playSamps = 0;static double fileRate, resampRatio;static int sampleRates[] = {48000, 44100, 37800, 32000, 22050, 18900,16000, 11025, 9600, 8000, 0};/* sampling rates available */static int amdSampleRate[] = {8000, 0};static int sizeof_sample;static int PortBufSize;static int playPortBufSize;static double playRateSave;static int playChannelSave;static int playNSampsPerChanSave;static int ctl_port = 0;#ifndef MAX_AUDIO_DEV_LEN/* * Paramter for the AUDIO_GETDEV ioctl to determine current audio device */#define MAX_AUDIO_DEV_LEN (16)typedef struct audio_device { char name[MAX_AUDIO_DEV_LEN]; char version[MAX_AUDIO_DEV_LEN]; char config[MAX_AUDIO_DEV_LEN];} audio_device_t;#endif#ifndef AUDIO_GETDEV#define AUDIO_GETDEV _IOR(A, 4, audio_device_t)#endifstatic audio_info_t recordPortInfo;static audio_info_t playPortInfo;char audio_error_msg[100];#define perror private_perrorextern int sys_nerr;extern char *sys_errlist[];static voidprivate_perror(s) char *s;{ if (errno < sys_nerr) sprintf(audio_error_msg, "%s: %s", s, sys_errlist[errno]); else sprintf(audio_error_msg, "%s: Unknown ioctl error", s);}static intGetFillable(fd) int fd;{ int canread; if (ioctl(fd, I_NREAD, &canread) < 0) { perror("error in GetFillable"); return (0); } return (PortBufSize - canread) / sizeof_sample;}static intGetFilled(fd) int fd;{ int canread; if (ioctl(fd, I_NREAD, &canread) < 0) { perror("error in GetFilled"); return (0); } return canread / sizeof_sample;}static intPortOverflow(fd) int fd;{ audio_info_t info; AUDIO_INITINFO(&info); if (ioctl(fd, AUDIO_GETINFO, &info) < 0) { perror("error in PortOverflow"); return (-1); } return (info.record.error);}intPortIsAvailable(){ int port, dev, c;#ifdef OS5 audio_device_t adev;#endif if (!ctl_port) { if ((ctl_port = open(audio_ctl, O_RDWR)) < 0) { sprintf(audio_error_msg, "Sparc audio device is not available; cannot open audio control device"); return 1; } amdDeviceMu = 0;#ifdef OS5 if (ioctl(ctl_port, AUDIO_GETDEV, &adev) < 0) { perror("PortIsAvailable ioctl:"); return 1; } if (!strcmp(adev.name, "SUNW,am79c30")) { amdDeviceMu = 1; }#else if (ioctl(ctl_port, AUDIO_GETDEV, &dev) < 0) { perror("PortIsAvailable ioctl:"); return 1; } if (AUDIO_DEV_AMD == dev) { amdDeviceMu = 1; }#endif } return 0;}/* * ----------------------------------------------------------------- * Record * ----------------------------------------------------------------- */int *AudioGetRates(){ int *s; s = (amdDeviceMu) ? amdSampleRate : sampleRates; return (s);}intAudioMaxChannels(){ return (amdDeviceMu ? 1 : 2);}intInitAudioRecord(srate, channel, nSampsPerChan) double srate; int channel; int nSampsPerChan;{ long buflen; extern int speaker_box; if (PortIsAvailable()) { sprintf(audio_error_msg,"Audio device not available on this hardware"); return (1); } sizeof_sample = (amdDeviceMu) ? sizeof(char) : sizeof(short); /* Set the sample rate. */ AUDIO_INITINFO(&recordPortInfo); if (amdDeviceMu && srate != 8000) { sprintf(audio_error_msg, "This audio device only supports 8000 Hz sample rate"); return (1); } recordPortInfo.record.sample_rate = (amdDeviceMu) ? 8000 : closest_srate(srate, sampleRates); if (amdDeviceMu && channel != 1) { sprintf(audio_error_msg, "This audio device device only supports 1 channel, %d requested", channel); return (1); } recordPortInfo.record.channels = (channel < 2) ? 1 : 2; recordPortInfo.record.precision = (amdDeviceMu) ? 8 : 16; recordPortInfo.record.encoding = (amdDeviceMu) ? AUDIO_ENCODING_ULAW : AUDIO_ENCODING_LINEAR; recordPortInfo.record.error = 0; PortBufSize = recordPortInfo._yyy[1] = nSampsPerChan * sizeof_sample * channel; if (amdDeviceMu) { muRecordData = (char *) malloc(nSampsPerChan * channel * sizeof(char)); if (!muRecordData) { sprintf(audio_error_msg, "InitAudioRecord: malloc failed."); return (1); } } return (0);}intCloseAudioRecord(){ if (recordPort >= 0) { /* save the state */ if (ioctl(recordPort, AUDIO_GETINFO, &recordPortInfo) < 0) { perror("error getting audio port mode in PauseAudioRecord"); (void)close(recordPort); return (1); } (void)ioctl(recordPort, I_FLUSH, FLUSHR); (void)close(recordPort); } recordPort = -1; return (0);}intPauseAudioRecord(){ return CloseAudioRecord();}intStartAudioRecord(){ /* open the audio device */ if ((recordPort = open(audio_dev, O_RDONLY | O_NDELAY)) < 0) { perror("Cannot open audio device for input"); return (1); } if (ioctl(recordPort, AUDIO_SETINFO, &recordPortInfo) < 0) { perror("error setting audio port mode in StartAudioRecord"); close(recordPort); recordPort = -1; return (1); } /* * This ioctl is needed. Otherwise, for some unknown reason, the * recording starting from now may or may not be garbage */ (void)ioctl(recordPort, I_FLUSH, FLUSHR); return (0);}intContAudioRecord(){ return StartAudioRecord();}/* * in case of STEREO recording, RecordAudio returns total number of samples * in both channels */intRecordAudio(inputBuffer) short *inputBuffer;{ int canread; /* Be sure that A/D hasn't lost real time. */ if (GetFillable(recordPort) < 10 || PortOverflow(recordPort)) { sprintf(audio_error_msg,"Lost realtime operation in RecordAudio"); return (-1); } canread = GetFilled(recordPort); if (amdDeviceMu) { (void) read(recordPort, muRecordData, canread * sizeof_sample); mu_to_linear(muRecordData, inputBuffer, canread); } else { (void) read(recordPort, inputBuffer, canread * sizeof_sample); } return (canread);}intSetAudioInputType(src) char *src;{ if (!strcmp(src, "mic")) recordPortInfo.record.port = AUDIO_MICROPHONE; else recordPortInfo.record.port = AUDIO_LINE_IN; if (recordPort > 0) if (ioctl(recordPort, AUDIO_SETINFO, &recordPortInfo) < 0) { perror("error setting audio port mode in SetAudioInputType"); return 1; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?