2

I am attempting to use a different input device with Speech Recognition Engine. I have attempted to use NAudio to achieve this, using WaveIn and the DataAvailable event, however I can't quite work out how to convert the buffer from the event into a stream usable with speech recognition engine's SetInputToAudioStream(). My current code looks like:

using System.Speech.Recognition;
using NAudio.Wave;

private SpeechRecognition sre;
private WaveInEvent wi;
private Stream st;

static void main(string[] args) {
    Choices words = new Choices(new string[] { "word", "test" });
    Grammar g = new Grammar(words);

    wi = new WaveInEvent();
    wi.DeviceNumber = 0; // Default device
    wi.DataAvailable += Wi_DataAvailable;
    wi.StartRecording();

    st = new MemoryStream();

    sre = new SpeechRecognitionEngine();
    sre.LoadGrammar(g);
    //sre.SetInputToDefaultAudioDevice();
    sre.SetInputToAudioStream(st, /* SpeechAudioFormatInfo */);
    sre.SpeechRecognized += Sre_SpeechRecognized;
    sre.RecognizeAsync(RecognizeMode.Multiple);
}

private void Wi_DataAvailable(object sender, WaveInEventArgs e) {
    // Convert e.Buffer to Stream st
}

private void Sre_SpeechRecognized(object sender, SpeechRecognizedEventArgs e) {
    Console.WriteLine(e.Result.Text);
}

Could anybody help me with this? Thanks.

Anon
  • 21
  • 2
  • Possible duplicate of [From naudio.Wave.WaveIn to Stream ?](https://stackoverflow.com/questions/34385261/from-naudio-wave-wavein-to-stream) – Isma Jan 24 '18 at 08:38
  • @Isma That's something I tried, but I don't think I quite understand it. – Anon Jan 24 '18 at 08:46
  • You have to listen to the DataAvailable event, it will fire when you receive data, that data is the stream you are looking for ;) – Isma Jan 24 '18 at 08:50
  • Yeah, I understand that, but converting the buffer I get from the event to a stream that can be used with the speech recognition engine doesn't seem to work. – Anon Jan 24 '18 at 08:53
  • @Isma I have updated my question with more information. – Anon Jan 24 '18 at 09:13
  • that's exactly what they do in the other question, no? Look at this line: Stream stream = new MemoryStream(byteList.ToArray()); //here is your stream! Maybe I am not understanding what your problem is? – Isma Jan 24 '18 at 09:36
  • @Isma What is the byteList though? They don't seem to explain that at all. I attempted st = new MemoryStream(e.Buffer); however that does not seem to work at all. – Anon Jan 24 '18 at 09:48
  • st.Write(e.Buffer, 0, e.BytesRecorded); is another I tried earlier that also appears to not work. – Anon Jan 24 '18 at 09:50
  • You need to add a private list of bytes to your class and every time the event is save you saved the data, then you need to process that list with a timer or something like that every x seconds... – Isma Jan 24 '18 at 09:52

0 Answers0