Example #1
0
        public async Task <string> ProcessVoiceToText(Stream stream, int bitRate)
        {
            var fn = $"a-{Guid.NewGuid()}-{bitRate}.wav";

            stream.Seek(0, SeekOrigin.Begin);
            var wavStream = new RawSourceWaveStream(stream, new WaveFormat(bitRate, 2));

            // Debugging only
            // WaveFileWriter.CreateWaveFile($"{fn}-source.wav", wavStream);

            stream.Seek(0, SeekOrigin.Begin);
            var newFormat = new WaveFormat(InputRate, 1);
            WaveFormatConversionStream cs = new WaveFormatConversionStream(newFormat, wavStream);

            // Debugging only
            // WaveFileWriter.CreateWaveFile(fn, cs);
            cs.Seek(0, SeekOrigin.Begin);
            speechRecognizer.StartRecognition(cs);
            var result = speechRecognizer.GetResult();

            speechRecognizer.StopRecognition();

            cs.Close();
            return(result?.GetHypothesis());
        }