gpt4 book ai didi

node.js - Promise 和 Azure 语音转文本

转载 作者:行者123 更新时间:2023-12-03 02:00:01 25 4
gpt4 key购买 nike

我希望以下代码输出inner() 响应的实际值而不是 promise 。我认为 .then() 会等待输出,直到 promise 得到解决。我应该改变什么?截至目前,输出将是未定义的,而不是 YourAudioFile.wav 的内容。我知道代码会以其他方式工作,因为如果我在inner()内console.log(response),响应的实际值将被打印到终端,而不是未定义或promise<>

const fs = require('fs');
const sdk = require("microsoft-cognitiveservices-speech-sdk");
const speechConfig = sdk.SpeechConfig.fromSubscription(process.env.SPEECH_KEY, process.env.SPEECH_REGION);
speechConfig.speechRecognitionLanguage = "he-IL";
const audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync("YourAudioFile.wav"));
const speechRecognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);

async function inner() {
let response = ""

speechRecognizer.startContinuousRecognitionAsync();

speechRecognizer.recognized = (s, e) => {
if (e.result.reason == sdk.ResultReason.RecognizedSpeech) {
//console.log(`RECOGNIZED: Text=${e.result.text}`);
response += (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) => {
speechRecognizer.stopContinuousRecognitionAsync();
return response
};

}

inner().then((msg) => {
console.log(msg)
}).catch((err) => {
console.log(err)
})

最佳答案

我对您的代码进行了一些更改,并获得了带有输入语音的文本输出。

代码:

const fs = require('fs');
const sdk = require("microsoft-cognitiveservices-speech-sdk");

const speechKey = "<speech-key>";
const speechRegion = "<speech-region>";

const speechConfig = sdk.SpeechConfig.fromSubscription(speechKey, speechRegion);
speechConfig.speechRecognitionLanguage = "en-US";
const audioConfig = sdk.AudioConfig.fromWavFileInput(fs.readFileSync("<audio-wav-file>"));
const speechRecognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);

async function inner() {
return new Promise((resolve, reject) => {
let response = "";

speechRecognizer.recognizing = (s, e) => {
if (e.result.reason === sdk.ResultReason.RecognizingSpeech) {
console.log(`RECOGNIZING: Text=${e.result.text}`);
response += e.result.text;
}
};

speechRecognizer.recognized = (s, e) => {
if (e.result.reason === sdk.ResultReason.RecognizedSpeech) {
console.log(`RECOGNIZED: Text=${e.result.text}`);
response += e.result.text;
} else if (e.result.reason === sdk.ResultReason.NoMatch) {
console.log("NOMATCH: Speech could not be recognized.");
}
};

speechRecognizer.sessionStopped = (s, e) => {
console.log("Session stopped.");
speechRecognizer.stopContinuousRecognitionAsync();
resolve(response);
};

speechRecognizer.startContinuousRecognitionAsync();
});
}

inner()
.then((msg) => {
console.log("Recognition completed. Final response:");
console.log(msg);
})
.catch((err) => {
console.log("Recognition error:");
console.error(err);
});

package.json:

{
"name": "speech-to-text-example",
"version": "1.0.0",
"description": "Speech to Text Example",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"dependencies": {
"microsoft-cognitiveservices-speech-sdk": "^1.20.0"
}
}

输出:

它运行成功并得到如下输入语音的文本输出,

enter image description here

关于node.js - Promise 和 Azure 语音转文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76649469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com