1

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.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);

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.

smichak
  • 4,716
  • 3
  • 35
  • 47
  • I guess that the answer is "no" on your first two questions. It is unlikely that anything will be faster than `new Float32Array(samples)` and LiveConnect generally isn't the fastest. AFAIK Java code can only communicate in Java objects, it cannot return a proper JavaScript object. – Wladimir Palant Oct 05 '11 at 20:19
  • I am quite new to this extension programming - is there a better alternative for using java code in a firefox extension? – smichak Oct 06 '11 at 04:31
  • Yes, using JavaScript obviously. Which would mean that your extension doesn't need to drag along an extra VM. – Wladimir Palant Oct 06 '11 at 06:02
  • OK - that's a last resort... is it possible to use Java Sound API in a firefox extension? that is, using LiveConnect? – smichak Oct 09 '11 at 21:43
  • I implemented a small wrapper in java for what I need in the Java Sound API. My wrapper object method takes a short[] and my javascript code simply pass it the `samples` variable. No conversion is needed and the performance is satisfactory as no conversions are made in Javascript code. – smichak Oct 10 '11 at 09:14
  • Nice work smichak. Can you please post an answer to the question yourself and then accept that answer so that we can close this question? Also, you need to accept answers to previous questions if they fix your problem. – Zecas May 23 '12 at 15:52

0 Answers0