0

I'm building a sample speech-to-text using microsoft-cognitiveservices-speech-sdk package. I received output text from recognizing or recognized only if I execute my sample on Cloud shell, Codesandbox or my home PC but I can't get output text if running this sample in my company (corporate environment).

Actual both functions recognizing or recognized did not trigger after the audio buffer is written so I can't get output text or error there.

Bellow is my code:

import * as sdk from "microsoft-cognitiveservices-speech-sdk";
import fs from "fs";

class SpeechRecognizer {
    static settings: any = {
        subscriptionKey: "subscriptionKey",
        serviceRegion: "serviceRegion", // e.g., "westus"
        language: "en-US",
        // Replace with the full path to a wav file you want to recognize or overwrite.
        filename: "sample.wav", // 16000 Hz, Mono
    };
    static format = sdk.AudioStreamFormat.getWaveFormatPCM(8000, 16, 2); 
    static pushStream = sdk.AudioInputStream.createPushStream(SpeechRecognizer.format);
    static audioConfig = sdk.AudioConfig.fromStreamInput(SpeechRecognizer.pushStream);

    private speechConfig: any = null;
    private speechClient: any = null;

    constructor() {
        this.speechConfig = sdk.SpeechConfig.fromSubscription(
            SpeechRecognizer.settings.subscriptionKey,
            SpeechRecognizer.settings.serviceRegion
        );
        this.speechConfig.speechRecognitionLanguage = SpeechRecognizer.settings.language;
        this.speechConfig.setProperty(
            sdk.PropertyId.SpeechServiceConnection_InitialSilenceTimeoutMs,
            "10000"
        ); // 10000ms
        this.speechClient = new sdk.SpeechRecognizer(this.speechConfig, SpeechRecognizer.audioConfig);
        this.speechClient.startContinuousRecognitionAsync(
            () => {
                console.log("Listenning...");
            },
            (err: any) => {
                console.log(err);
            }
        );
    }

    start() {
        // @ts-ignore
        this.speechClient.recognizing = function (s, e) {
            var str = "(recognizing) Reason: " + sdk.ResultReason[e.result.reason] + " Text: " + e.result.text;
            console.log(str);
        };
    };

    onData() {
        fs.createReadStream(SpeechRecognizer.settings.filename)
            .on("data", function (arrayBuffer: any) {
                SpeechRecognizer.pushStream.write(arrayBuffer.slice());
            })
            .on("close", function () {
            });
    }

};

const test = new SpeechRecognizer();
test.start();
test.onData();

Any suggestion? Have a nice day!

Duy Hoang Nguyen
  • 85
  • 1
  • 1
  • 6

0 Answers0