C语言使用libZPlay录制声音并写到文件的方法
本文实例讲述了C语言使用libZPlay录制声音并写到文件的方法。分享给大家供大家参考。具体实现方法如下:
/** *Recordsamplesfromline-inandsavetoout.mp3 * */ #defineWIN32_LEAN_AND_MEAN #include<windows.h> #include<olectl.h> #include<ole2.h> #include<stdio.h> #include<stdlib.h> #include<string.h> #include<conio.h> #include<dos.h> #include<conio.h> #include"../include/libzplay.h" usingnamespacelibZPlay; ZPlay*player; intnRate=100; intnPitch=100; intnTempo=100; intfCenterCut=0; intfSideCut=0; intfMixChannels=0; intlVolume=100; intrVolume=100; intfPause=0; intfEcho=0; intfEq=0; intfReverse=0; TStreamStatusstatus; TStreamTimepos; intmain(intargc,char**argv) { //clearscreen system("CLS"); //createclassinstance player=CreateZPlay(); //chekifwehaveclassinstance if(player==0) { printf("Error:Can'tcreateclassinstance!\nPresskeytoexit.\n"); getch(); return0; } //getlibraryversion intver=player->GetVersion(); //checkifwehaveversion2.00andabove if(ver<200) { printf("Error:Needlibraryversion2.00andabove!\nPresskeytoexit.\r\n"); getch(); player->Release(); return0; } //displayversioninfo printf("libZPlayv.%i.%02i\r\n\r\n",ver/100,ver%100); printf("x:play\tj:fadeout\ti:sidecut\td:pitchdown\r\n" "c:pause\tk:fadein\to:centercut\tf:pitchup\r\n" "v:stop\tl:loop\tn:volumedown\tg:tempodown\r\n" "y:rew\tp:equalizer\tm:volumeup\th:tempoup\r\n" "b:fwd\te:echo\ta:ratedown\r\n" "r:reverse\tt:channelmix\ts:rateup\tq:quit\r\n\r\n"); //openwaveinusingLineIn if(player->OpenFile("wavein://src=line;volume=50;",sfAutodetect)==0) { printf("Error:%s\nPresskeytoexit.\r\n",player->GetError()); getch(); player->Release(); return0; } //setwaveoutputtodiskfile,mp3encoding if(player->SetWaveOutFile("out.mp3",sfMp3,1)==0) { printf("Error:%s\nPresskeytoexit.\r\n",player->GetError()); getch(); player->Release(); return0; } //getstreaminfo TStreamInfopInfo; player->GetStreamInfo(&pInfo); //displayinfoaboutstream char*sVbr="CBR"; if(pInfo.VBR) sVbr="VBR"; printf("\r\n%s%iHz%sChannel:%uLength:%02i:%02i:%02i:%02i\r\n\r\n", pInfo.Description, pInfo.SamplingRate, sVbr, pInfo.ChannelNumber, pInfo.Length.hms.hour, pInfo.Length.hms.minute, pInfo.Length.hms.second, pInfo.Length.hms.millisecond); printf("Status|kbps|POS:|Echo|Equ|Cut|Fade|Rev|Rate|Pitch|Tempo|Vol|Loop|Chmix|\r\n"); //startplaying if(player->Play()==0) { printf("Error:%s\nPresskeytoexit.\r\n",player->GetError()); player->Release();//deleteZPlayclass return0; } while(1) { //getcurrentstatus player->GetStatus(&status); char*st=0; if(status.fPlay) st="Play"; elseif(status.fPause) st="Pause"; else st="Stop"; //getcurrentposition player->GetPosition(&pos); printf("%s%04i%02i:%02i:%02i%i%i%i%i%i%03i%03i%03i%03i%i%i\r", st, player->GetBitrate(0), pos.hms.hour, pos.hms.minute, pos.hms.second, status.fEcho, status.fEqualizer, status.fVocalCut, status.fSlideVolume, status.fReverse, player->GetRate(), player->GetPitch(), player->GetTempo(), lVolume, status.nLoop, fMixChannels ); if(kbhit()) { inta=getch(); switch(a) { case'i'://sidecut fSideCut=!fSideCut; player->StereoCut(fSideCut,1,0); fCenterCut=0; break; case'o'://center fCenterCut=!fCenterCut; player->StereoCut(fCenterCut,0,0); fSideCut=0; break; case's'://higherrate nRate+=5; player->SetRate(nRate); break; case'a'://lowerrate nRate-=5; if(nRate<0) nRate=0; player->SetRate(nRate); break; case'd'://lowerpitch nPitch-=5; if(nPitch<0) nPitch=0; player->SetPitch(nPitch); break; case'f'://higherpitch nPitch+=5; player->SetPitch(nPitch); break; case'g'://lowertempo nTempo-=5; if(nTempo<0) nTempo=0; player->SetTempo(nTempo); break; case'h'://highertempo nTempo+=5; player->SetTempo(nTempo); break; case'r'://reversemode fReverse=!fReverse; if(player->ReverseMode(fReverse)==0) { printf("Error:%s\n",player->GetError()); fReverse=0; } break; case'p'://enableordisableequalizer { fEq=!fEq; player->EnableEqualizer(fEq); if(fEq) { player->SetEqualizerPreampGain(-100000); player->SetEqualizerBandGain(0,104000); player->SetEqualizerBandGain(1,104000); player->SetEqualizerBandGain(2,104000); } } break; case'l'://playloop { TStreamTimepCurrentTime; player->GetPosition(&pCurrentTime); TStreamTimepEndTime; pEndTime.ms=pCurrentTime.ms+5000; player->PlayLoop(tfMillisecond,&pCurrentTime,tfMillisecond,&pEndTime,5,1); } break; case'j'://fadeoutvolume { TStreamTimestart; TStreamTimeend; player->GetPosition(&start); end.sec=start.sec+5; player->SlideVolume(tfSecond,&start,100,100,tfSecond,&end,0,0); } break; case'k'://fadeinvolume { TStreamTimestart; player->GetPosition(&start); TStreamTimeend; end.sec=start.sec+5; player->SlideVolume(tfSecond,&start,0,0,tfSecond,&end,100,100); } break; case'e'://enableecho { fEcho=!fEcho; player->EnableEcho(fEcho); } break; case'x'://play if(player->Play()==0) printf("Err:%s\n",player->GetError()); fPause=0; break; case'v'://stop player->Stop(); fPause=0; break; case'c'://pause if(fPause) { player->Resume(); fPause=0; } else { player->Pause(); fPause=1; } break; case'y'://jumpback5seconds { TStreamTimepTime; pTime.sec=5; player->Seek(tfSecond,&pTime,smFromCurrentBackward); } break; case'b'://jumpforward5seconds { TStreamTimepTime; pTime.sec=5; player->Seek(tfSecond,&pTime,smFromCurrentForward); } break; case't': { //mixstereochannelstomono fMixChannels=!fMixChannels; player->MixChannels(fMixChannels,50,50); } break; case'n'://volumedown if(lVolume>=5) lVolume-=5; if(rVolume>=5) rVolume-=5; player->SetMasterVolume(lVolume,rVolume); break; case'm'://volumeup if(lVolume<=95) lVolume+=5; if(rVolume<=95) rVolume+=5; player->SetMasterVolume(lVolume,rVolume); break; case'q': case'Q': player->Release(); return0; } } Sleep(50); } }
希望本文所述对大家的C语言程序设计有所帮助。