2

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

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Tr3y
  • 191
  • 4
  • 11
  • You are writing this in JQuery to run in a web-browser? Are you going to control which browser is used and what plugins a user might have installed that can handle playing a wav file? – Mark Aug 13 '11 at 22:56
  • No, I can't control for those variables. – Tr3y Aug 13 '11 at 22:57
  • Then you have a bigger problem, forget the lag, how can I universally play a sound using HTML/javascript. How can I interact with that flash element or HTML5 audio tag, etc... using javascript. – Mark Aug 13 '11 at 23:03
  • Rethinking it...yes I can control for those variables in the limited trial where accuracy is very very important. After that, I'll be opening it up to general use on the web. At that stage accuracy is just "very" important. – Tr3y Aug 13 '11 at 23:08
  • is HTML + Javascript (+ jQuery) a requirement? Are other methods of controlling the sound possible? – rockerest Aug 13 '11 at 23:08
  • @Mark, Wow, really? I assumed that playing a sound was not a big deal in web 2.0 or whatever we're at. Anyway I'm looking for advice on the best cross browser solution as well. (note: I don't care if it works on mobile devices) – Tr3y Aug 13 '11 at 23:11
  • @rockerest, I guess not...but I've already written the entire experiment in javascript / jQuery (as per the recommendation [here](http://stackoverflow.com/questions/6960090/psych-experiment-in-python-w-django-how-to-port-to-interactive-web-app). If linking that up with another method of playing the sound is possible, please advise. I will rewrite the app in another language if I have to but that's a last resort, and whatever I use has to be web based. – Tr3y Aug 13 '11 at 23:12
  • @Tr3y I've never done this, nor do I know how it might be done, but you might look into embedding the sound into an invisible flash element. That way, you can just use Javascript to turn it on or off, rewind it, etc. based on your needs. – rockerest Aug 13 '11 at 23:14
  • I assumed I would be using a jQuery plugin like [this one](http://plugins.jquery.com/project/sound_plugin). – Tr3y Aug 13 '11 at 23:18

3 Answers3

1

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.

Mark
  • 106,305
  • 20
  • 172
  • 230
  • Thanks for your thoughts. I'll give that a shot and post results here. – Tr3y Aug 14 '11 at 00:15
  • Well, this was cake to set up, I'll give it that. And, as mentioned in another stack answer, it's "future proof". I'm going to use it with a flash fallback if the ` – Tr3y Aug 16 '11 at 03:04
0

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.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
0

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

Joseph Marikle
  • 76,418
  • 17
  • 112
  • 129
  • I don't know what I'm using to play sound either :) looking for advice. Probably not HTML5, I need to be strongly cross-browser. – Tr3y Aug 13 '11 at 23:04
  • @Tr3y In that case I have no idea what you would use. :P I never really went in too heavily into sound on web pages. – Joseph Marikle Aug 13 '11 at 23:06
  • I'd think using `(+new Date())` at the start and end and comparing the differences would be more accurate than using `setInterval`. – icktoofay Aug 13 '11 at 23:51