C# - 捕获 RTP 流并发送到语音识别

2024-05-08

我正在努力实现的目标:

  • 在 C# 中捕获 RTP 流
  • 将该流转发到 System.Speech.SpeechRecognitionEngine

我正在创建一个基于 Linux 的机器人,它将接受麦克风输入,将其发送给 Windows 机器,Windows 机器将使用 Microsoft 语音识别处理音频,并将响应发送回机器人。机器人可能距离服务器数百英里,所以我想通过互联网来完成此操作。

到目前为止我所做的:

  • 让机器人使用 FFmpeg 生成以 MP3 格式(其他可用格式)编码的 RTP 流(机器人在运行 Arch Linux 的 Raspberry Pi 上运行)
  • 使用 VLC ActiveX 控件在客户端计算机上捕获流
  • Found that the SpeechRecognitionEngine has the available methods:
    1. recognizer.SetInputToWaveStream()
    2. recognizer.SetInputToAudioStream()
    3. recognizer.SetInputToDefaultAudioDevice()
  • 看着使用 JACK 将应用程序的输出发送到线路输入,但完全被它搞糊涂了。

我需要什么帮助:

我一直不知道如何将流从 VLC 发送到 SpeechRecognitionEngine。 VLC 根本不公开流。有没有办法可以捕获流并将该流对象传递给 SpeechRecognitionEngine?或者RTP不是这里的解决方案?

在此先感谢您的帮助。


经过一番努力,我终于得到了Microsoft.SpeechRecognitionEngine接受 WAVE 音频流。过程如下:

在 Pi 上,我运行 ffmpeg。我使用此命令流式传输音频

ffmpeg -ac 1 -f alsa -i hw:1,0 -ar 16000 -acodec pcm_s16le -f rtp rtp://XXX.XXX.XXX.XXX:1234

