マイクからオーディオを録音していますが、WaveEncoder (または他のエンコーダー - いくつか試しました) を使用してこの生データを wav 形式にエンコードしようとすると、44100Hz で速すぎる (1 秒あたりのサンプル数が多い) 音声が生成されます。エンコーディング用のコードは次のとおりです。
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 にしたいと考えています。助言がありますか?