3

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();
        }
    }
}
Nick Div
  • 5,338
  • 12
  • 65
  • 127
  • 1
    tried your code, worked fine for me. please check, as @AndrewThompson suggested, format of both files. PS. 44 bytes is the length of wav header. – Denis Tulskiy Nov 15 '12 at 04:26
  • @AndrewThompson The original samples were simply downloaded from the Internet. They are simple mp3 sounds from "a" to "b" and "0" to "9". – Nick Div Nov 15 '12 at 08:12
  • @AndrewThompson I tried a software called audacity(audio-editor) that did the trick. Finally it is working. Thanks a lot for all the help. I appreciate it.It wouldn't have been possible without the help. – Nick Div Nov 15 '12 at 09:10

2 Answers2

2

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

someone
  • 6,577
  • 7
  • 37
  • 60
  • I have tried this one already. It gives the same output when trying to play the file. I mean it doesn't work. The size of the output is appropriate. Still the output file doesn't play. – Nick Div Nov 14 '12 at 17:52
  • The problem is not with the codec. Coz I downloaded every possible player with all codecs possible. Its definetly the output of the code. I dont think appropriate output is getting created. – Nick Div Nov 14 '12 at 19:00
  • What is a `SequenceAudioInputStream`? – Andrew Thompson Nov 15 '12 at 00:55
  • Sorry, I have added my SequenceAudioInputStream class. Please check it and change it to way you want. – someone Nov 15 '12 at 04:36
  • @AndrewThompson I already had SequenceAudioInputStream from the jresources.org website in my package. But is it wrong to use? – Nick Div Nov 15 '12 at 07:14
  • @Sura I already had SequenceAudioInputStream from the jresources.org website in my package. But is it wrong to use? – Nick Div Nov 15 '12 at 07:15
  • These are open source codes, I have used these in my IVR solutions and it working fine without any issues. – someone Nov 15 '12 at 07:21
  • @Sura Thanks for the link. It has been a while since I looked around the JS Resources site. – Andrew Thompson Nov 15 '12 at 08:02
  • @Sura Thanks a lot for all the help. I appreciate it.It wouldn't have been possible without the help. – Nick Div Nov 15 '12 at 09:10
1

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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433