在服务器端,我创建了一个UDPClient并监听端口 1234。我在单独的线程上接收数据包。首先,我去掉 RTP 标头(标头格式在这里解释 http://en.wikipedia.org/wiki/Real-time_Transport_Protocol#Packet_header)并将有效负载写入特殊流。我不得不使用SpeechStreamer class 肖恩的回应中描述 https://stackoverflow.com/questions/1682902/streaming-input-to-system-speech-recognition-speechrecognitionengine为了让 SpeechRecognitionEngine 正常工作。它不符合标准Memory Stream.

我在语音识别方面要做的唯一一件事是将输入设置为音频流而不是默认音频设备。

recognizer.SetInputToAudioStream( rtpClient.AudioStream,
    new SpeechAudioFormatInfo(WAVFile.SAMPLE_RATE, AudioBitsPerSample.Sixteen, AudioChannel.Mono));

我还没有对它进行广泛的测试(即让它流几天并看看它是否仍然有效),但我可以将音频样本保存在SpeechRecognized听起来很棒。我使用的采样率为 16 KHz。我可能会将其降低到 8 KHz 以减少数据传输量,但一旦它成为问题我就会担心。

我还应该提到,响应速度非常快。我可以说出整个句子并在不到一秒的时间内得到答复。 RTP 连接似乎给该过程增加了很少的开销。我必须尝试一个基准测试并将其与仅使用 MIC 输入进行比较。

编辑:这是我的 RTPClient 类。

    /// <summary>
    /// Connects to an RTP stream and listens for data
    /// </summary>
    public class RTPClient
    {
        private const int AUDIO_BUFFER_SIZE = 65536;

        private UdpClient client;
        private IPEndPoint endPoint;
        private SpeechStreamer audioStream;
        private bool writeHeaderToConsole = false;
        private bool listening = false;
        private int port;
        private Thread listenerThread; 

        /// <summary>
        /// Returns a reference to the audio stream
        /// </summary>
        public SpeechStreamer AudioStream
        {
            get { return audioStream; }
        }
        /// <summary>
        /// Gets whether the client is listening for packets
        /// </summary>
        public bool Listening
        {
            get { return listening; }
        }
        /// <summary>
        /// Gets the port the RTP client is listening on
        /// </summary>
        public int Port
        {
            get { return port; }
        }

        /// <summary>
        /// RTP Client for receiving an RTP stream containing a WAVE audio stream
        /// </summary>
        /// <param name="port">The port to listen on</param>
        public RTPClient(int port)
        {
            Console.WriteLine(" [RTPClient] Loading...");

            this.port = port;

            // Initialize the audio stream that will hold the data
            audioStream = new SpeechStreamer(AUDIO_BUFFER_SIZE);

            Console.WriteLine(" Done");
        }

        /// <summary>
        /// Creates a connection to the RTP stream
        /// </summary>
        public void StartClient()
        {
            // Create new UDP client. The IP end point tells us which IP is sending the data
            client = new UdpClient(port);
            endPoint = new IPEndPoint(IPAddress.Any, port);

            listening = true;
            listenerThread = new Thread(ReceiveCallback);
            listenerThread.Start();

            Console.WriteLine(" [RTPClient] Listening for packets on port " + port + "...");
        }

        /// <summary>
        /// Tells the UDP client to stop listening for packets.
        /// </summary>
        public void StopClient()
        {
            // Set the boolean to false to stop the asynchronous packet receiving
            listening = false;
            Console.WriteLine(" [RTPClient] Stopped listening on port " + port);
        }

        /// <summary>
        /// Handles the receiving of UDP packets from the RTP stream
        /// </summary>
        /// <param name="ar">Contains packet data</param>
        private void ReceiveCallback()
        {
            // Begin looking for the next packet
            while (listening)
            {
                // Receive packet
                byte[] packet = client.Receive(ref endPoint);

                // Decode the header of the packet
                int version = GetRTPHeaderValue(packet, 0, 1);
                int padding = GetRTPHeaderValue(packet, 2, 2);
                int extension = GetRTPHeaderValue(packet, 3, 3);
                int csrcCount = GetRTPHeaderValue(packet, 4, 7);
                int marker = GetRTPHeaderValue(packet, 8, 8);
                int payloadType = GetRTPHeaderValue(packet, 9, 15);
                int sequenceNum = GetRTPHeaderValue(packet, 16, 31);
                int timestamp = GetRTPHeaderValue(packet, 32, 63);
                int ssrcId = GetRTPHeaderValue(packet, 64, 95);

                if (writeHeaderToConsole)
                {
                    Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7} {8}",
                        version,
                        padding,
                        extension,
                        csrcCount,
                        marker,
                        payloadType,
                        sequenceNum,
                        timestamp,
                        ssrcId);
                }

                // Write the packet to the audio stream
                audioStream.Write(packet, 12, packet.Length - 12);
            }
        }

        /// <summary>
        /// Grabs a value from the RTP header in Big-Endian format
        /// </summary>
        /// <param name="packet">The RTP packet</param>
        /// <param name="startBit">Start bit of the data value</param>
        /// <param name="endBit">End bit of the data value</param>
        /// <returns>The value</returns>
        private int GetRTPHeaderValue(byte[] packet, int startBit, int endBit)
        {
            int result = 0;

            // Number of bits in value
            int length = endBit - startBit + 1;

            // Values in RTP header are big endian, so need to do these conversions
            for (int i = startBit; i <= endBit; i++)
            {
                int byteIndex = i / 8;
                int bitShift = 7 - (i % 8);
                result += ((packet[byteIndex] >> bitShift) & 1) * (int)Math.Pow(2, length - i + startBit - 1);
            }
            return result;
        }
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C# - 捕获 RTP 流并发送到语音识别 的相关文章

