Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
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 1941 times)
0 Members and 1 Guest are viewing this topic.
Offline Thimbletack

Junior Newbie





« Posted 2012-03-28 04: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 06: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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Online Riven
« League of Dukes »

JGO Overlord


Medals: 438
Projects: 4


Hand over your head.


« Reply #2 - Posted 2012-03-28 07: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 rankings
Projects: Revenge of the Titans, Titan Attacks, Droid Assault, and Ultratron
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline philfrei
« Reply #3 - Posted 2012-03-28 22: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.

"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
Offline Thimbletack

Junior Newbie





« Reply #4 - Posted 2012-03-29 06: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (101 views)
2013-05-17 21:29:12

alaslipknot (109 views)
2013-05-16 21:24:48

gouessej (139 views)
2013-05-16 00:53:38

gouessej (134 views)
2013-05-16 00:17:58

theagentd (146 views)
2013-05-15 15:01:13

theagentd (132 views)
2013-05-15 15:00:54

StreetDoggy (175 views)
2013-05-14 15:56:26

kutucuk (197 views)
2013-05-12 17:10:36

kutucuk (199 views)
2013-05-12 15:36:09

UnluckyDevil (205 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.082 seconds with 21 queries.