1
votes

I'm recording audio from Microphone but when I try to encode this raw data to wav format using WaveEncoder (or any other encoder - I tried several) it generates too fast (with higher sample per sec) speech with 44100Hz. Here's a code for encoding:

private static const RIFF:String = "RIFF";  
private static const WAVE:String = "WAVE";  
private static const FMT:String = "fmt ";   
private static const DATA:String = "data";  

public function encode( samples:ByteArray, channels:int=2, bits:int=16, rate:int=44100 ):ByteArray
{
    var data:ByteArray = create( samples );

    _bytes.length = 0;
    _bytes.endian = Endian.LITTLE_ENDIAN;

    _bytes.writeUTFBytes( WaveEncoder.RIFF );
    _bytes.writeInt( uint( data.length + 44 ) );
    _bytes.writeUTFBytes( WaveEncoder.WAVE );
    _bytes.writeUTFBytes( WaveEncoder.FMT );
    _bytes.writeInt( uint( 16 ) );
    _bytes.writeShort( uint( 1 ) );
    _bytes.writeShort( channels );
    _bytes.writeInt( rate );
    _bytes.writeInt( uint( rate * channels * ( bits >> 3 ) ) );
    _bytes.writeShort( uint( channels * ( bits >> 3 ) ) );
    _bytes.writeShort( bits );
    _bytes.writeUTFBytes( WaveEncoder.DATA );
    _bytes.writeInt( data.length );
    _bytes.writeBytes( data );
    _bytes.position = 0;

    return _bytes;
} 

And here's how I initialize Microphone in ActionScript:

soundClip = new ByteArray();
microphone = Microphone.getMicrophone();

microphone.rate = 44;
microphone.gain = 100;
microphone.addEventListener(SampleDataEvent.SAMPLE_DATA, microphone_sampleDataHandler);


protected function microphone_sampleDataHandler(event:SampleDataEvent):void
{
    level.width = microphone.activityLevel * 3;
    level.height = microphone.activityLevel * 3;

    while (event.data.bytesAvailable)
    {
        var sample:Number = event.data.readFloat();
        soundClip.writeFloat(sample);
    }
}

I can achieve normal speech only when I decrease rate to 22050 but I want it to be 44100 for later processing I'll do. Any suggestions?

1
The mic sound is mono but you are encoding the sound as stereo(for two channels). How many times you are pushing the samples in the microphone_sampleDataHandler function? If possible add that code.Moorthy
I updated the post and included microphone_sampleDataHandler functionZaur Guliyev

1 Answers

0
votes

Try this. It will helps you

protected function microphone_sampleDataHandler(event:SampleDataEvent):void
{
    level.width = microphone.activityLevel * 3;
    level.height = microphone.activityLevel * 3;

    while (event.data.bytesAvailable)
    {
        var sample:Number = event.data.readFloat();
        soundClip.writeFloat(sample);
        soundClip.writeFloat(sample);
    }
}