예제 #1
0
    public static void DemoFloats(Model model)
    {
        // Demo float array
        VoskRecognizer rec = new VoskRecognizer(model, 16000.0f);

        using (Stream source = File.OpenRead("test.wav")) {
            byte[] buffer = new byte[4096];
            int    bytesRead;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                float[] fbuffer = new float[bytesRead / 2];
                for (int i = 0, n = 0; i < fbuffer.Length; i++, n += 2)
                {
                    fbuffer[i] = (short)(buffer[n] | buffer[n + 1] << 8);
                }
                if (rec.AcceptWaveform(fbuffer, fbuffer.Length))
                {
                    Console.WriteLine(rec.Result());
                }
                else
                {
                    Console.WriteLine(rec.PartialResult());
                }
            }
        }
        Console.WriteLine(rec.FinalResult());
    }
예제 #2
0
    public static void DemoSpeaker(Model model)
    {
        // Output speakers
        SpkModel       spkModel = new SpkModel("model-spk");
        VoskRecognizer rec      = new VoskRecognizer(model, 16000.0f);

        rec.SetSpkModel(spkModel);

        using (Stream source = File.OpenRead("test.wav")) {
            byte[] buffer = new byte[4096];
            int    bytesRead;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                if (rec.AcceptWaveform(buffer, bytesRead))
                {
                    Console.WriteLine(rec.Result());
                }
                else
                {
                    Console.WriteLine(rec.PartialResult());
                }
            }
        }
        Console.WriteLine(rec.FinalResult());
    }
예제 #3
0
        public async Task <string> ConvertAsync(string filePath)
        {
            var mediaInfo = FFmpeg.GetMediaInfo(filePath).Result;


            IStream audioStream = mediaInfo.AudioStreams.FirstOrDefault()
                                  ?.SetCodec(AudioCodec.pcm_s16le)
                                  ?.SetChannels(1)
                                  ?.SetSampleRate(16000);

            var outputPath = Path.ChangeExtension(Path.GetTempFileName(), ".wav");

            await FFmpeg.Conversions.New().AddStream(audioStream).SetOutput(outputPath).Start();


            VoskRecognizer rec = new VoskRecognizer(VoskAudioRecognizer.VoskModel, 16000.0f);

            rec.SetMaxAlternatives(0);
            rec.SetWords(true);

            using (Stream source = File.OpenRead(outputPath))
            {
                byte[] buffer = new byte[4096];
                int    bytesRead;
                while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    rec.AcceptWaveform(buffer, bytesRead);
                }
            }
            string text = rec.FinalResult();

            ;
            return(JObject.Parse(text).Last.First.ToString());
        }
        public List <ResultText> TranscribeViaVosk(string waveFileName, string modelFileName)
        {
            Directory.SetCurrentDirectory(_voskFolder);
            Vosk.Vosk.SetLogLevel(0);
            if (_model == null)
            {
                labelProgress.Text = LanguageSettings.Current.AudioToText.LoadingVoskModel;
                labelProgress.Refresh();
                Application.DoEvents();
                _model = new Model(modelFileName);
            }
            var rec = new VoskRecognizer(_model, 16000.0f);

            rec.SetMaxAlternatives(0);
            rec.SetWords(true);
            var list = new List <ResultText>();

            labelProgress.Text = LanguageSettings.Current.AudioToText.Transcribing;
            labelProgress.Text = string.Format(LanguageSettings.Current.AudioToText.TranscribingXOfY, _batchFileNumber, listViewInputFiles.Items.Count);
            labelProgress.Refresh();
            Application.DoEvents();
            var buffer = new byte[4096];

            _bytesWavTotal = new FileInfo(waveFileName).Length;
            _bytesWavRead  = 0;
            _startTicks    = DateTime.UtcNow.Ticks;
            timer1.Start();
            using (var source = File.OpenRead(waveFileName))
            {
                int bytesRead;
                while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
                {
                    _bytesWavRead += bytesRead;
                    if (rec.AcceptWaveform(buffer, bytesRead))
                    {
                        var res     = rec.Result();
                        var results = ParseJsonToResult(res);
                        list.AddRange(results);
                    }
                    else
                    {
                        var res = rec.PartialResult();
                        textBoxLog.AppendText(res.RemoveChar('\r', '\n'));
                    }

                    if (_cancel)
                    {
                        TaskbarList.SetProgressState(_parentForm.Handle, TaskbarButtonProgressFlags.NoProgress);
                        return(null);
                    }
                }
            }

            var finalResult  = rec.FinalResult();
            var finalResults = ParseJsonToResult(finalResult);

            list.AddRange(finalResults);
            timer1.Stop();
            return(list);
        }
