Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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 6256 times)
0 Members and 1 Guest are viewing this topic.
Offline Thimbletack

Junior Newbie

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

   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.
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.;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.

music and music apps:
Offline Riven

« JGO Overlord »

Medals: 1371
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 rankings!
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.

music and music apps:
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,
Pages: [1]
  ignore  |  Print  

Riven (579 views)
2019-09-04 15:33:17

hadezbladez (5501 views)
2018-11-16 13:46:03

hadezbladez (2398 views)
2018-11-16 13:41:33

hadezbladez (5761 views)
2018-11-16 13:35:35

hadezbladez (1220 views)
2018-11-16 13:32:03

EgonOlsen (4660 views)
2018-06-10 19:43:48

EgonOlsen (5681 views)
2018-06-10 19:43:44

EgonOlsen (3196 views)
2018-06-10 19:43:20

DesertCoockie (4094 views)
2018-05-13 18:23:11

nelsongames (5113 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08 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‑
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!