Search code examples
node.jsspeech-recognitiongoogle-speech-apisoxnodejs-stream

How to perform real-time speech recognition | Google Cloud Speech-to-Text


I'm trying to transcribe audio from my speakers
I'm piping sound from speakers to node.js file (https://askubuntu.com/a/850174)

parec -d alsa_output.pci-0000_00_1b.0.analog-stereo.monitor --rate=16000 --channels=1 | node transcribe.js

This is my transcribe.js

const speech = require('@google-cloud/speech');

const client = new speech.SpeechClient();

const encoding = 'LINEAR16';
const sampleRateHertz = 16000;
const languageCode = 'en-US';

const request = {
    config: {
        encoding: encoding,
        sampleRateHertz: sampleRateHertz,
        languageCode: languageCode,
    },
    interimResults: false, // If you want interim results, set this to true
};

const recognizeStream = client
    .streamingRecognize(request)
    .on('error', console.error)
    .on('data', data => {
        console.log(
            `Transcription: ${data.results[0].alternatives[0].transcript}`
        );
    });

process.stdin.pipe(recognizeStream);

But Google Cloud Speech-to-Text have a limit for streaming recognition in ~1 min. So I have Error "Exceeded maximum allowed stream duration of 65 seconds."

How I can split the stream into chunks with silence as a splitter or into chunks with 30-sec duration?


Solution

  • We can pipe audio to sox utility for splitting it by silence with 0.3s duration and not longer than 55s

    sox -t raw -r 16k -e signed -b 16 -c 1 - ./chunks/output.wav  silence 1 0.3 0.1% 1 0.3 0.1% trim 0 55 : newfile : restart
    

    Now we can watch chunks dir for new files and stream their to the Google Cloud Speech-to-Text API