Search code examples
c#voice

Download wav stream


I am developing a voice recognition software and one of the requirements for the voice recognition engine is a wave stream. The function is recognition engine.setInputToWaveStream(Stream audioSource)

So I have started looking into how to get a a wave file from a webpage using memorystream. This is my current code.

using (WebClient webClient = new WebClient())
{
    byte[] data = webClient.DownloadData(@"http://192.0.2.82:6180/audio.wav");

    using (MemoryStream mem = new MemoryStream(data))
    {
        recEngine.SetInputToWaveStream(mem);
    }
} 

This is not working so could someone please point me to the right direction. I have tried looking at other resources but most of them are outdated and the NAudio library solutions are not working for me.


Solution

  • It looks like you're passing the data into the MemoryStream where the size of the stream should be declared.
    From the docs on MemoryStream()

    Initializes a new instance of the MemoryStream class with an expandable capacity initialized to zero.

    Try something like this:

    using (WebClient webClient = new WebClient())
    {
      byte[] data = webClient.DownloadData(@"http://192.0.2.82:6180/audio.wav");
    
      using (MemoryStream mem = new MemoryStream())
      {
        mem.Write(data, 0 , data.Length);
        recEngine.SetInputToWaveStream(mem);
      }
    }
    

    Depending on the format of your .wav data you could also use mem.WriteByte