1

我正在从麦克风录制音频,但是当我尝试使用 WaveEncoder(或任何其他编码器 - 我尝试了几个)将这些原始数据编码为 wav 格式时,它生成的语音太快(每秒采样率更高),频率为 44100Hz。这是编码的代码:

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;
} 

下面是我在 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);
    }
}

只有当我将速率降低到 22050 时,我才能实现正常语音,但我希望它是 44100 以供以后处理。有什么建议么?

4

1 回答 1

0

试试这个。它会帮助你

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);
    }
}
于 2012-09-17T10:14:02.060 回答