How do I send transcribed text from speech directly to another endpoint or an azure function from my speech resource?

Abdullah Nadeem 0 Reputation points
2025-02-07T07:56:13.02+00:00

I am using Azure Speech Service resource to transcribe real time audio from my mic using microsoft-cognitiveservices-speech-sdk. I want to send the transcribed text to another endpoint (or to an azure function through which I can send the text to another endpoint) before the recognized event runs on my browser app.

Is there a way to do this? I could not find anything to help me with this in the documentation or in the portal.

Any help is appreciated.

Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,903 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Saideep Anchuri 1,950 Reputation points Microsoft Vendor
    2025-02-07T11:45:31.04+00:00

    Hi Abdullah Nadeem

    You can send recognizing text with SDK only as portal way is not supported from Real time speech detection.  

     Attached Java SDK implementation        

    const sdk = require("microsoft-cognitiveservices-speech-sdk"); 
    const fetch = require("node-fetch");
    const speechSubscriptionKey = "YourSpeechServiceKey"; 
    const serviceRegion = "YourServiceRegion"; 
    const functionUrl = "https://<YourFunctionApp>.azurewebsites.net/api/<FunctionName>";
    const audioConfig = sdk.AudioConfig.fromDefaultMicrophoneInput(); 
    const speechConfig = sdk.SpeechConfig.fromSubscription(speechSubscriptionKey, serviceRegion); 
    const speechRecognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);
    speechRecognizer.recognizing = (s, e) => {     
    console.log(`RECOGNIZING: Text=${e.result.text}`); 
    };
    speechRecognizer.recognized = async (s, e) => {   
      if (e.result.reason === sdk.ResultReason.RecognizedSpeech) {         						     console.log(`RECOGNIZED: Text=${e.result.text}`);         
    await sendToAzureFunction(e.result.text);   
      } else if (e.result.reason === sdk.ResultReason.NoMatch) {         console.log("NOMATCH: Speech could not be recognized.");   
      }
     };
    speechRecognizer.canceled = (s, e) => {   
      console.log(`CANCELED: Reason=${e.reason}`);
    if (e.reason === sdk.CancellationReason.Error) {       
      console.log(`"CANCELED: ErrorCode=${e.errorCode}`);       
      console.log(`"CANCELED: ErrorDetails=${e.errorDetails}`);         console.log("CANCELED: Did you set the speech resource key and region values?");   
      }
    speechRecognizer.stopContinuousRecognitionAsync(); 
    };
    speechRecognizer.sessionStopped = (s, e) => { console.log("\n    Session stopped event."); speechRecognizer.stopContinuousRecognitionAsync(); };
    async function sendToAzureFunction(recognizedText) {    
     try {
       const response = await fetch(functionUrl, { method: "POST",           
      headers: { "Content-Type": "application/json" },          
       body: JSON.stringify({ text: recognizedText })     
        });
       console.log(`Status Code: ${response.status}`);    
     } catch (error) {       
      console.error("Error sending to Azure Function:", error);  
       }
     }
     
     
    
    

    Kindly refer below link: https://learn.microsoft.com/en-us/azure/ai-services/speech-service/how-to-recognize-speech?pivots=programming-language-javascript

    Thank You,

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.