I am recording audio input from microphone using the following function:
private function handleSampleData(p_sampleEvent:SampleDataEvent) :void
{
var data :Number;
while (p_sampleEvent.data.bytesAvailable)
{
data = p_sampleEvent.data.readFloat();
_buffer.writeFloat(data);
_buffer.writeFloat(data);
}
}
This seems to work. After I have finished recording, I copy the recorded data to another buffer like this:
_buffer.position = 0;
_lastRecord = new ByteArray();
while (_buffer.bytesAvailable)
{
_lastRecord.writeFloat(_buffer.readFloat());
}
_lastRecord.position = 0;
And after that, I use play() on a newly created sound and feed the _lastRecord buffer to it using this function:
public function handlePlaybackSampleData(p_sampleEvent:SampleDataEvent) :void
{
// Read data until either MAX_SAMPLES or all available samples are reached.
var i:int = 0;
var data :Number = 0;
while( i < 8192 )
{
if( _lastRecord.bytesAvailable )
{
data = _lastRecord.readFloat();
p_sampleEvent.data.writeFloat(data);
i++;
continue;
}
else
{
break;
}
}
}
While it does basically work, the resulting sound that is played is waaaaay too fast. I have taken most of the code from an example in which this works perfectly fine. I see no more vital differences between my code and this one: http://labs.makemachine.net/2011/04/record-visualize-save-microphone-input/
Still, the audio comes out way too fast. If I put even more data into the _buffer by adding more "_buffer.writeFloat(data);", it becomes better, and when I have 12 lines of that, it is the desired speed. But even that only helps when I just bump against my mic. If I actually speak, that does not seem sufficient, either.
But how comes that? And how comes that in the example I took that code from, only 2 lines of that is sufficient? Is there a way to determine how much data I need to write into the buffer while recording?