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()); }
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()); }
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); }
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()); }
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()); }