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.