合并两个WAVE文件JAVA代码

合并两个文件关键在于将ChunkSize和DataSize进行修改

在对WAVE文件头分析的代码基础上修改的

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//合并两个wave音频
public class ReadAndWriteTest {
	

	public static void main(String[] args) throws Exception {
		WaveMontage wf = new WaveMontage();
		wf.Analyse();
		wf.ShowInfo1();

	}
}
class WaveMontage {
	int ChunkID; // RIFF
	byte[] ChunkSize; // size=filelen-8B
	int ChunkFormat; // WAVE

	// subchunk-'fmt'
	int SubChunk1; // 'fmt'
	int SubChunk1Size;
	// PCMWAVEFORMAT
	short AudioFormat;
	short NumChannels;
	int SampleRate;
	int ByteRate;
	short BlockAlign;
	short BitsperSample;

	// subchunk-date
	int SubChunk2ID;
	byte[] SubChunk2Size;
	
	byte[] ChunkSize2;
	byte[] SubChunk2Size2;
	
	byte[] return_ChunkSize = new byte[4];
	byte[] return_SubChunk2Size = new byte[4];

	public void Analyse(){
		try {
			DataInputStream DemoWav = new DataInputStream(
			 new BufferedInputStream(
					 new FileInputStream(
							new File("E:\\programme\\BMP&WAV\\OriginWave\\record.wav"))));
			DataInputStream DemoWav2 = new DataInputStream(
					 new BufferedInputStream(
							new FileInputStream(
									new File("E:\\programme\\BMP&WAV\\OriginWave\\record2.wav"))));
			DataOutputStream wave_new = new DataOutputStream(
					 new BufferedOutputStream(
								new FileOutputStream(
										new File("E:\\programme\\BMP&WAV\\OriginWave\\wavenew.wav"))));
			try {
				ChunkID = DemoWav.readInt();
				ChunkSize = new byte[4];
				DemoWav.readFully(ChunkSize, 0, 4);
				ChunkFormat = DemoWav.readInt();
				SubChunk1 = DemoWav.readInt();
				SubChunk1Size = DemoWav.readInt();
				AudioFormat =  DemoWav.readShort();
				NumChannels =  DemoWav.readShort();
				SampleRate = DemoWav.readInt();
				ByteRate = DemoWav.readInt();
				BlockAlign =  DemoWav.readShort();
				BitsperSample =  DemoWav.readShort();
				SubChunk2ID = DemoWav.readInt();
				SubChunk2Size = new byte[4];
				DemoWav.readFully(SubChunk2Size, 0, 4);
				System.out.println("===========================================");
				System.out.println(DemoWav2.available());
				ChunkSize2 = new byte[4];
				DemoWav2.skipBytes(4);
				DemoWav2.readFully(ChunkSize2, 0, 4);
				System.out.println(DemoWav2.available());
				SubChunk2Size2 = new byte[4];
				DemoWav2.skipBytes(32);
				DemoWav2.readFully(SubChunk2Size2, 0, 4);
				System.out.println(DemoWav2.available());
				
				System.out.println(DemoWav.available());
				System.out.println("-----------------------------------------");
				
				new DesTwo(ChunkSize,ChunkSize2,return_ChunkSize);
				new DesTwo(SubChunk2Size,SubChunk2Size2,return_SubChunk2Size);
				
				wave_new.writeInt(ChunkID);wave_new.flush();
				wave_new.write(return_ChunkSize);wave_new.flush();
				wave_new.writeInt(ChunkFormat);wave_new.flush();
				wave_new.writeInt(SubChunk1);wave_new.flush();
				wave_new.writeInt(SubChunk1Size);wave_new.flush();
				wave_new.writeShort(AudioFormat);wave_new.flush();
				wave_new.writeShort(NumChannels);wave_new.flush();
				wave_new.writeInt(SampleRate);wave_new.flush();
				wave_new.writeInt(ByteRate);wave_new.flush();
				wave_new.writeShort(BlockAlign);wave_new.flush();
				wave_new.writeShort(BitsperSample);wave_new.flush();
				wave_new.writeInt(SubChunk2ID);wave_new.flush();
				wave_new.write(return_SubChunk2Size);
				wave_new.flush();
				
				while (DemoWav.available()>0) {
					wave_new.write(DemoWav.read());
					wave_new.flush();

				}
				while (DemoWav2.available()>0) {
					wave_new.write(DemoWav2.read());
					wave_new.flush();

				}
				System.out.println(DemoWav.available());
				System.out.println(DemoWav2.available());
				DemoWav.close();
				DemoWav2.close();
				wave_new.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				System.out.println("Analyse Error!");
			}
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("No file form the path !");
		}
	}
	
