Accurate BPM event listener in AS3

前端 未结 4 755
盖世英雄少女心
盖世英雄少女心 2021-01-03 12:08

I\'m trying to sync animation to music at a specific BPM. I\'ve tried using the Timer but it isn\'t accurate when dealing with small intervals in milliseconds. I did some re

相关标签:
4条回答
  • 2021-01-03 12:49

    Setting the frame rate so that the event interval is a multiple of the frame rate might help (for example, 167ms equals 6 fps; 12, 18, 24 etc. are then also ok).

    If I understood correctly, better solution would be to use the enterframe event. Instead of determining the position of the animation by counting the events, calculate it using elapsed time (getTimer or sound position). This would also make the animation work on slower computers that have lag.

    0 讨论(0)
  • 2021-01-03 12:51

    I was looking through the popforge library's AudioBuffer and tried using one of the approach. That's the create a sync sound. The following is what i did.

    var syncSamples:ByteArray = new ByteArray(); 
    syncSamples.length = (2646000 / _bpm) << 1; /*44100*60=2646000*/
    SoundFactory.fromByteArray(syncSamples, Audio.MONO, Audio.BIT16, Audio.RATE44100, soundInit);
    

    The ms delay is pretty close, eg: at 120 BPM, it's between 509 - 512ms. The question is, am I going in the right direction?

    0 讨论(0)
  • 2021-01-03 12:54

    This is very tricky to get right! There's a small lib called BeatTimer that tries to do this. I haven't tried this code myself, but if it does what it claims it should be exactly what you need.

    0 讨论(0)
  • 2021-01-03 12:56

    You can also use the new Sound API with the SampleDataEvent and basically play your MP3 manually using Sound.extract(). In that case you know the latency up front and can even count up to the sample when your (delayed) event should happen.

    This is what we do in the AudioTool and it works very well.

    0 讨论(0)
提交回复
热议问题