Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (515)
Games in Android Showcase (122)
games submitted by our members
Games in WIP (577)
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  
  javax.sound: Applet Clip latency  (Read 3549 times)
0 Members and 1 Guest are viewing this topic.
Offline Mibran

Senior Newbie





« Posted 2009-02-16 17:59:57 »

Hi there,

I'm new to Java game development and having a question about sound problems.

Using Ubuntu Linux 8.10, I encounter enormous playback latency when playing
sounds like the code sample below. For learning purposes, I'm programming
a Space Invaders clone, and taking every sound approx. 0,5 seconds to
happen isn't really acceptable here.

This is only on Linux/Firefox (where it also affects Flash games), on Windows
systems there is nearly no problem. Starting the clip from the GUI thread or
from an extra created thread does not change anything.

My question: Is there any way to give some runtime parameters to the JVM
whether to use Alsa, PulseAudio or OSS ?

As I've seen on the forum, many problems disappear when using OpenAL-Wrapper
libraries instead of the javax.sound framework. I'm trying to avoid any extra libraries,
so if there is an solution, please let me know...

Thanks in advance,
Michael

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
   private Clip loadSound(String name)
   {
      Clip clip=null;
      try
      {
         URL url=getClass().getResource(resDir + name);
         AudioInputStream sample=AudioSystem.getAudioInputStream(url);
         clip=AudioSystem.getClip();
         clip.open(sample);
      }
      catch (Exception e)
      {
         System.err.println(e.getMessage() + " : " + name);
      }
      return clip;
   }

   private void playSound(Clip sound)
   {
      sound.setFramePosition(0);
      sound.loop(0);
   }

Offline ShannonSmith
« Reply #1 - Posted 2009-02-16 19:05:17 »

Java has it's own mixer implementation that seams to have a massive fixed size delay buffer which screws you for any real time sound in Linux. The problem is not seen on windows because the default mixer is usually not Suns one. There are a few ways round this, you can use a properties file in the Java dir to set the default mixer to be something other than Suns, but this doesn't help with distribution. Alternatively use getClip(Mixer.Info mixerInfo) and pass a decent mixer. AudioSystem.getMixer(null).getMixerInfo() seems to return the system default mixer which should be the one you are after. If you know what sort of sound format you are using you can also do this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
            AudioFormat format = new AudioFormat(44100, 16, 1, true, true);
            Mixer.Info destMixer = null;
            for(Mixer.Info mixerInfo :AudioSystem.getMixerInfo()){
                Mixer mixer = AudioSystem.getMixer(mixerInfo);
                //System.out.println(mixerInfo.getDescription() + ", " + mixerInfo.getVendor());
                if (!mixerInfo.getVendor().contains("Sun")){ // Run Away!
                    for(Info info : mixer.getSourceLineInfo()){
                        if (info instanceof DataLine.Info){
                            ((DataLine.Info)info).isFormatSupported(FORMAT);
                            destMixer = mixerInfo;
                            break;
                        }
                    }
                }
            }          


Keep in mind that clips are not particularly nice as they use one SourceDataLine each and you have no control over it. Mixers have a limited number of lines that can be open at any time and each clip grabs one. What you really want to do is use a single SourceDataLine and write bytes to it. SourceDataLine also has a buffer size parameter in it's open() method which can help with latency problems.
Offline Mibran

Senior Newbie





« Reply #2 - Posted 2009-02-17 07:13:06 »

Thanks for the detailed answer so far!
Will take me some time to get familiar with that concepts.

Michael

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

Senior Newbie





« Reply #3 - Posted 2009-04-17 13:09:15 »

Ok, I've got behind it (even all that ThreadLocal usage I have to do if I want to play sounds simultaneously).
Unfortunately, all mixers my Ubuntu 8.10 with onboard sound gives me, are these:

mixer #1:Intel [plughw:0,1], version 1.0.17, maxLines:1)
mixer #2:Java Sound Audio Engine, version 1.0, maxLines:32)
mixer #3:Port Intel [hw:0], version 1.0.17, maxLines:1)

