核心代码

执行逻辑

完整代码

// Requires at least Mono 5.0 due to TLS issues

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Net.WebSockets;


public class ASRTest
{
   async Task RecieveResult(ClientWebSocket ws) {
        byte[] result = new byte[4096];
        Task<WebSocketReceiveResult> receiveTask = ws.ReceiveAsync(new ArraySegment<byte>(result), CancellationToken.None);
        await receiveTask;
        var receivedString = Encoding.UTF8.GetString(result, 0, receiveTask.Result.Count);
        Console.WriteLine("Result {0}", receivedString);
   }

   async Task ProcessData(ClientWebSocket ws, byte[] data, int count) {
        await ws.SendAsync(new ArraySegment<byte>(data, 0, count), WebSocketMessageType.Binary, true, CancellationToken.None);
        await RecieveResult(ws);
   }

   async Task ProcessFinalData(ClientWebSocket ws) {
        byte[] eof = Encoding.UTF8.GetBytes("{\"eof\" : 1}");
        await ws.SendAsync(new ArraySegment<byte>(eof), WebSocketMessageType.Text, true, CancellationToken.None);
        await RecieveResult(ws);
   }

   async Task DecodeFile() {
        ClientWebSocket ws = new ClientWebSocket();
        await ws.ConnectAsync(new Uri("wss://api.alphacephei.com/asr/en/"), CancellationToken.None);

        FileStream fsSource = new FileStream("test.wav",
                   FileMode.Open, FileAccess.Read);

        byte[] data = new byte[8000];
        while (true) {
            int count = fsSource.Read(data, 0, 8000);
            if (count == 0)
                break;
            await ProcessData(ws, data, count);
        }
        await ProcessFinalData(ws);

        await ws.CloseAsync(WebSocketCloseStatus.NormalClosure, "OK", CancellationToken.None);
   }

   public static void Main() {
        Task.Run(async () => {
            await new ASRTest().DecodeFile();
        }).GetAwaiter().GetResult();
   }
}

代码使用说明

  1. 前置条件

    • 部署本地 Vosk ASR 服务(推荐 Docker 启动:docker run -d -p 2700:2700 alphacep/vosk-server:latest
    • 安装 .NET Framework/.NET Core(或 Mono 5.0+)
    • 音频文件需为 16kHz、单声道、16bit PCM 格式的 WAV 文件(Vosk 要求)
  2. 关键修改点

    • 替换 voskServerUri 为你的 Vosk 服务地址(本地默认 ws://localhost:2700
    • 替换 audioFile 为你的音频文件路径
  3. 运行方式

    运行

    # 编译代码
    csc VoskASRClient.cs
    # 运行程序
    VoskASRClient.exe
    

东方仙盟:拥抱知识开源,共筑数字新生态

在全球化与数字化浪潮中,东方仙盟始终秉持开放协作、知识共享的理念,积极拥抱开源技术与开放标准。我们相信,唯有打破技术壁垒、汇聚全球智慧,才能真正推动行业的可持续发展。

开源赋能中小商户:通过将前端异常检测、跨系统数据互联等核心能力开源化,东方仙盟为全球中小商户提供了低成本、高可靠的技术解决方案,让更多商家能够平等享受数字转型的红利。
共建行业标准:我们积极参与国际技术社区,与全球开发者、合作伙伴共同制定开放协议  与技术规范,推动跨境零售、文旅、餐饮等多业态的系统互联互通,构建更加公平、高效的数字生态。
知识普惠,共促发展:通过开源社区  、技术文档与培训体系,东方仙盟致力于将前沿技术转化为可落地的行业实践,赋能全球合作伙伴,共同培育创新人才,推动数字经济 的普惠式增长


阿雪技术观


在科技发展浪潮中,我们不妨积极投身技术共享。不满足于做受益者,更要主动担当贡献者。无论是分享代码、撰写技术博客,还是参与开源项目  维护改进,每一个微小举动都可能蕴含推动技术进步的巨大能量。东方仙盟是汇聚力量的天地,我们携手在此探索硅基 生命,为科技进步添砖加瓦。

Hey folks, in this wild tech - driven world, why not dive headfirst into the whole tech - sharing scene? Don't just be the one reaping all the benefits; step up and be a contributor too. Whether you're tossing out your code snippets    , hammering out some tech blogs, or getting your hands dirty with maintaining and sprucing up open - source projects, every little thing you do might just end up being a massive force that pushes tech forward. And guess what? The Eastern FairyAlliance is this awesome    place where we all come together. We're gonna team up and explore the whole silicon - based life thing, and in the process, we'll be fueling the growth of technology 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