|
| 1 | +const sdk = require('microsoft-cognitiveservices-speech-sdk'); |
| 2 | + |
| 3 | +module.exports = async function (context, req) { |
| 4 | + // Handle CORS preflight requests (OPTIONS) |
| 5 | + if (req.method === 'OPTIONS') { |
| 6 | + context.res = { |
| 7 | + status: 200, |
| 8 | + headers: { |
| 9 | + 'Access-Control-Allow-Origin': '*', // Allow all origins or specify as needed |
| 10 | + 'Access-Control-Allow-Methods': 'GET, POST, OPTIONS', // Allowed methods |
| 11 | + 'Access-Control-Allow-Headers': 'Content-Type, Authorization' // Allowed headers |
| 12 | + }, |
| 13 | + body: "" |
| 14 | + }; |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const speechKey = process.env.AZURE_SPEECH_KEY; |
| 19 | + const speechRegion = process.env.AZURE_SPEECH_REGION; |
| 20 | + |
| 21 | + // Handle missing speech key or region |
| 22 | + if (!speechKey || !speechRegion) { |
| 23 | + context.res = { |
| 24 | + status: 500, |
| 25 | + body: JSON.stringify({ |
| 26 | + error: "Speech key or region is missing." |
| 27 | + }) |
| 28 | + }; |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + const action = req.query.action || (req.body && req.body.action); |
| 33 | + |
| 34 | + if (action === 'start') { |
| 35 | + try { |
| 36 | + // Configure Speech API |
| 37 | + const speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, speechRegion); |
| 38 | + const autoDetect = sdk.AutoDetectSourceLanguageConfig.fromLanguages(["en-US", "es-US", "es-ES", "hi-IN"]); |
| 39 | + const audioConfig = sdk.AudioConfig.fromDefaultMicrophoneInput(); // Correct microphone input method |
| 40 | + |
| 41 | + // Initialize SpeechRecognizer |
| 42 | + const speechRecognizer = sdk.SpeechRecognizer.FromConfig(speechConfig,autoDetect, audioConfig); |
| 43 | + |
| 44 | + |
| 45 | + // Start speech recognition and return result |
| 46 | + const result = await startSpeechRecognition(speechRecognizer); |
| 47 | + context.res = { |
| 48 | + status: 200, |
| 49 | + body: JSON.stringify(result) |
| 50 | + }; |
| 51 | + } catch (error) { |
| 52 | + context.res = { |
| 53 | + status: 500, |
| 54 | + body: JSON.stringify({ |
| 55 | + error: `Error during speech recognition: ${error.message}` |
| 56 | + }) |
| 57 | + }; |
| 58 | + } |
| 59 | + } else if (action === 'stop') { |
| 60 | + try { |
| 61 | + const result = await stopSpeechRecognition(); |
| 62 | + context.res = { |
| 63 | + status: 200, |
| 64 | + body: JSON.stringify(result) |
| 65 | + }; |
| 66 | + } catch (error) { |
| 67 | + context.res = { |
| 68 | + status: 500, |
| 69 | + body: JSON.stringify({ |
| 70 | + error: `Error stopping recognition: ${error.message}` |
| 71 | + }) |
| 72 | + }; |
| 73 | + } |
| 74 | + } else { |
| 75 | + context.res = { |
| 76 | + status: 400, |
| 77 | + body: JSON.stringify({ |
| 78 | + error: 'Invalid action. Use "start" or "stop".' |
| 79 | + }) |
| 80 | + }; |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +async function startSpeechRecognition(speechRecognizer) { |
| 85 | + return new Promise((resolve, reject) => { |
| 86 | + |
| 87 | + this.speechRecognizer.sessionStarted = (s, e) => { |
| 88 | + console.log('Session Started'); |
| 89 | + }; |
| 90 | + |
| 91 | + speechRecognizer.recognizing = (s, e) => { |
| 92 | + console.log('Recognizing: ' + e.result.text); |
| 93 | + }; |
| 94 | + |
| 95 | + speechRecognizer.recognizeOnceAsync(); |
| 96 | + speechRecognizer.recognized = (s, e) => { |
| 97 | + console.log('Recognition Result: ' + e.result.text); |
| 98 | + const languageDetectionResult = sdk.AutoDetectSourceLanguageResult.fromResult(e.result); |
| 99 | + if (e.result.reason === sdk.ResultReason.RecognizedSpeech) { |
| 100 | + resolve({ |
| 101 | + text: e.result.text, |
| 102 | + language: languageDetectionResult.language |
| 103 | + }); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + speechRecognizer.canceled = (s, e) => { |
| 108 | + console.log('Recognition canceled: ' + e.errorDetails); |
| 109 | + reject(new Error('Recognition canceled: ' + e.errorDetails)); |
| 110 | + }; |
| 111 | + |
| 112 | + // Start continuous recognition |
| 113 | + |
| 114 | + }); |
| 115 | +} |
| 116 | + |
| 117 | +async function stopSpeechRecognition() { |
| 118 | + return new Promise((resolve, reject) => { |
| 119 | + // Logic to stop the recognition (e.g., after a timeout or user command) |
| 120 | + try { |
| 121 | + resolve('Recognition stopped.'); |
| 122 | + } catch (error) { |
| 123 | + reject(new Error('Error stopping recognition: ' + error.message)); |
| 124 | + } |
| 125 | + }); |
| 126 | +} |
0 commit comments