如何立即将实时音频流发送到IBM Watson Speech-To-Text,而又不使用C#和IBM Watson SDK在本地保存音频文件?

问题描述

我需要使用IBM Watson SDK来使用麦克风录制音频,并使用C#将其发送到IBM Watson语音到文本。我可以通过将音频文件保存在本地,然后使用 NAudio 库将其发送来实现此功能。但是我的要求是使用流模式将实时音频发送到IBM Watson语音转文本服务,而无需物理存储音频文件。 我在SDK中找不到 RecognizeUsingWebSocket 服务。我只能找到识别服务。

下面是我的代码,用于在本地保存音频文件

有人可以帮我实现它吗?

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IBM.Cloud.SDK.Core.Authentication.Iam;
using IBM.Watson.Assistant.v1.Model;
using IBM.Watson.Assistant.v1;
using IBM.Cloud.SDK.Core.Authentication.Bearer;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NAudio.Wave;
using System.Net.WebSockets;
using IBM.Cloud.SDK.Core.Http;
using IBM.Watson.SpeechToText.v1;

namespace watsonConsole
{
    class Program
    {
        string apikey = "";
        string sttApiKey = "";
        string url = "";
        string stturl = "";
        string versionDate = "";
        string workspaceId = "";

        static public AssistantService service;
        static public SpeechToTextService sttservice;
        private WaveInEvent waveIn;
        private WaveFormat format = new WaveFormat(16000,16,1);
        private ClientWebSocket ws;

        static public WaveFileWriter waveFile;

        static void Main(string[] args)
        {
            Program pr = new Program();


            BearerTokenAuthenticator authenticator = new BearerTokenAuthenticator(
                bearerToken: pr.apikey);
            service = new AssistantService(pr.versionDate,authenticator);
            service.SetServiceUrl(pr.url);


            BearerTokenAuthenticator sttauthenticator = new BearerTokenAuthenticator(
            bearerToken: pr.sttApiKey);

            sttservice = new SpeechToTextService(sttauthenticator);
            sttservice.SetServiceUrl(pr.stturl);


            WaveInCapabilities deviceInfo = WaveIn.GetCapabilities(0);
            Console.WriteLine("Now recording...");
            WaveInEvent waveSource = new WaveInEvent();
            waveSource.DeviceNumber = 0;
            waveSource.WaveFormat = new WaveFormat(44100,1);

            waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);

            string tempFile = (@"C:/watsonConsole/bin/Debug/test/testaudiotest.wav");
            waveFile = new WaveFileWriter(tempFile,waveSource.WaveFormat);
            waveSource.StartRecording();
            Console.WriteLine("Press enter to stop");
            Console.ReadLine();
            waveSource.StopRecording();
            waveFile.dispose();
            pr.Recognize();

            Console.WriteLine("done");

            Console.ReadKey();
        }

        static void waveSource_DataAvailable(object sender,WaveInEventArgs e)
        {
            waveFile.Write(e.Buffer,e.BytesRecorded);
        }

        public void StartRecording()
        {
            waveIn = new WaveInEvent
            {
                BufferMilliseconds = 50,DeviceNumber = 0,WaveFormat = format
            };

            waveIn.StartRecording();
        }               

        public void Recognize()
        {

            var result = sttservice.Recognize(
                audio: File.ReadAllBytes("test/testaudiotest.wav"),contentType: "audio/wav",wordAlternativesThreshold: 0.9f,languageCustomizationId: "",acousticCustomizationId: "",customizationWeight: 0.7,smartFormatting: true
    );
        

            Console.WriteLine(result.Response);
        }

    }


}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)