예제 #5
0
    public static void Main()
    {
        Vosk.Vosk.SetLogLevel(0);
        Model          model = new Model("model");
        VoskRecognizer rec   = new VoskRecognizer(model, 16000.0f);

        using (Stream source = File.OpenRead("test.wav")) {
            byte[] buffer = new byte[4096];
            int    bytesRead;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                if (rec.AcceptWaveform(buffer, bytesRead))
                {
                    Console.WriteLine(rec.Result());
                }
                else
                {
                    Console.WriteLine(rec.PartialResult());
                }
            }
        }
        Console.WriteLine(rec.FinalResult());

        rec = new VoskRecognizer(model, 16000.0f);

        using (Stream source = File.OpenRead("test.wav")) {
            byte[] buffer = new byte[4096];
            int    bytesRead;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                float[] fbuffer = new float[bytesRead / 2];
                for (int i = 0, n = 0; i < fbuffer.Length; i++, n += 2)
                {
                    fbuffer[i] = (short)(buffer[n] | buffer[n + 1] << 8);
                }
                if (rec.AcceptWaveform(fbuffer, fbuffer.Length))
                {
                    Console.WriteLine(rec.Result());
                    GC.Collect();
                }
                else
                {
                    Console.WriteLine(rec.PartialResult());
                }
            }
        }
        Console.WriteLine(rec.FinalResult());
    }
예제 #6
0
    public static void DemoBytes(Model model)
    {
        // Demo byte buffer
        VoskRecognizer rec = new VoskRecognizer(model, 16000.0f);

        using (Stream source = File.OpenRead("test.wav")) {
            byte[] buffer = new byte[4096];
            int    bytesRead;
            while ((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0)
            {
                if (rec.AcceptWaveform(buffer, bytesRead))
                {
                    Console.WriteLine(rec.Result());
                }
                else
                {
                    Console.WriteLine(rec.PartialResult());
                }
            }
        }
        Console.WriteLine(rec.FinalResult());
    }
예제 #7
0
 internal static System.Runtime.InteropServices.HandleRef getCPtr(VoskRecognizer obj)
 {
     return((obj == null) ? new System.Runtime.InteropServices.HandleRef(null, System.IntPtr.Zero) : obj.handle);
 }
예제 #8
0
        static void Main()
        {
            var config = new ConfigurationBuilder <IConfiguration>()
                         .UseJsonFile("config.json")
                         .Build();

            Vosk.Vosk.SetLogLevel(0);
            var model = new Model("model");

            _voskRecognizer = new VoskRecognizer(model, 16000.0f);

            _tgBotClient = new TelegramBotClient(config.TgBotToken);

            var me = _tgBotClient.GetMeAsync().Result;

            Console.WriteLine(
                $"Hello, World! I am user {me.Id} and my name is {me.FirstName}."
                );

            _tgBotClient.OnMessage += TgBot_OnMessage;
            _tgBotClient.StartReceiving();

            _vkApi = new VkApi();
            _vkApi.Authorize(new ApiAuthParams()
            {
                AccessToken = config.VkGroupToken
            });

            var longPollServer = _vkApi.Groups.GetLongPollServer(config.VkGroupId);
            var ts             = longPollServer.Ts;

            new Task(async() =>
            {
                while (true)
                {
                    var poll = _vkApi.Groups.GetBotsLongPollHistory(new BotsLongPollHistoryParams()
                    {
                        Server = longPollServer.Server,
                        Ts     = ts,
                        Key    = longPollServer.Key,
                        Wait   = 25
                    });

                    if (poll.Updates == null)
                    {
                        continue;
                    }

                    ts = poll.Ts;
                    foreach (var update in poll.Updates.Where(gu => gu.Type == GroupUpdateType.MessageNew))
                    {
                        var message          = update.MessageNew.Message;
                        var forwardedMessage = message.ForwardedMessages.FirstOrDefault();

                        if (forwardedMessage != null &&
                            forwardedMessage.Attachments.Any(a => a.Type == typeof(AudioMessage)))
                        {
                            var rnd             = new Random();
                            var resultMessageId = _vkApi.Messages.Send(new MessagesSendParams()
                            {
                                RandomId = rnd.Next(),
                                PeerId   = message.FromId,
                                Message  = "Распознаю речь..."
                            });
                            var audioMessageAttachment =
                                forwardedMessage.Attachments.First(a => a.Type == typeof(AudioMessage));
                            var audioMessage = (AudioMessage)audioMessageAttachment.Instance;

                            var webClient = new WebClient();

                            var vkAudioFilePath = $"{Guid.NewGuid()}.ogg";
                            webClient.DownloadFile(audioMessage.LinkOgg, vkAudioFilePath);

                            var resultTextMessage = await RecognizeText(vkAudioFilePath);
                            File.Delete(vkAudioFilePath);

                            if (message.FromId != null)
                            {
                                _vkApi.Messages.Edit(new MessageEditParams
                                {
                                    Message   = resultTextMessage,
                                    MessageId = resultMessageId,
                                    PeerId    = (long)message.FromId
                                });
                            }
                        }
                    }
                }
            }).Start();

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();

            _tgBotClient.StopReceiving();
        }