united-coders

TwitterFacebookGoogleRSS
  • Home
  • Authors
    • Christian Harms
    • Nico Heid
  • Newsletter
Home » Manually concatenating two wave files

Manually concatenating two wave files

Posted on March 16, 2013 by Nico Heid Posted in Uncategorized

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;
    }
  • Bio
  • Latest Posts
My Google+ profile

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)

  • A scalable, affordable WordPress hosting, lessons learned - May 19, 2013
  • Code Jam – Candy Splitting - March 30, 2013
  • Manually concatenating two wave files - March 16, 2013
« A highly scalable WordPress self-hosting that is not a burden on your wallet
Code Jam – Candy Splitting »

Leave a comment Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Tags

android code jam code puzzle hackercup hosting java javascript permutations project euler python server

Recent Comments

  • garcinia cambogia walmart on Free IP to Geo Location script
  • Yoda Conditions | Pack 6 – Palo Alto on What are yoda conditions?
  • Nico Heid on The art of escaping
  • hardik on Use Android ActivityGroup within TabHost to show different Activity
  • james on Use Android ActivityGroup within TabHost to show different Activity

Recent Posts

  • A scalable, affordable WordPress hosting, lessons learned
  • google code jam 2013 – tic-tac-toe-Tomek solution
  • Google code jam 2013 – the lawnmower
  • code puzzles and permutations
  • Code Jam – Candy Splitting

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Copyright

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.
© united-coders
  • 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