microsoft / cognitive-services-speech-sdk-js

Microsoft Azure Cognitive Services Speech SDK for JavaScript
Other
267 stars 101 forks source link

continuous recognition can't be restarted after stop #276

Closed clehmeier closed 3 years ago

clehmeier commented 4 years ago

Hello to everybody,

thanks for having this great component!

Here is my little Problem:

  1. If I use startContinuousRecognitionAsync() everything works perfekt.
  2. If I stop the recognition to avoid recognizing a different spoken text it stops recognition as expected
  3. If I want to restart recognition again (startContinuousRecognitionAsync()) nothing happens (also no error message)

Is this an expected behaviour or am I doing something wrong? Or Should I use different method to mute/unmute (I have read the diskussion about it...)?

Here is my code:

public audioConfig = AudioConfig.fromDefaultMicrophoneInput();
public speechConfig;

async sttinitialization() {
            console.log('initializing stt...')
            this.speechConfig = SpeechConfig.fromSubscription(this.authkey, this.servicelocation);
            this.speechConfig.speechRecognitionLanguage = this.language;
            this._recognizer = await new SpeechRecognizer(this.speechConfig, this.audioConfig)
            this._recognizer.recognizing = this._recognizer.recognized = this.receivedrecognition.bind(this)
}

async start() {
    console.log("stt start recording...");
    await this._recognizer.startContinuousRecognitionAsync();
}

async stop() {
    console.log("stt stop recording...");
    await this._recognizer.stopContinuousRecognitionAsync();
}

receivedrecognition(s, message) {
    const reason = ResultReason[message.result.reason];
    if (reason == "RecognizedSpeech") {
      this.value.emit({ text: message.result.text });
      this.recognized.emit({ text: message.result.text });
    }
}

Angular: 9.1.12 microsoft-cognitiveservices-speech-sdk: 1.14.1

Thank you so much for your help! Christian

glharper commented 4 years ago

@clehmeier Currently startContinuousRecognitionAsync and stopContinuousRecognitionAsync are not actually awaitable, so I'm very curious how your code does not throw a syntax error on load. (The current plan is to make those and all other ...Async() functions in the SDK awaitable in v2.0). Currently the correct method signature is startContinuousRecognitionAsync(callback?: () => void, errorCallback?: (error: string) => void) with the same parameters for stopContinuousRecognitionAsync,

BrianMouncer commented 3 years ago

@clehmeier I'm following up on this issue report. do you have some example code that would explain how you are "awaiting" these calls?

Thanks,

glharper commented 3 years ago

@clehmeier there have been a couple of similar issues logged recently that have been fixed by this Pull Request which is now in master, and will be in 1.15 next month.

clehmeier commented 3 years ago

@BrianMouncer awaiting was a code relict of divers tries to get the code running, sorry glharper is absolutely right - startContinuousRecognitionAsync and stopContinuousRecognitionAsync are not awaitable. Sorry for this confusion.

clehmeier commented 3 years ago

@glharper thank you for taking care of this. actually i've got the code running (and of course without await... was an act of desperation :-)), but only by executing the initialization procedure with each (re)start. I think this is not wanted and not recommendet and expensive, but otherwise I can not restart it several times.

  start() {
    // Initialization
    this.audioConfig = sdk.AudioConfig.fromDefaultMicrophoneInput();
    this.speechConfig = sdk.SpeechConfig.fromSubscription(this.authkey, this.servicelocation);
    this.speechConfig.speechRecognitionLanguage = this.language;
    this.recognizer = new sdk.SpeechRecognizer(this.speechConfig, this.audioConfig)
    this.recognizer.recognizing = this.recognizer.recognized = this.receivedrecognition.bind(this)

    this.recognizer.startContinuousRecognitionAsync(
      startRecognizer.bind(this),
      function (err) {
        console.log("stt:" + err);
        startRecognizer.bind(this)
      }.bind(this))

    function startRecognizer() {
      this.started.emit(true);
      this.recognizing = true;
    }
  }

  stop() {
    this.recognizer.stopContinuousRecognitionAsync(
      stopRecognizer.bind(this),
      function (err) {
        console.log("stt:" + err);
        stopRecognizer.bind(this)
      }.bind(this))

    function stopRecognizer() {
      this.ended.emit(true);
      this.recognizing = false;
    }
  }

  receivedrecognition(s, message) {
    const reason = sdk.ResultReason[message.result.reason];
    if (reason == "RecognizedSpeech") {
      this.value.emit({ text: message.result.text });
      this.recognized.emit({ text: message.result.text });
    }
  }

So for me personally you pushed me the right way and I got the code running. Thank you very much!