从内存流读取时,Naudio和Syn语音null引用错误

本文关键字:语音 Syn null 引用 错误 Naudio 内存 读取 | 更新日期: 2023-09-27 18:06:47

我很确定我这样做是对的,但我似乎无法弄清楚是什么导致这个错误或如何修复它。任何帮助都会很感激。代码在下面,错误在下面。

代码。我张贴了所有的,因为我不知道什么是相关的这个错误。

    public class voiceStuff
  {
    public WaveInEvent waveInStream;
    private static StreamSpeechRecognizer _recognizer;
    WaveFileWriter writer;
    private MemoryStream mem;
    public void record()
    {
      waveInStream.NumberOfBuffers = 2;
      waveInStream.StartRecording();
      recognize();
    }
    public void recognize()
    {
      Console.ReadKey();
      waveInStream.StopRecording();
    }
    public voiceStuff()
    {
      Logger.LogReceived += Logger_LogReceived;
      waveInStream = new WaveInEvent();
      waveInStream.NumberOfBuffers =2;
      waveInStream.WaveFormat = new WaveFormat(16000, 1);
      mem = new MemoryStream();
      writer = new WaveFileWriter(mem, waveInStream.WaveFormat);
      waveInStream.DataAvailable += onDataAvailable;
      var modelPath = Path.Combine(Directory.GetCurrentDirectory(), "model/en-us");
      var dictionaryPath = Path.Combine(modelPath, "cmudict-en-us.dict");
      var languageModelPath = Path.Combine(modelPath, "en-us.lm.dmp");
      var configuration = new Configuration
      {
        AcousticModelPath = modelPath,
        DictionaryPath = dictionaryPath,
        LanguageModelPath = languageModelPath,
        UseGrammar = true,
        GrammarPath = "Models",
        GrammarName = "hello"
      };
      _recognizer = new StreamSpeechRecognizer(configuration);
      waveInStream.StartRecording();
      Console.ReadKey();
      waveInStream.StopRecording();
      Thread.Sleep(50);
      _recognizer.StartRecognition(mem, new TimeFrame(mem.Length));
      SpeechResult result = _recognizer.GetResult();
      _recognizer.StopRecognition();
      Console.WriteLine("result: " + result?.GetHypothesis());
      //syn speech
    }
    public void onDataAvailable(object sender, WaveInEventArgs e)
    {
      writer.Write(e.Buffer, 0, e.BytesRecorded);
    }
    static void Logger_LogReceived(object sender, LogReceivedEventArgs e)
    {
          Console.WriteLine(e.Message);
        }
      }
误差

     9/12/2016 9:55:42 PM Error StreamSpeechRecognizer System.NullReferenceException: Object reference not set to an instance of an object.
       at Syn.Speech.Linguist.Dictionary.TextDictionary.Allocate()
       at Syn.Speech.Linguist.Language.Grammar.Grammar.Allocate()
       at Syn.Speech.Linguist.Flat.FlatLinguist.Allocate()
       at Syn.Speech.Decoders.Search.SimpleBreadthFirstSearchManager.Allocate()
       at Syn.Speech.Recognizers.Recognizer.Allocate()
       at Syn.Speech.Api.StreamSpeechRecognizer.StartRecognition(Stream stream, TimeFrame timeFrame)
    9/12/2016 9:55:42 PM Error StreamSpeechRecognizer Syn.Speech.Helper.IllegalStateException: Expected state Ready actual state Allocating
       at Syn.Speech.Recognizers.Recognizer.CheckState(State desiredState)
       at Syn.Speech.Recognizers.Recognizer.Recognize(String referenceText)
       at Syn.Speech.Api.AbstractSpeechRecognizer.GetResult()
    9/12/2016 9:55:42 PM Error StreamSpeechRecognizer Syn.Speech.Helper.IllegalStateException: Expected state Ready actual state Allocating
       at Syn.Speech.Recognizers.Recognizer.CheckState(State desiredState)
       at Syn.Speech.Recognizers.Recognizer.Deallocate()
       at Syn.Speech.Api.StreamSpeechRecognizer.StopRecognition()

从内存流读取时,Naudio和Syn语音null引用错误

为了最终确定WAV文件结构,您至少需要Dispose您的WaveFileWriter。但是,也要注意处理内存流。我通常将它封装在NAudio的实用程序类IgnoreDisposeStream

然后您需要在将MemoryStream传递给语音识别之前将其倒带到开始。