Exemplo n.º 1
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);
        }
Exemplo n.º 3
0
    public static void DemoBytes(Model model)
    {
        // Demo byte buffer
        VoskRecognizer rec = new VoskRecognizer(model, 16000.0f);

        rec.SetMaxAlternatives(0);
        rec.SetWords(true);
        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());
    }