Wednesday, February 10, 2010

Sending and playing microphone audio over network

Introduction

This example shows you how to receive data from a microphone and stream it over UDP to another computer. The example application can act like a direct phone, if both endpoints listen for data and send microphone data to each other. One would probably suspect that no source code exists for that, but of course it does. I hate those who will do commercial advertising. There is also a second related project what will contain a UDP server that we need to send/receive audio and compress it with g711 codec.Though only UDP is not the best way to transport audio data, RTP is the right way to go. RTP adds quality of service to transported audio, you can see how many packets are lost and can even arrange disordered packets. I will try to add an RTP example soon, so be calm, it's under way. There are some similar example applications, but most of them aren't well commented and missing some parts, so I will try to fill this part.

The package contains:

* LumiSoft.Media - Audio related API (Included in example app)
* LumiSoft.Net - UDP server, G711 codec
* Example Application

Using the code

* WaveIn - class provides a simple way to receive audio from a microphone.
Actually all what you need to do is:
WavIn.Devices - returns all available input devices from where we can get data.
Collapse

///

/// Application main class.

///


public class Test
{
private WavIn m_pSoundReceiver = null;

///

/// Default constructor.

///


public Test()
{
// G711 needs 8KHZ 16 bit 1 channel audio,

// 400kb buffer gives us 25ms audio frame.

m_pSoundReceiver = new WavIn(WavIn.Devices[0],8000,16,1,400);
m_pSoundReceiver.BufferFull += new BufferFullHandler
(m_pSoundReceiver_BufferFull);
m_pSoundReceiver.Start();
}

///

/// This method is called when recording buffer is full

/// and we need to process it.

///


/// Recorded data.

private void m_pSoundReceiver_BufferFull(byte[] buffer)
{
// Just store audio data or stream it over the network ...

}
}

* WaveOut - class provides methods for playing out streaming data.
The only thing you need to do is just call waveoutInstance.Play method.
In my opinion, the whole example application has been coded well enough, so dig into the code.

Note: Sound quality depends on network delay jittering, if there will be too big a variance in delays, voice will have holes in it. In addition, UDP packet loss and disordered packets will affect it too.

No comments:

Post a Comment

Followers