wav文件格式分析(代碼)


這個是為上一篇文章做結尾用的。

這里我只把基本的數據提取出來了,沒有進行下一步處理,數據提取出來,后面怎么應用就看具體情況了。

#include <iostream>
#include <fstream>
using namespace std;

struct wav_struct
{
    unsigned long file_size;        //文件大小
    unsigned short channel;            //通道數
    unsigned long frequency;        //采樣頻率
    unsigned long Bps;                //Byte率
    unsigned short sample_num_bit;    //一個樣本的位數
    unsigned long data_size;        //數據大小
    unsigned char *data;            //音頻數據 ,這里要定義什么就看樣本位數了,我這里只是單純的復制數據

};

int main(int argc,char **argv)
{
    fstream fs;
    wav_struct WAV;
    fs.open("B:\\output.wav",ios::binary|ios::in);

//    fs.seekg(0x04);                //從文件數據中獲取文件大小
//    fs.read((char*)&WAV.file_size,sizeof(WAV.file_size));
//    WAV.file_size+=8;
    
    fs.seekg(0,ios::end);        //用c++常用方法獲得文件大小
    WAV.file_size=fs.tellg();

    fs.seekg(0x14);
    fs.read((char*)&WAV.channel,sizeof(WAV.channel));

    fs.seekg(0x18);
    fs.read((char*)&WAV.frequency,sizeof(WAV.frequency));

    fs.seekg(0x1c);
    fs.read((char*)&WAV.Bps,sizeof(WAV.Bps));

    fs.seekg(0x22);
    fs.read((char*)&WAV.sample_num_bit,sizeof(WAV.sample_num_bit));

    fs.seekg(0x28);
    fs.read((char*)&WAV.data_size,sizeof(WAV.data_size));

    WAV.data=new unsigned char[WAV.data_size];

    fs.seekg(0x2c);
    fs.read((char *)WAV.data,sizeof(char)*WAV.data_size);

    cout<<"文件大小為  :"<<WAV.file_size<<endl;
    cout<<"音頻通道數  :"<<WAV.channel<<endl;
    cout<<"采樣頻率    :"<<WAV.frequency<<endl;
    cout<<"Byte率      :"<<WAV.Bps<<endl;
    cout<<"樣本位數    :"<<WAV.sample_num_bit<<endl;
    cout<<"音頻數據大小:"<<WAV.data_size<<endl;
    cout<<"最后20個數據:"<<endl;

    for (unsigned long i=WAV.data_size-20;i<WAV.data_size;i++)
    {
        printf("%x  ",WAV.data[i]);
    }
    fs.close();

    delete[] WAV.data;
    system("pause");

}

運行結果:

可以看出各種數據和上一篇文章中的分析都是對應的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM