我在實(shí)現(xiàn)一個(gè)音頻處理功能的過程中,需要對wav文件進(jìn)行處理。我們知道在matlab中有現(xiàn)成的函數(shù)wavread('wavtest.wav'),本文的用途就是用c++代碼實(shí)現(xiàn)該函數(shù)的功能。 讀者可以在vs中新建一個(gè)工程,再將本文的代碼粘貼在文件中,編譯運(yùn)行:生成 —>編譯 —>生成wavread(U),打開vs工程目錄,例如我的是我的文檔/Visual Studio 2013/Projects/wavread,打開該目錄下的Debug文件夾,雙擊wavread.exe則可以看到下圖2所示的內(nèi)容。 輸入:wave.wav,你可以選擇你的wav文件目錄打開 輸出:如下圖2所示 更多實(shí)現(xiàn)原理,請您移步我的博文:http://my.oschina.net/liusicong/blog/323090 跳至 [1] [2] #include <iostream> #include <fstream> #include <string.h> #include<math.h> #include<cmath> #include<stdlib.h> #include <bitset> #include <iomanip> //要在int main()的前面加上函數(shù)的聲明,因?yàn)槟愕暮瘮?shù)寫在main函數(shù)的后面 int hex_char_value(char ss); int hex_to_decimal(const char* s); //string hex_to_binary(char* szHex); using namespace std; struct wav_struct { unsigned long file_size; //文件大小 unsigned short channel; //通道數(shù) unsigned long frequency; //采樣頻率 unsigned long Bps; //Byte率 unsigned short sample_num_bit; //一個(gè)樣本的位數(shù) unsigned long data_size; //數(shù)據(jù)大小 unsigned char *data; //音頻數(shù)據(jù) ,這里要定義什么就看樣本位數(shù)了,我這里只是單純的復(fù)制數(shù)據(jù) }; int main(int argc, char **argv) { fstream fs; wav_struct WAV; fs.open("F:\\wave.wav", ios::binary | ios::in); // fs.seekg(0x04); //從文件數(shù)據(jù)中獲取文件大小 // 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 << "音頻通道數(shù) :" << WAV.channel << endl; cout << "采樣頻率 :" << WAV.frequency << endl; cout << "Byte率 :" << WAV.Bps << endl; cout << "樣本位數(shù) :" << WAV.sample_num_bit << endl; cout << "音頻數(shù)據(jù)大?。? << WAV.data_size << endl; cout << "最后10個(gè)數(shù)據(jù):" << endl; for (unsigned long i =0; i<WAV.data_size; i = i + 2) { //右邊為大端 unsigned long data_low = WAV.data[i]; unsigned long data_high = WAV.data[i + 1]; double data_true = data_high * 256 + data_low; //printf("%d ",data_true); long data_complement = 0; //取大端的最高位(符號(hào)位) int my_sign = (int)(data_high / 128); //printf("%d ", my_sign); if (my_sign == 1) { data_complement = data_true - 65536; } else { data_complement = data_true; } //printf("%d ", data_complement); setprecision(4); double float_data = (double)(data_complement/(double)32768); printf("%f ", float_data); //data_normalization[i] = (char)float_data; //printf("%f ", data_normalization[i]); //bitset<8>lsc_high(data_high); //string high_binary = lsc_high.to_string(); //bitset<8> low_binary (low_data); } fs.close(); delete[] WAV.data; system("pause"); } int hex_char_value(char c) { if (c >= '0' && c <= '9') return c - '0'; else if (c >= 'a' && c <= 'f') return (c - 'a' + 10); else if (c >= 'A' && c <= 'F') return (c - 'A' + 10); //assert(0); return 0; } int hex_to_decimal(char* szHex) { int len = 2; int result = 0; for (int i = 0; i < len; i++) { result += (int)pow((float)16, (int)len - i - 1) * hex_char_value(szHex[i]); } return result; } /* string hex_to_binary(char* szHex) { int len = 2; string result; for (int i = 0; i < len;i++) } */ [2].[圖片] QQ截圖20140929094252.png 跳至 [1] [2] |
|