Copy link to clipboard
Copied
I tried to build a metronome in Flash AS3 with a rather lousy result because it won't keep the pace which would have been a suitable feature for a metronome. I tried a few different methods to do the sync with actionscript. I even tried to put the sound in the timeline but it didnt work any better of course. Here is the code:
------------
import flash.media.Sound;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;
var soundObject:snare = new snare();
var duration:Number = Number(intervalField.text);
var timerObject:Timer = new Timer(duration,0);
timerObject.addEventListener (TimerEvent.TIMER, stimuli);
runbut.addEventListener (MouseEvent.CLICK, clickHandler);
stopbut.addEventListener (MouseEvent.CLICK, clickHandler);
function clickHandler (event:MouseEvent):void {
switch (event.currentTarget.name) {
case "runbut" :
timerObject.stop ();
duration = Number(intervalField.text);
timerObject.delay = duration;
timerObject.start ();
break;
case "stopbut" :
timerObject.stop ();
break;
}
}
function stimuli (eventObject:TimerEvent):void {
soundObject.play ();
}
------------
I found some great working apps on the net for instance these:
http://www.metronomeonline.com/
Howcome these flashapps work so smoothly while my attempts just stinks? Whats the trick? Is it because I am such a bad bad bad person? Yes Im sure that's the score. Hmm. Must be.
And here are my failures in a bunch of variations if someone would like to listen which I doubt. (Pardon the swedish text there, "otakt" means out of pace and thats the name of this metronome application ha ha ha).
http://www.pellekarlsson.eu/otakt/otaktlinks.html
Thanks
Copy link to clipboard
Copied
please don't cross-post.
pick as3 or as2. if you're going to stay with as3, increase your fps to 50 and retest.
Copy link to clipboard
Copied
Neither Timer nor enter frame are accurate for this kind of tasks. Deviations from expected values are significant and inconsistent - human ear can definitely catch hiccups.
Copy link to clipboard
Copied
Submitted my previous post prematurely by accident.
Basically, you need to use code that will take into account these aforementioned deviation and adjust Timer delay or perform additional calculations to play sound close to the expected time.
Copy link to clipboard
Copied
Try this code. It is not perfect but interval between sound plays is close to a desired one. This example invokes sound every .5 seconds:
import flash.media.Sound;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.events.TimerEvent;
// start time
var stime:int = 0;
// current time
var ctime:int = 0;
// calculated time passed since last tick update
var cumulative:int = 0;
// tick interval
var interval:int = 500;
var sound:Sound = new snare();
var timer:Timer = new Timer(10);
timer.addEventListener(TimerEvent.TIMER, stimuli);
timer.start();
function stimuli(e:TimerEvent):void
{
// get current time
ctime = getTimer();
// add difference
cumulative += ctime - stime;
// reset start time
stime = ctime;
if (cumulative >= interval)
{
trace("cum", cumulative);
sound.play();
// reset cumulative
cumulative = 0;
}
}
Copy link to clipboard
Copied
Thank you both for youre answers. I will check it out.
Well, I checked it and it does not work any better than did my own previous failures.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now