【问题标题】:Capture real-time audio from mic in web application with Google Speech to Text API [closed]使用 Google Speech to Text API 从 Web 应用程序中的麦克风捕获实时音频 [关闭]
【发布时间】:2020-12-07 07:31:16
【问题描述】:

我有一个带有语音输入表单的 C# MVC Web 应用程序。我的意思是用户可以通过麦克风使用语音在文本框中输入值。我决定使用Google Speech to text API 来实现这一点。但我对使用 Google Speech to Text API 实现这一点有一些疑问。

因为我试图在 MVC Web 应用程序中实现这一点,所以无法从服务器端代码访问麦克风设备。我想将语音从实时音频转换并显示输出文本到网页。

【问题讨论】:

  • 您可以使用 SignalR 将输入发送到您的服务器调用 Google API 并将结果推送回客户端。
  • @Darem 非常感谢您的回复。您能否建议任何示例代码。
  • 这是来自微软官方页面。 docs.microsoft.com/en-us/aspnet/core/tutorials/…。这是一个简单的聊天应用程序。但是您可以用您的麦克风(字节)输入替换字符串输入并将其发送到您的后端。希望对你有帮助?
  • 您好@Darem,您能否发表您的评论作为答案,以便其他人更容易看到?
  • @Alejandro 我将我的评论添加为 anwser。

标签: c# asp.net-mvc google-cloud-platform speech-recognition speech-to-text


【解决方案1】:

解决此问题的一种方法是使用SignalR

您可以按照此 SO answer 获取麦克风输入,以及如何使用 websockets 处理麦克风输入的非常好的解释。

以下只是sudo代码解释概念!

它也大大简化了,因为我不知道,例如,谷歌的 API 是否可以处理你总是只发送语音输入片段等的事实。正如我所说,代码只提供了一个粗略的概述基本流程,如果服务器离线等没有逻辑。

但是在function process_microphone_buffer(event)函数内部你可以调用SignalR。

所以函数类似于

function process_microphone_buffer(event) {
    // you should handle this as a singelton
    const connection = new signalR.HubConnectionBuilder().withUrl("/speechToTextHub ").build();
    const microphone_output_buffer = event.inputBuffer.getChannelData(0);
    connection.invoke("SendMicrophoneBuffer", microphone_output_buffer).catch(function (err) {
        return console.error(err.toString());
    });
}

并在您的服务器上实现相应的集线器:

using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;

namespace SignalRChat.Hubs
{

    public class SpeechToTextHub : Hub
    {
        public async Task SendMicrophoneBuffer(byte[] buffer)
        {
            var googleApi = new GoogleApi();
            var speechToTextResult = await googleApi.GetTextFromSpeechAsync(buffer);
            Context.Clients.Client(Context.ConnectionId).SendAsync("SpeechToTextResult", speechToTextResult);
        }
    }
}

在你的客户端你有这样的东西

connection.on("SpeechToTextResult", function (textResult) {
   console.log(textResult);
});

如果答案对于 Stackoverflow 来说过于笼统,我也可以将其删除。 如果还有未解决的问题,我可以相应地扩展我的答案。

【讨论】:

    猜你喜欢
    • 2023-01-21
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多