0

I have an issue using naudio for a project of mine - most likely I just overlooked a tiny error, but I don't catch it, so may I kindly ask for some help.

I am working on a project to receive (and further work with) an audio stream I receive over a network. The stream is encoded with G.711 a-law using 8kHz and 8bit and sent in tiny pieces of 20ms (or 50 packets per second).

The following code receives the stream via UDP (basically whenever a UDP packet is received, it is read from the socket and added to the naudio BufferedWaveProvider:

Private Provider As New NAudio.Wave.BufferedWaveProvider(NAudio.Wave.WaveFormat.CreateALawFormat(8000, 1))
Private Sub FT636VOIP_U_Auslesen(ByVal ar As IAsyncResult)
    sample = FT636VOIPUSocket.EndReceive(ar, New Net.IPEndPoint("10.48.11.43", 60001))
    Provider.AddSamples(sample, 0, sample.Count)
    FT636VOIPUSocket.BeginReceive(New AsyncCallback(AddressOf FT636VOIP_U_Auslesen), FT636VOIPUSocket)
End Sub

Being started in another thread (to avoid blocking the main application), a WaveOutEvent is linked with the BufferedWaveProvider for playback.

Private Sub Audio()
    Dim wo As New NAudio.Wave.WaveOutEvent
    wo.DesiredLatency = 1000
    wo.Init(Provider)
    wo.Play()
    Do While wo.PlaybackState = NAudio.Wave.PlaybackState.Playing
        Threading.Thread.Sleep(500)
    Loop
End Sub

Well, the network connection is up and quickly filling the Buffer and the playback starts after the desired latency but only creates a 'choppy sound', though essentially there should only be silence...

Do I have to decode the stream at some stage (though the BufferedWaveProvider is initialized with the correct coded? Or do I miss something else...

1 Answer 1

0

You will get best results if you decode the audio as it arrives and put it into the BufferedWaveProvider as 16 bit audio. Also, are you sure that there is no surrounding metadata in the network packets being received? If so that needs to be stripped out or it will result in noise.

The NAudio demo project contains an example of this exact scenario, so you can use that as a reference if you need further help.

1
  • 1
    The second point actually was the problem. Though the documentation from the stream provider did not mention it, there were 12 byte of overhead included in each packet... adding the samples with an offset of 12 byte solved the issue. Commented May 6, 2018 at 3:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.