accurate sound loop timing

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
mslamanna
Posts: 2
Joined: Fri Feb 27, 2009 5:36 pm

accurate sound loop timing

Post by mslamanna » Fri Feb 27, 2009 7:07 pm

Hi, I've been using Enterprise to build a standalone app that plays a small sound file repeatedly at regular intervals while a timer counts down. I have the code all worked out for the timing but I'm having a problem with the accuracy of the intervals between the sounds.

The timer keeps the time correctly, so that's not the problem, but the sounds sometimes come at irregular intervals. And very often the right number of repetitions are not accomplished in the time, for instance, if I have it set to repeat 20 times per minute, I might get 19 or 21 repetitions. Anyone have any suggestions or ideas?

I'm thinking that the problem might be related to how the app uses the computer's resources because the problem is more or less severe depending on what computer is running it (really bad on a machine running Vista that can already barely handle Vista). Is there any way of reserving a certain amount of the computer's resources just for this app while it is running?

Any other ideas or explanations?

Thanks!

SparkOut
Posts: 2949
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Fri Feb 27, 2009 8:56 pm

I don't know how to get "super accurate" with timings because you're always reliant on the computer to keep an accurate clock, which it doesn't.

I guess you use a "send in time" construct for your timing loop or something like that. Any variation in the throughput will be compounded over the life of the cycle.

As a "top of the head" thought - this isn't necessarily any more "accurate" but at least it will always stay within a certain margin of error as synched with the computer's onboard clock, and therefore not creep wildly out of true.

Card script:

Code: Select all

global gRunFlag, gTock, gElapsed

on timeThis pInterval
  local tTick
  if gRunFlag then
    put the seconds into tTick
    add tTick - gTock to gElapsed
    if gElapsed >= pInterval then
       beep
       put zero into gElapsed
    end if
    put tTick into gTock
    send "timeThis 3" to me in 200 milliseconds
  end if
end timeThis
Button script (or other initialiser)

Code: Select all

global gRunFlag, gElapsed

on mouseUp
   put not gRunFlag into gRunFlag
   put 0 into gElapsed
   send "timeThis 3" to this card
end mouseUp
You can play with the intervals and/or switch to milliseconds, but it might help keep a more "constant" rhythm.

mslamanna
Posts: 2
Joined: Fri Feb 27, 2009 5:36 pm

Post by mslamanna » Tue Mar 03, 2009 7:42 pm

Thanks for your help, Spark!

I've been using a similar construct but you got me thinking about how I could streamline parts of the script. I think I got into trouble trying to have the program do too many other actions between the sound repetitions. I might have it worked out now, just have to get my hands on one of those slower Vista machines to test out the changes.


Thanks again.

Post Reply