google cloud speech not working in electron package

623 views Asked by At

When I run the application from command prompt using npm start command it works well. It returning the result from speech api.

I am using binaryServer and binaryclient to stream audio to google cloud API.

When I create package for electron application everything works but it not returning the result from speech api.

Here are my code snippe: Package.json

{
  "name": "test",
  "version": "1.0.0",
  "description": "test Web Server",
  "main": "main.js",
  "scripts": {  
    "start": "electron main.js"
  }, 
   "devDependencies": {   
    "electron": "^1.4.12"   
  },
  "dependencies": {    
    "binaryjs": "^0.2.1",
    "connect": "^3.3.4",
    "biased-opener": "^0.2.8",
    "serve-static": "^1.9.1",
    "uaparser": "^0.0.2", 
    "@google-cloud/speech" : "^0.5.0"
  }
}

Here is my main.js

app.on('ready', function () { 
    load_app();

});

 var workerProcess = child_process.spawn('node', __dirname + '/binaryServer.js');

    workerProcess.stdout.on('data', function (data) {
        console.log('stdout: ' + data);
    });

    workerProcess.stderr.on('data', function (data) {   
        console.log('stderr: ' + data);
    });

    workerProcess.on('close', function (code) {

        console.log('child process exited with code ' + code);
    });

    processes.push(workerProcess);

function load_app () { 
    // Launches the browser window
    mainWindow = new BrowserWindow({ width: 1080, height: 1920 });
    // Load just launched server in browser window


    mainWindow.loadURL("http://localhost:" + config.port);

    if (config.devMode) {
        mainWindow.webContents.openDevTools();
    }
    else {
        mainWindow.setFullScreen(true);
    }

}

here is my binary server

var binaryServer = require('binaryjs').BinaryServer,
    https = require('http'),
    connect = require('connect'),
    serveStatic = require('serve-static');
var config = require('./config');

var server = connect()
     .use(serveStatic(__dirname));

var speech = require('@google-cloud/speech')({
    projectId: config.speech.projectId,
    keyFilename: config.speech.keyFilename
});

httpServer = https.createServer(server);
httpServer.timeout = 0;
httpServer.listen(config.port);

var binarySer = binaryServer({ server: httpServer });

console.log("server pid" + process.pid);

binarySer.on('connection', function (client) {
    console.log("new connection...");



    client.on('stream', function (stream, meta) {

        var options = {
            config: {
                encoding: 'LINEAR16',
                sampleRate: meta.sampleRate,
                languageCode: "en-IN"

            },
            singleUtterance: false,
            interimResults: true,
            verbose: true

        };
        // Create a recognize stream
        const recognizeStream = speech.createRecognizeStream(options)
           .on('error', console.error)
          .on('data', function (data) { if (stream.writable && !stream.destroyed) stream.write(data) }); // send data to client

        if (recognizeStream.writable && !recognizeStream.destroyed && stream.readable && !stream.destroyed)
            stream.pipe(recognizeStream);  // pipe audio to cloud speech

    });

    client.on('close', function () {
        console.log("Connection Closed");
    });
});

Thanks for your help

1

There are 1 answers

0
evancohen On

Taking a shot in the dark here (without much familiarity with binaryServer, which realistically could be the issue). I'm also a bit unclear about where the audio stream actually comes from:

Electron packages its own version of V8. When you run npm install it will install (or compile on the fly) the native binaries targeted for the version of V8 that are installed on your machine (local version). When you spawn the child process it uses that same local version.

However, when you package your electron app it will try to spawn the process with Electron's version of V8 and there will be binary incompatibilities.

Put simply [Your version of V8] != [Electron's version of V8]

On to potential solutions