473,748 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can I play a repetitive sound and test response time in JS?

I want to play a repetitive sound, and have a user click on a button each
time it plays (say 5-10 times) and measure how far off he is from the
sound each time.

The only way I know to play a sound in JS is bgsound, and I know how to
loop and pause, so as to do the 1st part. I haven't figured out how to
then test the delay in response. Since some of the responses may also be
*before* the sound plays by some milliseconds, that also compicates it.

My real question is, can this even be done in JS, or should I be in PHP,
ASP, or do you have another suggestion. I'm thinking it needs to run
client-side so there are no network delays, and the results would be
reasonably accurate on different machines. I could easily write it in VB
but I don't want them to have to download anything. I'd just like it to
run from the page.

Any ideas would sure be appreciated.

Larry

Jul 23 '05 #1
5 2131
Jc
You can use (new Date()).getTime () to get the current time as
milliseconds, and compare it to see how many milliseconds have elapsed.

For example:

var iStartTime = (new Date()).getTime ();
//.... some delay here ....
alert("Millisec onds elapsed = "+((new Date()).getTime ()-iStartTime));

You'll probably want to prevent the user from clicking the button
before the sound plays by adding a small (3-8 second) random delay to
the time until the sound plays each time they pre-maturely click. Make
sure there is some visual indication of the fact that you've added a
delay, perhaps by changing the status bar (status = "Wait for the sound
before clicking the button.").

Note that you can use the Math object's "random" method to generate a
random number. For a good IE reference (JScript), see
http://msdn.microsoft.com/library/en...ereference.asp,
click Objects -> Math Object -> random method.

Additionally, you'll want to ensure that you use the onmousedown event
on the button instead of onclick, because onclick does not fire until
the user _releases_ the mouse button, while onmousedown fires when they
depress the mouse button.

Jul 23 '05 #2
In article <10************ **********@k26g 2000oda.googleg roups.com>, "Jc" <go****@weinric hs.com> wrote:
You can use (new Date()).getTime () to get the current time as
milliseconds , and compare it to see how many milliseconds have elapsed.

For example:

var iStartTime = (new Date()).getTime ();
//.... some delay here ....
alert("Millise conds elapsed = "+((new Date()).getTime ()-iStartTime));

You'll probably want to prevent the user from clicking the button
before the sound plays by adding a small (3-8 second) random delay to
the time until the sound plays each time they pre-maturely click. Make
sure there is some visual indication of the fact that you've added a
delay, perhaps by changing the status bar (status = "Wait for the sound
before clicking the button.").

Note that you can use the Math object's "random" method to generate a
random number. For a good IE reference (JScript), see
http://msdn.microsoft.com/library/en...criptlanguager
eference.asp ,
click Objects -> Math Object -> random method.

Additionally , you'll want to ensure that you use the onmousedown event
on the button instead of onclick, because onclick does not fire until
the user _releases_ the mouse button, while onmousedown fires when they
depress the mouse button.

JC,

Thanks, but not what I'm trying to accomplish. I know how to do what you
suggest, but I need something different. The sound has to be "in rhythm"
and I need to be able to find the time *before or after* it that a button
is clicked, done multiple times in a row. Essentially tapping a button to
a metronome and measuring how close you come.

I figured I could use an array with the total times from the 1st sound,
subtracting the normal gap out for each one to get the before & after. I
was also going to use onfocus for the reason you suggested.

So I ask again, can this be done in JS, or am I going to need something
else?

Larry
Jul 23 '05 #3
Larry L wrote:
<snip>
Thanks, but not what I'm trying to accomplish. <snip>

The quality of the question influences the nature of the answers it
attracts. If you don't explain what you are trying to accomplish people
won't know what it is (when I read the question I was left wondering
what the point of attempting this was, always useful information to give
when you don't know how to implement something yourself).

So I ask again, can this be done in JS, or am I
going to need something else?


If you want to synchronise the playing of sounds with monitoring user
input then the normal browser object model is going to be hard work (you
just don't have that much control over when a sound plays (if it ever
does)). I suspect that Flash would be the easiest medium for
implementation. You have to rely on the user having a Flash plug-in but
browsers need a plug-in to play sound anyway and at least with Flash it
will be the same plug-in each time (if maybe different versions).

You probably cannot time anything with Flash to less than the frame
interval, but javascript Date objects are not accurate to less than 10
milliseconds anyway.

Richard.
Jul 23 '05 #4
Jc
As far as recording events (such as when the user clicks the button and
when the master "heart-beat" cue occurs), and processing the timing
details of these events and displaying the results to the user,
javascript is entirely capable of doing this work.

However, I haven't played a whole lot with playing sounds, I know you
can embed Windows Media Player in the page using the <object> tag, and
there are quite a few options you can set. Not sure if it lets you do
things like ask it at what "time index" it is currently at as it plays
the sound, if it did that would likely allow you to achieve enough
accuracy to make this feasible.

And that's the real question here - how to make this accurate. It is
entirely possible to do what you are asking in javascript, but unless
you have some feedback as to exactly when the sound starts, and some
hard-coded information like delay between repititions, it won't be
accurate.

Assuming you're not limited to sound cues only, I would suggest using
visual cues, these can be controlled through DHTML, and would allow
decent accuracy.

A visual cue could be as simple as toggling the visibility of an object
on the screen, or you could make it more complicated and make some sort
of an animation.

Jul 23 '05 #5
In article <10************ **********@f14g 2000cwb.googleg roups.com>, "Jc" <go****@weinric hs.com> wrote:
As far as recording events (such as when the user clicks the button and
when the master "heart-beat" cue occurs), and processing the timing
details of these events and displaying the results to the user,
javascript is entirely capable of doing this work.

However, I haven't played a whole lot with playing sounds, I know you
can embed Windows Media Player in the page using the <object> tag, and
there are quite a few options you can set. Not sure if it lets you do
things like ask it at what "time index" it is currently at as it plays
the sound, if it did that would likely allow you to achieve enough
accuracy to make this feasible.

And that's the real question here - how to make this accurate. It is
entirely possible to do what you are asking in javascript, but unless
you have some feedback as to exactly when the sound starts, and some
hard-coded information like delay between repititions, it won't be
accurate.

Assuming you're not limited to sound cues only, I would suggest using
visual cues, these can be controlled through DHTML, and would allow
decent accuracy.

A visual cue could be as simple as toggling the visibility of an object
on the screen, or you could make it more complicated and make some sort
of an animation.


JC,

Thanks much for the ideas. I actually have it working at an acceptable
level now. I created a sound file of exactly 1 second, and have a "Start"
button that starts playing it continuously as the background sound. Then I
have another button that reads the time it's clicked into an array. When
the user presses a "Stop" button, it then loops through the array
subtracting each element from the previous one to determine the delay and
subtracts 1000 milliseconds each time. This basically gives me the data I
wanted, although the results are only in increments of 16ms. Apparantly
that is a limitation of JS I didn't know about.

You are correct, though that the data would be much richer if I could know
when the sound actualy starts playing, because the sound and the test are
actually independant of each other.

I may try a visual cue, but sound is what is really needed for
the testing here (different parts of your brain processing these items).

I'm still working on it, and very much appreciate your input, especially
your doing what is so rare in newsgroups and actually answering the
real question that was asked!

Larry L
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2565
by: The Road To Utopia | last post by:
Here's one for the trolls...a common jibe from them is setting up audio/video hardware under linux. Ok, true story: at work today, someone asked me if I could tell him why his XP Home would play the video from a DVD but not the audio. He had been searching for an answer for days on support.microsoft.com but found none. I suggested Google and Gateway. Gateway had nothing, but sure enough, search for *XP DVD no sound* on Google and...
1
4299
by: Ron Provost | last post by:
Hello, I'm developing a piece of software to assist illiteraate adults to learn to read. I'm trying to figure out how, if possible, to make audio playback asynchrnous but still controllable. I'm using python 2.4 with pymedia on XP. I started out with the example in the tutorials section of the pymedia website. The pymedia docs imply to me that playback using Output's play() method should already be asynchronous and controllable. I...
1
8006
by: Lam | last post by:
how can I play sound file in a .aspx page written in C#? I try to use the code like the following. But whenI call the play function play("sound.wav", this.SND_ASYNC) my computer give out "be" sound instead playing the file (same code works fine in a windows application written in c# , that mean there is nothing wrong with my sound card.) thanks ----------------------------------------------------------------------------...
1
4641
by: Dieter Vanderelst | last post by:
Hello, I'm having a problem with playing WAV files using Winsound. If I use winsound to play a certain WAV files only the first few seconds of the file get played. I think this comes because these files contain some parts of silence. There winsound seems the stop playing the files (windows media player plays the sounds correctly). Does anybody have an idea about how I could fix this problem? I have
3
10640
by: Jared | last post by:
I'm using the first code sample below to play WAV files stored as embedded resources. For some reason I *occasionally* get scratching and crackling. I'm using a couple WAVs that ship with Windows, and they play fine outside of my app. This code works, but has the random "scratching" problem: private static void PlayResource(string audioRes) { // Get the resource into a stream
9
5608
by: Morris Neuman | last post by:
Im working with VS 2005 and trying to use a Hyperlink field in a datagrid to play a wave file that is not located in the website folders but is in a plain folder on the same machine, windows 2003 server, WMP 10.0 . If I type the full path in an IE address field it plays the file in WMP When I test my Web page ( running the debugger in VS.) The datagrid has a column called "MsgFile" with the full path to the wave file e.g....
10
26202
ADezii
by: ADezii | last post by:
Most Access Users fail to realize that they can significantly enhance their applications by introducing various sounds (*.WAVs) in response to specific events. The only intrinsic sound that Access can generate is the DoCmd.Beep which is hardly worth the time to mention. You can incorporate sound functionality into your Access applications via a single call to a relatively simple API Function. A couple of arguments to the Function dictate where...
26
3902
by: Jake Barnes | last post by:
I did a search on the newsgroup comp.lang.javascript. I was searching for "how to play a sound with Javascript". I'm somewhat suprised that the majority of entries are from the 1990s, and there are almost no posts from the last 3 years: http://groups.google.com/group/comp.lang.javascript/search?group=comp.lang.javascript&q=how+to+play+a+sound+with+Javascript&qt_g=Search+this+group Even after sorting by date, there don't appear any...
1
2358
by: OBAFGKM_RNS | last post by:
In my html, I have an embedded sound wav. I access that sound from my javascript using Play() and Stop(). However, once stopped, the sound will aways resume from where it left off, (making Stop behave more like a Pause). I want it to start from the beginning. The only way I've been able to start the sound from the beginning every time, is to use inner.HTML to reload the embedded wav, thereby reopening the media plugin. There is always a...
0
9376
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8247
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.