So far for playing sounds in Java without use of external libraries... Sad
Any hints? JOAL?

cu
Michael

Offline DzzD
« Reply #4 - Posted 2009-04-17 13:14:35 »

hum did you try the old way ? AudioClip work nice for short sound fx and is stable

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #5 - Posted 2009-04-17 13:41:58 »

you really mean it when you said it would take some itme.
Offline Mibran

Senior Newbie





« Reply #6 - Posted 2009-04-19 09:13:31 »

So far, thanks for all helpful answers.

you really mean it when you said it would take some itme.

Assuming that I've spent 24/7 for two months on this topic, sure. But it had other reasons which needed my fully attention. I've been aware of that while posting the first message, that's why I said that. I know what you want to say: "If you are too dumb or do not have enough time, simply don't try to get started in java game programming, you won't arrive anyhwere, TGT. And don't bother us here any longer."

Message delivered and agreed. Ok, Mr. "JGO Ninja", your wish is fulfilled.

Offline CommanderKeith
« Reply #7 - Posted 2009-04-19 09:38:13 »


Assuming that I've spent 24/7 for two months on this topic, sure. But it had other reasons which needed my fully attention. I've been aware of that while posting the first message, that's why I said that. I know what you want to say: "If you are too dumb or do not have enough time, simply don't try to get started in java game programming, you won't arrive anyhwere, TGT. And don't bother us here any longer."

Message delivered and agreed. Ok, Mr. "JGO Ninja", your wish is fulfilled.


Don't worry about that, I'm sure heckboy didn't mean it in that way. He just likes to comment on everything. You should ignore his remark. Judging by how many spelling mistakes he makes, I'm not even sure if he even reads his own posts.

Good luck with sorting out this weird latency problem.

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #8 - Posted 2009-04-19 14:27:08 »

Wow your offensive.

I did not mean it like that!

Just imagine that I hadn't removed my extremely sarcastic post Wink.

P.S: There is that good enough grammar for you?
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #9 - Posted 2009-04-19 14:28:44 »

sry pressed quote instead of edit Tongue
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SimonH
« Reply #10 - Posted 2009-04-19 15:29:48 »

EgonOlson gave me some code which I used in Bloodridge and seems to work really well. If Bloodridge plays OK on the problem machine then I'm sure Egon wouldn't mind if I gave you the code.

People make games and games make people
Offline Mibran

Senior Newbie





« Reply #11 - Posted 2009-04-20 17:46:29 »

Thanks for all the advocacy, looks like I have found the right forum after all.  Grin

I did not mean it like that! Just imagine that I hadn't removed my extremely sarcastic post Wink.

Ok, lets put some cooldown and peace here. You simply caught me on the wrong foot as I was already angry having so little time for this hobby.
Was like throwing oil on the fire. Sorry if I sounded like a hothead here...  Wink

Meantime, I managed to get the game online: http://www.mibran-games.de/.
Still have to implement loading screen and the ufo. Code is ugly, nearly no OOP right now, simply 'hack & forget' spaghetti.
And there is a tiny bug when aliens change direction sometimes. But hey, first running game, helpful comments welcome  Wink
Has anybody bad experiences by violating copyrights of real classic arcade games (being some decades old)?

If Bloodridge plays OK on the problem machine then I'm sure Egon wouldn't mind if I gave you the code.

Thanks very much, same issue with it.
But even Flash plays sounds laggy. I really think now that it must be an OS problem...

cu
Michael

Offline h3ckboy

JGO Coder


Medals: 5



« Reply #12 - Posted 2009-04-20 17:48:55 »

Quote
Has anybody bad experiences by violating copyrights of real classic arcade games (being some decades old)?

well if it is over 100 years old all copyrights go void, but I dont think it is that old Wink.

I think you game is pace invaders from what I gather.

if they will get mad, then every programmer ever well get busted Wink
Offline h3ckboy

JGO Coder


Medals: 5



« Reply #13 - Posted 2009-04-20 17:56:27 »

i tried the game.