	public void ShowInfo1(){
		System.out.println("-----------------WAVE HEAD-------------------");
		System.out.println("ChunkID  "+Integer.toHexString(ChunkID));
		System.out.print("ChunkSize  "+Integer.toHexString(ChunkSize[0]&0xff));
		System.out.print("  "+Integer.toHexString(ChunkSize[1]&0xff));
		System.out.print("  "+Integer.toHexString(ChunkSize[2]&0xff));
		System.out.println("  "+Integer.toHexString(ChunkSize[3]&0xff));
		System.out.println("ChunkFormat  "+Integer.toHexString(ChunkFormat));
		System.out.println("-----------------SubChunk1-------------------");
		System.out.println("SubChunk1  "+Integer.toHexString(SubChunk1));
		System.out.println("SubChunk1Size  "+Integer.toHexString(SubChunk1Size));
		System.out.println("AudioFormat  "+Integer.toHexString(AudioFormat));
		System.out.println("NumChannels  "+Integer.toHexString(NumChannels));
		System.out.println("SampleRate  "+Integer.toHexString(SampleRate));
		System.out.println("ByteRate  "+Integer.toHexString(ByteRate));
		System.out.println("BlockAlign  "+Integer.toHexString(BlockAlign));
		System.out.println("BitsperSample  "+Integer.toHexString(BitsperSample));
		System.out.println("-----------------SubChunk2-------------------");
		System.out.println("SubChunk2ID  "+Integer.toHexString(SubChunk2ID));
		System.out.print("SubChunk2Size  "+Integer.toHexString(SubChunk2Size[0]&0xff));
		System.out.print("  "+Integer.toHexString(SubChunk2Size[1]&0xff));
		System.out.print("  "+Integer.toHexString(SubChunk2Size[2]&0xff));
		System.out.println("  "+Integer.toHexString(SubChunk2Size[3]&0xff));
	}

}
可以看出来做了一些微小的修改,原因是Wave文件头数据以字存储,且为倒序。

例如:ChunkSize解析出来为5c4a0000,其实际大小为0x00+0x00+0x4a+0x5c

所以在读数时修改了读法,改为按字节读。然后用自己写的一个DesTwo(byte[] byt1, byte[] byt2, byte[] return_num)方法进行运算,

附代码:

package ForTest;

/*
 * 
 * 字存储倒序数据的加法运算
 * 
 */
public class DesTwoTest {
	public static void main(String[] args) {//此处为测试,请无视
		byte[] byt1 = { (byte) 0xa1,(byte) 0x15,(byte) 0xff,(byte) 0xff };
		byte[] byt2 = { (byte) 0xa2,(byte) 0x15,(byte) 0xff,(byte) 0xff };
		byte[] byt_return = new byte[4];

		new DesTwo(byt1, byt2, byt_return);

		for (int j = 0;j< byt1.length;  j++) {
			System.out.println(Integer.toHexString(byt1[j]&0xff));
		}
		for (int j = 0;j< byt1.length;  j++) {
			System.out.println(Integer.toHexString(byt2[j]&0xff));
		}
		for (int j = 0;j< byt1.length;  j++) {
			System.out.println(Integer.toHexString(byt_return[j]&0xff));
		}
	}

}

class DesTwo {
	DesTwo() {

	}

	DesTwo(byte[] byt1, byte[] byt2, byte[] return_num) {

		
		int b = 0;
		for (int i = 0; i < byt1.length; i++) {
			if (b==0) {
				return_num[i] = (byte) (byt1[i] + byt2[i]);
				if ((return_num[i]&0xff) < (byt1[i]&0xff)) {
					b = 1;
				}
			} else {
				b = 0;
				return_num[i] = (byte) (byt1[i] + byt2[i] + 1);
				if (return_num[i] < byt1[i]) {
					b = 1;
				}
			}
		}
		// return return_num;

	}
}

如此,已经可以合并两个音频。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值