title: Transfer real time live audio From microphone over TCP/IP Using C# .net
body: <p>I want to make an app to transfer Audio from the microphone in a laptop or PC live, in real time, like a YouTube stream but without video. I will describe my process:</p>
  <ol>
  <li>I transfer a normal file by converting it to byte then back again to origin.</li>
  <li>I change the file type to MP3 or wav then use NAudio.  Works fine also I can play the file if transferred or while receiving</li>
  <li>I change the input file to Microphone and receive the audio.</li>
  </ol>
  <p><strong>Here is the problem</strong>: NAudio is unable to put the live stream from the mic then send it automatically. Always, the buffer gives me a <em>null pointer exception</em> while debugging ,then it gives me another error that the decoder of NAudio did not receive any data, &quot;not acceptable by the way&quot;.</p>
  <p>It should listen or keep receiving data until the port or connection closes.</p>
  <p>I've tried to search about any library about VoIP but found nothing except Ozeki, but no tutorial to handle. All I found is old videos that do not work. I searched about that over a week but no result. I don't want a fully developed project because I already found one, but it is too complex -- about 2K lines of code. All I need is to know what to do or to be given the code that solves the problem.</p>
  <p>This is client side code:</p>
  <pre><code>   public void client()
      {
          try
          {
              //byte[] send_data = Audio_to_byte(); // ORIGINAL WORK CODE
              byte[] send_data = new byte [BufferSize]; // 100M buffer size 
              TcpClient client = new TcpClient(serverip, port);
              NetworkStream stream = client.GetStream();
              // sourceStream and wavein is global vars
              sourceStream = new NAudio.Wave.WaveIn();
              sourceStream.DeviceNumber = 1;
              sourceStream.WaveFormat = new NAudio.Wave.WaveFormat(44100, NAudio.Wave.WaveIn.GetCapabilities(1).Channels);
              wavein = new NAudio.Wave.WaveInProvider(sourceStream);
              //wavein.Read(send_data, 0, send_data.Length); // this buffer not work some times give me full buffer
              BufferedWaveProvider pro = new BufferedWaveProvider(wavein.WaveFormat);
              pro.AddSamples(send_data, 0, send_data.Length); // Empty buffer or error full buffer
              stream.Write(send_data, 0, send_data.Length);
              stream.Close();
              client.Close();
  
          }
          catch (Exception e)
          {
              MessageBox.Show(&quot;Server is offline&quot; + e, &quot;Error&quot;);
  // Here the message is buffer is full or send it empty then the decoder did not receive anything give exception error or of them happen first
          }
      }
  </code></pre>
  <p>and this server side code with MP3 reader code</p>
  <pre><code> IPAddress ip = Dns.GetHostEntry(serverip).AddressList[0];
          TcpListener server_obj = new TcpListener(ip,port);
          TcpClient client = default(TcpClient);
          try
          {
              server_obj.Start();
              while (true)
              {
                  // accept all client
                  client = server_obj.AcceptTcpClient();
                  // make byte storage from network
                  byte[] received_buffer = new byte[BufferSize];
                  //get data from cst
                  NetworkStream stream = client.GetStream();
                  //save data from network to memory till finish then save with playing
                  MemoryStream ms = new MemoryStream();
  
                  int numBytesRead = 0;
                 
                  while ((numBytesRead = stream.Read(received_buffer, 0, received_buffer.Length)) &gt; 0)
                  {
                      // THIS STEP TO RECEIVE ALL DATA FROM CLIENT
                      ms.Write(received_buffer, 0, numBytesRead);
                      //receive sound then play it direct
                      WaveOut(ms.ToArray());
                  }
  
                  Byte_to_audio(ms.ToArray()); // YOU can make or allow override
              }
          }
          catch(Exception e)
          {
             MessageBox.Show(&quot;Error Message : &quot; + e, &quot;Error&quot;);
          }
      }
  </code></pre>
  <p>this is Method that read stream receiving data from network</p>
  <pre><code>       private void WaveOut(byte[] mp3Bytes)
      {
          // MP3 Format
          mp3Stream = new MemoryStream(mp3Bytes);
          mp3FileReader = new Mp3FileReader(mp3Stream);
          wave32 = new WaveChannel32(mp3FileReader, 0.3f, 3f);
          ds = new DirectSoundOut(); // but declration up global
          ds.Init(wave32);
          ds.Play(); // work code*/ 
      }
  </code></pre>