随机推荐

  • 如何为带有可选时间部分的通用日期格式定义 DateTime 解析格式?

    什么是正确的DateTime格式从通用日期格式的字符串中解析日期 G 带有可选时间部分 d 我可以有两种类型的日期 12 13 2012 6 30 00 PM 3 29 2013 如何统一解析它们 现在我正在尝试解析 G 格式 然后如果它没
  • gsl库中的span和array_view有什么区别?

    在最近的几次会议演讲中 我听到 Bjarne Stroustrup 和其他人提到了 C 的新编码指南以及一些支持它们的类型 具体来说 我记得的例子span
  • 读取用 php 分块的范围块

    我有一个输入字段 可以在其中粘贴下载网址 之后 我使用 AJAX 请求来获取文件信息 例如 headerinfo 内容长度 mime 类型 如果我使用curl 接受范围 然后 我开始连续循环 xhr2 请求 其中包含我的 php 文件的范围
  • Eclipse/Maven:运行 JUnit 测试时未编译它们

    我正在使用 Maven 和 Eclipse m2eclipse 插件 开发一个项目 我在 JUnit 测试中遇到问题 有时 当在 Eclipse 中运行它们时 它们不会被编译 而是使用旧的类文件 当我删除类文件时 我得到ClassNotFo
  • 如何将结果导出为 PDF 报告?

    我正在使用 SonarQube 5 6 3 如何将 SonarQube 分析详细信息报告创建为 PDF 表单 Excel 报告或 html 格式的报告 似乎没有可用的插件 我无法使用以下配置生成 html 文件 sonar issuesRe
  • 引起原因:com.datastax.driver.core.exceptions.InvalidQueryException:日期长度应为 8 或 0 字节 (13)

    我正在尝试使用 spring data cassandra 插入数据 但是当我的应用程序使用 Spring Boot 版本是 1 5 3 运行时 我遇到了以下错误 我在spring data中添加了一个实现CrudResposity的自定义
  • 如何递归列出远程 FTP 的目录内容

    从远程 UNIX FTP 服务器下载文件后 您想要验证是否已正确下载所有文件 您至少会在 Windows 命令提示符中获得类似于 dir s 命令的信息 FTP 客户端在 Windows 上运行 遗憾的是这是为 Unix Linux 用户编
  • 按下主页按钮时 onPause 不会触发

    我有一个安卓AppCompatActivity未能点火onPause按下主页按钮时发生的事件 根据安卓文档 https developer android com guide components activities activity l
  • 使用特殊字符创建正则表达式

    我正在为 mongodb 创建一个查询 app get content title function req res var regexp new RegExp req params title i db find title regexp
  • 删除匿名监听器

    当尝试采用使用匿名或嵌套类实现侦听器的风格时 以便隐藏除侦听之外的其他用途的通知方法 即我不希望任何人能够调用actionPerformed 例如来自java动作监听器 实现与匿名类 https stackoverflow com ques
  • HTML-Entity 转义以防止 XSS

    我有一些用户输入 在我的代码中 我确保对以下符号进行转义 gt amp lt gt lt gt gt gt OWASP https www owasp org index php XSS 28Cross Site Scripting 29
  • 扑。如何检查自动续订订阅是否仍然有效

    我的应用程序有 1 个月的自动续订订阅 当用户单击 购买订阅 按钮时 我将购买日期保存到共享首选项中 然后 1 个月后 我需要检查该订阅是否仍然有效 那么我该如何实施呢 2020 年 3 月 11 日更新 你好 我可以看到那些正在寻找如何在
  • 从云函数在 Google Cloud Storage 中创建新的 csv 文件

    第一次使用 Google 云存储 下面我有一个云函数 每当 csv 文件上传到时就会触发该函数my folder在我的桶里 我的目标是在同一文件夹中创建一个新的 csv 文件 读取上传的 csv 的内容并将每一行转换为将进入新创建的 csv
  • 在 Alpine 中找不到运行时/cgo

    In an alpine edge我安装的容器通过 RUN apk add no cache musl dev go 我试着跑go get github com golang protobuf protoc gen go then 这会导致
  • 来自完整 HTML 文档的 jQuery 对象

    是否可以将完整的 HTML 文档解析为完整的 jQuery 对象 当我尝试时 例如 var tmp p test p console log tmp I get title p test 即一个数组 将所有头部的子项与所有身体的子项组合起来
  • 将数组传递给 json.stringify

    我试图将数组传递给 json stringify 但返回的值返回为空 JSON stringify json data returns json 这是数据的内容 data from email protected cdn cgi l ema
  • Capistrano 杀死资产:预编译

    我正在尝试部署我的应用程序 但我不断收到 err xxx xxx xx xxx bash line 1 9953 Killed bundle exec rake RAILS ENV production RAILS GROUPS asset
  • AWS lambda只读文件系统错误,使用docker镜像存储ML模型

    我在 lambda 上使用 docker 容器映像来运行我的 ML 模型 我的 lambda 函数有一个 S3 触发器来获取图像 我正在尝试运行 lambda 函数 但收到此错误 有人可以帮帮我吗 PS 现在我知道 tmp 是 lambda
  • 同时支持 CommonJS 和 AMD

    有没有办法创建一个 javascript 微型库 没有依赖项的库 支持以下所有模块格式 异步模块定义 CommonJS 将库的导出公开为全局命名空间对象 无加载程序 是的 我把这个答案归功于ded https github com ded
  • C# - 捕获 RTP 流并发送到语音识别

    我正在努力实现的目标 在 C 中捕获 RTP 流 将该流转发到 System Speech SpeechRecognitionEngine 我正在创建一个基于 Linux 的机器人 它将接受麦克风输入 将其发送给 Windows 机器 Wi