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); } |
|
|
|
|
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); if (!mixerInfo.getVendor().contains("Sun")){ 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.
|
|
|
|
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!
|
|
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...  Any hints? JOAL? cu Michael
|
|
|
|
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
|
|
|
|
h3ckboy
|
 |
«
Reply #5 - Posted
2009-04-17 13:41:58 » |
|
you really mean it when you said it would take some itme.
|
|
|
|
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.
|
|
|
|
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.
|
|
|
|
h3ckboy
|
 |
«
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  . P.S: There is that good enough grammar for you?
|
|
|
|
h3ckboy
|
 |
«
Reply #9 - Posted
2009-04-19 14:28:44 » |
|
sry pressed quote instead of edit 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
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
|
|
|
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.  I did not mean it like that! Just imagine that I hadn't removed my extremely sarcastic post  . 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...  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  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
|
|
|
|
h3ckboy
|
 |
«
Reply #12 - Posted
2009-04-20 17:48:55 » |
|
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  . I think you game is pace invaders from what I gather. if they will get mad, then every programmer ever well get busted 
|
|
|
|
h3ckboy
|
 |
«
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 
|
|
|
|
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.  NP (every 100 acre wood has it's tiggers!  ) 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
|
|
|
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.  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...  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  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(); 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
|
|
|
|
Hansdampf
|
 |
«
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.
|
|
|
|
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 
|
|
|
|
DzzD
|
 |
«
Reply #18 - Posted
2009-04-25 15:21:31 » |
|
maybe you can add a nice loading to your game  , it will then look fully polished http://www.java-gaming.org/topics/3dzzd-applet-boot-source-code/20341/view.htmlEDIT: 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
|
|
|
|
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  My wife discovered the problem a few minutes ago when I proudly presented the new page look to her.  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!
|
|
|
|
|