基于C语言的 WAV 文件双声道转单声道的实现

本文介绍了一种处理16位PCM格式音频文件的方法,通过C语言实现,详细解析了WAV文件结构,包括读取文件头信息、声道转换等,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里只处理PCM是16位,encode是PCM格式的波形进行解析:

#include <stdio.h>
#include <stdlib.h>

#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned long
 
//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,否则会有很大的噪音!!! 
	long 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)
		close(fp);	
	if(fw!=NULL)
		close(fw);	
	if(Sound_Data!=NULL)
		free(Sound_Data);	
		
	system("pause");
	return 0;	
}
 

 

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值