开发者

Efficiently streaming audio samples retrieved from a java object in Gecko (firefox extension)

开发者 https://www.devze.com 2023-04-11 05:00 出处:网络
I am writing a firefox extension which needs to play a certain PCM stream. The samples are retrieved from a java module over LiveConnect:

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:

  1. Is there a way to do the conversion more efficiently?
  2. Is there a way to make the java code return a Javascript Float32Array object instead a java object?
  3. 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号