We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSound,  Music Libraries › Moved latency on AudioSample triggers
Page Index Toggle Pages: 1
Moved latency on AudioSample triggers (Read 774 times)
Moved latency on AudioSample triggers
Nov 12th, 2007, 5:02pm
 
Does anyone know how to get rid of the latency when triggering an AudioSample?  I'm making a game, and trying to trigger a sound effect whenever the player fires his weapon.
Moved latency on AudioSample triggers
Reply #1 - Nov 16th, 2007, 1:58am
 
Hmm, the latency shouldn't be all that bad. I made a real simple sketch that triggers samples with key presses and it feels pretty responsive. Are you sure that the sample you are using doesn't have a fraction of a second of silence at the beginning?
Moved latency on AudioSample triggers
Reply #2 - Nov 16th, 2007, 2:00am
 
I'm sure, I've edited the sound quite a bit.  I've kept trying to fix this, but have yet to solve it.
Moved latency on AudioSample triggers
Reply #3 - Nov 16th, 2007, 11:13am
 
I'd suggest using an AudioSnippet for game samples.
Moved latency on AudioSample triggers
Reply #4 - Nov 16th, 2007, 8:01pm
 
Interesting.  I will definitely check that out.  I hadn't considered them because the Java docs say, "An AudioSnippet is a good choice if all you need to do is play a short sound at some point. If your aim is to repeatedly trigger a sound, you should use an AudioSample instead."

I want to trigger a sound every time the player fires his laser canon, so would I need to call sound.play(), sound.pause(), and sound.rewind() after every shot?  

Thanks!!
Moved latency on AudioSample triggers
Reply #5 - Nov 17th, 2007, 4:44pm
 
Yes, that would be the case. And if the player shot again before the sound finished playing, you wouldn't get it to play a second time. If the sound is really short you might could load it into several AudioSnippets and when an overlap situation happens, simply play one that isn't currently playing. How many you'd need would depend on how long your sample is and how quickly the player can fire.
Moved latency on AudioSample triggers
Reply #6 - Nov 19th, 2007, 12:13am
 
This works great, thank you!! Out of curiosity, why do snippets work better than samples like this?

Also, this brings up a new (related?) problem - I now keep getting a Minim error telling me the I/O stream closed.  What would be causing this?
Moved latency on AudioSample triggers
Reply #7 - Nov 23rd, 2007, 9:02pm
 
AudioSnippets are faster because they use a JavaSound Clip for playing audio. The sound is buffered in memory and no samples are made available to the user. This means that the audio doesn't need to be converted to normalized floats on its way in or out, basically just piping bytes from memory to an audio output.

http://java.sun.com/j2se/1.5.0/docs/api/javax/sound/sampled/Clip.html

I'd have to see some of your code to answer about the Minim error.
Page Index Toggle Pages: 1