title: Real-Time Audio Data to Byte-Array
body: <p>With the codes below, the byte array is created as soon as the audio record wav file is saved.</p>
  <p>But I need to create this <strong>byte-array real-time, for live streams.</strong></p>
  <p>How can I do this?</p>
  <pre><code>        [DllImport(&quot;winmm.dll&quot;)]
          private static extern long mciSendString(string command, StringBuilder retstrign, int Returnlength, IntPtr callback);
          public Form1()
          {
              InitializeComponent();
              mciSendString(&quot;open new Type waveaudio alias recsound&quot;, null, 0, IntPtr.Zero);
              button1.Click += new EventHandler(this.startbutton_Click);
  
          }
  
          private void startbutton_Click(object sender, EventArgs e)
          {
              //    throw new NotImplementedException();
              mciSendString(&quot;record recsound&quot;, null, 0, IntPtr.Zero);
              button2.Click += new EventHandler(this.stopbutton_Click);
  
          }
  
          private void stopbutton_Click(object sender, EventArgs e)
          {
              string filename = @&quot;C:\Users\seymk\source\repos\AudioFromSoundCard\test.wav&quot;;
              //throw new NotImplementedException();
              mciSendString(@&quot;save recsound C:\Users\seymk\source\repos\AudioFromSoundCard\test.wav&quot;, null, 0, IntPtr.Zero);
              mciSendString(&quot;close recsound&quot;, null, 0, IntPtr.Zero);
  
              //ReadWav(filename, out float[] L, out float[] R);
  
              using (WaveFileReader reader = new WaveFileReader(filename))
              {
                  Assert.AreEqual(8, reader.WaveFormat.BitsPerSample, &quot;&quot;);
                  byte[] buffer = new byte[reader.Length];
                  int read = reader.Read(buffer, 0, buffer.Length);
                  short[] sampleBuffer = new short[read / 2];
                  Buffer.BlockCopy(buffer, 0, sampleBuffer, 0, read);
  
                  string text = &quot;Some metadata........&quot; + Environment.NewLine +
   String.Join(&quot; &quot;, buffer.Select(x =&gt; x.ToString(&quot;x2&quot;))); // &lt;- encoding 
  
                  File.WriteAllText(@&quot;C:\Users\seymk\source\repos\AudioFromSoundCard\Foo.txt&quot;, text);
              }
          }
  </code></pre>