我希望下面的代码输出内部()响应的实际值,而不是Promise.我认为.Then()将等待输出,直到promise 得到解决.我应该换些什么? 到目前为止,输出将是未定义的,而不是YourAudioFile.wav的内容. 我知道代码的工作方式并非如此,因为如果我在INTERN()中使用sole.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)
})

推荐答案

100

Code:

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"
  }
}

Output:

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

enter image description here

Node.js相关问答推荐

如何解决无法获得本地颁发者证书的问题

GitLab SAST中的Nodejcan未找到匹配项

自动将Selify打开的Chrome窗口移动到Mac OS中的第三个显示器

聚合操作不返回任何具有mongoose模式的内容

如何修复PostgreSQL和NodeJS/NestJS应用程序之间的日期时间和时区问题?

Nestjs swc 错误:找不到模块项目路径/src/app.module

SvelteKit应用程序立即退出,没有错误

如何使用Next.js/Node/TS正确地上传至S3

Postgressql的BIGSERIAL自增序列,即使由于唯一约束错误没有创建行,也会自动增加

在对象数组中的数组中嵌套 $lookup - Mongodb

错误 node :错误:绑定消息提供 16 个参数,但准备语句需要 15 个

如何使用 Remix 仅在客户端呈现组件?

我如何保护nodejs中的路由

如何从哪个应用程序用户中找到在 Firebase 身份验证中进行身份验证的用户

如何在不使用位置运算符 $ 的情况下更新 mongodb 文档中数组中的嵌套文档?

仅显示用户在 Reactjs 中使用服务器端发布的帖子是 Node Js、Mongodb

node_modules 中 .bin 文件夹的用途是什么?

提供静态文件到底是什么意思?

如何使用 UglifyJS 缩小文件夹中的多个 Javascript 文件?

用于轻松部署和更新的 Node.js 设置