woah! I felt like I was at an arcade machine, very good clone. It had a old fasion kinda feel. Most space invaders today would have fancy graphics, but I liked yours Smiley
Offline SimonH
« Reply #14 - Posted 2009-04-20 18:15:45 »

Thanks for all the advocacy, looks like I have found the right forum after all.  Grin
NP (every 100 acre wood has it's tiggers!  Wink)

But even Flash plays sounds laggy. I really think now that it must be an OS problem...
Sounds like it...

Meantime, I managed to get the game online
Unoriginal but solid - a pretty polished first go at gaming. Given that you've credited them I doubt that Taito will sue you unless you start generating revenue from the game.

People make games and games make people
Offline DzzD
« Reply #15 - Posted 2009-04-20 19:16:40 »

Thanks for all the advocacy, looks like I have found the right forum after all.  Grin

Ok, lets put some cooldown and peace here. You simply caught me on the wrong foot as I was already angry having so little time for this hobby.
Was like throwing oil on the fire. Sorry if I sounded like a hothead here...  Wink

Meantime, I managed to get the game online: http://www.mibran-games.de/.
Still have to implement loading screen and the ufo. Code is ugly, nearly no OOP right now, simply 'hack & forget' spaghetti.
And there is a tiny bug when aliens change direction sometimes. But hey, first running game, helpful comments welcome  Wink
Has anybody bad experiences by violating copyrights of real classic arcade games (being some decades old)?

Thanks very much, same issue with it.
But even Flash plays sounds laggy. I really think now that it must be an OS problem...

cu
Michael

really for the sound & game kind you are targetting just use
1  
2  
3  
AudioCilip sound=getAudioClip("yoursound.au");
sound.play(); //this is no requiered but for the first sound play you will do it avoid lag by preloading
sound.stop();


then do sound.play() when you want to play that sound, this is the most stable way and for the kind of sound you use it will work nice for everyone

Offline Hansdampf

Senior Duke


Projects: 3


too offending?


« Reply #16 - Posted 2009-04-20 20:16:47 »

The Java console tells me that you are loading the sound each time it is played... maybe that is not sooo good.

lots of sillystupid games: http://www.emaggame.com
Offline Mibran

Senior Newbie





« Reply #17 - Posted 2009-04-25 15:05:05 »

Hi there,

thanks to h3ckboy,SimonH,DzzD,Hansdampf for comments and help!
Went back to simple usage of AudioClip as recommended. Having implemented the ufo and speeded up gameplay meanwhile.

Although simply copying these classics isn't a creative masterpiece, it simply helps getting me started with simple concepts.
Already have some ideas for own games  Wink

Offline DzzD
« Reply #18 - Posted 2009-04-25 15:21:31 »

maybe you can add a nice loading to your game Smiley, it will then look fully polished

http://www.java-gaming.org/topics/3dzzd-applet-boot-source-code/20341/view.html

EDIT: ouch... my ears !
you must hande when Applet is destroyed to stop all sound... leaving when there is the Red alien make the sound continue to play even when  game web page closed and that's really annoying

Offline Mibran

Senior Newbie





« Reply #19 - Posted 2009-05-08 18:03:45 »

EDIT: ouch... my ears ! you must hande when Applet is destroyed to stop all sound... leaving when there is the Red alien make the sound continue to play even when  game web page closed and that's really annoying

Ups, of course  Undecided My wife discovered the problem a few minutes ago when I proudly presented the new page look to her.  Grin Ok, fixed that.

I noticed another interesting thing: The sound in Ubuntu plays *ways* better if I use Applet.newAudioClip() instead of the JApplet-Method getAudioClip(). The latency problems gets better, the sounds are all preloaded before first play, the sound stutters less.

Thanks for the link to "Applet Boot", will add this tomorrow!

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.

TehJavaDev (30 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (39 views)
2014-10-26 19:37:11

Luminem (21 views)
2014-10-26 10:17:50

Luminem (25 views)
2014-10-26 10:14:04

theagentd (31 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (57 views)
2014-10-16 15:22:06

Norakomi (46 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
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!