Forum Moderators: open

Message Too Old, No Replies

Something more consistent then setInterval?

Trying to build a metronome and setInterval is too inconsistent.

         

eth0real

9:33 am on Feb 20, 2006 (gmt 0)

10+ Year Member



Hi,

I wanted to build a metronome in flash for my website. I realized that it would be pretty easy with the setInterval() function.

I finished coding the project and then i tested it out.
I find that it's inconsistent, very slight, but it definantly is. Sometimes it pauses for like a 10th of a second. For a metronome that is suppose to help people stay consistent, this is unacceptable.

Is there something else in flash that i can use to play a short sound over and over again in specific consistent intervals, and is adjustable?

Or is there someway i can optimize the setInterval code to make it more consistent?

Here is the code if you want to see it:


stop();
stopAllsounds();
// load sounds
snare = new Sound();
snare.attachSound("snare");
tick = new Sound();
tick.attachSound("tick");
kick = new Sound();
kick.attachSound("kick");
// default sound
var strSnd:String = "tick";
theSound = new Sound();
theSound.attachSound(strSnd.toString());
// Default BPM
var numBPM:Number = 88;
var numTime:Number = 1000*(60/numBPM);
var nPlaySoundInterval:Number;
// PLAY BUTTON
btnPlay.onRelease = function():Void {
stopAllSounds();
clearInterval(nPlaySoundInterval);
nPlaySoundInterval = setInterval(playSound, numTime);
};
// STOP BUTTON
btnStop.onPress = function():Void {
stopAllSounds();
clearInterval(nPlaySoundInterval);
};
// INTERVAL FUNCTION
function playSound():Void {
theSound.start(0,1);
}
// Beat Per Minute Adjuster OBJECT LISTENER
form = new Object();
form.change = function(eventObj){
numBPM = eventObj.target.value;
var numTime:Number = 1000*(60/(numBPM));
trace(numTime);
clearInterval(nPlaySoundInterval);
nPlaySoundInterval = setInterval(playSound, numTime);
}
bpm.addEventListener("change", form);
// SOUND TYPE OBJECT LISTENER
form2 = new Object();
form2.click = function(eventObj2){
strSnd = eventObj2.target.selectedData;
theSound.attachSound(strSnd.toString());
clearInterval(nPlaySoundInterval);
nPlaySoundInterval = setInterval(playSound, numTime);
}
radioGroup.addEventListener("click", form2);

Thanks

Richard_N

6:30 pm on Feb 20, 2006 (gmt 0)



Trouble is the movie is reliant upon the connection speed, the available pipe, the end users computer and a lot of other variables outside your control. Sound synching is virtually impossible to do perfectly given present conditions

eth0real

7:32 pm on Feb 20, 2006 (gmt 0)

10+ Year Member



that's what i was beginning to think.

thanks for the quick response.