I am writing a firefox extension which needs to play a certain PCM stream. The samples are retrieved from a java module over LiveConnect:
Java code:
public class Synthesizer
{
...
public
float[] synthesizeFloats(int[] symbols)
{
// Some code to generate 32bit float PCM samples
...
return floatSamples;
}
...
}
Javascript code:
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.d开发者_如何学编程efaultModulationProperties();
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);
The above code works but very slowly. It appears that converting the java byte array into a Float32Array is quite expensive. The conversion is necessary as one can't pass a java byte array to the mozWriteAudio function.
My questions are:
- Is there a way to do the conversion more efficiently?
- Is there a way to make the java code return a Javascript Float32Array object instead a java object?
- Is there a java implementation that allows playing PCM audio that may be used in a firefox extension? Using that java implementation from withing the javascript code will not necessitate the above conversion.
Any other ideas / directions would be appreciated.
精彩评论