基于C语言实现把WAV双声道文件差分成两个单声道文件

本deemo基于下面这篇文章的deemo实现

基于C语言的 WAV 文件双声道转单声道的实现_快乐的老鼠的博客-CSDN博客_wav 双声道转单声道

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

#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned long
#define WAV_URL "./test2ch.wav"
#define WAV_LEFT_URL "./test2ch_left.wav"
#define WAV_RIGHT_URL "./test2ch_right.wav"

//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];		
	long w;
};
union Union_Data uniondat;

int debug_print_cnt;
long fw_cnt_left, fw_cnt_right;
short currpos = -1;
int main()
{
	FILE *fp, *fwLeft, *fwRight;
	int ret;
	int i, j, res;
	short s_tmp;
	char a, b[4] = { 0 };

	if ((fp = fopen(WAV_URL, "rb")) == NULL)	//打开操作不成功
		return 1;
	if ((fwLeft = fopen(WAV_LEFT_URL, "wb")) == NULL)	//打开操作不成功
		return 2;
	if ((fwRight = fopen(WAV_RIGHT_URL, "wb")) == NULL)	//打开操作不成功
		return 3;

	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, fwLeft) == 1)
		printf("写入左声道文件信息成功!\n");
	else
		printf("############### 写入左声道文件信息失败!!! #################\n");

	if (fwrite(&WAVFile_Array, sizeof(WAVFile_Array), 1, fwRight) == 1)
		printf("写入右声道文件信息成功!\n");
	else
		printf("############### 写入右声道文件信息失败!!! #################\n");


	printf("截取后的声音数据大小: %d\n", WAVFile_Array.sound_size);

	Sound_Data = ((u16 *)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_left = 0;
	fw_cnt_right = 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_left += fwrite(&uniondat.s[0], sizeof(uniondat.s[0]), 1, fwLeft);
		fw_cnt_right += fwrite(&uniondat.s[1], sizeof(uniondat.s[1]), 1, fwRight);
	}

	printf("SingleLeft total write %d bytes!\n", fw_cnt_left);
	printf("SingleRight total write %d bytes!\n", fw_cnt_right);
	//if (fw_cnt_left == WAVFile_Array.sound_size / 2)
	//{
	//	printf("已经成功生成单声道文件了!\n");
	//}

exit:
	if (fp != NULL)
		fclose(fp);
	if (fwLeft != NULL)
		fclose(fwLeft);
	if (fwRight != NULL)
		fclose(fwRight);
	if (Sound_Data != NULL)
		free(Sound_Data);

	system("pause");
	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值