views:

41

answers:

1

Below is a snippet from the Adobe Live Docs SampleDataEvent class. It demonstrates how to create an audible sine wave by pushing samples into a ByteArray. The part that I am hung up on is why you need to push the same value into the writeFloat() method twice?

var mySound:Sound = new Sound();
function sineWaveGenerator(event:SampleDataEvent):void 
{
    for ( var c:int=0; c<8192; c++ ) {
        event.data.writeFloat( Math.sin((Number(c+event.position)/Math.PI/2))*0.25 );
        event.data.writeFloat( Math.sin((Number(c+event.position)/Math.PI/2))*0.25 );
    }
}

mySound.addEventListener(SampleDataEvent.SAMPLE_DATA,sineWaveGenerator);
mySound.play();

As a test, I removed one of the calls to writeFloat() and increased the buffer to 16384 samples ( twice the current ). This created an audible gap and click in the the audio, but didn't enlighten me much as to why. Perhaps you can...

Thanks again :)

+1  A: 

It takes two writes because it's stereo. Each channel takes one sample. In this case, the value being written is the same, but if you wanted to pan the sound 100% to one side, for example, you could write the value with the first (or the second) writeFloat, and pass 0 to the other call.

Juan Pablo Califano
Thanks, for the reply. So why is it that if I doubled the buffer size and only called writeFloat() once, did I experience the same problem?
jeremynealbrown
I think it's because in that case you are not writting the same value twice, since the value you pass to writeFloat changes depending on your loop counter. You'd be writting the 2 "snapshots" of the sine wave into the same time spot (one on the right channel and the other in the left)
Juan Pablo Califano
got it... makes complete sense now, thankyou!
jeremynealbrown