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

Live playing data captured from mic.

Jan 9, 2015 at 11:11 AM
I have been able to capture the data from the mic and send it over to the network.I captured the data at the event :
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
       IPEndPoint sending_end_point = new IPEndPoint(send_to_address, 12345);
        try
        {
            sending_socket.SendTo(e.Buffer, sending_end_point);
        }
        catch (Exception send_exception)
        {
            Console.WriteLine(" Exception {0}", send_exception.Message);
        }
    }
Also i was able to receive the the same data at the receiving end.At the receiving end i have the following code:
    private WaveOut waveOut = null;

    private WaveFormat waveFormat = new WaveFormat(8000, 16, 1);
    private BufferedWaveProvider waveProvider;


    private const int listenPort = 12345;
    UdpClient listener;

    string received_data;
    byte[] receive_byte_array;
    public Form1()
    {
        InitializeComponent();

        listener = new UdpClient(listenPort);

        waveProvider=new BufferedWaveProvider(waveFormat);
        waveOut = new WaveOut();
        waveOut.DesiredLatency = 100;
        waveOut.Init(waveProvider);
        waveOut.PlaybackStopped += wavePlayer_PlaybackStopped;

        waveOut.Play();
    }
private void button1_Click(object sender, EventArgs e)
    {

        richTextBox1.Text = "Listining to port.....";
        bool done = false;
        IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
            try
            {
                while (true)
                {
                    receive_byte_array = listener.Receive(ref groupEP);
                    if (waveProvider != null)
                        waveProvider.AddSamples(receive_byte_array, 0, receive_byte_array.Length);

                }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.ToString());
            }
        }
    }
now what the problem is, when i start both the sender and the receiver, the sender sends the data the receiver buffers the data in the waveProvider but doesnot play it as soon as it receive (Instead it play after buffer full exception occurs) This results in buffer overflow and i receive the buffer full exception.
I even tried to place
waveOut.Play();
just beneath as
if (waveProvider != null)
                        waveProvider.AddSamples(receive_byte_array, 0, receive_byte_array.Length);
                    waveOut.Play();
and also clear the waveProvider.ClearBuffer() as:
if (waveProvider != null)
                        waveProvider.AddSamples(receive_byte_array, 0, receive_byte_array.Length);
                    waveOut.Play();
                    waveProvider.ClearBuffer();
still no luck.
what i want is, as soon as the data is received in the receiver end, play it and the free the buffer.

Thanks in advance.
Jan 9, 2015 at 10:06 PM
The right way is to start playing once from the buffered waveprovider and fill it as soon as you receive audio. If the buffer is overflowing, maybe the block size of audio you are sending is too large, or maybe you are sending faster than realtime. You can increase the size of the buffer behind BufferedWaveProvider

Mark
Jan 12, 2015 at 9:48 AM
Thanks Mark,
I want the same thing you stated above.The problem with me is, i receive the byte array from socket as shown here
try
        {
            while (true)
            {
                __receive_byte_array = listener.Receive(ref groupEP);__
                if (waveProvider != null)
                    waveProvider.AddSamples(receive_byte_array, 0, receive_byte_array.Length);

            }
        }
        catch (Exception ex)
        {
           MessageBox.Show(ex.ToString());
        }
but in my case then the buffer is full it start playing the sound but next time it says buffer full.I have once worked in project of taking the input from the mic and then playing it at once.
// create wave input from mic
        waveIn = new WaveIn(this.Handle);
        waveIn.BufferMilliseconds = 25;
        waveIn.RecordingStopped += waveIn_RecordingStopped;
        waveIn.DataAvailable += waveIn_DataAvailable;

        // create wave provider
        waveProvider = new BufferedWaveProvider(waveIn.WaveFormat);

        // create wave output to speakers
        waveOut = new WaveOut();
        waveOut.DesiredLatency = 100;
        waveOut.Init(waveProvider);
        waveOut.PlaybackStopped += wavePlayer_PlaybackStopped;

        // start recording and playback
        waveIn.StartRecording();
        waveOut.Play();
and
void waveIn_DataAvailable(object sender, WaveInEventArgs e)
    {
        // add received data to waveProvider buffer
        if (waveProvider != null)
            waveProvider.AddSamples(e.Buffer, 0, e.BytesRecorded);
    }
here as soon as data is available it is filled in waveProvider same thing i want with my code.As soon as the byte array is arrived at the socket fill it in the wave provider and then plat it.
In my case instead of "void waveIn_DataAvailable(object sender, WaveInEventArgs e)" event i have used
while (true)
            {
                receive_byte_array = listener.Receive(ref groupEP);
                if (waveProvider != null)
                    waveProvider.AddSamples(receive_byte_array, 0, receive_byte_array.Length);

            } 
what should i do to get the behavior similar as ""void waveIn_DataAvailable(object sender, WaveInEventArgs e)"" event in my end.
Thanks,
Ganesh