WAVファイルの読み書き
6327 ワード
基礎知識の参考https://blog.csdn.net/brookicv/article/details/52809128
1.WAV書き込み
ヘッダ関数が必要
1)インフラストラクチャ定義
2)WAVヘッダ書込み
3データ書き込みおよび末尾
2.ファイル読み込み
1.WAV書き込み
ヘッダ関数が必要
#include
#include
#include
#include
#include
#include
#include
#include
1)インフラストラクチャ定義
typedef struct WAVE_HEADER {
char fccID[4];
unsigned long dwSize;
char fccType[4];
}WAVE_HEADER;
typedef struct WAVE_FMT {
char fccID[4];
unsigned long dwSize;
unsigned short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short uiBitsPerSample;
}WAVE_FMT;
typedef struct WAVE_DATA {
char fccID[4];
unsigned long dwSize;
}WAVE_DATA;
int channels = 1;
int sample_rate = 48000;
//int bits = 16;
int bits = 16;
WAVE_HEADER pcmHEADER;
WAVE_FMT pcmFMT;
WAVE_DATA pcmDATA;
//unsigned short m_pcmData;
BYTE* floatdata;
floatdata = (BYTE*)malloc( 2* (int)blockSize);
memset(floatdata, 0, 2 * (int)blockSize);
FILE *fpout;
char wavepath[65];
2)WAVヘッダ書込み
GetLocalTime(&st);
sprintf_s(wavepath, "%4d-%02d-%02d-%02d-%02d-%02d.wav", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);//
fpout = fopen(wavepath, "wb+");
if (fpout == NULL) {
printf("create wav file error
");
}
//WAVE_HEADER
memcpy(pcmHEADER.fccID, "RIFF", strlen("RIFF"));
memcpy(pcmHEADER.fccType, "WAVE", strlen("WAVE"));
fseek(fpout, sizeof(WAVE_HEADER), 1);
//WAVE_FMT
pcmFMT.dwSamplesPerSec = sample_rate;
pcmFMT.dwAvgBytesPerSec = pcmFMT.dwSamplesPerSec * sizeof(unsigned short);
pcmFMT.uiBitsPerSample = bits;
memcpy(pcmFMT.fccID, "fmt ", strlen("fmt "));
pcmFMT.dwSize = bits;
pcmFMT.wBlockAlign = 2;
pcmFMT.wChannels = channels;
pcmFMT.wFormatTag = 1;
fwrite(&pcmFMT, sizeof(WAVE_FMT), 1, fpout);
//WAVE_DATA;
memcpy(pcmDATA.fccID, "data", strlen("data"));
pcmDATA.dwSize = 0;
fseek(fpout, sizeof(WAVE_DATA), SEEK_CUR);
3データ書き込みおよび末尾
if (savetime < 550)
{
//memcpy(floatdata, Volt, 2048);
pcmDATA.dwSize += 2048;
savetime++;
for (int i = 0; i < 1024; i++)
{
floatdata[2*i] = (int)((Volt[60*i+1]*256 + Volt[60*i]-32768)*5.6234/0.012589) &0xff;
floatdata[2*i +1] = (int)((Volt[60*i+1]*256 + Volt[60*i]-32768)*5.6234/0.012589) >> 8;
}
fwrite(floatdata, sizeof(unsigned short), 1024, fpout);
if (savetime > 548)
{
pcmHEADER.dwSize = 44 + pcmDATA.dwSize;
rewind(fpout);
fwrite(&pcmHEADER, sizeof(WAVE_HEADER), 1, fpout);
fseek(fpout, sizeof(WAVE_FMT), SEEK_CUR);
fwrite(&pcmDATA, sizeof(WAVE_DATA), 1, fpout);
//fclose(fp);
fclose(fpout);
printf("
");
2.ファイル読み込み
#include
#include
#include
#include
#include
#include
#include
#include
// int main() , main
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; //
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("F:\\wave.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 << " 10 :" << endl;
for (unsigned long i =0; ilsc_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;
}