i尝试了以下节点。JS代码将语音转换为文本使用
microsoft-cognitiveservices-speech-sdk
。我使用麦克风录制了音频,将其保存为.wav文件,然后通过javascript中的HTTP触发的Azure函数将抄录的文本发送到另一个端点。
Index.js:
const fs = require('fs');
const sdk = require("microsoft-cognitiveservices-speech-sdk");
const axios = require('axios');
const speechKey = "<speechKey>";
const speechRegion = "<speechRegion>";
const speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, speechRegion);
speechConfig.speechRecognitionLanguage = "en-US";
const audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync("kamsp.wav"));
const speechRecognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);
const functionEndpoint = "http://localhost:7071/api/ProcessSpeech";
speechRecognizer.recognized = async (s, e) => {
if (e.result.reason === sdk.ResultReason.RecognizedSpeech) {
console.log(`RECOGNIZED: Text=${e.result.text}`);
try {
await axios.post(functionEndpoint, { text: e.result.text }, { headers: { 'Content-Type': 'application/json' } });
console.log("Transcription sent to function.");
} catch (error) {
console.error("Error sending transcription:", error);
}
}
};
speechRecognizer.sessionStopped = (s, e) => {
speechRecognizer.stopContinuousRecognitionAsync();
};
speechRecognizer.startContinuousRecognitionAsync();
Httptrigger1.js:
const { app } = require('@azure/functions');
let latestTranscription = "";
app.http('processSpeech', {
methods: ['GET', 'POST'],
authLevel: 'anonymous',
handler: async (request, context) => {
context.log("Speech-to-text function triggered");
if (request.method === "POST") {
try {
const requestBody = await request.json();
const text = requestBody.text;
if (text) {
latestTranscription = text;
context.log(`Received transcription: ${text}`);
return { body: "Transcription received", status: 200 };
}
return { body: "No transcription received", status: 400 };
} catch (error) {
context.log(`Error: ${error}`);
return { body: "Error processing transcription", status: 500 };
}
} else if (request.method === "GET") {
return { body: `Latest Transcription: ${latestTranscription}`, status: 200 };
}
}
});
NODEJS输出:
HPPT触发功能输出: