0
votes

I am trying to stream audio bytes into an naudio waveout stream but it isn't working. Here is the pseudocode:

byte[] by = new byte[2560]  //audio packet sizes
int counter=0;

while (true) {  //keep receiving bytes from incoming TCP socket
  int bytesAvailable = await stream.ReadAsync(by, 0, 2560);  //read incoming bytes
  if(counter==0) {
    using (var MemoryStream = new MemoryStream(by)) {
      var waveOut = new WaveOut();
      var waveFormat = new WaveFormat(16000, 16, 2); // wave format
      var rawSource = new RawSourceWaveStream(MemoryStream, waveFormat);
      waveOut.Init(rawSource);
      waveOut.Play();
      counter++;
  }    
}    

..but I am not hearing anything. I am guessing it just reads one packet and stops, but I don't know.

Anyone know what is wrong/how to fix? I know the bytes are coming in because I print out the last byte so it's nothing to do with the network receive.

2
nevermind, got it working using BufferLength, DiscardOnBufferOverflow, and AddSamples. Have to play a bit with bufferlength, but I'd say I have it to maybe 85% clear with a fraction of a second latency. I'm happy with that. Would still like to see someone else's implementation as it's probably better.E.D.
can you by chance post an answer with your code that worked? I know I'm very late but it'd really help me out!Ortund
Posting a new comment below. But, I haven't looked at the code since then, so not sure what state it is in. Not sure how it will work for you, let me know.E.D.

2 Answers

1
votes

it would be better to use a BufferedWaveProvider for this. Just create a single WaveOut device that is playing from the BufferedWaveProvider, and add the byte array of received data to the BufferedWaveProvider as it arrives over the network.

0
votes

for Ortund:

private async void StartClient()
    {
        String sName = "<server ip>";
        int port = <server port>;

        WaveOut waveout = new WaveOut(); 

        TcpClient conn = new TcpClient();

        try
        {

            int counter = 0;

            conn.Connect(sName, port);
            stream = conn.GetStream();               

            by = new byte[2560];  // new byte array to store received data

            var bufferedWaveProvider = new BufferedWaveProvider(new WaveFormat(16000, 16, 2));
            bufferedWaveProvider.BufferLength = 2560 * 16;      //16 is pretty good
            bufferedWaveProvider.DiscardOnBufferOverflow = true;

            while (true)
            {
                //wait until buffer has data, then returns buffer length
                int bytesAvailable = await stream.ReadAsync(by, 0, 2560);

                if (counter == 0)
                {
                    msg = Encoding.UTF8.GetString(by, 0, bytesAvailable);
                    devicehash = msg.TrimEnd();
                    DispatchMethod(by[0].ToString());
                    counter++;                        
                }
                else
                {
                    //send to speakers

                    bufferedWaveProvider.AddSamples(by, 0, 2560);

                    if (counter == 1)
                    {
                        waveout.Init(bufferedWaveProvider);
                        waveout.Play();
                        counter++;
                    }                     
                }
            }
        }
        catch (Exception e)
        { 
        }
    }