audio_sun4.c
来自「speech signal process tools」· C语言 代码 · 共 682 行 · 第 1/2 页
C
682 行
return 0;}intSetAudioInputGain(gainL, gainR) int gainL, gainR;{ /* for SUN, the left & right gains are the same */ if (gainL > 100) gainL = 100; if (gainR > 100) gainR = 100; /* 0 - 255, 0 min gain */ if (gainL < 0) gainL = 0; if (gainR < 0) gainR = 0; gainL = gainL * 2.55; gainR = gainR * 2.55; recordPortInfo.record.gain = (gainL > gainR) ? gainL : gainR; if (recordPort > 0) if (ioctl(recordPort, AUDIO_SETINFO, &recordPortInfo) < 0) { perror("error setting audio port mode in SetAudioInputGain"); return 1; } return 0;}/* * ----------------------------------------------------------------- * Play * ----------------------------------------------------------------- */static doubleTimeDiff(a, b) struct timeval *a, *b;{ double at, bt; bt = b->tv_sec + b->tv_usec * 1E-6; at = a->tv_sec + a->tv_usec * 1E-6; return (bt - at);}intInitAudioPlay(srate, channel, nSampsPerChan) double srate; int channel; int nSampsPerChan;{ long buflen; playRateSave = srate; playChannelSave = channel; playNSampsPerChanSave = nSampsPerChan; 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(&playPortInfo); playPortInfo.play.sample_rate = (amdDeviceMu) ? 8000 : closest_srate(srate, sampleRates); fileRate = srate; if (amdDeviceMu) resampRatio = srate / 8000.0; playPortInfo.play.precision = (amdDeviceMu) ? 8 : 16; if (amdDeviceMu && channel != 1) { sprintf(audio_error_msg, "This audio device device only supports 1 channel, %d requested", channel); return (1); } playPortInfo.play.channels = playChan = (channel < 2) ? 1 : 2; playPortInfo.play.encoding = (amdDeviceMu) ? AUDIO_ENCODING_ULAW : AUDIO_ENCODING_LINEAR; playPortInfo.play.error = 0; playPortInfo._yyy[0] = 0; playPortBufSize = nSampsPerChan * sizeof_sample * channel; if (amdDeviceMu) { muPlayData = (char *) malloc(nSampsPerChan * channel * sizeof(char)); resampBuffer = (short *) malloc(nSampsPerChan * channel * sizeof(short)); if (!muPlayData || !resampBuffer) { sprintf(audio_error_msg, "InitAudioPlay: malloc failed."); return (1); } } return (0);}intGetPlayFilled(){ int sampsUnPlayed, sampsPlayed; struct timeval currTime; if (playPort < 0) return (0); gettimeofday(&currTime, NULL); sampsPlayed = fileRate * TimeDiff(&playBgnTime, &currTime) * playChan; sampsUnPlayed = playSamps - sampsPlayed; if (sampsUnPlayed < 0) sampsUnPlayed = 0; return (sampsUnPlayed);}intCanWriteAudio(){ int sampsUnplayed; int canwriteSamps; if (playPort < 0) return (0); canwriteSamps = playPortBufSize / sizeof_sample - GetPlayFilled(); if (canwriteSamps < 0) canwriteSamps = 0; return (canwriteSamps);}intPlayAudioDrain(){ audio_info_t info; if (playPort < 0) return 0; (void)ioctl(playPort, AUDIO_DRAIN, NULL); return 0;}intCloseAudioPlay(hold_port) int hold_port; /* if non-zero, hold port open */{ audio_info_t info; if (playPort >= 0) { ioctl(playPort, I_FLUSH, FLUSHW); if (!hold_port) { close(playPort); fclose(playPortFp); } } if (!hold_port) { playPort = -1; playPortFp = NULL; } return (0);}static intOpenPlayPort(){ if (playPort == -1) { if ((playPort = open(audio_dev, O_WRONLY | O_NDELAY)) < 0) { perror("Cannot open audio device for output"); return (1); } playPortFp = fdopen(playPort, "w"); } if (ioctl(playPort, AUDIO_SETINFO, &playPortInfo) < 0) { perror("error setting audio port mode in StartAudioPlay"); close(playPort); playPort = -1; playPortFp = NULL; return (1); } return (0);}intStartAudioPlay(){ int err; playSamps = 0; err = OpenPlayPort(); return (err);}intPauseAudioPlay(){ int sampsUnPlayed; struct timeval stopTime; if (playPort < 0) return (0); (void)CloseAudioPlay(0); gettimeofday(&stopTime, NULL); sampsUnPlayed = playSamps - fileRate * TimeDiff(&playBgnTime, &stopTime) * playChan; return (sampsUnPlayed);}intContAudioPlay(){ int err; err = InitAudioPlay(playRateSave, playChannelSave, playNSampsPerChanSave); if (err) return err; err = StartAudioPlay(); if (err) return err;}/*************************************************************************//* * The following resample function was written by Tom Veatch, Department of * Linguistics, University of Pennsylvania, Philadelphia, PA 19104. Thanks, * Tom. * * Copyright (c) 1990 Thomas C. Veatch * * Tom grants anyone the right to make or distribute verbatim copies of this * resample function, provided that the copyright notice and permission * notice are preserved, and that the distributor grants the recipient * permission for further redistribution as permitted by this notice. * * modified by: Derek Lin */#define SETCOEFSINDS {j=(int)i*step;k=j+1;c2=fmod(i*step,1.0);c1=1.0-c2;}static intresample(in, out, step, n, gain) register short *in; /* input data array */ register short *out; /* output data array */ register float step; /* step: input sf / SPARC_SAMPLE_FREQ */ register float gain; /* gain factor */ long n; /* size of elements in in/out */{ register int i, j, k, nout; register float c1, c2; /* 2-point interpolate samples from input to output. */ nout = n / step; /* max index of output samples */ if (gain != 1.0) for (i = 0; i < nout; i++) { SETCOEFSINDS out[i] = gain * (c1 * in[j] + c2 * in[k]); } else for (i = 0; i < nout; i++) { SETCOEFSINDS out[i] = (c1 * in[j] + c2 * in[k]); } return (nout);}/**********************************************************************/intPlayAudio(buffer, nSamps) short *buffer; int nSamps;{ int wrote; if (playPort < 0) return (0); if (amdDeviceMu) { float nRatio; long nResamps; nResamps = resample(buffer, resampBuffer, resampRatio, nSamps, 1.0); linear_to_mu(resampBuffer, muPlayData, nResamps); wrote = resampRatio * fwrite((char *) muPlayData, sizeof(char), nResamps, playPortFp); } else { wrote = fwrite((char *) buffer, sizeof(short), nSamps, playPortFp); } if (playSamps == 0) gettimeofday(&playBgnTime, NULL); fflush(playPortFp); playSamps += nSamps; return (0);}intSetAudioOutputType(out) char *out;{ int status = 0; if (!strcmp(out, "speaker")) playPortInfo.play.port = AUDIO_SPEAKER; else if (!strcmp(out, "headphone")) playPortInfo.play.port = AUDIO_HEADPHONE; else if (!strcmp(out, "lineOut")) playPortInfo.play.port = (amdDeviceMu) ? AUDIO_HEADPHONE : AUDIO_LINE_OUT; else { sprintf(audio_error_msg,"Unknown Output destination %s, speaker used"); playPortInfo.play.port = AUDIO_SPEAKER; status = 0; } if (playPort > 0) if (ioctl(playPort, AUDIO_SETINFO, &playPortInfo) < 0) { perror("error setting audio port mode in SetAudioOutputType"); return 1; } return status;}intSetAudioOutputGain(gainL, gainR) int gainL, gainR;{ /* for SUN, the left & right gains are the same */ if (gainL > 100) gainL = 100; if (gainR > 100) gainR = 100; /* 0 - 255, 0 min gain */ if (gainL < 0) gainL = 0; if (gainR < 0) gainR = 0; gainL = gainL * 2.55; gainR = gainR * 2.55; playPortInfo.play.gain = (gainL > gainR) ? gainL : gainR; if (playPort > 0) if (ioctl(playPort, AUDIO_SETINFO, &playPortInfo) < 0) { perror("error setting audio port mode in SetAudioOutputGain"); return 1; } return 0;}voidSendAudioZeros(){}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?