Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (523)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (592)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  sound problem, how to play two wavs simultaneously  (Read 4698 times)
0 Members and 1 Guest are viewing this topic.
Offline mdakin

Junior Newbie




Java games rock!


« Posted 2003-09-06 17:37:38 »

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.
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #1 - Posted 2003-09-06 17:46:11 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
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:
1  
2  
3  
sou = new SoundManager();
sou.addClip("mclick.wav");
sou.addClip("yes.wav");


and played like this:
1  
sou.playSound(0);


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

1  
2  
3  
4  
5  
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 Smiley

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

弾幕 ☆ @mahonnaiseblog
Offline mdakin

Junior Newbie




Java games rock!


« Reply #2 - Posted 2003-09-06 18:32:00 »

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);
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #3 - Posted 2003-09-06 21:33:31 »

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]

弾幕 ☆ @mahonnaiseblog
Offline mdakin

Junior Newbie




Java games rock!


« Reply #4 - Posted 2003-09-07 07:28:50 »

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.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #5 - Posted 2003-09-07 19:18:30 »

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

Offline cfmdobbie

Senior Devvie


Medals: 1


Who, me?


« Reply #6 - Posted 2003-09-07 22:15:47 »

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?

Hellomynameis Charlie Dobbie.
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #7 - Posted 2003-09-07 23:09:23 »

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


Could be, but it is still wrong.  Looks like it should be 44.1 stereo.  not 44.1 mono or 88.2 stereo

Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #8 - Posted 2003-09-09 11:26:41 »

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 Wink).

弾幕 ☆ @mahonnaiseblog
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #9 - Posted 2003-09-09 20:35:45 »

Quote
I'm confused by that 88.2 too (is that kbits or something?)


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)

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #10 - Posted 2003-09-09 21:04:02 »

*ahem*

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

:>

弾幕 ☆ @mahonnaiseblog
Offline swpalmer

JGO Coder


Exp: 12 years


Where's the Kaboom?


« Reply #11 - Posted 2003-09-10 17:56:38 »

:-/

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

SHC (24 views)
2014-11-25 12:00:59

SHC (23 views)
2014-11-25 11:53:45

Norakomi (22 views)
2014-11-25 11:26:43

Gibbo3771 (22 views)
2014-11-24 19:59:16

trollwarrior1 (36 views)
2014-11-22 12:13:56

xFryIx (74 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (41 views)
2014-11-12 21:09:33

kovacsa (68 views)
2014-11-07 19:57:14
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!