gpt4 book ai didi

actionscript-3 - 麦克风数据采样

转载 作者:行者123 更新时间:2023-12-03 02:01:11 25 4
gpt4 key购买 nike

我在将麦克风数据采样转换为16位PCM有符号整数时遇到问题。

我拥有的应用程序在带有 ActionScript 3的Adobe AIR中,但是我具有使用Web Audio API的服务演示代码,并且该代码说明:

/**
* Creates a Blob type: 'audio/l16' with the
* chunk coming from the microphone.
*/
var exportDataBuffer = function(buffer, bufferSize) {
var pcmEncodedBuffer = null,
dataView = null,
index = 0,
volume = 0x7FFF; //range from 0 to 0x7FFF to control the volume

pcmEncodedBuffer = new ArrayBuffer(bufferSize * 2);
dataView = new DataView(pcmEncodedBuffer);

/* Explanation for the math: The raw values captured from the Web Audio API are
* in 32-bit Floating Point, between -1 and 1 (per the specification).
* The values for 16-bit PCM range between -32768 and +32767 (16-bit signed integer).
* Multiply to control the volume of the output. We store in little endian.
*/
for (var i = 0; i < buffer.length; i++) {
dataView.setInt16(index, buffer[i] * volume, true);
index += 2;
}

// l16 is the MIME type for 16-bit PCM
return new Blob([dataView], { type: 'audio/l16' });
};

我需要一种以相同方式转换样本的方法。

这是我所拥有的,但似乎没有用:
function micSampleDataHandler(event:SampleDataEvent):void
{

while(event.data.bytesAvailable)
{
var sample:Number = event.data.readFloat();
var integer:int;
sample = sample * 32768 ;
if( sample > 32767 ) sample = 32767;
if( sample < -32768 ) sample = -32768;
integer = int(sample) ;
soundBytes.writeInt(integer);
}

}

任何建议都会帮助我很多,谢谢

编辑:

这是我拥有的WaveEncoder函数。可以用来将样本编码为所需的格式:
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;
}

编辑2:

问题似乎出在:dataview.setInt16(byteOffset,value [,littleEndian])

我如何在as3中执行byteOffset?

最佳答案

得到它了。 writeInt()写32位,而您只需要写16位。请改用writeShort()

soundBytes.writeShort(integer);

关于actionscript-3 - 麦克风数据采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31384259/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com