C++如何读取WAV音频文件的头部数据-创新互联
这篇文章主要介绍了C++如何读取WAV音频文件的头部数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
创新互联成立与2013年,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元澄江做网站,已为上家服务,为澄江各地企业和个人服务,联系电话:18982081108C++读取WAV音频文件的头部数据的实现方法
实现代码:
#include#include #include using namespace std; using std::string; using std::fstream; typedef struct WAV_HEADER{ char RIFF[4]; // RIFF Header Magic header unsigned long ChunkSize; // RIFF Chunk Size char WAVE[4]; // WAVE Header char fmt[4]; // FMT header unsigned long Subchunk1Size; // Size of the fmt chunk unsigned short AudioFormat; // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM unsigned short NumOfChan; // Number of channels 1=Mono 2=Sterio unsigned long SamplesPerSec; // Sampling Frequency in Hz unsigned long bytesPerSec; // bytes per second unsigned short blockAlign; // 2=16-bit mono, 4=16-bit stereo unsigned short bitsPerSample; // Number of bits per sample char Subchunk2ID[4]; // "data" string unsigned long Subchunk2Size; // Sampled data length }wav_hdr; // Function prototypes int getFileSize(FILE *inFile); int main(int argc,char *argv[]){ wav_hdr wavHeader; FILE *wavFile; int headerSize = sizeof(wav_hdr),filelength = 0; string answer; do{ string input; string answer; const char* filePath; cout << "Pick wav file from the Windows Media File: "; cin >> input; cin.get(); cout << endl; path = "C:\\Windows\\Media\\" + input + ".wav"; filePath = path.c_str(); wavFile = fopen( filePath , "r" ); if(wavFile == NULL){ printf("Can not able to open wave file\n"); //exit(EXIT_FAILURE); } fread(&wavHeader,headerSize,1,wavFile); filelength = getFileSize(wavFile); fclose(wavFile); cout << "File is :" << filelength << " bytes." << endl; cout << "RIFF header :" << wavHeader.RIFF[0] << wavHeader.RIFF[1] << wavHeader.RIFF[2] << wavHeader.RIFF[3] << endl; cout << "WAVE header :" << wavHeader.WAVE[0] << wavHeader.WAVE[1] << wavHeader.WAVE[2] << wavHeader.WAVE[3] << endl; cout << "FMT :" << wavHeader.fmt[0] << wavHeader.fmt[1] << wavHeader.fmt[2] << wavHeader.fmt[3] << endl; cout << "Data size :" << wavHeader.ChunkSize << endl; // Display the sampling Rate form the header cout << "Sampling Rate :" << wavHeader.SamplesPerSec << endl; cout << "Number of bits used :" << wavHeader.bitsPerSample << endl; cout << "Number of channels :" << wavHeader.NumOfChan << endl; cout << "Number of bytes per second :" << wavHeader.bytesPerSec << endl; cout << "Data length :" << wavHeader.Subchunk2Size << endl; cout << "Audio Format :" << wavHeader.AudioFormat << endl; // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM cout << "Block align :" << wavHeader.blockAlign << endl; cout << "Data string :" << wavHeader.Subchunk2ID[0] << wavHeader.Subchunk2ID[1] << wavHeader.Subchunk2ID[2] << wavHeader.Subchunk2ID[3] << endl; cout << endl << endl << "Try something else? (y/n)"; cin >> answer; //cin.get(); cout << endl << endl; }while( answer == "y" ); getchar(); return 0; } // find the file size int getFileSize(FILE *inFile){ int fileSize = 0; fseek(inFile,0,SEEK_END); fileSize=ftell(inFile); fseek(inFile,0,SEEK_SET); return fileSize; }
感谢你能够认真阅读完这篇文章,希望小编分享的“C++如何读取WAV音频文件的头部数据”这篇文章对大家有帮助,同时也希望大家多多支持创新互联建站,关注创新互联网站建设公司行业资讯频道,更多相关知识等着你来学习!
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
网站栏目:C++如何读取WAV音频文件的头部数据-创新互联
本文路径:http://myzitong.com/article/hcsdo.html