Azure-Samples / cognitive-services-speech-sdk

Sample code for the Microsoft Cognitive Services Speech SDK
MIT License
2.85k stars 1.84k forks source link

onAudioEnd callback of speechsdk.SpeakerAudioDestination working is not consistent #2021

Closed gv1006 closed 11 months ago

gv1006 commented 1 year ago

Describe the bug OnAudioEnd callback working is not consistent. After text to speech audio is completed, sometimes we're not getting any onAudioEnd callbacks and sometimes we're getting lot of callbacks. Details:

  1. We use speechsdk.SpeechSynthesizer for text to speech synthesis.
  2. For audio Config, we're using player(speechsdk.SpeakerAudioDestination);
  3. And we're registering onAudioEnd callback.

To Reproduce Steps to reproduce the behavior:

  1. Try to do text to speech using synthesizer.speakSsmlAsync.
  2. After text to speech audio is completed, please observe the onPlayerAudioEnd callback logs.

Expected behavior After text to speech audio is completed, we expect only one onPlayerAudioEnd callback.

Actual behaviour Sometimes we're getting only one callback Sometimes we're not getting callbacks at all Sometimes it's getting crashed and we're getting lot of callbacks.

Version of the Cognitive Services Speech SDK 1.30

Platform, Operating System, and Programming Language

Additional context

Code ` const onPlayerAudioStart = function () { console.log('Audio started'); };

const onPlayerAudioEnd = function () { console.log('Audio ended'); closePlayer(); };

export const stopAudioTalkBalk = function () { if (player) { player.pause(); closePlayer(); } };

export const handleTextToSpeech = async (text: string, language: string) => { try { stopAudioTalkBalk(); const { authToken, error, region } = await getTokenOrRefresh(); if (error || !authToken || !region) { console.log('something went wrong when fetching token'); return; } const speechConfig = speechsdk.SpeechConfig.fromAuthorizationToken( authToken, region, ); let speechSynthesisVoiceName = 'en-IN-NeerjaNeural'; const speakerGender = 'fe'; if (language && languageSpeechMapping[language]) { speechSynthesisVoiceName = languageSpeechMapping[language][speakerGender]; }

const ssml = textToSpeechSSML(language, speechSynthesisVoiceName, text);

player = new speechsdk.SpeakerAudioDestination();

player.onAudioStart = onPlayerAudioStart;
player.onAudioEnd = onPlayerAudioEnd;

const adconfig = AudioConfig.fromSpeakerOutput(player);
synthesizer = new speechsdk.SpeechSynthesizer(speechConfig, adconfig);
synthesizer.speakSsmlAsync(
  ssml,
  function (result: any) {
    if (
      result.reason === speechsdk.ResultReason.SynthesizingAudioCompleted
    ) {
      console.log('result', result);
      console.log('sessionId', result.sessionId);
      console.log('synthesis finished.');
    } else {
      console.error(
        'Speech synthesis canceled, ' +
          result.errorDetails +
          '\nDid you set the speech resource key and region values?',
      );
    }
    synthesizer.close();
    synthesizer = null;
  },
  function (err: any) {
    console.trace('err - ' + err);
    synthesizer.close();
    synthesizer = null;
  },
);

} catch (e) { console.log(An error occurred: ${e}); } }; ` Please observe in the following image, we have audio started callback and we don't have audio ended callbacks.

Screenshot 2023-07-22 at 8 25 13 AM

In the following image, we haven't faced any issue and we have both audio started and audio ended callbacks.

Screenshot 2023-07-22 at 8 28 24 AM
github-actions[bot] commented 1 year ago

This item has been open without activity for 19 days. Provide a comment on status and remove "update needed" label.

glharper commented 1 year ago

@gv1006 Thank you for using JS Speech SDK, and writing this issue up. In the sample code above, the method closePlayer does not seem to be defined. Are you seeing this issue when closePlayer is properly defined?

github-actions[bot] commented 12 months ago

This item has been open without activity for 19 days. Provide a comment on status and remove "update needed" label.

glecaros commented 11 months ago

There hasn't been any activity on this item for quite some time after @glharper's question. Closing. If the problem persists, please let us know and either reopen this issue or create a new one.