Even though there are libraries and tools to concatenate two canonical wave files, here is how you can do it manually, just in case you need to or are interested.
Take one of the files and add the second files minus the header. Then modify sizes of the file and the actual data part.
You might want to look at the definition of a wave file first.
public byte[] mergeTwoWavFiles(byte[] fileOne, byte[] fileTwo) throws UnsupportedAudioFileException, IOException { long lengthOne = getChunkLength(fileOne); long lengthTwo = getChunkLength(fileTwo); // result length is wave file lengths minus one header (44) byte[] result = new byte[fileOne.length + fileTwo.length - 44]; System.arraycopy(fileOne, 0, result, 0, fileOne.length); System.arraycopy(fileTwo, 44, result, fileOne.length, fileTwo.length - 44); // modify ChunkSize long combinedChunkSize = lengthOne + lengthTwo; byte[] data = longToByteArray(combinedChunkSize); result[4] = data[3]; result[5] = data[2]; result[6] = data[1]; result[7] = data[0]; // modify SubChunkSize combinedChunkSize = fileOne.length + fileTwo.length - 88; data = longToByteArray(combinedChunkSize); result[40] = data[3]; result[41] = data[2]; result[42] = data[1]; result[43] = data[0]; return result; /** * get the chunksize length coded in 4 byte * * @param combinedChunkSize * @return */ private byte[] longToByteArray(long combinedChunkSize) { byte[] result = new byte[4]; for (int i = 0; i < 4; ++i) { int shift = i << 3; // i * 8 result[3 - i] = (byte) ((combinedChunkSize & (0xff << shift)) >>> shift); } return result; } /** * get the length of the wav from its chunksize info * * @param chunkSize * @return */ private long getChunkLength(byte[] chunkSize) { long length = 0; for (int i = 7; i >= 4; i--) { length = (length << 8) + (chunkSize[i] & 0xff); } return length; }
The following two tabs change content below.

Nico Heid
I work as a software engineer during the day and sometimes hack a bit in my free time. That currently includes anything from software, system and networks to raspberry pi and hardware.
You can find most of my results on this blog.

Latest posts by Nico Heid (see all)
- Raspberry Pi Supply Switch, start and shut down your Pi like your PC - August 24, 2015
- Infinite House of Pancakes – code jam 2015 - July 13, 2015
- google code jam 2014 – magic trick - April 18, 2014