2

Can't understand what the problem is.

There is a server that is accepting data from the client and sends back. Client sends the sound recorded from the microphone, which when receiving from the server is plays. For some reason the client does not want to receive data from the server if the buffer is more than 47 milliseconds. However, even if the buffer holds 45 milliseconds of the sound, the sound received from the server is not played back. If you make a buffer, for example 30 milliseconds, then the audio received from the server will be playable, but it will be wrong(you only hear the knock). Some paranormal phenomena?

using NAudio.Wave;
using System.Net.Sockets;
class Program
{
 private void Main()
 {
  Recorder rec = new Recorder();
  Console.ReadKey();
 }
}
    class Recorder
        {
            UdpClient client = new UdpClient(755);
            WaveInEvent wave = new WaveInEvent();
            public Recorder()
            {
                Thread play = new Thread(new ThreadStart(Play));
                play.Start();
                wave.BufferMilliseconds = 100;
                wave.DeviceNumber = 0;
                wave.WaveFormat = new WaveFormat(8000, 16, 2);
                wave.DataAvailable += Wave_DataAvailable;
                wave.StartRecording();
            }
            private void Play()
            {
                WaveOutEvent output = new WaveOutEvent();
                BufferedWaveProvider buffer = new BufferedWaveProvider(new WaveFormat(8000, 16, 2));
                output.Init(buffer);
                output.Play();
                for(;;)
                {
                    IPEndPoint remoteEP = null;
                    byte[] data = client.Receive(ref remoteEP);
                    buffer.AddSamples(data, 0, data.Length);
                }
            }
            private void Wave_DataAvailable(object sender, WaveInEventArgs e)
            {
                client.Send(e.Buffer, e.BytesRecorded, "46.174.49.51", 54321);
            }
        }

To convince you that the server indeed sends the data back to the client, here is the code:

UdpClient client = new UdpClient(755);
byte[] sdata = Encoding.UTF8.GetBytes("Test");
client.Send(sdata, sdata.Length, "46.174.49.51", 54321);
IPEndPoint remoteEP = null;
byte[] rdata = client.Receive(ref remoteEP);
Console.WriteLine(Encoding.UTF8.GetString(rdata) + " from " + remoteEP.Port);
Console.ReadLine();

0 Answers0