您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++怎么讀取WAV音頻文件的頭部數據”,在日常操作中,相信很多人在C++怎么讀取WAV音頻文件的頭部數據問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++怎么讀取WAV音頻文件的頭部數據”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
C++讀取WAV音頻文件的頭部數據的實現方法
實現代碼:
#include <iostream> #include <string> #include <fstream> 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音頻文件的頭部數據”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。