开发者

How can I play a sound with absolute minimal or *quantified* lag?

开发者 https://www.devze.com 2023-03-27 18:35 出处:网络
I\'m writing a psychology app in jQuery.Part of my project requires measurement of the reaction time of a user to a sound (user presses a key).Thus, I need to play a sound file with the smallest possi

I'm writing a psychology app in jQuery. Part of my project requires measurement of the reaction time of a user to a sound (user presses a key). Thus, I need to play a sound file with the smallest possible lag between when I call (& timestamp) the sound file and when it actually starts playing. Most sound plugins don't detail how they handle lag. Please advise me on the best method!

Only answers rooted in solid CS (not "this plugin sounds fast") are useful to me. At the very least, I need to know what the possible range of lag is for the method I'm using (for calculating a confidence interval).

An alternative and actually pref开发者_如何学Pythonerable solution would be a method of quantifying the lag. In that case the length of lag would be unimportant, because I can easily correct for it.

I don't know what sound files I'll be using, but I think they'll be very small. Two .wav files @ 100kb each is probably a safe estimate.


After the comment discussion, I'm going to recommend that you use the HTML5 audio tag to play/control the sound. [The JQuery plugin you link to uses, the "embed" tag to play the sound (this is going to call a plugin on the client computer which you won't have any control over).]

With HTML5, you'll lose some older/mobile broswers but that is always the crux with web programming.

For a tutorial on controlling the "audio" tag, see here. Make sure you preload the audio.

I think the only other option besides HTML5 is to use flash to play the sound. You can pretty easily interact flash and javascript.


First thing I would do is cache the sound file (idk what you plan on using for sound. HTML5 audio tag?) and then when you call the play function you can run something like

var counter = 0;
var timer = setInterval(function(){counter++}, 1);

in the user response call

clearInterval(timer);

and timer will be the milliseconds for response (minus the time it takes to start playing).


I run into the very same problem: I tried buffering, audio events even AnalyserNode, but there seem to be a hardware lag between the sound data is parsed and when the sound is produced: in my testing it ranges between 50-200ms.

The lag however can be quantified by capturing sound:

  1. play the sound (whether is buffered or not) and start the timer
  2. stop the timer when sound capturing catches something (watch the sound interference near the mic)

Read more about javascript sound capturing here.

0

精彩评论

暂无评论...
验证码 换一张
取 消