问题描述
有没有办法将内存中的原始数据作为样本添加到Web Audio Api?我想添加一个Int8Array(或Int16Array)作为缓冲区,缓冲区只有样本,没有格式为WAV或MP3。我已经尝试过audioContext.createBuffer,但没有成功。
Is there a way to add raw data from memory as a sample to the Web Audio Api? I would like to add a Int8Array (or Int16Array) as a buffer, the buffer just have the samples and no format as WAV or MP3. I have tried the audioContext.createBuffer and such without succes.
这样的事情:
var buffer = audioContext.createBuffer(1,8192, 22000);
var intArray = new Int8Array(....);
// -- fill intarray
buffer.buffer = intArray;
...
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
如果不可能,那么有一种与contetx.decodeAudio()兼容的声音格式很容易在内存中模仿?即只是标题或其他东西。
If that is not possible is there a sound format compatible with contetx.decodeAudio() which is easy to "emulate" in memory? Ie just a header or something.
推荐答案
这既不是非常直观,也没有明确记录在规范中,也不是很容易在网上找到:
It is neither very intuitive, nor explicitly documented in the specs and also not really easy to find on the net:
但是,您只需修改 getChannelData(< idx of)返回的
。 Float32Array
任何缓冲区上的频道>)
but, you can simply modify the Float32Array
which is returned by getChannelData(<idx of channel>)
on any buffer.
不要忘记将样本缩放到范围[-1,1]
don't forget to scale your samples to the range [-1, 1]
对于您的示例,您可以将intArray中的值缩放并复制到通过调用获得的floatArray:
for your example you would scale and copy the values in the intArray to the floatArray obtained by calling:
floatArray = buffer.getChannelData(0)
这篇关于可以从内存创建一个AudioBuffer,即Int8Array的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!