3

I've been trying to setup a node service for streaming live audio to the Google Speech API, but I've hit a problem that I think might be authentication related.

The service is written in node using Express and BinaryServer, I'm receiving data without any problems (and have been able to save it to the local disk, but that section of code isn't in the below example), but when I try to submit to the Google API I don't receive anything back (Although if I remove the keyFileName from the request then I get "Error: Could not load the default credentials" which is fair enough because I'm running outside the GCE)

var express = require("express");
var app = express();
var port = 54180;
var BinaryServer = require('binaryjs').BinaryServer;

var server = BinaryServer({
    port: port
});

app.listen(port, function () {
    console.log('server open on port ' + port);
});

binaryServer = BinaryServer({
    port: 9001
});

binaryServer.on('connection', function (client) {
    console.log('Binary Server connection started');

    client.on('stream', function (stream, meta) {
        console.log('>>>Incoming audio stream');

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

        const request = {
            config: {
                encoding: 'LINEAR16',
                sampleRate: 16000
            },
            singleUtterance: false,
            interimResults: true
        };

        // Create a recognize stream
        const recognizeStream = speech.createRecognizeStream(request)
            .on('error', function (error) {
                console.log('Error');
                console.log(error)
            })
            .on('data', function (data) {
                console.log('Data');
                console.log(data);
            });

        // Send the microphone input to the Speech API
        stream.pipe(recognizeStream);

        stream.on('end', function () {
            fileWriter.end();
            recognizeStream.end();
            console.log('||| Audio stream ended');
        });
    });
});

I'll also admit this is the first time I've tried to re-pipe to another API, so it could be that I've screwed that part up, but the default credentials message makes me think it's piping OK and it's just rejecting my request without returning a reason.

Can anyone spot what I'm getting wrong?

Cheers.

Neil Hill
  • 61
  • 1
  • 1
  • 7
  • 1
    I'm also looking for similar solution. Here is my question https://stackoverflow.com/questions/43505524/nodejs-convert-int16array-binary-buffer-to-linear16-encoded-raw-stream-for-googl – Siva Kumar Apr 19 '17 at 20:33

1 Answers1

-1

See Shiv's question for the answer to this problem

NodeJS Convert Int16Array binary Buffer to LINEAR16 encoded raw stream for Google Speech API

The code answer he gave was

We can write the buffer directly to recognizerStream which created from GoogleSpeech as follows:

const recognizer = getGoogleSpeechStreamRecognizer();

recognizer.write(int16ArrayBuffer)
Neil Hill
  • 61
  • 1
  • 1
  • 7