This project has moved and is read-only. For the latest updates, please go here.

Playback the streamed audio byte array on client side ;captured by WasapiloopbackCapture on server,sent via sockets

Sep 16, 2014 at 9:20 PM
Hello Everyone !! Please Help !!!

I was trying to record any audio being played on my PC and have achieved this using WasapiloopbackCapture........this is the server part.Then I try to stream the recorded audio in WaveInEventArgs buffer via sockets in real time and finally on the client side I want to playback this byte array.I want the byte array to have Stereo audio, possibly it already does.

The code does not work currently apart from recording a stereo wav file to a disk but that part is not needed and commented out. I think the AddSample function is not useful ...I just got it from net. I need to have a low latency therefore I tried to avoid writing to other buffers and then sending them but I maybe wrong in doing so.Also I wanted to utilize the current resources as much as possible.

P.S I am a beginner in coding.

The problems I want to clear and understand better:
1)Can I pass WaveInEventArgs buffer on a socket or does it need to be altered first ?? Should I convert the array to short samples ?
2)On the client side how to read the incoming buffer byte array so that it can be played back as a wav or whichever format it is in ??
I cannot determine the format in which wasapi records wav file tried right clicking and checking properties and also used dbpowerAmp.....
3)


Please ask me if there are any inconsistencies with problem data.......
Server Side

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
       
        private WasapiLoopbackCapture capture;
        private WaveFileWriter writer;
        static Socket sck;
        public void Start(string fileName)
        {
            capture = new WasapiLoopbackCapture();
            capture.DataAvailable += CaptureOnDataAvailable;
            capture.RecordingStopped += CaptureOnRecordingStopped;
            writer = new WaveFileWriter(fileName, capture.WaveFormat);
            capture.StartRecording();
        }

        public void Stop()
        {
            capture.StopRecording();

        }
        private void CaptureOnDataAvailable(object sender, WaveInEventArgs waveInEventArgs)
        {
            sck= new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            sck.Bind(new IPEndPoint(IPAddress.Any, 5554));
            sck.Listen(10);
            Socket accept = sck.Accept();
            sck.Send(waveInEventArgs.Buffer);
            sck.Close();


           // writer.Write(waveInEventArgs.Buffer, 0, waveInEventArgs.BytesRecorded);
          //sck.Send(waveInEventArgs.Buffer, SocketFlags)
        }
        private void CaptureOnRecordingStopped(object sender, StoppedEventArgs stoppedEventArgs)
        {
            writer.Dispose();
            capture.Dispose();
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Start("abc.wav");
        }
      }
    }
namespace Client
{

    public partial class MainWindow : Window
    {
         static Socket sock;
       static byte[] buffer = null;
        
        static void main(string[] args) {
            Connector();

        }
       

        public static void Connector()
        {
            sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint localEndpoint = new IPEndPoint(IPAddress.Parse("127.0.0.0"), 5554);
            try
            {
                sock.Connect(localEndpoint);
            }
            catch
            {
                Console.Write("Unable to connect");

            }
            sock.Receive(buffer);
           
            sock.Close();

        }
        
        public static void AddSamples(byte[] array)
        {

            BufferedWaveProvider PlayBuffer = new BufferedWaveProvider(new WaveFormat(441000, 16, 2));
            PlayBuffer.AddSamples(buffer, 0, 8192);
            WaveOut player = new WaveOut();
            player.Init(PlayBuffer);
            player.Play();

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            AddSamples(buffer);
        }
    }

}
Thanks in Advance

Anurag
anurag356@gmail.com