wav檔案的讀取,預設為雙聲道(若為單聲道根據頭檔案中的參數做個判斷即可),一個聲道的一組資料為兩個位元組,是以左聲道總資料數為dataSize(位元組為機關)/4
#include <stdlib.h>
#include <stdio.h>
#include<malloc.h>
#include <math.h>
struct RIFF_HEADER
{
//RIFF_HEADER
unsigned char szRiffID[]; // 'R','I','F','F'
unsigned long dwRiffSize;
unsigned char szRiffFormat[]; // 'W','A','V','E'
// WAVE_FORMAT
}riff_header;
struct FMT_BLOCK
{
// FMT_BLOCK
unsigned char szFmtID[]; // 'f','m','t',' '
unsigned long dwFmtSize;
// dwFmtSize為16則其後16位元組,否則為18,末尾有2個位元組的附加資訊
unsigned short wFormatTag;
unsigned short wChannels;
unsigned long dwSamplesPerSec;
unsigned long dwAvgBytesPerSec;
unsigned short wBlockAlign;
unsigned short wBitsPerSample;
// unsigned short ultraMessage;
}fmt_block;
struct FACT_BLOCK
{
unsigned char szFactID[]; // 'f','a','c','t'
unsigned long dwFactSize;
}fact_block;
struct DATA_BLOCK
{
// DATA_BLOCK
unsigned char szDataID[]; // 'd','a','t','a'
unsigned long dwDataSize;
}data_block;
int main()
{
// int i; //用作循環計數
//unsigned char ch[ch_size]; //用來存儲wav檔案的頭資訊
FILE *fp;
const char* wav_path= "d:\\wavcs\\cat.wav";
fp=fopen(wav_path,"rb");//為讀,打開一個wav檔案
if((fp=fopen(wav_path,"rb"))==NULL) //若打開檔案失敗,退出
{
printf("can't open this file\n");
exit();
}
fread(&riff_header,sizeof(struct RIFF_HEADER),,fp);
printf("riff_header\n");
printf("%10s%10x%10s",riff_header.szRiffID,riff_header.dwRiffSize,riff_header.szRiffFormat);
printf("\n");
fread(&fmt_block,sizeof(struct FMT_BLOCK),,fp);
printf("fmt_block\n");
printf("%10s%10x%10x%10x\n%10x%10x%10x%10x",fmt_block.szFmtID,fmt_block.dwFmtSize,fmt_block.wFormatTag,fmt_block.wChannels,fmt_block.dwSamplesPerSec,fmt_block.dwAvgBytesPerSec,fmt_block.wBlockAlign,fmt_block.wBitsPerSample);
printf("\n");
fread(&data_block,sizeof(struct DATA_BLOCK),,fp);
printf("data_block\n");
printf("%10s%10x",data_block.szDataID,data_block.dwDataSize);
printf("\n");
int data_size=data_block.dwDataSize/;
printf("datasize%d\n",data_size);
short *datacontent=(short *)malloc(sizeof(short)*data_size);
fread(&datacontent,sizeof(short),data_size,fp);
printf("datacontent\n");
// 讀取資料
// int m;//全資料輸出的測試
// for(m=354303;m<354304;m++)
// {
// printf("datacount%d\n",m);
// float databyfloat=datacontent[m]/32768.0000;
// printf("%0.4f\n",databyfloat);
// };
int i=;//取出右聲道部分
int n=;
int single_size=data_block.dwDataSize/;
printf("singletracksize%d\n",single_size);
float *singletrackdata = (float *)malloc(single_size*sizeof(float));
for(;i <data_size;i++) {//取出左聲道部分資料放入數組singletrackdata
if(i% == ) {
singletrackdata[n] = datacontent[i]/;
//printf("singletrackdata%d\n",n+1);
//printf("%0.4f\n",singletrackdata[n]);
n++;
}
}
// int m;//單聲道資料輸出的測試
// for(m=354301;m<354304;m++)
// {
// printf("datacount%d\n",m);
// float databyfloat=singletrackdata[m];
// printf("%0.4f\n",databyfloat);
// };