原始数据为WAV格式 - 调整率44100

时间:2012-09-17 06:37:53

标签: actionscript-3 audio

我正在录制来自Microphone的音频,但是当我尝试使用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中初始化Microphone的方法:

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以便以后处理我会做。有什么建议吗?

1 个答案:

答案 0 :(得分: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);
    }
}