audio_hpold.c
来自「speech signal process tools」· C语言 代码 · 共 902 行 · 第 1/2 页
C
902 行
return 0;}intSetAudioInputGain(gainL, gainR) int gainL, gainR;{ long status; float ratio; ratio = (maxOut - minOut) / 100.0; if (gainL > 100) gainL = 100; if (gainR > 100) gainR = 100; /* 0 - 100, 0 min gain */ if (gainL < 0) gainL = 0; if (gainR < 0) gainR = 0; recordGainL = gainL * ratio + minOut; recordGainR = gainR * ratio + minOut; recordGainSet = 1; switch (recordAttribs.attr.sampled_attr.channels) { case 1: ASetChannelGain(audio, recordXid, ACTMono, recordGainL, &status); break; case 2: default: ASetChannelGain(audio, recordXid, ACTLeft, recordGainL, &status); ASetChannelGain(audio, recordXid, ACTRight, recordGainR, &status); break; } return 0;}/* * ----------------------------------------------------------------- Play * ----------------------------------------------------------------- */static voidtakenap(usec) unsigned int usec;{ struct timeval timeout; timeout.tv_sec = usec / 1000000; timeout.tv_usec = usec % 1000000; if (select(0, 0, 0, 0, &timeout) < 0) perror("takenap");}static doubleTimeDiff(a, b) struct timeval *a, *b;{ double d; d = (b->tv_sec + b->tv_usec * 1E-6) - (a->tv_sec + a->tv_usec * 1E-6); if (d > 0) return (d); else return (0);}static intOpenPlayPort(){ int stat; /* * create a stream socket */ if (playSocket > -1) return 0; playSocket = socket(AF_INET, SOCK_STREAM, 0); if (playSocket < 0) { perror("Socket creation failed"); return 1; } /* * connect the stream socket to the audio stream port */ stat = connect(playSocket, (struct sockaddr *) & playStream.tcp_sockaddr, sizeof(struct sockaddr_in)); if (stat < 0) { perror("Connect failed"); return 1; }#ifdef WRITE_TEST test_fp = fopen("test_play_data","w");#endif return (0);}intInitAudioPlay(srate, channel, nSampsPerChan) double srate; int channel; int nSampsPerChan;{ long length, soffset; AByteOrder byte_order; char *c;#ifdef DEBUG fprintf(stderr, "InitAudioPlay: rate: %f, channel: %d, samps: %d\n", srate, channel, nSampsPerChan);#endif playRate = srate; playChan = channel; playNSampsPerChan = nSampsPerChan; if (PortIsAvailable()) { (void) sprintf(audio_error_msg, "Sorry, this play program requires HP audio.\n"); return (1); } if ((c = getenv("SPEAKER"))) if (*c == 'E' || *c == 'e') useIntSpeaker = 0; else if (*c == 'I' || *c == 'i') useIntSpeaker = 1; playAttribsMask = 0; playSrcAttribsMask = 0; soffset = 0; sizeof_sample = sizeof(short); playSrcAttribsMask = (playSrcAttribsMask | ASSamplingRateMask | ASChannelsMask); playAttribsMask = (playAttribsMask | ASSamplingRateMask | ASChannelsMask); fileRate = playAttribs.attr.sampled_attr.sampling_rate = playSrcAttribs.attr.sampled_attr.sampling_rate = closest_srate(srate, sampleRates); playSrcAttribs.attr.sampled_attr.channels = playAttribs.attr.sampled_attr.channels = (channel < 2) ? 1 : 2; playSrcAttribs.attr.sampled_attr.interleave = playSrcAttribs.attr.sampled_attr.interleave = 1; AChooseSourceAttributes(audio, NULL, NULL, AFFRawLin16, playSrcAttribsMask, &playSrcAttribs, &soffset, &length, &byte_order, NULL); AChoosePlayAttributes(audio, &playSrcAttribs, 0, &playAttribs, &byte_order, NULL); switch (playAttribs.attr.sampled_attr.channels) { case 1: playGainEntry[0].u.o.out_ch = AOCTMono; playGainEntry[0].gain = AUnityGain; playGainEntry[0].u.o.out_dst = useIntSpeaker ? AODTMonoIntSpeaker : AODTMonoJack; break; case 2: default: playGainEntry[0].u.o.out_ch = AOCTLeft; playGainEntry[1].u.o.out_ch = AOCTRight; playGainEntry[0].gain = AUnityGain; playGainEntry[1].gain = AUnityGain; playGainEntry[0].u.o.out_dst = useIntSpeaker ? AODTRightIntSpeaker : AODTRightJack; playGainEntry[1].u.o.out_dst = useIntSpeaker ? AODTRightIntSpeaker : AODTLeftJack; break; } playStreamParams.priority = APriorityNormal; playStreamParams.event_mask = 0; /* don't solicit any events */ playStreamParams.gain_matrix.type = AGMTOutput; /* gain matrix */ playStreamParams.gain_matrix.num_entries = playAttribs.attr.sampled_attr.channels; playStreamParams.gain_matrix.gain_entries = playGainEntry; playXid = APlaySStream(audio, ~0, &playAttribs, &playStreamParams, &playStream, NULL); playPortBufSize = nSampsPerChan * sizeof_sample * channel; playInitCalled = 1; return (0);}intGetPlayFilled(){ int sampsUnPlayed, sampsPlayed; struct timeval currTime; if (playSocket < 0) return 0; if (playInitCalled && first_write) return 0; gettimeofday(&currTime, NULL); sampsPlayed = fileRate * TimeDiff(&playBgnTime, &currTime) * playChan; sampsUnPlayed = playSamps - sampsPlayed; if (sampsUnPlayed < 0) sampsUnPlayed = 0; return (sampsUnPlayed);}intCanWriteAudio(){ int canwriteSamps; if (playSocket < 0) return (0); canwriteSamps = (playPortBufSize / sizeof_sample) - GetPlayFilled(); if (canwriteSamps < 0) canwriteSamps = 0; return (canwriteSamps);}intPlayAudioDrain(){ return 0;}intCloseAudioPlay(hold) int hold;{ long status; if (playSocket < 0) return (0);fprintf(stderr,"Close: left: %d",GetPlayFilled());#ifdef WRITE_TEST if(test_fp) fclose(test_fp);#endif if (playPause) { AResumeAudio(audio, playXid, NULL, NULL); playPause = 0; } first_write=0;fputc('!',stderr); AStopAudio(audio, playXid, ASMLinkedTrans, NULL, NULL);fputc('@',stderr); close(playSocket);fputc('#',stderr); playSocket = -1; playInitCalled = 0; if (!hold) {/* ASetCloseDownMode(audio, ADestroyAll, NULL);*/fputc('%',stderr); ACloseAudio(audio, &status);fputc('^',stderr); audio = NULL; }fputc('.',stderr);fputc('\n',stderr); return (status);}intStartAudioPlay(){ int err; long status; playSamps = 0; err = OpenPlayPort(); if (err) { sprintf(audio_error_msg, "StartAudioPlay: trouble OpenPlayPort()\n"); return (1); } if (playGainSet) { switch (playAttribs.attr.sampled_attr.channels) { case 1: ASetChannelGain(audio, playXid, ACTMono, playGainL, &status); break; case 2: default: /* assume no more than 2 channels */ ASetChannelGain(audio, playXid, ACTLeft, playGainL, &status); ASetChannelGain(audio, playXid, ACTRight, playGainR, &status); break; } }#ifdef PAUSE /* * start stream paused so we can transfer enough data (PLAYHOLD seconds * worth) before playing starts to prevent stream from running out */ APauseAudio(audio, playXid, NULL, NULL); playPauseCount = (PLAYHOLD / 1000000.0) * playAttribs.attr.sampled_attr.channels * playAttribs.attr.sampled_attr.sampling_rate; playPause = 1;#endif first_write=1; return (0);}/* * HP's APauseAudio doesn't stop the audio output immediately, due to their * double layers of network buffering. */intPauseAudioPlay(){ int sampsUnPlayed; struct timeval stopTime; if (playSocket < 0) return (0); AStopAudio(audio, playXid, ASMLinkedTrans, NULL, NULL); playDrain = 0; CloseAudioPlay(1); gettimeofday(&stopTime, NULL); sampsUnPlayed = playSamps - fileRate * TimeDiff(&playBgnTime, &stopTime) * playChan; return (sampsUnPlayed);}intContAudioPlay(){ InitAudioPlay(playRate, playChan, playNSampsPerChan); StartAudioPlay(); return (0);}voidSendAudioZeros(){ static short buff[400]; if (playSocket < 0) return; write(playSocket, buff, 400*sizeof(short));} intPlayAudio(buffer, nSamps) char *buffer; int nSamps;{ int i, n, to_write; if (playSocket < 0) return 0;#ifdef WRITE_TEST fwrite(buffer,sizeof(short),nSamps,test_fp);#endif n = nSamps * sizeof_sample; while (n > 0) { to_write = n; if ((i = write(playSocket, buffer, to_write)) < 0) { perror("PlayAudio"); return 0; } if(first_write) { first_write=0; gettimeofday(&playBgnTime, NULL);#ifdef PAUSE playBgnTime.tv_usec += PLAYHOLD;#endif if (playBgnTime.tv_usec > 1000000) { playBgnTime.tv_sec++; playBgnTime.tv_usec -= 1000000; } } buffer += i; n -= i; playSamps += (i / sizeof_sample); if (playPause) { playPauseCount -= (i / sizeof_sample); if ((i == 0) || (playPauseCount < 0)) { AResumeAudio(audio, playXid, NULL, NULL); playPause = 0; } } } return (0);}intSetAudioOutputType(out) char *out;{ if (!strcmp(out, "speaker")) useIntSpeaker = 1; else if (!strcmp(out, "lineOut")) useIntSpeaker = 0; else { sprintf(audio_error_msg, "Unknown play port specified\n"); return 0; } /* * Because InitAudioPlay must be called first to know how many channel we * have, We can't set playGainEntry which setting is channel dependent * before the device is open. */ if (playInitCalled) { /* * close up the connection and restart. Unfortunately HP won't let you * change output destination while device is open. */ CloseAudioPlay(0); InitAudioPlay(playRate, playChan, playNSampsPerChan); } return 0;}intSetAudioOutputGain(gainL, gainR) int gainL, gainR;{ long status; float ratio; ratio = (maxOut - minOut) / 100.0; if (gainL > 100) gainL = 100; if (gainR > 100) gainR = 100; /* 0 - 100, 0 min gain */ if (gainL < 0) gainL = 0; if (gainR < 0) gainR = 0; playGainL = gainL * ratio + minOut; playGainR = gainR * ratio + minOut; playGainSet = 1; switch (playAttribs.attr.sampled_attr.channels) { case 1: ASetChannelGain(audio, playXid, ACTMono, playGainL, &status); break; case 2: default: /* assume no more than 2 channels */ ASetChannelGain(audio, playXid, ACTLeft, playGainL, &status); ASetChannelGain(audio, playXid, ACTRight, playGainR, &status); break; } return 0;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?