sound problem, how to play two wavs simultaneously

Almost all examples seem to say they can play more than one sound clip at a time (polyphony) but as I tried them I got this:

  • For one sound, they work, I can hear the wav .
  • For two sound, I hear only noise
  • For three I hear nothing

Can anyone post a simple code which can play the same sound file clips more than one instance at one time? I mean we need to hear all sounds together in a game right? not only one sound at a time.

thanks for any help.


import java.io.*;
import java.net.URL;
import java.util.*;
import javax.sound.sampled.*;

public class SoundManager
{
       private javax.sound.sampled.Line.Info lineInfo;

    private Vector afs;
    private Vector sizes;
    private Vector infos;
    private Vector audios;
    private int num=0;

    public SoundManager()
    {
            afs=new Vector();
            sizes=new Vector();
            infos=new Vector();
            audios=new Vector();
    }

    public void addClip(String s)
        throws IOException, UnsupportedAudioFileException, LineUnavailableException
    {
        URL url = getClass().getResource(s);
        //InputStream inputstream = url.openStream();
        AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(loadStream(url.openStream()));
            AudioFormat af = audioInputStream.getFormat();
            int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio = new byte[size];
            DataLine.Info info = new DataLine.Info(Clip.class, af, size);
            audioInputStream.read(audio, 0, size);

            afs.add(af);
            sizes.add(new Integer(size));
            infos.add(info);
            audios.add(audio);

            num++;
    }

    private ByteArrayInputStream loadStream(InputStream inputstream)
              throws IOException
      {
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            byte data[] = new byte[1024];
            for(int i = inputstream.read(data); i != -1; i = inputstream.read(data))
                  bytearrayoutputstream.write(data, 0, i);

            inputstream.close();
            bytearrayoutputstream.close();
            data = bytearrayoutputstream.toByteArray();
            return new ByteArrayInputStream(data);
    }

    public void playSound(int x)
          throws UnsupportedAudioFileException, LineUnavailableException
    {
            if(x>num)
            {
                  System.out.println("playSound: sample nr["+x+"] is not available");
            }
            else
            {
                  Clip clip = (Clip) AudioSystem.getLine((DataLine.Info)infos.elementAt(x));
                  clip.open((AudioFormat)afs.elementAt(x), (byte[])audios.elementAt(x), 0, ((Integer)sizes.elementAt(x)).intValue());
                  clip.start();
            }
      }
}

sounds get loaded like this:


sou = new SoundManager();
sou.addClip("mclick.wav");
sou.addClip("yes.wav");

and played like this:


sou.playSound(0);

You can change the addClip method to return the array number if you like…


int pang_sound;
[...]
pang_sound=sou.addClip("mclick.wav");
[...]
sou.playSound(pang_sound);

It works quite nicely… well it’s really basic but usually it’s enough :slight_smile:

If you are wondering about loadStream - it’s necessary for loading the wavs from jars correctly.

no luck, same result again, second and third sounds are heard like noise. here blabla.wav is a long sound , others are shorter. I want to hear them clearly, but no luck for now.

    int s1 = sou.addClip("sound/blabla.wav");
    int s2 = sou.addClip("sound/diamond1.wav");
    int s3 = sou.addClip("sound/diamond2.wav");
    int s4 = sou.addClip("sound/diamond3.wav");
    int s5 = sou.addClip("sound/rock1.wav");

    sou.playSound(s1); 
    sou.playSound(s2);
    sou.playSound(s3);
    Thread.sleep(100);
    sou.playSound(s4);
    sou.playSound(s5);

Hm… strange… I hadn’t had any problems on all machines I tryed that.

So… do you have 1.4.2?
Wich operating system do you use?
What are the soundformats you use? (mono/stereo, 8/16bit, 11/22/44khz)

[I use PCM - 11025Hz 8bit-Mono for all of my samples]

Finally!

The wrong thing was the wav files I used. I think they were not correct somehow, I tried some other wav files (windows’s own files) and now I got the result I want.

Maybe java is a little bit selective on wavs format. My files were

PCM - 44.1 data rate - 16 bit - 22.5Khz sample rate - Mono

working files are:

PCM - 88.2 data rate - 16 bit - 22.5Khz sample rate - Stereo

Can you tell me why Java didnt like the first one?

Thanks for your help.

88.2 ? I have never heard of such a rate ever being used. Usually people go for 96kHz if they go above 48kHz.
I highly suspect that the 88.2 number is an error and it should say 44.1

Could the 88.2 be calculated from the fact that you have two 44KHz streams: left and right?

So maybe the answer is that Java didn’t like the fact that the first one was Mono?

I’ve frequently found sound file readers to be a little temperamental - I’ve had many Wav files that work in one application, but turn into noise in another. Someone just doesn’t implement all of the spec, assumes something, then things go totally wrong when that assumption doesn’t hold. Maybe something similar is going on here?

[quote]Could the 88.2 be calculated from the fact that you have two 44KHz streams: left and right?
[/quote]
Could be, but it is still wrong. Looks like it should be 44.1 stereo. not 44.1 mono or 88.2 stereo

I’m confused by that 88.2 too (is that kbits or something?).

And actually I don’t know the limitations of the java sound api… I just intended to point you (mdakin) into that direction, because in the past I’d quite often noticed, that some applications have trouble with specific wavs.

Therefore I only use one format, wich was produced by one programm (the wav editor I use for trimming ;)).

[quote]I’m confused by that 88.2 too (is that kbits or something?)
[/quote]
kHz (kiloHertz) referring to the number of digital samples per second of sound.
CD quality is either 44.1 or 48 kHz, I can never remember which. Anything higher that 48kHz is generally a complete waste unless you are programming sounds for dogs. (although recently it seems that some people claim to be able to sense frequencies higher than the previously thought limit of human hearing of around 20kHz… so high end audio systems have jumped up to 96kHz)

The largest frequency that you can playback is half of the sampling rate. If your sound contains frequencies higher than that, you must filter out the higher frequencies before it is sampled, or else you will get artefacts in lower frequencies (aliasing)

ahem

“PCM - 88.2 data rate - 16 bit - 22.5Khz sample rate - Stereo”

:>

:-/

Doh! I should read more carefully.

But seriously who ever talks about data rate with no units, or audio files in terms of bytes per second. (In this case the data rate is shown in bytes per second.)

22.5 kHz * 2 for stereo * 2 for 16 bit samples
= 88.2 bytes per second.

Ok, so that makes sense now… it’s just an odd way to talk about audio.