Question

I have this simple code to concatenate two wav files. Its pretty simple and the code runs without any errors. But there is a problem with the output file. The output file generated does not play, and surprisingly its size is only 44 bytes whereas my input files "a.wav" & "b.wav" are both more than 500Kb in size.

Here is my code:

import java.io.File;
import java.io.IOException;
import java.io.SequenceInputStream;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;

public class WavConcat {
    public static void main(String[] args) {
        String wFile1 = "./sounds/a.wav";
        String wFile2 = "./sounds/b.wav";

        try {
            AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wFile1));
            AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wFile2));

            AudioInputStream appendedFiles = 
                            new AudioInputStream(
                                new SequenceInputStream(clip1, clip2),     
                                clip1.getFormat(), 
                                clip1.getFrameLength() + clip2.getFrameLength());

            AudioSystem.write(appendedFiles, 
                            AudioFileFormat.Type.WAVE,new File("./sounds/ab.wav"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Was it helpful?

Solution 2

clip1.getFormat() returns-->

MPEG2L3 24000.0 Hz, unknown bits per sample, mono, unknown frame size, 41.666668 frames/second

clip2.getFormat() returns-->

MPEG2L3 24000.0 Hz, unknown bits per sample, mono, unknown frame size, 41.666668 frames/second

That is an odd format. I can imagine the 'unknown bits per sample' is causing a problem, but also the MPEG2L3, since JavaSound has no inbuilt encoder for MP3. It seems like they are not encoded properly. Try loading them in sound editing software and save them as a type of WAV or AU that Java Sound can understand 'out of the box'. Hopefully the editing software:

  1. Can understand the broken MP3, and..
  2. Will write a valid WAV or AU.

If you can convert them to 8 bit mono & 8KHz during the conversion, it might reduce the byte[] size by a factor of 6 to 1. 8KHz is considered good enough to understand speech, and for this use you need to serve the bytes of the combined sound out to the browser - so reducing it in size is crucial.

OTHER TIPS

Try this kind of structure. This worked for me

List audioInputStreamList = new ArrayList();

    String wFile1 = "./sounds/a.wav";
    String wFile2 = "./sounds/b.wav";

 AudioInputStream audioInputStream1 = AudioSystem.getAudioInputStream(new File(wFile1));
 AudioInputStream audioInputStream2 = AudioSystem.getAudioInputStream(new File(wFile2));

 audioInputStreamList.add(audioInputStream1);
 audioInputStreamList.add(audioInputStream2);

 AudioFormat audioFormat = audioInputStream1.getFormat(); // audioInputStream2 format also same

 AudioInputStream udioInputStream = new SequenceAudioInputStream(audioFormat,audioInputStreamList);

 AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE,new File("./sounds/ab.wav"));

UPDATE

check for SequenceAudioInputStream

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top