0

I'm looking to create a drum machine in ActionScript 3 (as an Adobe AIR Android app), which will keep to a user defined tempo (BPM).

I am struggling to find a way to keep the project in time, I have, at the moment, made it so that 5 different sounds are represented in rows of 8 squares, and the user can click each square to choose when to play that sound (hope this makes sense).

At the moment I am using Timer to keep the project in time, which is very laggy and inconsistent.

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
Tom Haddad
  • 377
  • 2
  • 5
  • 12
  • sounds to me like there is a design flaw. Timer only lags when the call back is bloated. – The_asMan Mar 18 '13 at 17:27
  • Could you elaborate on that please? Timer basically doesn't seem to be very accurate, it leaves different sized gaps between each 'beat' – Tom Haddad Mar 18 '13 at 17:44
  • I recently built something just like this. I utilized the timer class too, and it was OKAY but not great. Playing sounds is a very costly operation, and that combined with minimal animations made my movie sometimes "skip" beats or add minor delays. I couldn't find a way to perfect it. Make sure to debug with getTimer() to measure exactly delays. – lostPixels Mar 18 '13 at 19:41
  • @TomHaddad Timer is not exact because of "lag" from the local machine. For example run it in a browser then shrink and then maximize the browser it is running in and you see it "lag" anything done in a timer event should be optimized. – The_asMan Mar 18 '13 at 20:35
  • Okay cheers for your help, much appreciated! – Tom Haddad Mar 19 '13 at 13:17

1 Answers1

2

using timer is a bad idea for this, there I said it...

The issue is that the timer has a drift and fires several milliseconds later. Try a simple test where you have a timer that executes every 500ms, and then compare the getTimer() count. What I have found in my experiments that the timer is continually off and it looks like it doesn't self correct. I've tried using a self-correcting timer, that changes the firing time based on the getTimer() difference since last run, but it's still not reliable. and anytime your processor's load picks up, the timer will be off anyway.

The correct way of dealing with this is to use byteArray data as a source for the sound. Based on the calculation of sampling resolution you can populate the stream with the data in advance, and the sound will play on time, pretty much guaranteed. I haven't gone as far as to create something that does this myself. But there are several libraries that you can utilize that can help you with this.

My top two decremented libraries are SiON and tonfall

you can see a sample of SiON here http://wonderfl.net/c/qf4b
and tonfall example at http://tonematrix.audiotool.com/

While I haven't tried them on android, I think either should work

Daniel
  • 34,125
  • 17
  • 102
  • 150