2

Here is my code that concatenates four wav files and produces wavAppended.wav. This concatenated file nicely plays in Windows Media Player. But through the PlaySound class, only the one.wav can be heard. Can anyone help?

class PlaySound extends Object implements LineListener
{
   File soundFile;
   JDialog playingDialog;
   Clip clip;

   public void PlaySnd(String s) throws Exception
   {
      JFileChooser chooser = new JFileChooser();
      soundFile = new File(s);
      Line.Info linfo = new Line.Info(Clip.class);
      Line line = AudioSystem.getLine(linfo);
      clip = (Clip) line;
      clip.addLineListener(this);
      AudioInputStream ais = AudioSystem.getAudioInputStream(soundFile);
      clip.open(ais);
      clip.start();
    }

    public void update(LineEvent le)
    {
        LineEvent.Type type = le.getType();
        playingDialog.setVisible(false);
        clip.stop();
        clip.close();
     }
}

public class Main
{
   public static void main(String[] args)
   {
      int i;
      String wavFile[] = new String[4];
      wavFile[0] = "D://one.wav";
      wavFile[1] = "D://two.wav";
      wavFile[2] = "D://three.wav";
      wavFile[3] = "D://space.au";
      AudioInputStream appendedFiles;

      try
      {
          AudioInputStream clip0=AudioSystem.getAudioInputStream(new File(wavFile[0]));
          AudioInputStream clip1=AudioSystem.getAudioInputStream(new File(wavFile[1]));
          AudioInputStream clip3;

          for (i=0;i<4;i++)
          {
              appendedFiles = new AudioInputStream(
                 new SequenceInputStream(clip0, clip1),
              clip0.getFormat(),
              clip0.getFrameLength() + clip1.getFrameLength());
              AudioSystem.write(appendedFiles, AudioFileFormat.Type.WAVE, new File("D:\\wavAppended.wav"));

              clip3 = AudioSystem.getAudioInputStream(new File("D:\\wavAppended.wav"));
              clip0=clip3;
              clip1 = AudioSystem.getAudioInputStream(new File(wavFile[i+2]));

            }

            PlaySound p = new PlaySound();
            p.PlaySnd("D://wavAppended.wav");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

3 Answers 3

1

WAV files don't work that way -- you can't just throw multiple files together (same as you can't concatenate JPEG images, for instance), as there's a header on the data, and there are multiple different formats the data may be in. I'm surprised that the file loads at all.

0
1

To get you started with the WAV processing you may have a look at my small project. It can copy and paste WAV files together based on an time index file. The project should contain all the Java WAV processing you need (using javax.sound.sampled). The Butcher implementation and Composer contain the actual processing.

The idea is simple: take input audio files and create a index of words contained in these files. The index entry is the word, start time and end time. When a new sentence is created it will be stitched together with single words taken from the index.

The AudioInputStream is the main class to interact with the Java Sound API. You read audio data from it. If you create audio data you do this by creating a AudioInputStream the AudioSystem can read from. The actual encoding is done by the AudioSystem implementation depending on the output audio format.

The Butcher class is the one concerned with audio files. It can read and write audio files and create AudioInputStreams from an input byte array. The other interesting think the Butcher can is cutting samples from a AudioInputStream. The AudioInputStream consists of frames that represent the samples of the PCM signal. Frames have a length of multiple bytes. To cut a valid range of frames from the AudioInputStream one has to take the frame size into account. The start and end time in milliseconds have to be translated to start byte and end bytes of the start frame and end frame. (The start and end data is stored as timestamps to keep them independent from the underlying encoding of the file used.)

The Composer creates the output file. For a given sentence it takes the audio data for each word from the input files, concatenates the audio data and writes the result to disk.

In the end you'll need some understanding of the PCM and the WAV format. The Java sound API does not abstract that away.

3
  • Mr. Thomas, thank you for your reply. But I am at a preliminary stage in handling wav files. Would you please demonstrate through implementation of the two classes of yours?
    – PS Nayak
    Jan 9, 2012 at 15:31
  • The project itself was meant as a demonstration what can be done with the Java sound API. Your problem is much simpler. You just have to read all bytes from your AudioInputStreams and write them to one consolidated AudioInputStreams. (The Composer does basicly the same, but it's a bit obscured by cutting parts from the input streams.) Jan 9, 2012 at 17:35
  • I think that this is common problem where it is needed to convert from OutputStream to InputStream. Check here for few suggestions how to do it.
    – Rade_303
    Feb 29, 2012 at 11:55
1

In above given example you need to use the SequenceInputStream then it will work fine. please find my code below to join two files.

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 JoinWav{

        public static void main(String... args) throws Exception{
                String wav_1 = "1497434542598100215.wav";
                String wav_2 = "104860397153760.wav";
                AudioInputStream stream_1 = AudioSystem.getAudioInputStream(new File(wav_1));
                AudioInputStream stream_2 = AudioSystem.getAudioInputStream(new File(wav_2));
                System.out.println("Info : Format ["+stream_1.getFormat()+"] Frame Length ["+stream_1.getFrameLength()+"]");
                AudioInputStream stream_join = new AudioInputStream(new SequenceInputStream(stream_1,stream_2),stream_1.getFormat(),stream_1.getFrameLength()+stream_2.getFrameLength());
                AudioSystem.write(stream_join,AudioFileFormat.Type.WAVE,new File("join.wav"));
        }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.