0

I have done some research online on how to send audio from one computer to the other in VB.net / C# using NAudio. With all the information that I've found on the internet I made two test programs: One that sends the audio (Wasapi Loopback) and one that receives and plays the audio. They look like this

Sender:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Using Capture As New WasapiLoopbackCapture()
            MsgBox("Format information" + vbNewLine + vbNewLine + "  Sample Rate: " + Capture.WaveFormat.SampleRate.ToString() + vbNewLine + "  Bits per Sample: " + Capture.WaveFormat.BitsPerSample.ToString() + vbNewLine + "  Channels: " + Capture.WaveFormat.Channels.ToString(), 64, "")

            Dim client As New UdpClient
            Dim ep As New IPEndPoint(IPAddress.Parse("192.168.1.101"), 12345)
            client.Connect(ep)

            Capture.StartRecording()

            Using w As New WaveFileWriter("dump.wav", Capture.WaveFormat)

                AddHandler Capture.DataAvailable, Sub(s As Object, f As WaveInEventArgs)
                                                      client.Send(f.Buffer, f.BytesRecorded)
                                                      w.Write(f.Buffer, 0, f.BytesRecorded)
                                                  End Sub
                Threading.Thread.Sleep(10000)
                Capture.StopRecording()
            End Using
        End Using
    End Sub

Receiving End:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim client As New UdpClient(12345)
        Dim endPoint As New IPEndPoint(IPAddress.Any, 12345)

        Dim ms As MemoryStream
        While True

            Dim data() As Byte
            data = client.Receive(endPoint)
            ms = New MemoryStream(data)
            ms.Position = 0

            Dim rs As New RawSourceWaveStream(ms, New WaveFormat(44100, 32, 2))
            Dim wo As New WaveOut()
            wo.Init(rs)
            wo.Play()
        End While
    End Sub

This does work and all, it creates the dump.wav file (which is not distorted) and sends the audio over to the receiving end and it plays the audio, however this audio is very loud and heavily distorted.

I am not sure what is causing this problem, but it may be caused by the UDPClient. But I don't think the sender is causing the problem, as the dump.wav is generated with no distortions or loud audio.

I was unable to find any solution online and have no idea what the source of the problem actually is.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75

2 Answers2

2

I suspect its a WaveFormat mismatch causing the distortion. It's not 32 bit PCM, but floating point so use WaveFormat.CreateIeeeFloatWaveFormat.

But you may want to reconsider sending this volume of audio data over the network. Usually for chat applications you'd want to reduce the bandwidth by compressing the audio, and maybe going for mono and a lower sample rate.

The NAudioDemo project has a network chat sample showing how to do this.

Mark Heath
  • 48,273
  • 29
  • 137
  • 194
0

On its own, the UDP protocol has no way of knowing if a data packet is lost along the way, thus it does not guarantee arrival of the packets you send.

If a data packet is lost then your file will (of course) become corrupted. The solution is either to switch to TCP (which guarantees data arrival and order as long as the connection is active) or find yourself a reliable UDP implementation.

Two bottlenecks with TCP are:

  • It is a streamed protocol, meaning there is no notion of packets on the application layer. It just delivers a (constant) stream of bytes.

  • It is somewhat slower than UDP (but not necessarily too slow!). Whether its speed is sufficient you'll have to determine yourself.

The first bottleneck can be resolved by implementing your own message framing protocol, meaning code that keeps track of the length of each piece of data you send.

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75