一 代码
转载文章,修改了几处代码风格
#include <stdio.h>
#include <stdlib.h>
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int
//44字节
typedef struct {
u8 riff_mark[4]; //0
u32 file_size; //4
u8 wave_str[4]; //8
u8 fmt_str[4];
u32 pcm_bit_num;
u16 pcm_encode;
u16 sound_channel;
u32 pcm_sample_freq;
u32 byte_freq;
u16 block_alin;
u16 sample_bits;
u8 data_mark[4];
u32 sound_size;
} WAV_Typedef;
WAV_Typedef WAVFile_Array;
u16 *Sound_Data;
u8 *Read_Sound_Data;
union Union_Data{
char b[4];
short s[2]; //这个short千万别定义成u16,否则会有很大的噪音!!!
int w;
};
union Union_Data uniondat;
int debug_print_cnt;
long fw_cnt;
short currpos=-1;
int main()
{
FILE *fp,*fw;
int ret;
int i,j,res;
short s_tmp;
char a,b[4]={0};
if((fp=fopen("sea.wav","rb"))==NULL) //打开操作不成功
{
return 1;
}
if((fw=fopen("out-sea.wav","wb"))==NULL) //打开操作不成功
{
return 2;
}
if(fread(&WAVFile_Array,sizeof(WAVFile_Array),1,fp)==1)
{
printf("wav file read ok!\n");
}
printf("资源交换文件标志: %.4s\n",WAVFile_Array.riff_mark);
printf("文件大小: %d\n",WAVFile_Array.file_size+8);
printf("文件格式: %.4s\n",WAVFile_Array.wave_str);
printf("波形格式: %.4s\n",WAVFile_Array.fmt_str);
printf("PCM: %dbits\n",WAVFile_Array.pcm_bit_num);
printf("pcm_encode: %d\n",WAVFile_Array.pcm_encode);
printf("声道: %s\n",WAVFile_Array.sound_channel==1?"单声道":"双声道");
printf("采样速率: %d\n",WAVFile_Array.pcm_sample_freq);
printf("码率: %d Bps\n",WAVFile_Array.byte_freq);
printf("块对齐: %d\n",WAVFile_Array.block_alin);
printf("采样位宽: %d\n",WAVFile_Array.sample_bits);
printf("Data标志: %.4s\n",WAVFile_Array.data_mark);
if(memcmp(WAVFile_Array.data_mark,"LIST",4)==0)
{
printf("\n########## 发现LIST类型数据 ###########\n");
fseek(fp,sizeof(WAV_Typedef)-4,SEEK_SET);
for(i=0;i<256;i++)
{
if(fgetc(fp)==0x00 && (a=fgetc(fp))!=0x00)
{
if(a=='d')
{
for(j=0;j<3;j++)
b[j]=fgetc(fp);
if(memcmp(b,"ata",3)==0)
{
currpos=ftell(fp);
printf("\tFound data Mark!!! currpos=%d\n",currpos);
break;
}
}
}
}
if(i<currpos+4)
{
for(j=0;j<4;j++)
uniondat.b[j]=fgetc(fp);
WAVFile_Array.sound_size=uniondat.w;
}
}
printf("声音数据大小: %d\n",WAVFile_Array.sound_size);
if(WAVFile_Array.sound_channel==1)
{
printf("已经是单声道了!!!\n");
goto exit;
}
/*********** 生成新的单声道文件头部 *****************/
WAVFile_Array.sound_channel=1;
WAVFile_Array.sound_size/=2;
WAVFile_Array.block_alin/=2;
WAVFile_Array.byte_freq/=2;
WAVFile_Array.file_size=WAVFile_Array.sound_size+44-8;
memcpy(WAVFile_Array.data_mark,"data",4);
if(fwrite(&WAVFile_Array,sizeof(WAVFile_Array),1,fw)==1)
printf("写入文件信息成功!\n");
else
printf("############### 写入文件信息失败!!! #################\n");
printf("截取后的声音数据大小: %d\n",WAVFile_Array.sound_size);
Sound_Data=malloc(WAVFile_Array.sound_size*2);
Read_Sound_Data=(u8 *)Sound_Data;
if(Sound_Data==NULL)
{
printf("malloc failed!\n");
return 2;
}
debug_print_cnt=20;
fw_cnt=0;
for(i=0;i<WAVFile_Array.sound_size/2;i++)
{
res=fread(&uniondat.b,1,4,fp);
if(res<=0)
{
printf("fread over,res=%d\n",res);
break;
}
if(debug_print_cnt>0)
{
printf("%08X\t",uniondat.w);
}
/* 生成单声道有多种方法,可以左+右再除以2或者只单独提取某一通道 */
s_tmp=(uniondat.s[0]+uniondat.s[1])>>1;
if(debug_print_cnt>0)
{
printf("s_tmp= (%04X,%04X=%X)\n",(u16)uniondat.s[0],(u16)uniondat.s[1],(u16)s_tmp);
}
debug_print_cnt--;
fw_cnt+=fwrite(&s_tmp,sizeof(s_tmp),1,fw);
}
printf("total write %d bytes!\n",fw_cnt);
if(fw_cnt==WAVFile_Array.sound_size/2)
{
printf("已经成功生成单声道文件了!\n");
}
exit:
if(fp!=NULL)
{
fclose(fp);
}
if(fw!=NULL)
{
fclose(fw);
}
if(Sound_Data!=NULL)
{
free(Sound_Data);
}
system("pause");
return 0;
}