Copy link to clipboard
Copied
I'm trying to record a sound using the Microphone API and store the mono stream of samples to a file.
Later I'll open the file and play it using dynamic sound API but creating Sound object and regster to SampleEvent and filling the buffer from the recorded samples.
As far as I know the microphone give me mono samples and the output expect stereo so I write each sample twice.
The problem is that on the AIR simulator and Android devices everything works great but on iOS device the sound plays twice as fast.
This is the onSample function:
private function onSampleData(event : SampleDataEvent):void | ||
{ |
// the following lines of code are modifying the sound object and creates a "pitch" effect | |||
var sample:Number; |
var outputLength:int = 0; | |||||
while (outputLength < 2048) { | |||||
// until we have filled up enough output buffer | |||||
if (_micSamplesStream.bytesAvailable < 4) | |||||
{ | |||||
break; | |||||
} |
// read out the left and right channels at this position | ||||
sample = _micSamplesStream.readFloat(); |
// write the samples to our output buffer | ||||
event.data.writeFloat(sample); | ||||
event.data.writeFloat(sample); |
outputLength++; | ||||
} | ||||
} |
Copy link to clipboard
Copied
Check what the default microphone rate is on iOS. You could set it to 44, and that might help.
Copy link to clipboard
Copied
Hi,
Thanks for the advice.
I'm setting it to 44 already.
Ido.
Copy link to clipboard
Copied
One thing, you don’t need to write the data twice. Write it once, then on playback use a single readfloat to write both the left and right channels of the playback sound.
Copy link to clipboard
Copied
Sure, that code I did past here, I read one value from the microphone persisted stream and write it twice to the output buffer.
Copy link to clipboard
Copied
If this is still a problem, be sure to make sure that you've granted the app microphone permissions, which started being required in iOS 7. Otherwise, recorded audio will sound like a "chipmunk" voice when played back.