Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
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  
  Reliable Sound Playing?  (Read 3429 times)
0 Members and 1 Guest are viewing this topic.
Offline Thimbletack

Junior Newbie





« Posted 2012-03-28 02:47:18 »

Hello,
   I am quite new to audio programming with java and was recently trying to create a metronome in java and ran into problems with the sound playback from Clip having a lot of latency and being unreliable. It worked fine at about 100 bpm but once I started pushing 120 it started dropping beats. The program runs fine and in time without the audio playback and just a flashing dot, but as soon as I add the sound it starts slowing down. I just wanted to ask if anyone had any recommendations for either a way to reliably play a repeated sound within the standard java API or if there was another thing I should try using that would work better.
   Thanks,
      Thimbletack
Offline philfrei
« Reply #1 - Posted 2012-03-28 04:00:06 »

Here is an interesting article that discusses some of the challenges of what you want to do, if you are doing it in pure Java.
http://quod.lib.umich.edu/cgi/p/pod/dod-idx?c=icmc;idno=bbp2372.2007.131
"Real-Time, Low Latency Audio Processing in Java"

NoiseFever's "nordOsc" synth (a nearby thread) has very solid rhythm and gives a few hints about how he achieves it near the end of the thread. For myself, it was only when running a mixer I wrote in the background, continually, and feeding it sounds that I've been able to get decent timing. It is a fair bit of work to set something like this up, so if the goal is to just get it done, you might consider latching onto a library that can already do this.

By the way, are you loading the Clip once, then playing/replaying it (best practice) or loading it anew with each playback (common newbie error)? The former will perform better, but probably won't eliminate all the timing problems. Even loading and playing from file (using SourceDataLine) starts the sound more reliably than a loading a playing a Clip, but resetting and restarting an existing Clip should be the best.

There is also the problem that Microsoft OS's having a rather infrequent clock interrupt (once every 15 or 16 msec) which impacts the accuracy of sleep amounts and timers and game loops. Linux & Mac systems seem to have closer to 1msec accuracy. But even so, there are issues with code going in and out of RAM from bytecode, garbage collection and other JVM timing issues, as the article discusses.

Good luck! There will be others who will have libraries to recommend, I'm pretty sure.

"It's after the end of the world! Don't you know that yet?"
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2012-03-28 05:01:59 »

Open a single audio channel, write raw audio into it. Write the metronome tick, silence, another metronome tick, silence, etc, etc.

It will have the accuracy of the sample-rate, so at 48kHz its accuracy will be about 0.0208 millis (!) - GC and swapping won't be an issue either, if you make your native buffers big enough.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2012-03-28 20:06:26 »

Thanks Riven! That boils things down to the essence. I was having trouble explaining to myself why I was getting better accuracy with my own code where I have mixer running and playing clips as a mixer track vs. playing the clips individually. In the first case raw audio (including silence when needed) is being written to a continually playing SourceDataLine.

"It's after the end of the world! Don't you know that yet?"
Offline Thimbletack

Junior Newbie





« Reply #4 - Posted 2012-03-29 04:09:57 »

Thanks for the help guys. I am indeed just calling on the file once then playing it over and over. I will look in to what you've mentioned and get back to you later.

Thanks again,
Thimbletack
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.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (20 views)
2014-12-14 19:50:38

BurntPizza (42 views)
2014-12-09 22:41:13

BurntPizza (76 views)
2014-12-08 04:46:31

JscottyBieshaar (37 views)
2014-12-05 12:39:02

SHC (51 views)
2014-12-03 16:27:13

CopyableCougar4 (49 views)
2014-11-29 21:32:03

toopeicgaming1999 (115 views)
2014-11-26 15:22:04

toopeicgaming1999 (105 views)
2014-11-26 15:20:36

toopeicgaming1999 (31 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26: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
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!