我正在录制来自麦克风的音频,但是当我尝试使用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,以便以后进行处理。有什么建议么?

最佳答案

试试这个。它会帮助你

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

关于actionscript-3 - 将原始数据转换为WAV格式-调整率44100,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12454201/

10-11 14:34