Audio Timer

One major issue with dealing with sound is having a rock solid timer available. Coming from an Actionscript background, it appears that Corona is in the same situation that Flash was prior to AS3, where it was difficult to synchronize audio events due to timers based on frame rates and/or polling the system clock.

One solution (in the flash world) is to read bytes from a silent audio resource (file). Then fire events based on the "position".

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var _bpm = 120;
var _bpmTrigger = (60 / _bpm ) * 1000;
 
var _sound = new Sound("10_seconds_of_silence.wav");
var silenceBA:ByteArray = new ByteArray(); 
var silenceBA_len:Number = Math.floor ((_sound.length/1000)*44100);
_sound.extract (silenceBA, silenceBA_len);
silenceBA.position = 0;
 
function fSampleData(event:SampleDataEvent): void {
        for(i = 0; i<NUM_SAMPLES; i++){
                silenceBA.readFloat();
                silenceBA.readFloat();
                
                // 10,000 because our WAV is a known 10 second value
                ms = 10000 * (silenceBA.position/silenceBA_len);
                if(ms >= _bpmTrigger){
                        silenceBA.position = 0;
                        fireEvent();
                }
        }
}
 
function fireEvent();
        // do something
}
                
_sound.addEventListener( SampleDataEvent.SAMPLE_DATA, fSampleData );
views:1635 update:2011/9/18 14:43:09
corona forums © 2003-2011