WAV 文件双声道转单声道的实现

一 代码

转载文章,修改了几处代码风格

#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;	
}
 

二 出处

WAV转单声道

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实现音频双声道变声道,我们需要读取音频文件,将左右两个声道的音频数据合并为一个声道,并输出声道的音频文件。以下是实现音频双声道变声道的Java代码示例: ``` import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; import java.io.File; import java.io.IOException; public class StereoToMono { public static void main(String[] args) throws IOException { // 读取音频文件 File audioFile = new File("audio_stereo.wav"); AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(audioFile); // 获取音频格式 AudioFileFormat fileFormat = AudioSystem.getAudioFileFormat(audioFile); // 双声声道 if (fileFormat.getChannels() == 2) { byte[] buffer = new byte[1024]; int bytesRead = 0; int totalBytesRead = 0; while ((bytesRead = audioInputStream.read(buffer, 0, buffer.length)) != -1) { totalBytesRead += bytesRead; for (int i = 0; i < bytesRead; i += fileFormat.getFrameSize() * 2) { buffer[i / 2] = buffer[i]; buffer[i / 2 + 1] = buffer[i + 1]; } if (totalBytesRead == bytesRead) { audioInputStream = new AudioInputStream(audioInputStream, fileFormat, totalBytesRead / fileFormat.getFrameSize() / 2); } else { audioInputStream = new AudioInputStream(audioInputStream, fileFormat, (totalBytesRead - bytesRead) / fileFormat.getFrameSize() / 2); } } } // 输出声道音频文件 File monoFile = new File("audio_mono.wav"); AudioSystem.write(audioInputStream, fileFormat.getType(), monoFile); } } ``` 这个代码示例通过AudioSystem类和AudioInputStream类读取音频文件,将左右两个声道的音频数据合并为一个声道,然后输出声道的音频文件。其中,我们通过操作字节数组实现双声声道的功能,具体实现可参考代码注释。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值