我正在编写一个需要播放某个PCM流的firefox扩展。通过LiveConnect从java模块中检索样本:
Java代码:
public class Synthesizer
{
...
public
float[] synthesizeFloats(int[] symbols)
{
// Some code to generate 32bit float PCM samples
...
return floatSamples;
}
...
}
Javascript代码:
scream: function(samples)
{
var start = 0;
var elapsed = 0;
start = (new Date()).getTime();
var floatSamples = new Float32Array(samples);
elapsed = (new Date()).getTime() - start;
Firebug.Console.log("Converting array (2) - Elapsed time in ms " + elapsed);
var modulationProperties = this.defaultModulationProperties();
var audio = new Audio();
audio.mozSetup(1, modulationProperties.sampleFrequency);
var written = 0;
while (written < floatSamples.length) {
written += audio.mozWriteAudio(floatSamples.subarray(written));
}
},
// Synthesizer class was loaded and instantiaded over LiveConnect
var samples = synthesizer.synthesizeFloats(symbols);
scream(samples);
以上代码的工作原理非常缓慢。似乎将java字节数组转换为Float32Array非常昂贵。转换是必要的,因为无法将java字节数组传递给mozWriteAudio函数。
我的问题是:
任何其他想法/方向将不胜感激。